Skip to content

Commit

Permalink
fix(browser_adapter.ts): baseElement.getAttribute
Browse files Browse the repository at this point in the history
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
  • Loading branch information
PatrickJS authored and btford committed Aug 18, 2015
1 parent 52da220 commit 235dec2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
6 changes: 5 additions & 1 deletion modules/angular2/src/dom/browser_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion modules/angular2/src/dom/browser_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]; }
Expand All @@ -313,7 +314,7 @@ function getBaseElementHref(): string {
return null;
}
}
return baseElement.attr('href');
return baseElement.getAttribute('href');
}

// based on urlUtils.js in AngularJS 1
Expand Down
1 change: 1 addition & 0 deletions modules/angular2/src/dom/dom_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
4 changes: 4 additions & 0 deletions modules/angular2/src/dom/html_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,10 @@ class Html5LibDomAdapter implements DomAdapter {
throw 'not implemented';
}

resetBaseElement() {
throw 'not implemented';
}

String getUserAgent() {
throw 'not implemented';
}
Expand Down
2 changes: 2 additions & 0 deletions modules/angular2/src/dom/parse5_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"; }
Expand Down
39 changes: 38 additions & 1 deletion modules/angular2/test/dom/dom_adapter_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
}


});
}
}

0 comments on commit 235dec2

Please sign in to comment.