Skip to content

Commit 8296dce

Browse files
committed
fix(facade): use base element to get base href
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.
1 parent 3df8363 commit 8296dce

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

modules/angular2/src/dom/browser_adapter.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,11 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
342342
return window.location;
343343
}
344344
getBaseHref() {
345-
var uri = document.baseUri;
346-
var baseUri = Uri.parse(uri);
345+
var href = getBaseElementHref();
346+
if (href == null) {
347+
return null;
348+
}
349+
var baseUri = Uri.parse(href);
347350
return baseUri.path;
348351
}
349352
String getUserAgent() {
@@ -360,3 +363,15 @@ class BrowserDomAdapter extends GenericBrowserDomAdapter {
360363
js.context[name] = value;
361364
}
362365
}
366+
367+
368+
var baseElement = null;
369+
String getBaseElementHref() {
370+
if (baseElement == null) {
371+
baseElement = document.querySelector('base');
372+
if (baseElement == null) {
373+
return null;
374+
}
375+
}
376+
return baseElement.getAttribute('href');
377+
}

modules/angular2/src/dom/browser_adapter.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,32 @@ export class BrowserDomAdapter extends GenericBrowserDomAdapter {
263263
}
264264
getHistory(): History { return window.history; }
265265
getLocation(): Location { return window.location; }
266-
getBaseHref(): string { return relativePath(document.baseURI); }
266+
getBaseHref(): string {
267+
var href = getBaseElementHref();
268+
if (isBlank(href)) {
269+
return null;
270+
}
271+
return relativePath(href);
272+
}
267273
getUserAgent(): string { return window.navigator.userAgent; }
268274
setData(element, name: string, value: string) { element.dataset[name] = value; }
269275
getData(element, name: string): string { return element.dataset[name]; }
270276
// TODO(tbosch): move this into a separate environment class once we have it
271277
setGlobalVar(name: string, value: any) { global[name] = value; }
272278
}
273279

280+
281+
var baseElement = null;
282+
function getBaseElementHref(): string {
283+
if (isBlank(baseElement)) {
284+
baseElement = document.querySelector('base');
285+
if (isBlank(baseElement)) {
286+
return null;
287+
}
288+
}
289+
return baseElement.attr('href');
290+
}
291+
274292
// based on urlUtils.js in AngularJS 1
275293
var urlParsingNode = null;
276294
function relativePath(url): string {

0 commit comments

Comments
 (0)