fix(browser_adapter.ts): baseElement.getAttribute

currently throwing an error
```error
Error during instantiation of LocationStrategy! (RouterLink -> Router
-> Location -> LocationStrategy).
ORIGINAL ERROR: TypeError: baseElement.attr is not a function
```

Closes #3214
This commit is contained in:
gdi2290 2015-08-11 22:17:39 -07:00 committed by Brian Ford
parent 52da220016
commit 235dec26fc
6 changed files with 52 additions and 3 deletions

View File

@ -423,7 +423,11 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
return null;
}
var baseUri = Uri.parse(href);
return baseUri.path;
return baseUri.path[0] == '/' ? baseUri.path : ('/' + baseUri.path);
}
resetBaseElement() {
baseElement = null;
}
String getUserAgent() {

View File

@ -297,6 +297,7 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
}
return relativePath(href);
}
resetBaseElement(): void { baseElement = null; }
getUserAgent(): string { return window.navigator.userAgent; }
setData(element, name: string, value: string) { element.dataset[name] = value; }
getData(element, name: string): string { return element.dataset[name]; }
@ -313,7 +314,7 @@ function getBaseElementHref(): string {
return null;
}
}
return baseElement.attr('href');
return baseElement.getAttribute('href');
}
// based on urlUtils.js in AngularJS 1

View File

@ -127,6 +127,7 @@ export class DomAdapter {
getHistory(): History { throw _abstract(); }
getLocation(): Location { throw _abstract(); }
getBaseHref(): string { throw _abstract(); }
resetBaseElement(): void { throw _abstract(); }
getUserAgent(): string { throw _abstract(); }
setData(element, name: string, value: string) { throw _abstract(); }
getData(element, name: string): string { throw _abstract(); }

View File

@ -400,6 +400,10 @@ class Html5LibDomAdapter implements DomAdapter {
throw 'not implemented';
}
resetBaseElement() {
throw 'not implemented';
}
String getUserAgent() {
throw 'not implemented';
}

View File

@ -532,6 +532,8 @@ export class Parse5DomAdapter extends DomAdapter {
return this.defaultDoc().body;
}
}
getBaseHref(): string { throw 'not implemented'; }
resetBaseElement(): void { throw 'not implemented'; }
getHistory(): History { throw 'not implemented'; }
getLocation(): Location { throw 'not implemented'; }
getUserAgent(): string { return "Fake user agent"; }

View File

@ -49,5 +49,42 @@ export function main() {
expect(DOM.isElementNode(secondChild)).toBe(true);
});
if (DOM.supportsDOMEvents()) {
describe('getBaseHref', () => {
beforeEach(() => DOM.resetBaseElement());
it('should return null if base element is absent',
() => { expect(DOM.getBaseHref()).toBeNull(); });
it('should return the value of the base element', () => {
var baseEl = DOM.createElement('base');
DOM.setAttribute(baseEl, 'href', '/drop/bass/connon/');
var headEl = DOM.defaultDoc().head;
DOM.appendChild(headEl, baseEl);
var baseHref = DOM.getBaseHref();
DOM.removeChild(headEl, baseEl);
DOM.resetBaseElement();
expect(baseHref).toEqual('/drop/bass/connon/');
});
it('should return a relative url', () => {
var baseEl = DOM.createElement('base');
DOM.setAttribute(baseEl, 'href', 'base');
var headEl = DOM.defaultDoc().head;
DOM.appendChild(headEl, baseEl);
var baseHref = DOM.getBaseHref();
DOM.removeChild(headEl, baseEl);
DOM.resetBaseElement();
expect(baseHref).toEqual('/base');
});
});
}
});
}