compatibility.js 47 KB

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