'use strict';
exports.__esModule = true;
exports.isCrossOrigin = exports.getFileExtension = exports.getAbsoluteURL = exports.parseUrl = undefined;
var _document = require('global/document');
var _document2 = _interopRequireDefault(_document);
var _window = require('global/window');
var _window2 = _interopRequireDefault(_window);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
/**
* @typedef {Object} url:URLObject
*
* @property {string} protocol
* The protocol of the url that was parsed.
*
* @property {string} hostname
* The hostname of the url that was parsed.
*
* @property {string} port
* The port of the url that was parsed.
*
* @property {string} pathname
* The pathname of the url that was parsed.
*
* @property {string} search
* The search query of the url that was parsed.
*
* @property {string} hash
* The hash of the url that was parsed.
*
* @property {string} host
* The host of the url that was parsed.
*/
/**
* Resolve and parse the elements of a URL.
*
* @param {String} url
* The url to parse
*
* @return {url:URLObject}
* An object of url details
*/
/**
* @file url.js
* @module url
*/
var parseUrl = exports.parseUrl = function parseUrl(url) {
var props = ['protocol', 'hostname', 'port', 'pathname', 'search', 'hash', 'host'];
// add the url to an anchor and let the browser parse the URL
var a = _document2['default'].createElement('a');
a.href = url;
// IE8 (and 9?) Fix
// ie8 doesn't parse the URL correctly until the anchor is actually
// added to the body, and an innerHTML is needed to trigger the parsing
var addToBody = a.host === '' && a.protocol !== 'file:';
var div = void 0;
if (addToBody) {
div = _document2['default'].createElement('div');
div.innerHTML = '';
a = div.firstChild;
// prevent the div from affecting layout
div.setAttribute('style', 'display:none; position:absolute;');
_document2['default'].body.appendChild(div);
}
// Copy the specific URL properties to a new object
// This is also needed for IE8 because the anchor loses its
// properties when it's removed from the dom
var details = {};
for (var i = 0; i < props.length; i++) {
details[props[i]] = a[props[i]];
}
// IE9 adds the port to the host property unlike everyone else. If
// a port identifier is added for standard ports, strip it.
if (details.protocol === 'http:') {
details.host = details.host.replace(/:80$/, '');
}
if (details.protocol === 'https:') {
details.host = details.host.replace(/:443$/, '');
}
if (addToBody) {
_document2['default'].body.removeChild(div);
}
return details;
};
/**
* Get absolute version of relative URL. Used to tell flash correct URL.
*
*
* @param {string} url
* URL to make absolute
*
* @return {string}
* Absolute URL
*
* @see http://stackoverflow.com/questions/470832/getting-an-absolute-url-from-a-relative-one-ie6-issue
*/
var getAbsoluteURL = exports.getAbsoluteURL = function getAbsoluteURL(url) {
// Check if absolute URL
if (!url.match(/^https?:\/\//)) {
// Convert to absolute URL. Flash hosted off-site needs an absolute URL.
var div = _document2['default'].createElement('div');
div.innerHTML = 'x';
url = div.firstChild.href;
}
return url;
};
/**
* Returns the extension of the passed file name. It will return an empty string
* if passed an invalid path.
*
* @param {string} path
* The fileName path like '/path/to/file.mp4'
*
* @returns {string}
* The extension in lower case or an empty string if no
* extension could be found.
*/
var getFileExtension = exports.getFileExtension = function getFileExtension(path) {
if (typeof path === 'string') {
var splitPathRe = /^(\/?)([\s\S]*?)((?:\.{1,2}|[^\/]+?)(\.([^\.\/\?]+)))(?:[\/]*|[\?].*)$/i;
var pathParts = splitPathRe.exec(path);
if (pathParts) {
return pathParts.pop().toLowerCase();
}
}
return '';
};
/**
* Returns whether the url passed is a cross domain request or not.
*
* @param {string} url
* The url to check.
*
* @return {boolean}
* Whether it is a cross domain request or not.
*/
var isCrossOrigin = exports.isCrossOrigin = function isCrossOrigin(url) {
var winLoc = _window2['default'].location;
var urlInfo = parseUrl(url);
// IE8 protocol relative urls will return ':' for protocol
var srcProtocol = urlInfo.protocol === ':' ? winLoc.protocol : urlInfo.protocol;
// Check if url is for another domain/origin
// IE8 doesn't know location.origin, so we won't rely on it here
var crossOrigin = srcProtocol + urlInfo.host !== winLoc.protocol + winLoc.host;
return crossOrigin;
};