2
0

compatibility.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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 (!('requestAnimationFrame' in window)) {
  507. installFakeAnimationFrameFunctions();
  508. }
  509. })();
  510. (function checkCanvasSizeLimitation() {
  511. if (isIOS || isAndroid) {
  512. PDFJS.maxCanvasPixels = 5242880;
  513. }
  514. })();
  515. (function checkFullscreenSupport() {
  516. if (!hasDOM) {
  517. return;
  518. }
  519. if (isIE && window.parent !== window) {
  520. PDFJS.disableFullscreen = true;
  521. }
  522. })();
  523. (function checkCurrentScript() {
  524. if (!hasDOM) {
  525. return;
  526. }
  527. if ('currentScript' in document) {
  528. return;
  529. }
  530. Object.defineProperty(document, 'currentScript', {
  531. get: function () {
  532. var scripts = document.getElementsByTagName('script');
  533. return scripts[scripts.length - 1];
  534. },
  535. enumerable: true,
  536. configurable: true
  537. });
  538. })();
  539. (function checkInputTypeNumberAssign() {
  540. if (!hasDOM) {
  541. return;
  542. }
  543. var el = document.createElement('input');
  544. try {
  545. el.type = 'number';
  546. } catch (ex) {
  547. var inputProto = el.constructor.prototype;
  548. var typeProperty = Object.getOwnPropertyDescriptor(inputProto, 'type');
  549. Object.defineProperty(inputProto, 'type', {
  550. get: function () {
  551. return typeProperty.get.call(this);
  552. },
  553. set: function (value) {
  554. typeProperty.set.call(this, value === 'number' ? 'text' : value);
  555. },
  556. enumerable: true,
  557. configurable: true
  558. });
  559. }
  560. })();
  561. (function checkDocumentReadyState() {
  562. if (!hasDOM) {
  563. return;
  564. }
  565. if (!document.attachEvent) {
  566. return;
  567. }
  568. var documentProto = document.constructor.prototype;
  569. var readyStateProto = Object.getOwnPropertyDescriptor(documentProto, 'readyState');
  570. Object.defineProperty(documentProto, 'readyState', {
  571. get: function () {
  572. var value = readyStateProto.get.call(this);
  573. return value === 'interactive' ? 'loading' : value;
  574. },
  575. set: function (value) {
  576. readyStateProto.set.call(this, value);
  577. },
  578. enumerable: true,
  579. configurable: true
  580. });
  581. })();
  582. (function checkChildNodeRemove() {
  583. if (!hasDOM) {
  584. return;
  585. }
  586. if (typeof Element.prototype.remove !== 'undefined') {
  587. return;
  588. }
  589. Element.prototype.remove = function () {
  590. if (this.parentNode) {
  591. this.parentNode.removeChild(this);
  592. }
  593. };
  594. })();
  595. (function checkPromise() {
  596. if (globalScope.Promise) {
  597. if (typeof globalScope.Promise.all !== 'function') {
  598. globalScope.Promise.all = function (iterable) {
  599. var count = 0,
  600. results = [],
  601. resolve,
  602. reject;
  603. var promise = new globalScope.Promise(function (resolve_, reject_) {
  604. resolve = resolve_;
  605. reject = reject_;
  606. });
  607. iterable.forEach(function (p, i) {
  608. count++;
  609. p.then(function (result) {
  610. results[i] = result;
  611. count--;
  612. if (count === 0) {
  613. resolve(results);
  614. }
  615. }, reject);
  616. });
  617. if (count === 0) {
  618. resolve(results);
  619. }
  620. return promise;
  621. };
  622. }
  623. if (typeof globalScope.Promise.resolve !== 'function') {
  624. globalScope.Promise.resolve = function (value) {
  625. return new globalScope.Promise(function (resolve) {
  626. resolve(value);
  627. });
  628. };
  629. }
  630. if (typeof globalScope.Promise.reject !== 'function') {
  631. globalScope.Promise.reject = function (reason) {
  632. return new globalScope.Promise(function (resolve, reject) {
  633. reject(reason);
  634. });
  635. };
  636. }
  637. if (typeof globalScope.Promise.prototype.catch !== 'function') {
  638. globalScope.Promise.prototype.catch = function (onReject) {
  639. return globalScope.Promise.prototype.then(undefined, onReject);
  640. };
  641. }
  642. return;
  643. }
  644. var STATUS_PENDING = 0;
  645. var STATUS_RESOLVED = 1;
  646. var STATUS_REJECTED = 2;
  647. var REJECTION_TIMEOUT = 500;
  648. var HandlerManager = {
  649. handlers: [],
  650. running: false,
  651. unhandledRejections: [],
  652. pendingRejectionCheck: false,
  653. scheduleHandlers: function scheduleHandlers(promise) {
  654. if (promise._status === STATUS_PENDING) {
  655. return;
  656. }
  657. this.handlers = this.handlers.concat(promise._handlers);
  658. promise._handlers = [];
  659. if (this.running) {
  660. return;
  661. }
  662. this.running = true;
  663. setTimeout(this.runHandlers.bind(this), 0);
  664. },
  665. runHandlers: function runHandlers() {
  666. var RUN_TIMEOUT = 1;
  667. var timeoutAt = Date.now() + RUN_TIMEOUT;
  668. while (this.handlers.length > 0) {
  669. var handler = this.handlers.shift();
  670. var nextStatus = handler.thisPromise._status;
  671. var nextValue = handler.thisPromise._value;
  672. try {
  673. if (nextStatus === STATUS_RESOLVED) {
  674. if (typeof handler.onResolve === 'function') {
  675. nextValue = handler.onResolve(nextValue);
  676. }
  677. } else if (typeof handler.onReject === 'function') {
  678. nextValue = handler.onReject(nextValue);
  679. nextStatus = STATUS_RESOLVED;
  680. if (handler.thisPromise._unhandledRejection) {
  681. this.removeUnhandeledRejection(handler.thisPromise);
  682. }
  683. }
  684. } catch (ex) {
  685. nextStatus = STATUS_REJECTED;
  686. nextValue = ex;
  687. }
  688. handler.nextPromise._updateStatus(nextStatus, nextValue);
  689. if (Date.now() >= timeoutAt) {
  690. break;
  691. }
  692. }
  693. if (this.handlers.length > 0) {
  694. setTimeout(this.runHandlers.bind(this), 0);
  695. return;
  696. }
  697. this.running = false;
  698. },
  699. addUnhandledRejection: function addUnhandledRejection(promise) {
  700. this.unhandledRejections.push({
  701. promise: promise,
  702. time: Date.now()
  703. });
  704. this.scheduleRejectionCheck();
  705. },
  706. removeUnhandeledRejection: function removeUnhandeledRejection(promise) {
  707. promise._unhandledRejection = false;
  708. for (var i = 0; i < this.unhandledRejections.length; i++) {
  709. if (this.unhandledRejections[i].promise === promise) {
  710. this.unhandledRejections.splice(i);
  711. i--;
  712. }
  713. }
  714. },
  715. scheduleRejectionCheck: function scheduleRejectionCheck() {
  716. if (this.pendingRejectionCheck) {
  717. return;
  718. }
  719. this.pendingRejectionCheck = true;
  720. setTimeout(function rejectionCheck() {
  721. this.pendingRejectionCheck = false;
  722. var now = Date.now();
  723. for (var i = 0; i < this.unhandledRejections.length; i++) {
  724. if (now - this.unhandledRejections[i].time > REJECTION_TIMEOUT) {
  725. var unhandled = this.unhandledRejections[i].promise._value;
  726. var msg = 'Unhandled rejection: ' + unhandled;
  727. if (unhandled.stack) {
  728. msg += '\n' + unhandled.stack;
  729. }
  730. try {
  731. throw new Error(msg);
  732. } catch (_) {
  733. console.warn(msg);
  734. }
  735. this.unhandledRejections.splice(i);
  736. i--;
  737. }
  738. }
  739. if (this.unhandledRejections.length) {
  740. this.scheduleRejectionCheck();
  741. }
  742. }.bind(this), REJECTION_TIMEOUT);
  743. }
  744. };
  745. var Promise = function Promise(resolver) {
  746. this._status = STATUS_PENDING;
  747. this._handlers = [];
  748. try {
  749. resolver.call(this, this._resolve.bind(this), this._reject.bind(this));
  750. } catch (e) {
  751. this._reject(e);
  752. }
  753. };
  754. Promise.all = function Promise_all(promises) {
  755. var resolveAll, rejectAll;
  756. var deferred = new Promise(function (resolve, reject) {
  757. resolveAll = resolve;
  758. rejectAll = reject;
  759. });
  760. var unresolved = promises.length;
  761. var results = [];
  762. if (unresolved === 0) {
  763. resolveAll(results);
  764. return deferred;
  765. }
  766. function reject(reason) {
  767. if (deferred._status === STATUS_REJECTED) {
  768. return;
  769. }
  770. results = [];
  771. rejectAll(reason);
  772. }
  773. for (var i = 0, ii = promises.length; i < ii; ++i) {
  774. var promise = promises[i];
  775. var resolve = function (i) {
  776. return function (value) {
  777. if (deferred._status === STATUS_REJECTED) {
  778. return;
  779. }
  780. results[i] = value;
  781. unresolved--;
  782. if (unresolved === 0) {
  783. resolveAll(results);
  784. }
  785. };
  786. }(i);
  787. if (Promise.isPromise(promise)) {
  788. promise.then(resolve, reject);
  789. } else {
  790. resolve(promise);
  791. }
  792. }
  793. return deferred;
  794. };
  795. Promise.isPromise = function Promise_isPromise(value) {
  796. return value && typeof value.then === 'function';
  797. };
  798. Promise.resolve = function Promise_resolve(value) {
  799. return new Promise(function (resolve) {
  800. resolve(value);
  801. });
  802. };
  803. Promise.reject = function Promise_reject(reason) {
  804. return new Promise(function (resolve, reject) {
  805. reject(reason);
  806. });
  807. };
  808. Promise.prototype = {
  809. _status: null,
  810. _value: null,
  811. _handlers: null,
  812. _unhandledRejection: null,
  813. _updateStatus: function Promise__updateStatus(status, value) {
  814. if (this._status === STATUS_RESOLVED || this._status === STATUS_REJECTED) {
  815. return;
  816. }
  817. if (status === STATUS_RESOLVED && Promise.isPromise(value)) {
  818. value.then(this._updateStatus.bind(this, STATUS_RESOLVED), this._updateStatus.bind(this, STATUS_REJECTED));
  819. return;
  820. }
  821. this._status = status;
  822. this._value = value;
  823. if (status === STATUS_REJECTED && this._handlers.length === 0) {
  824. this._unhandledRejection = true;
  825. HandlerManager.addUnhandledRejection(this);
  826. }
  827. HandlerManager.scheduleHandlers(this);
  828. },
  829. _resolve: function Promise_resolve(value) {
  830. this._updateStatus(STATUS_RESOLVED, value);
  831. },
  832. _reject: function Promise_reject(reason) {
  833. this._updateStatus(STATUS_REJECTED, reason);
  834. },
  835. then: function Promise_then(onResolve, onReject) {
  836. var nextPromise = new Promise(function (resolve, reject) {
  837. this.resolve = resolve;
  838. this.reject = reject;
  839. });
  840. this._handlers.push({
  841. thisPromise: this,
  842. onResolve: onResolve,
  843. onReject: onReject,
  844. nextPromise: nextPromise
  845. });
  846. HandlerManager.scheduleHandlers(this);
  847. return nextPromise;
  848. },
  849. catch: function Promise_catch(onReject) {
  850. return this.then(undefined, onReject);
  851. }
  852. };
  853. globalScope.Promise = Promise;
  854. })();
  855. (function checkWeakMap() {
  856. if (globalScope.WeakMap) {
  857. return;
  858. }
  859. var id = 0;
  860. function WeakMap() {
  861. this.id = '$weakmap' + id++;
  862. }
  863. WeakMap.prototype = {
  864. has: function (obj) {
  865. return !!Object.getOwnPropertyDescriptor(obj, this.id);
  866. },
  867. get: function (obj, defaultValue) {
  868. return this.has(obj) ? obj[this.id] : defaultValue;
  869. },
  870. set: function (obj, value) {
  871. Object.defineProperty(obj, this.id, {
  872. value: value,
  873. enumerable: false,
  874. configurable: true
  875. });
  876. },
  877. delete: function (obj) {
  878. delete obj[this.id];
  879. }
  880. };
  881. globalScope.WeakMap = WeakMap;
  882. })();
  883. (function checkURLConstructor() {
  884. var hasWorkingUrl = false;
  885. try {
  886. if (typeof URL === 'function' && typeof URL.prototype === 'object' && 'origin' in URL.prototype) {
  887. var u = new URL('b', 'http://a');
  888. u.pathname = 'c%20d';
  889. hasWorkingUrl = u.href === 'http://a/c%20d';
  890. }
  891. } catch (e) {}
  892. if (hasWorkingUrl) {
  893. return;
  894. }
  895. var relative = Object.create(null);
  896. relative['ftp'] = 21;
  897. relative['file'] = 0;
  898. relative['gopher'] = 70;
  899. relative['http'] = 80;
  900. relative['https'] = 443;
  901. relative['ws'] = 80;
  902. relative['wss'] = 443;
  903. var relativePathDotMapping = Object.create(null);
  904. relativePathDotMapping['%2e'] = '.';
  905. relativePathDotMapping['.%2e'] = '..';
  906. relativePathDotMapping['%2e.'] = '..';
  907. relativePathDotMapping['%2e%2e'] = '..';
  908. function isRelativeScheme(scheme) {
  909. return relative[scheme] !== undefined;
  910. }
  911. function invalid() {
  912. clear.call(this);
  913. this._isInvalid = true;
  914. }
  915. function IDNAToASCII(h) {
  916. if (h === '') {
  917. invalid.call(this);
  918. }
  919. return h.toLowerCase();
  920. }
  921. function percentEscape(c) {
  922. var unicode = c.charCodeAt(0);
  923. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) === -1) {
  924. return c;
  925. }
  926. return encodeURIComponent(c);
  927. }
  928. function percentEscapeQuery(c) {
  929. var unicode = c.charCodeAt(0);
  930. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) === -1) {
  931. return c;
  932. }
  933. return encodeURIComponent(c);
  934. }
  935. var EOF,
  936. ALPHA = /[a-zA-Z]/,
  937. ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/;
  938. function parse(input, stateOverride, base) {
  939. function err(message) {
  940. errors.push(message);
  941. }
  942. var state = stateOverride || 'scheme start',
  943. cursor = 0,
  944. buffer = '',
  945. seenAt = false,
  946. seenBracket = false,
  947. errors = [];
  948. loop: while ((input[cursor - 1] !== EOF || cursor === 0) && !this._isInvalid) {
  949. var c = input[cursor];
  950. switch (state) {
  951. case 'scheme start':
  952. if (c && ALPHA.test(c)) {
  953. buffer += c.toLowerCase();
  954. state = 'scheme';
  955. } else if (!stateOverride) {
  956. buffer = '';
  957. state = 'no scheme';
  958. continue;
  959. } else {
  960. err('Invalid scheme.');
  961. break loop;
  962. }
  963. break;
  964. case 'scheme':
  965. if (c && ALPHANUMERIC.test(c)) {
  966. buffer += c.toLowerCase();
  967. } else if (c === ':') {
  968. this._scheme = buffer;
  969. buffer = '';
  970. if (stateOverride) {
  971. break loop;
  972. }
  973. if (isRelativeScheme(this._scheme)) {
  974. this._isRelative = true;
  975. }
  976. if (this._scheme === 'file') {
  977. state = 'relative';
  978. } else if (this._isRelative && base && base._scheme === this._scheme) {
  979. state = 'relative or authority';
  980. } else if (this._isRelative) {
  981. state = 'authority first slash';
  982. } else {
  983. state = 'scheme data';
  984. }
  985. } else if (!stateOverride) {
  986. buffer = '';
  987. cursor = 0;
  988. state = 'no scheme';
  989. continue;
  990. } else if (c === EOF) {
  991. break loop;
  992. } else {
  993. err('Code point not allowed in scheme: ' + c);
  994. break loop;
  995. }
  996. break;
  997. case 'scheme data':
  998. if (c === '?') {
  999. this._query = '?';
  1000. state = 'query';
  1001. } else if (c === '#') {
  1002. this._fragment = '#';
  1003. state = 'fragment';
  1004. } else {
  1005. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  1006. this._schemeData += percentEscape(c);
  1007. }
  1008. }
  1009. break;
  1010. case 'no scheme':
  1011. if (!base || !isRelativeScheme(base._scheme)) {
  1012. err('Missing scheme.');
  1013. invalid.call(this);
  1014. } else {
  1015. state = 'relative';
  1016. continue;
  1017. }
  1018. break;
  1019. case 'relative or authority':
  1020. if (c === '/' && input[cursor + 1] === '/') {
  1021. state = 'authority ignore slashes';
  1022. } else {
  1023. err('Expected /, got: ' + c);
  1024. state = 'relative';
  1025. continue;
  1026. }
  1027. break;
  1028. case 'relative':
  1029. this._isRelative = true;
  1030. if (this._scheme !== 'file') {
  1031. this._scheme = base._scheme;
  1032. }
  1033. if (c === EOF) {
  1034. this._host = base._host;
  1035. this._port = base._port;
  1036. this._path = base._path.slice();
  1037. this._query = base._query;
  1038. this._username = base._username;
  1039. this._password = base._password;
  1040. break loop;
  1041. } else if (c === '/' || c === '\\') {
  1042. if (c === '\\') {
  1043. err('\\ is an invalid code point.');
  1044. }
  1045. state = 'relative slash';
  1046. } else if (c === '?') {
  1047. this._host = base._host;
  1048. this._port = base._port;
  1049. this._path = base._path.slice();
  1050. this._query = '?';
  1051. this._username = base._username;
  1052. this._password = base._password;
  1053. state = 'query';
  1054. } else if (c === '#') {
  1055. this._host = base._host;
  1056. this._port = base._port;
  1057. this._path = base._path.slice();
  1058. this._query = base._query;
  1059. this._fragment = '#';
  1060. this._username = base._username;
  1061. this._password = base._password;
  1062. state = 'fragment';
  1063. } else {
  1064. var nextC = input[cursor + 1];
  1065. var nextNextC = input[cursor + 2];
  1066. if (this._scheme !== 'file' || !ALPHA.test(c) || nextC !== ':' && nextC !== '|' || nextNextC !== EOF && nextNextC !== '/' && nextNextC !== '\\' && nextNextC !== '?' && nextNextC !== '#') {
  1067. this._host = base._host;
  1068. this._port = base._port;
  1069. this._username = base._username;
  1070. this._password = base._password;
  1071. this._path = base._path.slice();
  1072. this._path.pop();
  1073. }
  1074. state = 'relative path';
  1075. continue;
  1076. }
  1077. break;
  1078. case 'relative slash':
  1079. if (c === '/' || c === '\\') {
  1080. if (c === '\\') {
  1081. err('\\ is an invalid code point.');
  1082. }
  1083. if (this._scheme === 'file') {
  1084. state = 'file host';
  1085. } else {
  1086. state = 'authority ignore slashes';
  1087. }
  1088. } else {
  1089. if (this._scheme !== 'file') {
  1090. this._host = base._host;
  1091. this._port = base._port;
  1092. this._username = base._username;
  1093. this._password = base._password;
  1094. }
  1095. state = 'relative path';
  1096. continue;
  1097. }
  1098. break;
  1099. case 'authority first slash':
  1100. if (c === '/') {
  1101. state = 'authority second slash';
  1102. } else {
  1103. err('Expected \'/\', got: ' + c);
  1104. state = 'authority ignore slashes';
  1105. continue;
  1106. }
  1107. break;
  1108. case 'authority second slash':
  1109. state = 'authority ignore slashes';
  1110. if (c !== '/') {
  1111. err('Expected \'/\', got: ' + c);
  1112. continue;
  1113. }
  1114. break;
  1115. case 'authority ignore slashes':
  1116. if (c !== '/' && c !== '\\') {
  1117. state = 'authority';
  1118. continue;
  1119. } else {
  1120. err('Expected authority, got: ' + c);
  1121. }
  1122. break;
  1123. case 'authority':
  1124. if (c === '@') {
  1125. if (seenAt) {
  1126. err('@ already seen.');
  1127. buffer += '%40';
  1128. }
  1129. seenAt = true;
  1130. for (var i = 0; i < buffer.length; i++) {
  1131. var cp = buffer[i];
  1132. if (cp === '\t' || cp === '\n' || cp === '\r') {
  1133. err('Invalid whitespace in authority.');
  1134. continue;
  1135. }
  1136. if (cp === ':' && this._password === null) {
  1137. this._password = '';
  1138. continue;
  1139. }
  1140. var tempC = percentEscape(cp);
  1141. if (this._password !== null) {
  1142. this._password += tempC;
  1143. } else {
  1144. this._username += tempC;
  1145. }
  1146. }
  1147. buffer = '';
  1148. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  1149. cursor -= buffer.length;
  1150. buffer = '';
  1151. state = 'host';
  1152. continue;
  1153. } else {
  1154. buffer += c;
  1155. }
  1156. break;
  1157. case 'file host':
  1158. if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  1159. if (buffer.length === 2 && ALPHA.test(buffer[0]) && (buffer[1] === ':' || buffer[1] === '|')) {
  1160. state = 'relative path';
  1161. } else if (buffer.length === 0) {
  1162. state = 'relative path start';
  1163. } else {
  1164. this._host = IDNAToASCII.call(this, buffer);
  1165. buffer = '';
  1166. state = 'relative path start';
  1167. }
  1168. continue;
  1169. } else if (c === '\t' || c === '\n' || c === '\r') {
  1170. err('Invalid whitespace in file host.');
  1171. } else {
  1172. buffer += c;
  1173. }
  1174. break;
  1175. case 'host':
  1176. case 'hostname':
  1177. if (c === ':' && !seenBracket) {
  1178. this._host = IDNAToASCII.call(this, buffer);
  1179. buffer = '';
  1180. state = 'port';
  1181. if (stateOverride === 'hostname') {
  1182. break loop;
  1183. }
  1184. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  1185. this._host = IDNAToASCII.call(this, buffer);
  1186. buffer = '';
  1187. state = 'relative path start';
  1188. if (stateOverride) {
  1189. break loop;
  1190. }
  1191. continue;
  1192. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  1193. if (c === '[') {
  1194. seenBracket = true;
  1195. } else if (c === ']') {
  1196. seenBracket = false;
  1197. }
  1198. buffer += c;
  1199. } else {
  1200. err('Invalid code point in host/hostname: ' + c);
  1201. }
  1202. break;
  1203. case 'port':
  1204. if (/[0-9]/.test(c)) {
  1205. buffer += c;
  1206. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#' || stateOverride) {
  1207. if (buffer !== '') {
  1208. var temp = parseInt(buffer, 10);
  1209. if (temp !== relative[this._scheme]) {
  1210. this._port = temp + '';
  1211. }
  1212. buffer = '';
  1213. }
  1214. if (stateOverride) {
  1215. break loop;
  1216. }
  1217. state = 'relative path start';
  1218. continue;
  1219. } else if (c === '\t' || c === '\n' || c === '\r') {
  1220. err('Invalid code point in port: ' + c);
  1221. } else {
  1222. invalid.call(this);
  1223. }
  1224. break;
  1225. case 'relative path start':
  1226. if (c === '\\') {
  1227. err('\'\\\' not allowed in path.');
  1228. }
  1229. state = 'relative path';
  1230. if (c !== '/' && c !== '\\') {
  1231. continue;
  1232. }
  1233. break;
  1234. case 'relative path':
  1235. if (c === EOF || c === '/' || c === '\\' || !stateOverride && (c === '?' || c === '#')) {
  1236. if (c === '\\') {
  1237. err('\\ not allowed in relative path.');
  1238. }
  1239. var tmp;
  1240. if (tmp = relativePathDotMapping[buffer.toLowerCase()]) {
  1241. buffer = tmp;
  1242. }
  1243. if (buffer === '..') {
  1244. this._path.pop();
  1245. if (c !== '/' && c !== '\\') {
  1246. this._path.push('');
  1247. }
  1248. } else if (buffer === '.' && c !== '/' && c !== '\\') {
  1249. this._path.push('');
  1250. } else if (buffer !== '.') {
  1251. if (this._scheme === 'file' && this._path.length === 0 && buffer.length === 2 && ALPHA.test(buffer[0]) && buffer[1] === '|') {
  1252. buffer = buffer[0] + ':';
  1253. }
  1254. this._path.push(buffer);
  1255. }
  1256. buffer = '';
  1257. if (c === '?') {
  1258. this._query = '?';
  1259. state = 'query';
  1260. } else if (c === '#') {
  1261. this._fragment = '#';
  1262. state = 'fragment';
  1263. }
  1264. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  1265. buffer += percentEscape(c);
  1266. }
  1267. break;
  1268. case 'query':
  1269. if (!stateOverride && c === '#') {
  1270. this._fragment = '#';
  1271. state = 'fragment';
  1272. } else if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  1273. this._query += percentEscapeQuery(c);
  1274. }
  1275. break;
  1276. case 'fragment':
  1277. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  1278. this._fragment += c;
  1279. }
  1280. break;
  1281. }
  1282. cursor++;
  1283. }
  1284. }
  1285. function clear() {
  1286. this._scheme = '';
  1287. this._schemeData = '';
  1288. this._username = '';
  1289. this._password = null;
  1290. this._host = '';
  1291. this._port = '';
  1292. this._path = [];
  1293. this._query = '';
  1294. this._fragment = '';
  1295. this._isInvalid = false;
  1296. this._isRelative = false;
  1297. }
  1298. function JURL(url, base) {
  1299. if (base !== undefined && !(base instanceof JURL)) {
  1300. base = new JURL(String(base));
  1301. }
  1302. this._url = url;
  1303. clear.call(this);
  1304. var input = url.replace(/^[ \t\r\n\f]+|[ \t\r\n\f]+$/g, '');
  1305. parse.call(this, input, null, base);
  1306. }
  1307. JURL.prototype = {
  1308. toString: function () {
  1309. return this.href;
  1310. },
  1311. get href() {
  1312. if (this._isInvalid) {
  1313. return this._url;
  1314. }
  1315. var authority = '';
  1316. if (this._username !== '' || this._password !== null) {
  1317. authority = this._username + (this._password !== null ? ':' + this._password : '') + '@';
  1318. }
  1319. return this.protocol + (this._isRelative ? '//' + authority + this.host : '') + this.pathname + this._query + this._fragment;
  1320. },
  1321. set href(href) {
  1322. clear.call(this);
  1323. parse.call(this, href);
  1324. },
  1325. get protocol() {
  1326. return this._scheme + ':';
  1327. },
  1328. set protocol(protocol) {
  1329. if (this._isInvalid) {
  1330. return;
  1331. }
  1332. parse.call(this, protocol + ':', 'scheme start');
  1333. },
  1334. get host() {
  1335. return this._isInvalid ? '' : this._port ? this._host + ':' + this._port : this._host;
  1336. },
  1337. set host(host) {
  1338. if (this._isInvalid || !this._isRelative) {
  1339. return;
  1340. }
  1341. parse.call(this, host, 'host');
  1342. },
  1343. get hostname() {
  1344. return this._host;
  1345. },
  1346. set hostname(hostname) {
  1347. if (this._isInvalid || !this._isRelative) {
  1348. return;
  1349. }
  1350. parse.call(this, hostname, 'hostname');
  1351. },
  1352. get port() {
  1353. return this._port;
  1354. },
  1355. set port(port) {
  1356. if (this._isInvalid || !this._isRelative) {
  1357. return;
  1358. }
  1359. parse.call(this, port, 'port');
  1360. },
  1361. get pathname() {
  1362. return this._isInvalid ? '' : this._isRelative ? '/' + this._path.join('/') : this._schemeData;
  1363. },
  1364. set pathname(pathname) {
  1365. if (this._isInvalid || !this._isRelative) {
  1366. return;
  1367. }
  1368. this._path = [];
  1369. parse.call(this, pathname, 'relative path start');
  1370. },
  1371. get search() {
  1372. return this._isInvalid || !this._query || this._query === '?' ? '' : this._query;
  1373. },
  1374. set search(search) {
  1375. if (this._isInvalid || !this._isRelative) {
  1376. return;
  1377. }
  1378. this._query = '?';
  1379. if (search[0] === '?') {
  1380. search = search.slice(1);
  1381. }
  1382. parse.call(this, search, 'query');
  1383. },
  1384. get hash() {
  1385. return this._isInvalid || !this._fragment || this._fragment === '#' ? '' : this._fragment;
  1386. },
  1387. set hash(hash) {
  1388. if (this._isInvalid) {
  1389. return;
  1390. }
  1391. this._fragment = '#';
  1392. if (hash[0] === '#') {
  1393. hash = hash.slice(1);
  1394. }
  1395. parse.call(this, hash, 'fragment');
  1396. },
  1397. get origin() {
  1398. var host;
  1399. if (this._isInvalid || !this._scheme) {
  1400. return '';
  1401. }
  1402. switch (this._scheme) {
  1403. case 'data':
  1404. case 'file':
  1405. case 'javascript':
  1406. case 'mailto':
  1407. return 'null';
  1408. }
  1409. host = this.host;
  1410. if (!host) {
  1411. return '';
  1412. }
  1413. return this._scheme + '://' + host;
  1414. }
  1415. };
  1416. var OriginalURL = globalScope.URL;
  1417. if (OriginalURL) {
  1418. JURL.createObjectURL = function (blob) {
  1419. return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
  1420. };
  1421. JURL.revokeObjectURL = function (url) {
  1422. OriginalURL.revokeObjectURL(url);
  1423. };
  1424. }
  1425. globalScope.URL = JURL;
  1426. })();
  1427. }