compatibility.js 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. if (typeof PDFJS === 'undefined' || !PDFJS.compatibilityChecked) {
  17. var globalScope = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : undefined;
  18. var userAgent = typeof navigator !== 'undefined' && navigator.userAgent || '';
  19. var isAndroid = /Android/.test(userAgent);
  20. var isAndroidPre3 = /Android\s[0-2][^\d]/.test(userAgent);
  21. var isAndroidPre5 = /Android\s[0-4][^\d]/.test(userAgent);
  22. var isChrome = userAgent.indexOf('Chrom') >= 0;
  23. var isChromeWithRangeBug = /Chrome\/(39|40)\./.test(userAgent);
  24. var isIOSChrome = userAgent.indexOf('CriOS') >= 0;
  25. var isIE = userAgent.indexOf('Trident') >= 0;
  26. var isIOS = /\b(iPad|iPhone|iPod)(?=;)/.test(userAgent);
  27. var isOpera = userAgent.indexOf('Opera') >= 0;
  28. var isSafari = /Safari\//.test(userAgent) && !/(Chrome\/|Android\s)/.test(userAgent);
  29. var hasDOM = typeof window === 'object' && typeof document === 'object';
  30. if (typeof PDFJS === 'undefined') {
  31. globalScope.PDFJS = {};
  32. }
  33. PDFJS.compatibilityChecked = true;
  34. (function checkTypedArrayCompatibility() {
  35. if (typeof Uint8Array !== 'undefined') {
  36. if (typeof Uint8Array.prototype.subarray === 'undefined') {
  37. Uint8Array.prototype.subarray = function subarray(start, end) {
  38. return new Uint8Array(this.slice(start, end));
  39. };
  40. Float32Array.prototype.subarray = function subarray(start, end) {
  41. return new Float32Array(this.slice(start, end));
  42. };
  43. }
  44. if (typeof Float64Array === 'undefined') {
  45. globalScope.Float64Array = Float32Array;
  46. }
  47. return;
  48. }
  49. function subarray(start, end) {
  50. return new TypedArray(this.slice(start, end));
  51. }
  52. function setArrayOffset(array, offset) {
  53. if (arguments.length < 2) {
  54. offset = 0;
  55. }
  56. for (var i = 0, n = array.length; i < n; ++i, ++offset) {
  57. this[offset] = array[i] & 0xFF;
  58. }
  59. }
  60. function TypedArray(arg1) {
  61. var result, i, n;
  62. if (typeof arg1 === 'number') {
  63. result = [];
  64. for (i = 0; i < arg1; ++i) {
  65. result[i] = 0;
  66. }
  67. } else if ('slice' in arg1) {
  68. result = arg1.slice(0);
  69. } else {
  70. result = [];
  71. for (i = 0, n = arg1.length; i < n; ++i) {
  72. result[i] = arg1[i];
  73. }
  74. }
  75. result.subarray = subarray;
  76. result.buffer = result;
  77. result.byteLength = result.length;
  78. result.set = setArrayOffset;
  79. if (typeof arg1 === 'object' && arg1.buffer) {
  80. result.buffer = arg1.buffer;
  81. }
  82. return result;
  83. }
  84. globalScope.Uint8Array = TypedArray;
  85. globalScope.Int8Array = TypedArray;
  86. globalScope.Uint32Array = TypedArray;
  87. globalScope.Int32Array = TypedArray;
  88. globalScope.Uint16Array = TypedArray;
  89. globalScope.Float32Array = TypedArray;
  90. globalScope.Float64Array = TypedArray;
  91. })();
  92. (function normalizeURLObject() {
  93. if (!globalScope.URL) {
  94. globalScope.URL = globalScope.webkitURL;
  95. }
  96. })();
  97. (function checkObjectDefinePropertyCompatibility() {
  98. if (typeof Object.defineProperty !== 'undefined') {
  99. var definePropertyPossible = true;
  100. try {
  101. if (hasDOM) {
  102. Object.defineProperty(new Image(), 'id', { value: 'test' });
  103. }
  104. var Test = function Test() {};
  105. Test.prototype = {
  106. get id() {}
  107. };
  108. Object.defineProperty(new Test(), 'id', {
  109. value: '',
  110. configurable: true,
  111. enumerable: true,
  112. writable: false
  113. });
  114. } catch (e) {
  115. definePropertyPossible = false;
  116. }
  117. if (definePropertyPossible) {
  118. return;
  119. }
  120. }
  121. Object.defineProperty = function objectDefineProperty(obj, name, def) {
  122. delete obj[name];
  123. if ('get' in def) {
  124. obj.__defineGetter__(name, def['get']);
  125. }
  126. if ('set' in def) {
  127. obj.__defineSetter__(name, def['set']);
  128. }
  129. if ('value' in def) {
  130. obj.__defineSetter__(name, function objectDefinePropertySetter(value) {
  131. this.__defineGetter__(name, function objectDefinePropertyGetter() {
  132. return value;
  133. });
  134. return value;
  135. });
  136. obj[name] = def.value;
  137. }
  138. };
  139. })();
  140. (function checkXMLHttpRequestResponseCompatibility() {
  141. if (typeof XMLHttpRequest === 'undefined') {
  142. return;
  143. }
  144. var xhrPrototype = XMLHttpRequest.prototype;
  145. var xhr = new XMLHttpRequest();
  146. if (!('overrideMimeType' in xhr)) {
  147. Object.defineProperty(xhrPrototype, 'overrideMimeType', {
  148. value: function xmlHttpRequestOverrideMimeType(mimeType) {}
  149. });
  150. }
  151. if ('responseType' in xhr) {
  152. return;
  153. }
  154. Object.defineProperty(xhrPrototype, 'responseType', {
  155. get: function xmlHttpRequestGetResponseType() {
  156. return this._responseType || 'text';
  157. },
  158. set: function xmlHttpRequestSetResponseType(value) {
  159. if (value === 'text' || value === 'arraybuffer') {
  160. this._responseType = value;
  161. if (value === 'arraybuffer' && typeof this.overrideMimeType === 'function') {
  162. this.overrideMimeType('text/plain; charset=x-user-defined');
  163. }
  164. }
  165. }
  166. });
  167. if (typeof VBArray !== 'undefined') {
  168. Object.defineProperty(xhrPrototype, 'response', {
  169. get: function xmlHttpRequestResponseGet() {
  170. if (this.responseType === 'arraybuffer') {
  171. return new Uint8Array(new VBArray(this.responseBody).toArray());
  172. }
  173. return this.responseText;
  174. }
  175. });
  176. return;
  177. }
  178. Object.defineProperty(xhrPrototype, 'response', {
  179. get: function xmlHttpRequestResponseGet() {
  180. if (this.responseType !== 'arraybuffer') {
  181. return this.responseText;
  182. }
  183. var text = this.responseText;
  184. var i,
  185. n = text.length;
  186. var result = new Uint8Array(n);
  187. for (i = 0; i < n; ++i) {
  188. result[i] = text.charCodeAt(i) & 0xFF;
  189. }
  190. return result.buffer;
  191. }
  192. });
  193. })();
  194. (function checkWindowBtoaCompatibility() {
  195. if ('btoa' in globalScope) {
  196. return;
  197. }
  198. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  199. globalScope.btoa = function (chars) {
  200. var buffer = '';
  201. var i, n;
  202. for (i = 0, n = chars.length; i < n; i += 3) {
  203. var b1 = chars.charCodeAt(i) & 0xFF;
  204. var b2 = chars.charCodeAt(i + 1) & 0xFF;
  205. var b3 = chars.charCodeAt(i + 2) & 0xFF;
  206. var d1 = b1 >> 2,
  207. d2 = (b1 & 3) << 4 | b2 >> 4;
  208. var d3 = i + 1 < n ? (b2 & 0xF) << 2 | b3 >> 6 : 64;
  209. var d4 = i + 2 < n ? b3 & 0x3F : 64;
  210. buffer += digits.charAt(d1) + digits.charAt(d2) + digits.charAt(d3) + digits.charAt(d4);
  211. }
  212. return buffer;
  213. };
  214. })();
  215. (function checkWindowAtobCompatibility() {
  216. if ('atob' in globalScope) {
  217. return;
  218. }
  219. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  220. globalScope.atob = function (input) {
  221. input = input.replace(/=+$/, '');
  222. if (input.length % 4 === 1) {
  223. throw new Error('bad atob input');
  224. }
  225. for (var bc = 0, bs, buffer, idx = 0, output = ''; buffer = input.charAt(idx++); ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer, bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0) {
  226. buffer = digits.indexOf(buffer);
  227. }
  228. return output;
  229. };
  230. })();
  231. (function checkFunctionPrototypeBindCompatibility() {
  232. if (typeof Function.prototype.bind !== 'undefined') {
  233. return;
  234. }
  235. Function.prototype.bind = function functionPrototypeBind(obj) {
  236. var fn = this,
  237. headArgs = Array.prototype.slice.call(arguments, 1);
  238. var bound = function functionPrototypeBindBound() {
  239. var args = headArgs.concat(Array.prototype.slice.call(arguments));
  240. return fn.apply(obj, args);
  241. };
  242. return bound;
  243. };
  244. })();
  245. (function checkDatasetProperty() {
  246. if (!hasDOM) {
  247. return;
  248. }
  249. var div = document.createElement('div');
  250. if ('dataset' in div) {
  251. return;
  252. }
  253. Object.defineProperty(HTMLElement.prototype, 'dataset', {
  254. get: function () {
  255. if (this._dataset) {
  256. return this._dataset;
  257. }
  258. var dataset = {};
  259. for (var j = 0, jj = this.attributes.length; j < jj; j++) {
  260. var attribute = this.attributes[j];
  261. if (attribute.name.substring(0, 5) !== 'data-') {
  262. continue;
  263. }
  264. var key = attribute.name.substring(5).replace(/\-([a-z])/g, function (all, ch) {
  265. return ch.toUpperCase();
  266. });
  267. dataset[key] = attribute.value;
  268. }
  269. Object.defineProperty(this, '_dataset', {
  270. value: dataset,
  271. writable: false,
  272. enumerable: false
  273. });
  274. return dataset;
  275. },
  276. enumerable: true
  277. });
  278. })();
  279. (function checkClassListProperty() {
  280. function changeList(element, itemName, add, remove) {
  281. var s = element.className || '';
  282. var list = s.split(/\s+/g);
  283. if (list[0] === '') {
  284. list.shift();
  285. }
  286. var index = list.indexOf(itemName);
  287. if (index < 0 && add) {
  288. list.push(itemName);
  289. }
  290. if (index >= 0 && remove) {
  291. list.splice(index, 1);
  292. }
  293. element.className = list.join(' ');
  294. return index >= 0;
  295. }
  296. if (!hasDOM) {
  297. return;
  298. }
  299. var div = document.createElement('div');
  300. if ('classList' in div) {
  301. return;
  302. }
  303. var classListPrototype = {
  304. add: function (name) {
  305. changeList(this.element, name, true, false);
  306. },
  307. contains: function (name) {
  308. return changeList(this.element, name, false, false);
  309. },
  310. remove: function (name) {
  311. changeList(this.element, name, false, true);
  312. },
  313. toggle: function (name) {
  314. changeList(this.element, name, true, true);
  315. }
  316. };
  317. Object.defineProperty(HTMLElement.prototype, 'classList', {
  318. get: function () {
  319. if (this._classList) {
  320. return this._classList;
  321. }
  322. var classList = Object.create(classListPrototype, {
  323. element: {
  324. value: this,
  325. writable: false,
  326. enumerable: true
  327. }
  328. });
  329. Object.defineProperty(this, '_classList', {
  330. value: classList,
  331. writable: false,
  332. enumerable: false
  333. });
  334. return classList;
  335. },
  336. enumerable: true
  337. });
  338. })();
  339. (function checkWorkerConsoleCompatibility() {
  340. if (typeof importScripts === 'undefined' || 'console' in globalScope) {
  341. return;
  342. }
  343. var consoleTimer = {};
  344. var workerConsole = {
  345. log: function log() {
  346. var args = Array.prototype.slice.call(arguments);
  347. globalScope.postMessage({
  348. targetName: 'main',
  349. action: 'console_log',
  350. data: args
  351. });
  352. },
  353. error: function error() {
  354. var args = Array.prototype.slice.call(arguments);
  355. globalScope.postMessage({
  356. targetName: 'main',
  357. action: 'console_error',
  358. data: args
  359. });
  360. },
  361. time: function time(name) {
  362. consoleTimer[name] = Date.now();
  363. },
  364. timeEnd: function timeEnd(name) {
  365. var time = consoleTimer[name];
  366. if (!time) {
  367. throw new Error('Unknown timer name ' + name);
  368. }
  369. this.log('Timer:', name, Date.now() - time);
  370. }
  371. };
  372. globalScope.console = workerConsole;
  373. })();
  374. (function checkConsoleCompatibility() {
  375. if (!hasDOM) {
  376. return;
  377. }
  378. if (!('console' in window)) {
  379. window.console = {
  380. log: function () {},
  381. error: function () {},
  382. warn: function () {}
  383. };
  384. return;
  385. }
  386. if (!('bind' in console.log)) {
  387. console.log = function (fn) {
  388. return function (msg) {
  389. return fn(msg);
  390. };
  391. }(console.log);
  392. console.error = function (fn) {
  393. return function (msg) {
  394. return fn(msg);
  395. };
  396. }(console.error);
  397. console.warn = function (fn) {
  398. return function (msg) {
  399. return fn(msg);
  400. };
  401. }(console.warn);
  402. return;
  403. }
  404. })();
  405. (function checkOnClickCompatibility() {
  406. function ignoreIfTargetDisabled(event) {
  407. if (isDisabled(event.target)) {
  408. event.stopPropagation();
  409. }
  410. }
  411. function isDisabled(node) {
  412. return node.disabled || node.parentNode && isDisabled(node.parentNode);
  413. }
  414. if (isOpera) {
  415. document.addEventListener('click', ignoreIfTargetDisabled, true);
  416. }
  417. })();
  418. (function checkOnBlobSupport() {
  419. if (isIE || isIOSChrome) {
  420. PDFJS.disableCreateObjectURL = true;
  421. }
  422. })();
  423. (function checkNavigatorLanguage() {
  424. if (typeof navigator === 'undefined') {
  425. return;
  426. }
  427. if ('language' in navigator) {
  428. return;
  429. }
  430. PDFJS.locale = navigator.userLanguage || 'en-US';
  431. })();
  432. (function checkRangeRequests() {
  433. if (isSafari || isAndroidPre3 || isChromeWithRangeBug || isIOS) {
  434. PDFJS.disableRange = true;
  435. PDFJS.disableStream = true;
  436. }
  437. })();
  438. (function checkHistoryManipulation() {
  439. if (!hasDOM) {
  440. return;
  441. }
  442. if (!history.pushState || isAndroidPre3) {
  443. PDFJS.disableHistory = true;
  444. }
  445. })();
  446. (function checkSetPresenceInImageData() {
  447. if (!hasDOM) {
  448. return;
  449. }
  450. if (window.CanvasPixelArray) {
  451. if (typeof window.CanvasPixelArray.prototype.set !== 'function') {
  452. window.CanvasPixelArray.prototype.set = function (arr) {
  453. for (var i = 0, ii = this.length; i < ii; i++) {
  454. this[i] = arr[i];
  455. }
  456. };
  457. }
  458. } else {
  459. var polyfill = false,
  460. versionMatch;
  461. if (isChrome) {
  462. versionMatch = userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
  463. polyfill = versionMatch && parseInt(versionMatch[2]) < 21;
  464. } else if (isAndroid) {
  465. polyfill = isAndroidPre5;
  466. } else if (isSafari) {
  467. versionMatch = userAgent.match(/Version\/([0-9]+)\.([0-9]+)\.([0-9]+) Safari\//);
  468. polyfill = versionMatch && parseInt(versionMatch[1]) < 6;
  469. }
  470. if (polyfill) {
  471. var contextPrototype = window.CanvasRenderingContext2D.prototype;
  472. var createImageData = contextPrototype.createImageData;
  473. contextPrototype.createImageData = function (w, h) {
  474. var imageData = createImageData.call(this, w, h);
  475. imageData.data.set = function (arr) {
  476. for (var i = 0, ii = this.length; i < ii; i++) {
  477. this[i] = arr[i];
  478. }
  479. };
  480. return imageData;
  481. };
  482. contextPrototype = null;
  483. }
  484. }
  485. })();
  486. (function checkRequestAnimationFrame() {
  487. function installFakeAnimationFrameFunctions() {
  488. window.requestAnimationFrame = function (callback) {
  489. return window.setTimeout(callback, 20);
  490. };
  491. window.cancelAnimationFrame = function (timeoutID) {
  492. window.clearTimeout(timeoutID);
  493. };
  494. }
  495. if (!hasDOM) {
  496. return;
  497. }
  498. if (isIOS) {
  499. installFakeAnimationFrameFunctions();
  500. return;
  501. }
  502. if ('requestAnimationFrame' in window) {
  503. return;
  504. }
  505. window.requestAnimationFrame = window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame;
  506. if (window.requestAnimationFrame) {
  507. return;
  508. }
  509. installFakeAnimationFrameFunctions();
  510. })();
  511. (function checkCanvasSizeLimitation() {
  512. if (isIOS || isAndroid) {
  513. PDFJS.maxCanvasPixels = 5242880;
  514. }
  515. })();
  516. (function checkFullscreenSupport() {
  517. if (!hasDOM) {
  518. return;
  519. }
  520. if (isIE && window.parent !== window) {
  521. PDFJS.disableFullscreen = true;
  522. }
  523. })();
  524. (function checkCurrentScript() {
  525. if (!hasDOM) {
  526. return;
  527. }
  528. if ('currentScript' in document) {
  529. return;
  530. }
  531. Object.defineProperty(document, 'currentScript', {
  532. get: function () {
  533. var scripts = document.getElementsByTagName('script');
  534. return scripts[scripts.length - 1];
  535. },
  536. enumerable: true,
  537. configurable: true
  538. });
  539. })();
  540. (function checkInputTypeNumberAssign() {
  541. if (!hasDOM) {
  542. return;
  543. }
  544. var el = document.createElement('input');
  545. try {
  546. el.type = 'number';
  547. } catch (ex) {
  548. var inputProto = el.constructor.prototype;
  549. var typeProperty = Object.getOwnPropertyDescriptor(inputProto, 'type');
  550. Object.defineProperty(inputProto, 'type', {
  551. get: function () {
  552. return typeProperty.get.call(this);
  553. },
  554. set: function (value) {
  555. typeProperty.set.call(this, value === 'number' ? 'text' : value);
  556. },
  557. enumerable: true,
  558. configurable: true
  559. });
  560. }
  561. })();
  562. (function checkDocumentReadyState() {
  563. if (!hasDOM) {
  564. return;
  565. }
  566. if (!document.attachEvent) {
  567. return;
  568. }
  569. var documentProto = document.constructor.prototype;
  570. var readyStateProto = Object.getOwnPropertyDescriptor(documentProto, 'readyState');
  571. Object.defineProperty(documentProto, 'readyState', {
  572. get: function () {
  573. var value = readyStateProto.get.call(this);
  574. return value === 'interactive' ? 'loading' : value;
  575. },
  576. set: function (value) {
  577. readyStateProto.set.call(this, value);
  578. },
  579. enumerable: true,
  580. configurable: true
  581. });
  582. })();
  583. (function checkChildNodeRemove() {
  584. if (!hasDOM) {
  585. return;
  586. }
  587. if (typeof Element.prototype.remove !== 'undefined') {
  588. return;
  589. }
  590. Element.prototype.remove = function () {
  591. if (this.parentNode) {
  592. this.parentNode.removeChild(this);
  593. }
  594. };
  595. })();
  596. (function checkPromise() {
  597. if (globalScope.Promise) {
  598. if (typeof globalScope.Promise.all !== 'function') {
  599. globalScope.Promise.all = function (iterable) {
  600. var count = 0,
  601. results = [],
  602. resolve,
  603. reject;
  604. var promise = new globalScope.Promise(function (resolve_, reject_) {
  605. resolve = resolve_;
  606. reject = reject_;
  607. });
  608. iterable.forEach(function (p, i) {
  609. count++;
  610. p.then(function (result) {
  611. results[i] = result;
  612. count--;
  613. if (count === 0) {
  614. resolve(results);
  615. }
  616. }, reject);
  617. });
  618. if (count === 0) {
  619. resolve(results);
  620. }
  621. return promise;
  622. };
  623. }
  624. if (typeof globalScope.Promise.resolve !== 'function') {
  625. globalScope.Promise.resolve = function (value) {
  626. return new globalScope.Promise(function (resolve) {
  627. resolve(value);
  628. });
  629. };
  630. }
  631. if (typeof globalScope.Promise.reject !== 'function') {
  632. globalScope.Promise.reject = function (reason) {
  633. return new globalScope.Promise(function (resolve, reject) {
  634. reject(reason);
  635. });
  636. };
  637. }
  638. if (typeof globalScope.Promise.prototype.catch !== 'function') {
  639. globalScope.Promise.prototype.catch = function (onReject) {
  640. return globalScope.Promise.prototype.then(undefined, onReject);
  641. };
  642. }
  643. return;
  644. }
  645. var STATUS_PENDING = 0;
  646. var STATUS_RESOLVED = 1;
  647. var STATUS_REJECTED = 2;
  648. var REJECTION_TIMEOUT = 500;
  649. var HandlerManager = {
  650. handlers: [],
  651. running: false,
  652. unhandledRejections: [],
  653. pendingRejectionCheck: false,
  654. scheduleHandlers: function scheduleHandlers(promise) {
  655. if (promise._status === STATUS_PENDING) {
  656. return;
  657. }
  658. this.handlers = this.handlers.concat(promise._handlers);
  659. promise._handlers = [];
  660. if (this.running) {
  661. return;
  662. }
  663. this.running = true;
  664. setTimeout(this.runHandlers.bind(this), 0);
  665. },
  666. runHandlers: function runHandlers() {
  667. var RUN_TIMEOUT = 1;
  668. var timeoutAt = Date.now() + RUN_TIMEOUT;
  669. while (this.handlers.length > 0) {
  670. var handler = this.handlers.shift();
  671. var nextStatus = handler.thisPromise._status;
  672. var nextValue = handler.thisPromise._value;
  673. try {
  674. if (nextStatus === STATUS_RESOLVED) {
  675. if (typeof handler.onResolve === 'function') {
  676. nextValue = handler.onResolve(nextValue);
  677. }
  678. } else if (typeof handler.onReject === 'function') {
  679. nextValue = handler.onReject(nextValue);
  680. nextStatus = STATUS_RESOLVED;
  681. if (handler.thisPromise._unhandledRejection) {
  682. this.removeUnhandeledRejection(handler.thisPromise);
  683. }
  684. }
  685. } catch (ex) {
  686. nextStatus = STATUS_REJECTED;
  687. nextValue = ex;
  688. }
  689. handler.nextPromise._updateStatus(nextStatus, nextValue);
  690. if (Date.now() >= timeoutAt) {
  691. break;
  692. }
  693. }
  694. if (this.handlers.length > 0) {
  695. setTimeout(this.runHandlers.bind(this), 0);
  696. return;
  697. }
  698. this.running = false;
  699. },
  700. addUnhandledRejection: function addUnhandledRejection(promise) {
  701. this.unhandledRejections.push({
  702. promise: promise,
  703. time: Date.now()
  704. });
  705. this.scheduleRejectionCheck();
  706. },
  707. removeUnhandeledRejection: function removeUnhandeledRejection(promise) {
  708. promise._unhandledRejection = false;
  709. for (var i = 0; i < this.unhandledRejections.length; i++) {
  710. if (this.unhandledRejections[i].promise === promise) {
  711. this.unhandledRejections.splice(i);
  712. i--;
  713. }
  714. }
  715. },
  716. scheduleRejectionCheck: function scheduleRejectionCheck() {
  717. if (this.pendingRejectionCheck) {
  718. return;
  719. }
  720. this.pendingRejectionCheck = true;
  721. setTimeout(function rejectionCheck() {
  722. this.pendingRejectionCheck = false;
  723. var now = Date.now();
  724. for (var i = 0; i < this.unhandledRejections.length; i++) {
  725. if (now - this.unhandledRejections[i].time > REJECTION_TIMEOUT) {
  726. var unhandled = this.unhandledRejections[i].promise._value;
  727. var msg = 'Unhandled rejection: ' + unhandled;
  728. if (unhandled.stack) {
  729. msg += '\n' + unhandled.stack;
  730. }
  731. try {
  732. throw new Error(msg);
  733. } catch (_) {
  734. console.warn(msg);
  735. }
  736. this.unhandledRejections.splice(i);
  737. i--;
  738. }
  739. }
  740. if (this.unhandledRejections.length) {
  741. this.scheduleRejectionCheck();
  742. }
  743. }.bind(this), REJECTION_TIMEOUT);
  744. }
  745. };
  746. var Promise = function Promise(resolver) {
  747. this._status = STATUS_PENDING;
  748. this._handlers = [];
  749. try {
  750. resolver.call(this, this._resolve.bind(this), this._reject.bind(this));
  751. } catch (e) {
  752. this._reject(e);
  753. }
  754. };
  755. Promise.all = function Promise_all(promises) {
  756. var resolveAll, rejectAll;
  757. var deferred = new Promise(function (resolve, reject) {
  758. resolveAll = resolve;
  759. rejectAll = reject;
  760. });
  761. var unresolved = promises.length;
  762. var results = [];
  763. if (unresolved === 0) {
  764. resolveAll(results);
  765. return deferred;
  766. }
  767. function reject(reason) {
  768. if (deferred._status === STATUS_REJECTED) {
  769. return;
  770. }
  771. results = [];
  772. rejectAll(reason);
  773. }
  774. for (var i = 0, ii = promises.length; i < ii; ++i) {
  775. var promise = promises[i];
  776. var resolve = function (i) {
  777. return function (value) {
  778. if (deferred._status === STATUS_REJECTED) {
  779. return;
  780. }
  781. results[i] = value;
  782. unresolved--;
  783. if (unresolved === 0) {
  784. resolveAll(results);
  785. }
  786. };
  787. }(i);
  788. if (Promise.isPromise(promise)) {
  789. promise.then(resolve, reject);
  790. } else {
  791. resolve(promise);
  792. }
  793. }
  794. return deferred;
  795. };
  796. Promise.isPromise = function Promise_isPromise(value) {
  797. return value && typeof value.then === 'function';
  798. };
  799. Promise.resolve = function Promise_resolve(value) {
  800. return new Promise(function (resolve) {
  801. resolve(value);
  802. });
  803. };
  804. Promise.reject = function Promise_reject(reason) {
  805. return new Promise(function (resolve, reject) {
  806. reject(reason);
  807. });
  808. };
  809. Promise.prototype = {
  810. _status: null,
  811. _value: null,
  812. _handlers: null,
  813. _unhandledRejection: null,
  814. _updateStatus: function Promise__updateStatus(status, value) {
  815. if (this._status === STATUS_RESOLVED || this._status === STATUS_REJECTED) {
  816. return;
  817. }
  818. if (status === STATUS_RESOLVED && Promise.isPromise(value)) {
  819. value.then(this._updateStatus.bind(this, STATUS_RESOLVED), this._updateStatus.bind(this, STATUS_REJECTED));
  820. return;
  821. }
  822. this._status = status;
  823. this._value = value;
  824. if (status === STATUS_REJECTED && this._handlers.length === 0) {
  825. this._unhandledRejection = true;
  826. HandlerManager.addUnhandledRejection(this);
  827. }
  828. HandlerManager.scheduleHandlers(this);
  829. },
  830. _resolve: function Promise_resolve(value) {
  831. this._updateStatus(STATUS_RESOLVED, value);
  832. },
  833. _reject: function Promise_reject(reason) {
  834. this._updateStatus(STATUS_REJECTED, reason);
  835. },
  836. then: function Promise_then(onResolve, onReject) {
  837. var nextPromise = new Promise(function (resolve, reject) {
  838. this.resolve = resolve;
  839. this.reject = reject;
  840. });
  841. this._handlers.push({
  842. thisPromise: this,
  843. onResolve: onResolve,
  844. onReject: onReject,
  845. nextPromise: nextPromise
  846. });
  847. HandlerManager.scheduleHandlers(this);
  848. return nextPromise;
  849. },
  850. catch: function Promise_catch(onReject) {
  851. return this.then(undefined, onReject);
  852. }
  853. };
  854. globalScope.Promise = Promise;
  855. })();
  856. (function checkWeakMap() {
  857. if (globalScope.WeakMap) {
  858. return;
  859. }
  860. var id = 0;
  861. function WeakMap() {
  862. this.id = '$weakmap' + id++;
  863. }
  864. WeakMap.prototype = {
  865. has: function (obj) {
  866. return !!Object.getOwnPropertyDescriptor(obj, this.id);
  867. },
  868. get: function (obj, defaultValue) {
  869. return this.has(obj) ? obj[this.id] : defaultValue;
  870. },
  871. set: function (obj, value) {
  872. Object.defineProperty(obj, this.id, {
  873. value: value,
  874. enumerable: false,
  875. configurable: true
  876. });
  877. },
  878. delete: function (obj) {
  879. delete obj[this.id];
  880. }
  881. };
  882. globalScope.WeakMap = WeakMap;
  883. })();
  884. (function checkURLConstructor() {
  885. var hasWorkingUrl = false;
  886. try {
  887. if (typeof URL === 'function' && typeof URL.prototype === 'object' && 'origin' in URL.prototype) {
  888. var u = new URL('b', 'http://a');
  889. u.pathname = 'c%20d';
  890. hasWorkingUrl = u.href === 'http://a/c%20d';
  891. }
  892. } catch (e) {}
  893. if (hasWorkingUrl) {
  894. return;
  895. }
  896. var relative = Object.create(null);
  897. relative['ftp'] = 21;
  898. relative['file'] = 0;
  899. relative['gopher'] = 70;
  900. relative['http'] = 80;
  901. relative['https'] = 443;
  902. relative['ws'] = 80;
  903. relative['wss'] = 443;
  904. var relativePathDotMapping = Object.create(null);
  905. relativePathDotMapping['%2e'] = '.';
  906. relativePathDotMapping['.%2e'] = '..';
  907. relativePathDotMapping['%2e.'] = '..';
  908. relativePathDotMapping['%2e%2e'] = '..';
  909. function isRelativeScheme(scheme) {
  910. return relative[scheme] !== undefined;
  911. }
  912. function invalid() {
  913. clear.call(this);
  914. this._isInvalid = true;
  915. }
  916. function IDNAToASCII(h) {
  917. if (h === '') {
  918. invalid.call(this);
  919. }
  920. return h.toLowerCase();
  921. }
  922. function percentEscape(c) {
  923. var unicode = c.charCodeAt(0);
  924. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) === -1) {
  925. return c;
  926. }
  927. return encodeURIComponent(c);
  928. }
  929. function percentEscapeQuery(c) {
  930. var unicode = c.charCodeAt(0);
  931. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) === -1) {
  932. return c;
  933. }
  934. return encodeURIComponent(c);
  935. }
  936. var EOF,
  937. ALPHA = /[a-zA-Z]/,
  938. ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/;
  939. function parse(input, stateOverride, base) {
  940. function err(message) {
  941. errors.push(message);
  942. }
  943. var state = stateOverride || 'scheme start',
  944. cursor = 0,
  945. buffer = '',
  946. seenAt = false,
  947. seenBracket = false,
  948. errors = [];
  949. loop: while ((input[cursor - 1] !== EOF || cursor === 0) && !this._isInvalid) {
  950. var c = input[cursor];
  951. switch (state) {
  952. case 'scheme start':
  953. if (c && ALPHA.test(c)) {
  954. buffer += c.toLowerCase();
  955. state = 'scheme';
  956. } else if (!stateOverride) {
  957. buffer = '';
  958. state = 'no scheme';
  959. continue;
  960. } else {
  961. err('Invalid scheme.');
  962. break loop;
  963. }
  964. break;
  965. case 'scheme':
  966. if (c && ALPHANUMERIC.test(c)) {
  967. buffer += c.toLowerCase();
  968. } else if (c === ':') {
  969. this._scheme = buffer;
  970. buffer = '';
  971. if (stateOverride) {
  972. break loop;
  973. }
  974. if (isRelativeScheme(this._scheme)) {
  975. this._isRelative = true;
  976. }
  977. if (this._scheme === 'file') {
  978. state = 'relative';
  979. } else if (this._isRelative && base && base._scheme === this._scheme) {
  980. state = 'relative or authority';
  981. } else if (this._isRelative) {
  982. state = 'authority first slash';
  983. } else {
  984. state = 'scheme data';
  985. }
  986. } else if (!stateOverride) {
  987. buffer = '';
  988. cursor = 0;
  989. state = 'no scheme';
  990. continue;
  991. } else if (c === EOF) {
  992. break loop;
  993. } else {
  994. err('Code point not allowed in scheme: ' + c);
  995. break loop;
  996. }
  997. break;
  998. case 'scheme data':
  999. if (c === '?') {
  1000. this._query = '?';
  1001. state = 'query';
  1002. } else if (c === '#') {
  1003. this._fragment = '#';
  1004. state = 'fragment';
  1005. } else {
  1006. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  1007. this._schemeData += percentEscape(c);
  1008. }
  1009. }
  1010. break;
  1011. case 'no scheme':
  1012. if (!base || !isRelativeScheme(base._scheme)) {
  1013. err('Missing scheme.');
  1014. invalid.call(this);
  1015. } else {
  1016. state = 'relative';
  1017. continue;
  1018. }
  1019. break;
  1020. case 'relative or authority':
  1021. if (c === '/' && input[cursor + 1] === '/') {
  1022. state = 'authority ignore slashes';
  1023. } else {
  1024. err('Expected /, got: ' + c);
  1025. state = 'relative';
  1026. continue;
  1027. }
  1028. break;
  1029. case 'relative':
  1030. this._isRelative = true;
  1031. if (this._scheme !== 'file') {
  1032. this._scheme = base._scheme;
  1033. }
  1034. if (c === EOF) {
  1035. this._host = base._host;
  1036. this._port = base._port;
  1037. this._path = base._path.slice();
  1038. this._query = base._query;
  1039. this._username = base._username;
  1040. this._password = base._password;
  1041. break loop;
  1042. } else if (c === '/' || c === '\\') {
  1043. if (c === '\\') {
  1044. err('\\ is an invalid code point.');
  1045. }
  1046. state = 'relative slash';
  1047. } else if (c === '?') {
  1048. this._host = base._host;
  1049. this._port = base._port;
  1050. this._path = base._path.slice();
  1051. this._query = '?';
  1052. this._username = base._username;
  1053. this._password = base._password;
  1054. state = 'query';
  1055. } else if (c === '#') {
  1056. this._host = base._host;
  1057. this._port = base._port;
  1058. this._path = base._path.slice();
  1059. this._query = base._query;
  1060. this._fragment = '#';
  1061. this._username = base._username;
  1062. this._password = base._password;
  1063. state = 'fragment';
  1064. } else {
  1065. var nextC = input[cursor + 1];
  1066. var nextNextC = input[cursor + 2];
  1067. if (this._scheme !== 'file' || !ALPHA.test(c) || nextC !== ':' && nextC !== '|' || nextNextC !== EOF && nextNextC !== '/' && nextNextC !== '\\' && nextNextC !== '?' && nextNextC !== '#') {
  1068. this._host = base._host;
  1069. this._port = base._port;
  1070. this._username = base._username;
  1071. this._password = base._password;
  1072. this._path = base._path.slice();
  1073. this._path.pop();
  1074. }
  1075. state = 'relative path';
  1076. continue;
  1077. }
  1078. break;
  1079. case 'relative slash':
  1080. if (c === '/' || c === '\\') {
  1081. if (c === '\\') {
  1082. err('\\ is an invalid code point.');
  1083. }
  1084. if (this._scheme === 'file') {
  1085. state = 'file host';
  1086. } else {
  1087. state = 'authority ignore slashes';
  1088. }
  1089. } else {
  1090. if (this._scheme !== 'file') {
  1091. this._host = base._host;
  1092. this._port = base._port;
  1093. this._username = base._username;
  1094. this._password = base._password;
  1095. }
  1096. state = 'relative path';
  1097. continue;
  1098. }
  1099. break;
  1100. case 'authority first slash':
  1101. if (c === '/') {
  1102. state = 'authority second slash';
  1103. } else {
  1104. err('Expected \'/\', got: ' + c);
  1105. state = 'authority ignore slashes';
  1106. continue;
  1107. }
  1108. break;
  1109. case 'authority second slash':
  1110. state = 'authority ignore slashes';
  1111. if (c !== '/') {
  1112. err('Expected \'/\', got: ' + c);
  1113. continue;
  1114. }
  1115. break;
  1116. case 'authority ignore slashes':
  1117. if (c !== '/' && c !== '\\') {
  1118. state = 'authority';
  1119. continue;
  1120. } else {
  1121. err('Expected authority, got: ' + c);
  1122. }
  1123. break;
  1124. case 'authority':
  1125. if (c === '@') {
  1126. if (seenAt) {
  1127. err('@ already seen.');
  1128. buffer += '%40';
  1129. }
  1130. seenAt = true;
  1131. for (var i = 0; i < buffer.length; i++) {
  1132. var cp = buffer[i];
  1133. if (cp === '\t' || cp === '\n' || cp === '\r') {
  1134. err('Invalid whitespace in authority.');
  1135. continue;
  1136. }
  1137. if (cp === ':' && this._password === null) {
  1138. this._password = '';
  1139. continue;
  1140. }
  1141. var tempC = percentEscape(cp);
  1142. if (this._password !== null) {
  1143. this._password += tempC;
  1144. } else {
  1145. this._username += tempC;
  1146. }
  1147. }
  1148. buffer = '';
  1149. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  1150. cursor -= buffer.length;
  1151. buffer = '';
  1152. state = 'host';
  1153. continue;
  1154. } else {
  1155. buffer += c;
  1156. }
  1157. break;
  1158. case 'file host':
  1159. if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  1160. if (buffer.length === 2 && ALPHA.test(buffer[0]) && (buffer[1] === ':' || buffer[1] === '|')) {
  1161. state = 'relative path';
  1162. } else if (buffer.length === 0) {
  1163. state = 'relative path start';
  1164. } else {
  1165. this._host = IDNAToASCII.call(this, buffer);
  1166. buffer = '';
  1167. state = 'relative path start';
  1168. }
  1169. continue;
  1170. } else if (c === '\t' || c === '\n' || c === '\r') {
  1171. err('Invalid whitespace in file host.');
  1172. } else {
  1173. buffer += c;
  1174. }
  1175. break;
  1176. case 'host':
  1177. case 'hostname':
  1178. if (c === ':' && !seenBracket) {
  1179. this._host = IDNAToASCII.call(this, buffer);
  1180. buffer = '';
  1181. state = 'port';
  1182. if (stateOverride === 'hostname') {
  1183. break loop;
  1184. }
  1185. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  1186. this._host = IDNAToASCII.call(this, buffer);
  1187. buffer = '';
  1188. state = 'relative path start';
  1189. if (stateOverride) {
  1190. break loop;
  1191. }
  1192. continue;
  1193. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  1194. if (c === '[') {
  1195. seenBracket = true;
  1196. } else if (c === ']') {
  1197. seenBracket = false;
  1198. }
  1199. buffer += c;
  1200. } else {
  1201. err('Invalid code point in host/hostname: ' + c);
  1202. }
  1203. break;
  1204. case 'port':
  1205. if (/[0-9]/.test(c)) {
  1206. buffer += c;
  1207. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#' || stateOverride) {
  1208. if (buffer !== '') {
  1209. var temp = parseInt(buffer, 10);
  1210. if (temp !== relative[this._scheme]) {
  1211. this._port = temp + '';
  1212. }
  1213. buffer = '';
  1214. }
  1215. if (stateOverride) {
  1216. break loop;
  1217. }
  1218. state = 'relative path start';
  1219. continue;
  1220. } else if (c === '\t' || c === '\n' || c === '\r') {
  1221. err('Invalid code point in port: ' + c);
  1222. } else {
  1223. invalid.call(this);
  1224. }
  1225. break;
  1226. case 'relative path start':
  1227. if (c === '\\') {
  1228. err('\'\\\' not allowed in path.');
  1229. }
  1230. state = 'relative path';
  1231. if (c !== '/' && c !== '\\') {
  1232. continue;
  1233. }
  1234. break;
  1235. case 'relative path':
  1236. if (c === EOF || c === '/' || c === '\\' || !stateOverride && (c === '?' || c === '#')) {
  1237. if (c === '\\') {
  1238. err('\\ not allowed in relative path.');
  1239. }
  1240. var tmp;
  1241. if (tmp = relativePathDotMapping[buffer.toLowerCase()]) {
  1242. buffer = tmp;
  1243. }
  1244. if (buffer === '..') {
  1245. this._path.pop();
  1246. if (c !== '/' && c !== '\\') {
  1247. this._path.push('');
  1248. }
  1249. } else if (buffer === '.' && c !== '/' && c !== '\\') {
  1250. this._path.push('');
  1251. } else if (buffer !== '.') {
  1252. if (this._scheme === 'file' && this._path.length === 0 && buffer.length === 2 && ALPHA.test(buffer[0]) && buffer[1] === '|') {
  1253. buffer = buffer[0] + ':';
  1254. }
  1255. this._path.push(buffer);
  1256. }
  1257. buffer = '';
  1258. if (c === '?') {
  1259. this._query = '?';
  1260. state = 'query';
  1261. } else if (c === '#') {
  1262. this._fragment = '#';
  1263. state = 'fragment';
  1264. }
  1265. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  1266. buffer += percentEscape(c);
  1267. }
  1268. break;
  1269. case 'query':
  1270. if (!stateOverride && c === '#') {
  1271. this._fragment = '#';
  1272. state = 'fragment';
  1273. } else if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  1274. this._query += percentEscapeQuery(c);
  1275. }
  1276. break;
  1277. case 'fragment':
  1278. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  1279. this._fragment += c;
  1280. }
  1281. break;
  1282. }
  1283. cursor++;
  1284. }
  1285. }
  1286. function clear() {
  1287. this._scheme = '';
  1288. this._schemeData = '';
  1289. this._username = '';
  1290. this._password = null;
  1291. this._host = '';
  1292. this._port = '';
  1293. this._path = [];
  1294. this._query = '';
  1295. this._fragment = '';
  1296. this._isInvalid = false;
  1297. this._isRelative = false;
  1298. }
  1299. function JURL(url, base) {
  1300. if (base !== undefined && !(base instanceof JURL)) {
  1301. base = new JURL(String(base));
  1302. }
  1303. this._url = url;
  1304. clear.call(this);
  1305. var input = url.replace(/^[ \t\r\n\f]+|[ \t\r\n\f]+$/g, '');
  1306. parse.call(this, input, null, base);
  1307. }
  1308. JURL.prototype = {
  1309. toString: function () {
  1310. return this.href;
  1311. },
  1312. get href() {
  1313. if (this._isInvalid) {
  1314. return this._url;
  1315. }
  1316. var authority = '';
  1317. if (this._username !== '' || this._password !== null) {
  1318. authority = this._username + (this._password !== null ? ':' + this._password : '') + '@';
  1319. }
  1320. return this.protocol + (this._isRelative ? '//' + authority + this.host : '') + this.pathname + this._query + this._fragment;
  1321. },
  1322. set href(href) {
  1323. clear.call(this);
  1324. parse.call(this, href);
  1325. },
  1326. get protocol() {
  1327. return this._scheme + ':';
  1328. },
  1329. set protocol(protocol) {
  1330. if (this._isInvalid) {
  1331. return;
  1332. }
  1333. parse.call(this, protocol + ':', 'scheme start');
  1334. },
  1335. get host() {
  1336. return this._isInvalid ? '' : this._port ? this._host + ':' + this._port : this._host;
  1337. },
  1338. set host(host) {
  1339. if (this._isInvalid || !this._isRelative) {
  1340. return;
  1341. }
  1342. parse.call(this, host, 'host');
  1343. },
  1344. get hostname() {
  1345. return this._host;
  1346. },
  1347. set hostname(hostname) {
  1348. if (this._isInvalid || !this._isRelative) {
  1349. return;
  1350. }
  1351. parse.call(this, hostname, 'hostname');
  1352. },
  1353. get port() {
  1354. return this._port;
  1355. },
  1356. set port(port) {
  1357. if (this._isInvalid || !this._isRelative) {
  1358. return;
  1359. }
  1360. parse.call(this, port, 'port');
  1361. },
  1362. get pathname() {
  1363. return this._isInvalid ? '' : this._isRelative ? '/' + this._path.join('/') : this._schemeData;
  1364. },
  1365. set pathname(pathname) {
  1366. if (this._isInvalid || !this._isRelative) {
  1367. return;
  1368. }
  1369. this._path = [];
  1370. parse.call(this, pathname, 'relative path start');
  1371. },
  1372. get search() {
  1373. return this._isInvalid || !this._query || this._query === '?' ? '' : this._query;
  1374. },
  1375. set search(search) {
  1376. if (this._isInvalid || !this._isRelative) {
  1377. return;
  1378. }
  1379. this._query = '?';
  1380. if (search[0] === '?') {
  1381. search = search.slice(1);
  1382. }
  1383. parse.call(this, search, 'query');
  1384. },
  1385. get hash() {
  1386. return this._isInvalid || !this._fragment || this._fragment === '#' ? '' : this._fragment;
  1387. },
  1388. set hash(hash) {
  1389. if (this._isInvalid) {
  1390. return;
  1391. }
  1392. this._fragment = '#';
  1393. if (hash[0] === '#') {
  1394. hash = hash.slice(1);
  1395. }
  1396. parse.call(this, hash, 'fragment');
  1397. },
  1398. get origin() {
  1399. var host;
  1400. if (this._isInvalid || !this._scheme) {
  1401. return '';
  1402. }
  1403. switch (this._scheme) {
  1404. case 'data':
  1405. case 'file':
  1406. case 'javascript':
  1407. case 'mailto':
  1408. return 'null';
  1409. }
  1410. host = this.host;
  1411. if (!host) {
  1412. return '';
  1413. }
  1414. return this._scheme + '://' + host;
  1415. }
  1416. };
  1417. var OriginalURL = globalScope.URL;
  1418. if (OriginalURL) {
  1419. JURL.createObjectURL = function (blob) {
  1420. return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
  1421. };
  1422. JURL.revokeObjectURL = function (url) {
  1423. OriginalURL.revokeObjectURL(url);
  1424. };
  1425. }
  1426. globalScope.URL = JURL;
  1427. })();
  1428. }