3 exports.__esModule = true;
4 exports.isCrossOrigin = exports.getFileExtension = exports.getAbsoluteURL = exports.parseUrl = undefined;
6 var _document = require('global/document');
8 var _document2 = _interopRequireDefault(_document);
10 var _window = require('global/window');
12 var _window2 = _interopRequireDefault(_window);
14 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
17 * @typedef {Object} url:URLObject
19 * @property {string} protocol
20 * The protocol of the url that was parsed.
22 * @property {string} hostname
23 * The hostname of the url that was parsed.
25 * @property {string} port
26 * The port of the url that was parsed.
28 * @property {string} pathname
29 * The pathname of the url that was parsed.
31 * @property {string} search
32 * The search query of the url that was parsed.
34 * @property {string} hash
35 * The hash of the url that was parsed.
37 * @property {string} host
38 * The host of the url that was parsed.
42 * Resolve and parse the elements of a URL.
47 * @return {url:URLObject}
48 * An object of url details
54 var parseUrl = exports.parseUrl = function parseUrl(url) {
55 var props = ['protocol', 'hostname', 'port', 'pathname', 'search', 'hash', 'host'];
57 // add the url to an anchor and let the browser parse the URL
58 var a = _document2['default'].createElement('a');
63 // ie8 doesn't parse the URL correctly until the anchor is actually
64 // added to the body, and an innerHTML is needed to trigger the parsing
65 var addToBody = a.host === '' && a.protocol !== 'file:';
69 div = _document2['default'].createElement('div');
70 div.innerHTML = '<a href="' + url + '"></a>';
72 // prevent the div from affecting layout
73 div.setAttribute('style', 'display:none; position:absolute;');
74 _document2['default'].body.appendChild(div);
77 // Copy the specific URL properties to a new object
78 // This is also needed for IE8 because the anchor loses its
79 // properties when it's removed from the dom
82 for (var i = 0; i < props.length; i++) {
83 details[props[i]] = a[props[i]];
86 // IE9 adds the port to the host property unlike everyone else. If
87 // a port identifier is added for standard ports, strip it.
88 if (details.protocol === 'http:') {
89 details.host = details.host.replace(/:80$/, '');
92 if (details.protocol === 'https:') {
93 details.host = details.host.replace(/:443$/, '');
97 _document2['default'].body.removeChild(div);
104 * Get absolute version of relative URL. Used to tell flash correct URL.
107 * @param {string} url
108 * URL to make absolute
113 * @see http://stackoverflow.com/questions/470832/getting-an-absolute-url-from-a-relative-one-ie6-issue
115 var getAbsoluteURL = exports.getAbsoluteURL = function getAbsoluteURL(url) {
116 // Check if absolute URL
117 if (!url.match(/^https?:\/\//)) {
118 // Convert to absolute URL. Flash hosted off-site needs an absolute URL.
119 var div = _document2['default'].createElement('div');
121 div.innerHTML = '<a href="' + url + '">x</a>';
122 url = div.firstChild.href;
129 * Returns the extension of the passed file name. It will return an empty string
130 * if passed an invalid path.
132 * @param {string} path
133 * The fileName path like '/path/to/file.mp4'
136 * The extension in lower case or an empty string if no
137 * extension could be found.
139 var getFileExtension = exports.getFileExtension = function getFileExtension(path) {
140 if (typeof path === 'string') {
141 var splitPathRe = /^(\/?)([\s\S]*?)((?:\.{1,2}|[^\/]+?)(\.([^\.\/\?]+)))(?:[\/]*|[\?].*)$/i;
142 var pathParts = splitPathRe.exec(path);
145 return pathParts.pop().toLowerCase();
153 * Returns whether the url passed is a cross domain request or not.
155 * @param {string} url
159 * Whether it is a cross domain request or not.
161 var isCrossOrigin = exports.isCrossOrigin = function isCrossOrigin(url) {
162 var winLoc = _window2['default'].location;
163 var urlInfo = parseUrl(url);
165 // IE8 protocol relative urls will return ':' for protocol
166 var srcProtocol = urlInfo.protocol === ':' ? winLoc.protocol : urlInfo.protocol;
168 // Check if url is for another domain/origin
169 // IE8 doesn't know location.origin, so we won't rely on it here
170 var crossOrigin = srcProtocol + urlInfo.host !== winLoc.protocol + winLoc.host;