Skip to content

Commit

Permalink
fix(facade): use base element to get base href
Browse files Browse the repository at this point in the history
Previously, calls to getBaseHref used document.baseURI, which defaults
to the current path in the absence of a base element in the document.
This leads to surprising behavior.

With this change, getBaseHref returns null when a base element is not
present in the document.
  • Loading branch information
btford committed Jul 17, 2015
1 parent 3df8363 commit 8296dce
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
19 changes: 17 additions & 2 deletions modules/angular2/src/dom/browser_adapter.dart
Expand Up @@ -342,8 +342,11 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
return window.location;
}
getBaseHref() {
var uri = document.baseUri;
var baseUri = Uri.parse(uri);
var href = getBaseElementHref();
if (href == null) {
return null;
}
var baseUri = Uri.parse(href);
return baseUri.path;
}
String getUserAgent() {
Expand All @@ -360,3 +363,15 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
js.context[name] = value;
}
}


var baseElement = null;
String getBaseElementHref() {
if (baseElement == null) {
baseElement = document.querySelector('base');
if (baseElement == null) {
return null;
}
}
return baseElement.getAttribute('href');
}
20 changes: 19 additions & 1 deletion modules/angular2/src/dom/browser_adapter.ts
Expand Up @@ -263,14 +263,32 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
}
getHistory(): History { return window.history; }
getLocation(): Location { return window.location; }
getBaseHref(): string { return relativePath(document.baseURI); }
getBaseHref(): string {
var href = getBaseElementHref();
if (isBlank(href)) {
return null;
}
return relativePath(href);
}
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]; }
// TODO(tbosch): move this into a separate environment class once we have it
setGlobalVar(name: string, value: any) { global[name] = value; }
}


var baseElement = null;
function getBaseElementHref(): string {
if (isBlank(baseElement)) {
baseElement = document.querySelector('base');
if (isBlank(baseElement)) {
return null;
}
}
return baseElement.attr('href');

This comment has been minimized.

Copy link
@PatrickJS

PatrickJS Jul 23, 2015

Member

You might have mixed dart and ts. You can either use baseElement.href or baseElement.getAttribute('href') since this throws an error
TypeError: baseElement.attr is not a function

This comment has been minimized.

Copy link
@markharding

markharding Jul 23, 2015

Yes getting

Error during instantiation of LocationStrategy! (Navigation -> Navigation -> Router -> function (_platformStrategy, href) {
        var $__0 = this;
        this._platformStrategy = _platformStrategy;
        this._subject = new EventEmitter();
        var browserBaseHref = isPresent(href) ? href : this._platformStrategy.getBaseHref();
        if (isBlank(browserBaseHref)) {
          throw new BaseException("No base href set. Either provide a binding to \"appBaseHrefToken\" or add a base element.");
        }
        this._baseHref = stripTrailingSlash(stripIndexHtml(browserBaseHref));
        this._platformStrategy.onPopState((function(_) {
          return $__0._onPopState(_);
        }));
      } -> LocationStrategy). ORIGINAL ERROR: TypeError: baseElement.attr is not a function

in alpha-32 also

This comment has been minimized.

Copy link
@PatrickJS

PatrickJS Jul 23, 2015

Member

a quick fix is including this right after <base href>
<script>baseElement = document.querySelector('base');baseElement.attr = baseElement.getAttribute;</script>

}

// based on urlUtils.js in AngularJS 1
var urlParsingNode = null;
function relativePath(url): string {
Expand Down

0 comments on commit 8296dce

Please sign in to comment.