Security update for Core, with self-updated composer
[yaffs-website] / node_modules / video.js / es5 / utils / url.js
1 'use strict';
2
3 exports.__esModule = true;
4 exports.isCrossOrigin = exports.getFileExtension = exports.getAbsoluteURL = exports.parseUrl = undefined;
5
6 var _document = require('global/document');
7
8 var _document2 = _interopRequireDefault(_document);
9
10 var _window = require('global/window');
11
12 var _window2 = _interopRequireDefault(_window);
13
14 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
15
16 /**
17  * @typedef {Object} url:URLObject
18  *
19  * @property {string} protocol
20  *           The protocol of the url that was parsed.
21  *
22  * @property {string} hostname
23  *           The hostname of the url that was parsed.
24  *
25  * @property {string} port
26  *           The port of the url that was parsed.
27  *
28  * @property {string} pathname
29  *           The pathname of the url that was parsed.
30  *
31  * @property {string} search
32  *           The search query of the url that was parsed.
33  *
34  * @property {string} hash
35  *           The hash of the url that was parsed.
36  *
37  * @property {string} host
38  *           The host of the url that was parsed.
39  */
40
41 /**
42  * Resolve and parse the elements of a URL.
43  *
44  * @param  {String} url
45  *         The url to parse
46  *
47  * @return {url:URLObject}
48  *         An object of url details
49  */
50 /**
51  * @file url.js
52  * @module url
53  */
54 var parseUrl = exports.parseUrl = function parseUrl(url) {
55   var props = ['protocol', 'hostname', 'port', 'pathname', 'search', 'hash', 'host'];
56
57   // add the url to an anchor and let the browser parse the URL
58   var a = _document2['default'].createElement('a');
59
60   a.href = url;
61
62   // IE8 (and 9?) Fix
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:';
66   var div = void 0;
67
68   if (addToBody) {
69     div = _document2['default'].createElement('div');
70     div.innerHTML = '<a href="' + url + '"></a>';
71     a = div.firstChild;
72     // prevent the div from affecting layout
73     div.setAttribute('style', 'display:none; position:absolute;');
74     _document2['default'].body.appendChild(div);
75   }
76
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
80   var details = {};
81
82   for (var i = 0; i < props.length; i++) {
83     details[props[i]] = a[props[i]];
84   }
85
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$/, '');
90   }
91
92   if (details.protocol === 'https:') {
93     details.host = details.host.replace(/:443$/, '');
94   }
95
96   if (addToBody) {
97     _document2['default'].body.removeChild(div);
98   }
99
100   return details;
101 };
102
103 /**
104  * Get absolute version of relative URL. Used to tell flash correct URL.
105  *
106  *
107  * @param  {string} url
108  *         URL to make absolute
109  *
110  * @return {string}
111  *         Absolute URL
112  *
113  * @see http://stackoverflow.com/questions/470832/getting-an-absolute-url-from-a-relative-one-ie6-issue
114  */
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');
120
121     div.innerHTML = '<a href="' + url + '">x</a>';
122     url = div.firstChild.href;
123   }
124
125   return url;
126 };
127
128 /**
129  * Returns the extension of the passed file name. It will return an empty string
130  * if passed an invalid path.
131  *
132  * @param {string} path
133  *        The fileName path like '/path/to/file.mp4'
134  *
135  * @returns {string}
136  *          The extension in lower case or an empty string if no
137  *          extension could be found.
138  */
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);
143
144     if (pathParts) {
145       return pathParts.pop().toLowerCase();
146     }
147   }
148
149   return '';
150 };
151
152 /**
153  * Returns whether the url passed is a cross domain request or not.
154  *
155  * @param {string} url
156  *        The url to check.
157  *
158  * @return {boolean}
159  *         Whether it is a cross domain request or not.
160  */
161 var isCrossOrigin = exports.isCrossOrigin = function isCrossOrigin(url) {
162   var winLoc = _window2['default'].location;
163   var urlInfo = parseUrl(url);
164
165   // IE8 protocol relative urls will return ':' for protocol
166   var srcProtocol = urlInfo.protocol === ':' ? winLoc.protocol : urlInfo.protocol;
167
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;
171
172   return crossOrigin;
173 };