2
0

compatibility.js 47 KB

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