@@ -68,7 +68,18 @@ function urlResolve(url) {
68
68
69
69
urlParsingNode . setAttribute ( 'href' , href ) ;
70
70
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
+ } ;
72
83
}
73
84
74
85
/**
@@ -94,18 +105,9 @@ function urlIsSameOrigin(requestUrl) {
94
105
* @returns {boolean } Whether the URL is same-origin as the document base URL.
95
106
*/
96
107
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
- }
107
108
var parsed = ( isString ( requestUrl ) ) ? urlResolve ( requestUrl ) : requestUrl ;
108
- return urlsAreSameOrigin ( parsed , anchorElementToObject ( baseUrlParsingNode ) ) ;
109
+ var base = urlResolve ( getBaseUrl ( ) ) ;
110
+ return urlsAreSameOrigin ( parsed , base ) ;
109
111
}
110
112
111
113
/**
@@ -126,23 +128,22 @@ function urlsAreSameOrigin(url1, url2) {
126
128
}
127
129
128
130
/**
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 }
133
133
*/
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 ;
148
149
}
0 commit comments