compatibility.js 48 KB

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