|
1 | | -'use strict'; |
2 | | - |
3 | | -import utils from './../utils.js'; |
4 | 1 | import platform from '../platform/index.js'; |
5 | 2 |
|
6 | | -export default platform.hasStandardBrowserEnv ? |
7 | | - |
8 | | -// Standard browser envs have full support of the APIs needed to test |
9 | | -// whether the request URL is of the same origin as current location. |
10 | | - (function standardBrowserEnv() { |
11 | | - const msie = platform.navigator && /(msie|trident)/i.test(platform.navigator.userAgent); |
12 | | - const urlParsingNode = document.createElement('a'); |
13 | | - let originURL; |
14 | | - |
15 | | - /** |
16 | | - * Parse a URL to discover its components |
17 | | - * |
18 | | - * @param {String} url The URL to be parsed |
19 | | - * @returns {Object} |
20 | | - */ |
21 | | - function resolveURL(url) { |
22 | | - let href = url; |
23 | | - |
24 | | - if (msie) { |
25 | | - // IE needs attribute set twice to normalize properties |
26 | | - urlParsingNode.setAttribute('href', href); |
27 | | - href = urlParsingNode.href; |
28 | | - } |
29 | | - |
30 | | - urlParsingNode.setAttribute('href', href); |
31 | | - |
32 | | - // urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils |
33 | | - return { |
34 | | - href: urlParsingNode.href, |
35 | | - protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '', |
36 | | - host: urlParsingNode.host, |
37 | | - search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '', |
38 | | - hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '', |
39 | | - hostname: urlParsingNode.hostname, |
40 | | - port: urlParsingNode.port, |
41 | | - pathname: (urlParsingNode.pathname.charAt(0) === '/') ? |
42 | | - urlParsingNode.pathname : |
43 | | - '/' + urlParsingNode.pathname |
44 | | - }; |
45 | | - } |
46 | | - |
47 | | - originURL = resolveURL(window.location.href); |
48 | | - |
49 | | - /** |
50 | | - * Determine if a URL shares the same origin as the current location |
51 | | - * |
52 | | - * @param {String} requestURL The URL to test |
53 | | - * @returns {boolean} True if URL shares the same origin, otherwise false |
54 | | - */ |
55 | | - return function isURLSameOrigin(requestURL) { |
56 | | - const parsed = (utils.isString(requestURL)) ? resolveURL(requestURL) : requestURL; |
57 | | - return (parsed.protocol === originURL.protocol && |
58 | | - parsed.host === originURL.host); |
59 | | - }; |
60 | | - })() : |
61 | | - |
62 | | - // Non standard browser envs (web workers, react-native) lack needed support. |
63 | | - (function nonStandardBrowserEnv() { |
64 | | - return function isURLSameOrigin() { |
65 | | - return true; |
66 | | - }; |
67 | | - })(); |
| 3 | +export default platform.hasStandardBrowserEnv ? ((origin, isMSIE) => (url) => { |
| 4 | + url = new URL(url, platform.origin); |
| 5 | + |
| 6 | + return ( |
| 7 | + origin.protocol === url.protocol && |
| 8 | + origin.host === url.host && |
| 9 | + (isMSIE || origin.port === url.port) |
| 10 | + ); |
| 11 | +})( |
| 12 | + new URL(platform.origin), |
| 13 | + platform.navigator && /(msie|trident)/i.test(platform.navigator.userAgent) |
| 14 | +) : () => true; |
0 commit comments