8
8
// service.
9
9
var urlParsingNode = window . document . createElement ( 'a' ) ;
10
10
var originUrl = urlResolve ( window . location . href ) ;
11
- var baseUrlParsingNode ;
12
11
13
12
14
13
/**
@@ -44,16 +43,16 @@ var baseUrlParsingNode;
44
43
* @description Normalizes and parses a URL.
45
44
* @returns {object } Returns the normalized URL as a dictionary.
46
45
*
47
- * | member name | Description |
48
- * |---------------|------------------------------------------------------------------------ |
46
+ * | member name | Description |
47
+ * |---------------|----------------|
49
48
* | href | A normalized version of the provided URL if it was not an absolute URL |
50
- * | protocol | The protocol without the trailing colon |
49
+ * | protocol | The protocol including the trailing colon |
51
50
* | host | The host and port (if the port is non-default) of the normalizedUrl |
52
51
* | search | The search params, minus the question mark |
53
- * | hash | The hash string, minus the hash symbol |
54
- * | hostname | The hostname |
55
- * | port | The port, without ":" |
56
- * | pathname | The pathname, beginning with "/" |
52
+ * | hash | The hash string, minus the hash symbol
53
+ * | hostname | The hostname
54
+ * | port | The port, without ":"
55
+ * | pathname | The pathname, beginning with "/"
57
56
*
58
57
*/
59
58
function urlResolve ( url ) {
@@ -68,6 +67,7 @@ function urlResolve(url) {
68
67
69
68
urlParsingNode . setAttribute ( 'href' , href ) ;
70
69
70
+ // urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
71
71
return {
72
72
href : urlParsingNode . href ,
73
73
protocol : urlParsingNode . protocol ? urlParsingNode . protocol . replace ( / : $ / , '' ) : '' ,
@@ -90,57 +90,7 @@ function urlResolve(url) {
90
90
* @returns {boolean } Whether the request is for the same origin as the application document.
91
91
*/
92
92
function urlIsSameOrigin ( requestUrl ) {
93
- return urlsAreSameOrigin ( requestUrl , originUrl ) ;
94
- }
95
-
96
- /**
97
- * Parse a request URL and determine whether it is same-origin as the current document base URL.
98
- *
99
- * Note: The base URL is usually the same as the document location (`location.href`) but can
100
- * be overriden by using the `<base>` tag.
101
- *
102
- * @param {string|object } requestUrl The url of the request as a string that will be resolved
103
- * or a parsed URL object.
104
- * @returns {boolean } Whether the URL is same-origin as the document base URL.
105
- */
106
- function urlIsSameOriginAsBaseUrl ( requestUrl ) {
107
- return urlsAreSameOrigin ( requestUrl , getBaseUrl ( ) ) ;
108
- }
109
-
110
- /**
111
- * Determines if two URLs share the same origin.
112
- *
113
- * @param {string|object } url1 First URL to compare as a string or a normalized URL in the form of
114
- * a dictionary object returned by `urlResolve()`.
115
- * @param {string|object } url2 Second URL to compare as a string or a normalized URL in the form of
116
- * a dictionary object returned by `urlResolve()`.
117
- * @return {boolean } True if both URLs have the same origin, and false otherwise.
118
- */
119
- function urlsAreSameOrigin ( url1 , url2 ) {
120
- url1 = ( isString ( url1 ) ) ? urlResolve ( url1 ) : url1 ;
121
- url2 = ( isString ( url2 ) ) ? urlResolve ( url2 ) : url2 ;
122
-
123
- return ( url1 . protocol === url2 . protocol &&
124
- url1 . host === url2 . host ) ;
125
- }
126
-
127
- /**
128
- * Returns the current document base URL.
129
- * @return {string }
130
- */
131
- function getBaseUrl ( ) {
132
- if ( window . document . baseURI ) {
133
- return window . document . baseURI ;
134
- }
135
-
136
- // document.baseURI is available everywhere except IE
137
- if ( ! baseUrlParsingNode ) {
138
- baseUrlParsingNode = window . document . createElement ( 'a' ) ;
139
- baseUrlParsingNode . href = '.' ;
140
-
141
- // Work-around for IE bug described in Implementation Notes. The fix in urlResolve() is not
142
- // suitable here because we need to track changes to the base URL.
143
- baseUrlParsingNode = baseUrlParsingNode . cloneNode ( false ) ;
144
- }
145
- return baseUrlParsingNode . href ;
93
+ var parsed = ( isString ( requestUrl ) ) ? urlResolve ( requestUrl ) : requestUrl ;
94
+ return ( parsed . protocol === originUrl . protocol &&
95
+ parsed . host === originUrl . host ) ;
146
96
}
0 commit comments