compatibility.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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. if ((typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object' && typeof obj !== 'function' || obj === null) {
  931. return false;
  932. }
  933. return !!Object.getOwnPropertyDescriptor(obj, this.id);
  934. },
  935. get: function get(obj) {
  936. return this.has(obj) ? obj[this.id] : undefined;
  937. },
  938. set: function set(obj, value) {
  939. Object.defineProperty(obj, this.id, {
  940. value: value,
  941. enumerable: false,
  942. configurable: true
  943. });
  944. },
  945. delete: function _delete(obj) {
  946. delete obj[this.id];
  947. }
  948. };
  949. globalScope.WeakMap = WeakMap;
  950. })();
  951. (function checkURLConstructor() {
  952. var hasWorkingUrl = false;
  953. try {
  954. if (typeof URL === 'function' && _typeof(URL.prototype) === 'object' && 'origin' in URL.prototype) {
  955. var u = new URL('b', 'http://a');
  956. u.pathname = 'c%20d';
  957. hasWorkingUrl = u.href === 'http://a/c%20d';
  958. }
  959. } catch (e) {}
  960. if (hasWorkingUrl) {
  961. return;
  962. }
  963. var relative = Object.create(null);
  964. relative['ftp'] = 21;
  965. relative['file'] = 0;
  966. relative['gopher'] = 70;
  967. relative['http'] = 80;
  968. relative['https'] = 443;
  969. relative['ws'] = 80;
  970. relative['wss'] = 443;
  971. var relativePathDotMapping = Object.create(null);
  972. relativePathDotMapping['%2e'] = '.';
  973. relativePathDotMapping['.%2e'] = '..';
  974. relativePathDotMapping['%2e.'] = '..';
  975. relativePathDotMapping['%2e%2e'] = '..';
  976. function isRelativeScheme(scheme) {
  977. return relative[scheme] !== undefined;
  978. }
  979. function invalid() {
  980. clear.call(this);
  981. this._isInvalid = true;
  982. }
  983. function IDNAToASCII(h) {
  984. if (h === '') {
  985. invalid.call(this);
  986. }
  987. return h.toLowerCase();
  988. }
  989. function percentEscape(c) {
  990. var unicode = c.charCodeAt(0);
  991. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) === -1) {
  992. return c;
  993. }
  994. return encodeURIComponent(c);
  995. }
  996. function percentEscapeQuery(c) {
  997. var unicode = c.charCodeAt(0);
  998. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) === -1) {
  999. return c;
  1000. }
  1001. return encodeURIComponent(c);
  1002. }
  1003. var EOF,
  1004. ALPHA = /[a-zA-Z]/,
  1005. ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/;
  1006. function parse(input, stateOverride, base) {
  1007. function err(message) {
  1008. errors.push(message);
  1009. }
  1010. var state = stateOverride || 'scheme start',
  1011. cursor = 0,
  1012. buffer = '',
  1013. seenAt = false,
  1014. seenBracket = false,
  1015. errors = [];
  1016. loop: while ((input[cursor - 1] !== EOF || cursor === 0) && !this._isInvalid) {
  1017. var c = input[cursor];
  1018. switch (state) {
  1019. case 'scheme start':
  1020. if (c && ALPHA.test(c)) {
  1021. buffer += c.toLowerCase();
  1022. state = 'scheme';
  1023. } else if (!stateOverride) {
  1024. buffer = '';
  1025. state = 'no scheme';
  1026. continue;
  1027. } else {
  1028. err('Invalid scheme.');
  1029. break loop;
  1030. }
  1031. break;
  1032. case 'scheme':
  1033. if (c && ALPHANUMERIC.test(c)) {
  1034. buffer += c.toLowerCase();
  1035. } else if (c === ':') {
  1036. this._scheme = buffer;
  1037. buffer = '';
  1038. if (stateOverride) {
  1039. break loop;
  1040. }
  1041. if (isRelativeScheme(this._scheme)) {
  1042. this._isRelative = true;
  1043. }
  1044. if (this._scheme === 'file') {
  1045. state = 'relative';
  1046. } else if (this._isRelative && base && base._scheme === this._scheme) {
  1047. state = 'relative or authority';
  1048. } else if (this._isRelative) {
  1049. state = 'authority first slash';
  1050. } else {
  1051. state = 'scheme data';
  1052. }
  1053. } else if (!stateOverride) {
  1054. buffer = '';
  1055. cursor = 0;
  1056. state = 'no scheme';
  1057. continue;
  1058. } else if (c === EOF) {
  1059. break loop;
  1060. } else {
  1061. err('Code point not allowed in scheme: ' + c);
  1062. break loop;
  1063. }
  1064. break;
  1065. case 'scheme data':
  1066. if (c === '?') {
  1067. this._query = '?';
  1068. state = 'query';
  1069. } else if (c === '#') {
  1070. this._fragment = '#';
  1071. state = 'fragment';
  1072. } else {
  1073. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  1074. this._schemeData += percentEscape(c);
  1075. }
  1076. }
  1077. break;
  1078. case 'no scheme':
  1079. if (!base || !isRelativeScheme(base._scheme)) {
  1080. err('Missing scheme.');
  1081. invalid.call(this);
  1082. } else {
  1083. state = 'relative';
  1084. continue;
  1085. }
  1086. break;
  1087. case 'relative or authority':
  1088. if (c === '/' && input[cursor + 1] === '/') {
  1089. state = 'authority ignore slashes';
  1090. } else {
  1091. err('Expected /, got: ' + c);
  1092. state = 'relative';
  1093. continue;
  1094. }
  1095. break;
  1096. case 'relative':
  1097. this._isRelative = true;
  1098. if (this._scheme !== 'file') {
  1099. this._scheme = base._scheme;
  1100. }
  1101. if (c === EOF) {
  1102. this._host = base._host;
  1103. this._port = base._port;
  1104. this._path = base._path.slice();
  1105. this._query = base._query;
  1106. this._username = base._username;
  1107. this._password = base._password;
  1108. break loop;
  1109. } else if (c === '/' || c === '\\') {
  1110. if (c === '\\') {
  1111. err('\\ is an invalid code point.');
  1112. }
  1113. state = 'relative slash';
  1114. } else if (c === '?') {
  1115. this._host = base._host;
  1116. this._port = base._port;
  1117. this._path = base._path.slice();
  1118. this._query = '?';
  1119. this._username = base._username;
  1120. this._password = base._password;
  1121. state = 'query';
  1122. } else if (c === '#') {
  1123. this._host = base._host;
  1124. this._port = base._port;
  1125. this._path = base._path.slice();
  1126. this._query = base._query;
  1127. this._fragment = '#';
  1128. this._username = base._username;
  1129. this._password = base._password;
  1130. state = 'fragment';
  1131. } else {
  1132. var nextC = input[cursor + 1];
  1133. var nextNextC = input[cursor + 2];
  1134. if (this._scheme !== 'file' || !ALPHA.test(c) || nextC !== ':' && nextC !== '|' || nextNextC !== EOF && nextNextC !== '/' && nextNextC !== '\\' && nextNextC !== '?' && nextNextC !== '#') {
  1135. this._host = base._host;
  1136. this._port = base._port;
  1137. this._username = base._username;
  1138. this._password = base._password;
  1139. this._path = base._path.slice();
  1140. this._path.pop();
  1141. }
  1142. state = 'relative path';
  1143. continue;
  1144. }
  1145. break;
  1146. case 'relative slash':
  1147. if (c === '/' || c === '\\') {
  1148. if (c === '\\') {
  1149. err('\\ is an invalid code point.');
  1150. }
  1151. if (this._scheme === 'file') {
  1152. state = 'file host';
  1153. } else {
  1154. state = 'authority ignore slashes';
  1155. }
  1156. } else {
  1157. if (this._scheme !== 'file') {
  1158. this._host = base._host;
  1159. this._port = base._port;
  1160. this._username = base._username;
  1161. this._password = base._password;
  1162. }
  1163. state = 'relative path';
  1164. continue;
  1165. }
  1166. break;
  1167. case 'authority first slash':
  1168. if (c === '/') {
  1169. state = 'authority second slash';
  1170. } else {
  1171. err('Expected \'/\', got: ' + c);
  1172. state = 'authority ignore slashes';
  1173. continue;
  1174. }
  1175. break;
  1176. case 'authority second slash':
  1177. state = 'authority ignore slashes';
  1178. if (c !== '/') {
  1179. err('Expected \'/\', got: ' + c);
  1180. continue;
  1181. }
  1182. break;
  1183. case 'authority ignore slashes':
  1184. if (c !== '/' && c !== '\\') {
  1185. state = 'authority';
  1186. continue;
  1187. } else {
  1188. err('Expected authority, got: ' + c);
  1189. }
  1190. break;
  1191. case 'authority':
  1192. if (c === '@') {
  1193. if (seenAt) {
  1194. err('@ already seen.');
  1195. buffer += '%40';
  1196. }
  1197. seenAt = true;
  1198. for (var i = 0; i < buffer.length; i++) {
  1199. var cp = buffer[i];
  1200. if (cp === '\t' || cp === '\n' || cp === '\r') {
  1201. err('Invalid whitespace in authority.');
  1202. continue;
  1203. }
  1204. if (cp === ':' && this._password === null) {
  1205. this._password = '';
  1206. continue;
  1207. }
  1208. var tempC = percentEscape(cp);
  1209. if (this._password !== null) {
  1210. this._password += tempC;
  1211. } else {
  1212. this._username += tempC;
  1213. }
  1214. }
  1215. buffer = '';
  1216. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  1217. cursor -= buffer.length;
  1218. buffer = '';
  1219. state = 'host';
  1220. continue;
  1221. } else {
  1222. buffer += c;
  1223. }
  1224. break;
  1225. case 'file host':
  1226. if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  1227. if (buffer.length === 2 && ALPHA.test(buffer[0]) && (buffer[1] === ':' || buffer[1] === '|')) {
  1228. state = 'relative path';
  1229. } else if (buffer.length === 0) {
  1230. state = 'relative path start';
  1231. } else {
  1232. this._host = IDNAToASCII.call(this, buffer);
  1233. buffer = '';
  1234. state = 'relative path start';
  1235. }
  1236. continue;
  1237. } else if (c === '\t' || c === '\n' || c === '\r') {
  1238. err('Invalid whitespace in file host.');
  1239. } else {
  1240. buffer += c;
  1241. }
  1242. break;
  1243. case 'host':
  1244. case 'hostname':
  1245. if (c === ':' && !seenBracket) {
  1246. this._host = IDNAToASCII.call(this, buffer);
  1247. buffer = '';
  1248. state = 'port';
  1249. if (stateOverride === 'hostname') {
  1250. break loop;
  1251. }
  1252. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  1253. this._host = IDNAToASCII.call(this, buffer);
  1254. buffer = '';
  1255. state = 'relative path start';
  1256. if (stateOverride) {
  1257. break loop;
  1258. }
  1259. continue;
  1260. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  1261. if (c === '[') {
  1262. seenBracket = true;
  1263. } else if (c === ']') {
  1264. seenBracket = false;
  1265. }
  1266. buffer += c;
  1267. } else {
  1268. err('Invalid code point in host/hostname: ' + c);
  1269. }
  1270. break;
  1271. case 'port':
  1272. if (/[0-9]/.test(c)) {
  1273. buffer += c;
  1274. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#' || stateOverride) {
  1275. if (buffer !== '') {
  1276. var temp = parseInt(buffer, 10);
  1277. if (temp !== relative[this._scheme]) {
  1278. this._port = temp + '';
  1279. }
  1280. buffer = '';
  1281. }
  1282. if (stateOverride) {
  1283. break loop;
  1284. }
  1285. state = 'relative path start';
  1286. continue;
  1287. } else if (c === '\t' || c === '\n' || c === '\r') {
  1288. err('Invalid code point in port: ' + c);
  1289. } else {
  1290. invalid.call(this);
  1291. }
  1292. break;
  1293. case 'relative path start':
  1294. if (c === '\\') {
  1295. err('\'\\\' not allowed in path.');
  1296. }
  1297. state = 'relative path';
  1298. if (c !== '/' && c !== '\\') {
  1299. continue;
  1300. }
  1301. break;
  1302. case 'relative path':
  1303. if (c === EOF || c === '/' || c === '\\' || !stateOverride && (c === '?' || c === '#')) {
  1304. if (c === '\\') {
  1305. err('\\ not allowed in relative path.');
  1306. }
  1307. var tmp;
  1308. if (tmp = relativePathDotMapping[buffer.toLowerCase()]) {
  1309. buffer = tmp;
  1310. }
  1311. if (buffer === '..') {
  1312. this._path.pop();
  1313. if (c !== '/' && c !== '\\') {
  1314. this._path.push('');
  1315. }
  1316. } else if (buffer === '.' && c !== '/' && c !== '\\') {
  1317. this._path.push('');
  1318. } else if (buffer !== '.') {
  1319. if (this._scheme === 'file' && this._path.length === 0 && buffer.length === 2 && ALPHA.test(buffer[0]) && buffer[1] === '|') {
  1320. buffer = buffer[0] + ':';
  1321. }
  1322. this._path.push(buffer);
  1323. }
  1324. buffer = '';
  1325. if (c === '?') {
  1326. this._query = '?';
  1327. state = 'query';
  1328. } else if (c === '#') {
  1329. this._fragment = '#';
  1330. state = 'fragment';
  1331. }
  1332. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  1333. buffer += percentEscape(c);
  1334. }
  1335. break;
  1336. case 'query':
  1337. if (!stateOverride && c === '#') {
  1338. this._fragment = '#';
  1339. state = 'fragment';
  1340. } else if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  1341. this._query += percentEscapeQuery(c);
  1342. }
  1343. break;
  1344. case 'fragment':
  1345. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  1346. this._fragment += c;
  1347. }
  1348. break;
  1349. }
  1350. cursor++;
  1351. }
  1352. }
  1353. function clear() {
  1354. this._scheme = '';
  1355. this._schemeData = '';
  1356. this._username = '';
  1357. this._password = null;
  1358. this._host = '';
  1359. this._port = '';
  1360. this._path = [];
  1361. this._query = '';
  1362. this._fragment = '';
  1363. this._isInvalid = false;
  1364. this._isRelative = false;
  1365. }
  1366. function JURL(url, base) {
  1367. if (base !== undefined && !(base instanceof JURL)) {
  1368. base = new JURL(String(base));
  1369. }
  1370. this._url = url;
  1371. clear.call(this);
  1372. var input = url.replace(/^[ \t\r\n\f]+|[ \t\r\n\f]+$/g, '');
  1373. parse.call(this, input, null, base);
  1374. }
  1375. JURL.prototype = {
  1376. toString: function toString() {
  1377. return this.href;
  1378. },
  1379. get href() {
  1380. if (this._isInvalid) {
  1381. return this._url;
  1382. }
  1383. var authority = '';
  1384. if (this._username !== '' || this._password !== null) {
  1385. authority = this._username + (this._password !== null ? ':' + this._password : '') + '@';
  1386. }
  1387. return this.protocol + (this._isRelative ? '//' + authority + this.host : '') + this.pathname + this._query + this._fragment;
  1388. },
  1389. set href(value) {
  1390. clear.call(this);
  1391. parse.call(this, value);
  1392. },
  1393. get protocol() {
  1394. return this._scheme + ':';
  1395. },
  1396. set protocol(value) {
  1397. if (this._isInvalid) {
  1398. return;
  1399. }
  1400. parse.call(this, value + ':', 'scheme start');
  1401. },
  1402. get host() {
  1403. return this._isInvalid ? '' : this._port ? this._host + ':' + this._port : this._host;
  1404. },
  1405. set host(value) {
  1406. if (this._isInvalid || !this._isRelative) {
  1407. return;
  1408. }
  1409. parse.call(this, value, 'host');
  1410. },
  1411. get hostname() {
  1412. return this._host;
  1413. },
  1414. set hostname(value) {
  1415. if (this._isInvalid || !this._isRelative) {
  1416. return;
  1417. }
  1418. parse.call(this, value, 'hostname');
  1419. },
  1420. get port() {
  1421. return this._port;
  1422. },
  1423. set port(value) {
  1424. if (this._isInvalid || !this._isRelative) {
  1425. return;
  1426. }
  1427. parse.call(this, value, 'port');
  1428. },
  1429. get pathname() {
  1430. return this._isInvalid ? '' : this._isRelative ? '/' + this._path.join('/') : this._schemeData;
  1431. },
  1432. set pathname(value) {
  1433. if (this._isInvalid || !this._isRelative) {
  1434. return;
  1435. }
  1436. this._path = [];
  1437. parse.call(this, value, 'relative path start');
  1438. },
  1439. get search() {
  1440. return this._isInvalid || !this._query || this._query === '?' ? '' : this._query;
  1441. },
  1442. set search(value) {
  1443. if (this._isInvalid || !this._isRelative) {
  1444. return;
  1445. }
  1446. this._query = '?';
  1447. if (value[0] === '?') {
  1448. value = value.slice(1);
  1449. }
  1450. parse.call(this, value, 'query');
  1451. },
  1452. get hash() {
  1453. return this._isInvalid || !this._fragment || this._fragment === '#' ? '' : this._fragment;
  1454. },
  1455. set hash(value) {
  1456. if (this._isInvalid) {
  1457. return;
  1458. }
  1459. this._fragment = '#';
  1460. if (value[0] === '#') {
  1461. value = value.slice(1);
  1462. }
  1463. parse.call(this, value, 'fragment');
  1464. },
  1465. get origin() {
  1466. var host;
  1467. if (this._isInvalid || !this._scheme) {
  1468. return '';
  1469. }
  1470. switch (this._scheme) {
  1471. case 'data':
  1472. case 'file':
  1473. case 'javascript':
  1474. case 'mailto':
  1475. return 'null';
  1476. case 'blob':
  1477. try {
  1478. return new JURL(this._schemeData).origin || 'null';
  1479. } catch (_) {}
  1480. return 'null';
  1481. }
  1482. host = this.host;
  1483. if (!host) {
  1484. return '';
  1485. }
  1486. return this._scheme + '://' + host;
  1487. }
  1488. };
  1489. var OriginalURL = globalScope.URL;
  1490. if (OriginalURL) {
  1491. JURL.createObjectURL = function (blob) {
  1492. return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
  1493. };
  1494. JURL.revokeObjectURL = function (url) {
  1495. OriginalURL.revokeObjectURL(url);
  1496. };
  1497. }
  1498. globalScope.URL = JURL;
  1499. })();
  1500. }