Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0c20c75

Browse files
committed
make urlIsSameOriginAsBaseUrl() run on Linux
1 parent 0588706 commit 0c20c75

File tree

1 file changed

+31
-30
lines changed

1 file changed

+31
-30
lines changed

src/ng/urlUtils.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,18 @@ function urlResolve(url) {
6868

6969
urlParsingNode.setAttribute('href', href);
7070

71-
return anchorElementToObject(urlParsingNode);
71+
return {
72+
href: urlParsingNode.href,
73+
protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
74+
host: urlParsingNode.host,
75+
search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
76+
hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
77+
hostname: urlParsingNode.hostname,
78+
port: urlParsingNode.port,
79+
pathname: (urlParsingNode.pathname.charAt(0) === '/')
80+
? urlParsingNode.pathname
81+
: '/' + urlParsingNode.pathname
82+
};
7283
}
7384

7485
/**
@@ -94,18 +105,9 @@ function urlIsSameOrigin(requestUrl) {
94105
* @returns {boolean} Whether the URL is same-origin as the document base URL.
95106
*/
96107
function urlIsSameOriginAsBaseUrl(requestUrl) {
97-
if (!baseUrlParsingNode) {
98-
baseUrlParsingNode = window.document.createElement('a');
99-
baseUrlParsingNode.href = '.';
100-
101-
if (msie) {
102-
// Work-around for IE bug described in Implementation Notes. The fix in urlResolve() is not
103-
// suitable here because we need to track changes to the base URL.
104-
baseUrlParsingNode = baseUrlParsingNode.cloneNode(false);
105-
}
106-
}
107108
var parsed = (isString(requestUrl)) ? urlResolve(requestUrl) : requestUrl;
108-
return urlsAreSameOrigin(parsed, anchorElementToObject(baseUrlParsingNode));
109+
var base = urlResolve(getBaseUrl());
110+
return urlsAreSameOrigin(parsed, base);
109111
}
110112

111113
/**
@@ -126,23 +128,22 @@ function urlsAreSameOrigin(url1, url2) {
126128
}
127129

128130
/**
129-
* Converts properties in the given anchor element into a dictionary object as described in the
130-
* documentation for `urlResolve()`.
131-
* @param {HTMLAnchorElement} elem
132-
* @returns {object}
131+
* Returns the current document base URL.
132+
* @return {string}
133133
*/
134-
function anchorElementToObject(elem) {
135-
// elem provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
136-
return {
137-
href: elem.href,
138-
protocol: elem.protocol ? elem.protocol.replace(/:$/, '') : '',
139-
host: elem.host,
140-
search: elem.search ? elem.search.replace(/^\?/, '') : '',
141-
hash: elem.hash ? elem.hash.replace(/^#/, '') : '',
142-
hostname: elem.hostname,
143-
port: elem.port,
144-
pathname: (elem.pathname.charAt(0) === '/')
145-
? elem.pathname
146-
: '/' + elem.pathname
147-
};
134+
function getBaseUrl() {
135+
if (window.document.baseURI) {
136+
return window.document.baseURI;
137+
}
138+
139+
// document.baseURI is available everywhere except IE
140+
if (!baseUrlParsingNode) {
141+
baseUrlParsingNode = window.document.createElement('a');
142+
baseUrlParsingNode.href = '.';
143+
144+
// Work-around for IE bug described in Implementation Notes. The fix in urlResolve() is not
145+
// suitable here because we need to track changes to the base URL.
146+
baseUrlParsingNode = baseUrlParsingNode.cloneNode(false);
147+
}
148+
return baseUrlParsingNode.href;
148149
}

0 commit comments

Comments
 (0)