You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.2 KiB

3 years ago
'use strict';
3 years ago
import utils from './../utils.js';
import platform from '../platform/index.js';
3 years ago
3 years ago
export default platform.isStandardBrowserEnv ?
3 years ago
3 years ago
// Standard browser envs have full support of the APIs needed to test
// whether the request URL is of the same origin as current location.
(function standardBrowserEnv() {
const msie = /(msie|trident)/i.test(navigator.userAgent);
const urlParsingNode = document.createElement('a');
let originURL;
3 years ago
3 years ago
/**
3 years ago
* Parse a URL to discover it's components
*
* @param {String} url The URL to be parsed
* @returns {Object}
*/
3 years ago
function resolveURL(url) {
let href = url;
3 years ago
3 years ago
if (msie) {
3 years ago
// IE needs attribute set twice to normalize properties
urlParsingNode.setAttribute('href', href);
3 years ago
href = urlParsingNode.href;
3 years ago
}
3 years ago
urlParsingNode.setAttribute('href', href);
// urlParsingNode provides the UrlUtils interface - http://url.spec.whatwg.org/#urlutils
return {
href: urlParsingNode.href,
protocol: urlParsingNode.protocol ? urlParsingNode.protocol.replace(/:$/, '') : '',
host: urlParsingNode.host,
search: urlParsingNode.search ? urlParsingNode.search.replace(/^\?/, '') : '',
hash: urlParsingNode.hash ? urlParsingNode.hash.replace(/^#/, '') : '',
hostname: urlParsingNode.hostname,
port: urlParsingNode.port,
pathname: (urlParsingNode.pathname.charAt(0) === '/') ?
urlParsingNode.pathname :
'/' + urlParsingNode.pathname
};
}
3 years ago
3 years ago
originURL = resolveURL(window.location.href);
/**
3 years ago
* Determine if a URL shares the same origin as the current location
*
* @param {String} requestURL The URL to test
* @returns {boolean} True if URL shares the same origin, otherwise false
*/
3 years ago
return function isURLSameOrigin(requestURL) {
const parsed = (utils.isString(requestURL)) ? resolveURL(requestURL) : requestURL;
return (parsed.protocol === originURL.protocol &&
parsed.host === originURL.host);
};
})() :
3 years ago
// Non standard browser envs (web workers, react-native) lack needed support.
3 years ago
(function nonStandardBrowserEnv() {
return function isURLSameOrigin() {
return true;
};
})();