compatibility.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. 'use strict';
  23. 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; };
  24. if (typeof PDFJS === 'undefined' || !PDFJS.compatibilityChecked) {
  25. var globalScope = require('./global_scope');
  26. var userAgent = typeof navigator !== 'undefined' && navigator.userAgent || '';
  27. var isAndroid = /Android/.test(userAgent);
  28. var isIOSChrome = userAgent.indexOf('CriOS') >= 0;
  29. var isIE = userAgent.indexOf('Trident') >= 0;
  30. var isIOS = /\b(iPad|iPhone|iPod)(?=;)/.test(userAgent);
  31. var isSafari = /Safari\//.test(userAgent) && !/(Chrome\/|Android\s)/.test(userAgent);
  32. var hasDOM = (typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && (typeof document === 'undefined' ? 'undefined' : _typeof(document)) === 'object';
  33. if (typeof PDFJS === 'undefined') {
  34. globalScope.PDFJS = {};
  35. }
  36. PDFJS.compatibilityChecked = true;
  37. (function checkOnBlobSupport() {
  38. if (isIE || isIOSChrome) {
  39. PDFJS.disableCreateObjectURL = true;
  40. }
  41. })();
  42. (function checkNavigatorLanguage() {
  43. if (typeof navigator === 'undefined') {
  44. return;
  45. }
  46. if ('language' in navigator) {
  47. return;
  48. }
  49. PDFJS.locale = navigator.userLanguage || 'en-US';
  50. })();
  51. (function checkRangeRequests() {
  52. if (isSafari || isIOS) {
  53. PDFJS.disableRange = true;
  54. PDFJS.disableStream = true;
  55. }
  56. })();
  57. (function checkCanvasSizeLimitation() {
  58. if (isIOS || isAndroid) {
  59. PDFJS.maxCanvasPixels = 5242880;
  60. }
  61. })();
  62. (function checkFullscreenSupport() {
  63. if (!hasDOM) {
  64. return;
  65. }
  66. if (isIE && window.parent !== window) {
  67. PDFJS.disableFullscreen = true;
  68. }
  69. })();
  70. (function checkCurrentScript() {
  71. if (!hasDOM) {
  72. return;
  73. }
  74. if ('currentScript' in document) {
  75. return;
  76. }
  77. Object.defineProperty(document, 'currentScript', {
  78. get: function get() {
  79. var scripts = document.getElementsByTagName('script');
  80. return scripts[scripts.length - 1];
  81. },
  82. enumerable: true,
  83. configurable: true
  84. });
  85. })();
  86. (function checkChildNodeRemove() {
  87. if (!hasDOM) {
  88. return;
  89. }
  90. if (typeof Element.prototype.remove !== 'undefined') {
  91. return;
  92. }
  93. Element.prototype.remove = function () {
  94. if (this.parentNode) {
  95. this.parentNode.removeChild(this);
  96. }
  97. };
  98. })();
  99. (function checkObjectValues() {
  100. if (Object.values) {
  101. return;
  102. }
  103. Object.values = require('core-js/fn/object/values');
  104. })();
  105. (function checkArrayIncludes() {
  106. if (Array.prototype.includes) {
  107. return;
  108. }
  109. Array.prototype.includes = require('core-js/fn/array/includes');
  110. })();
  111. (function checkMathLog2() {
  112. if (Math.log2) {
  113. return;
  114. }
  115. Math.log2 = require('core-js/fn/math/log2');
  116. })();
  117. (function checkNumberIsNaN() {
  118. if (Number.isNaN) {
  119. return;
  120. }
  121. Number.isNaN = require('core-js/fn/number/is-nan');
  122. })();
  123. (function checkNumberIsInteger() {
  124. if (Number.isInteger) {
  125. return;
  126. }
  127. Number.isInteger = require('core-js/fn/number/is-integer');
  128. })();
  129. (function checkPromise() {
  130. if (globalScope.Promise) {
  131. return;
  132. }
  133. globalScope.Promise = require('core-js/fn/promise');
  134. })();
  135. (function checkWeakMap() {
  136. if (globalScope.WeakMap) {
  137. return;
  138. }
  139. globalScope.WeakMap = require('core-js/fn/weak-map');
  140. })();
  141. (function checkURLConstructor() {
  142. var hasWorkingUrl = false;
  143. try {
  144. if (typeof URL === 'function' && _typeof(URL.prototype) === 'object' && 'origin' in URL.prototype) {
  145. var u = new URL('b', 'http://a');
  146. u.pathname = 'c%20d';
  147. hasWorkingUrl = u.href === 'http://a/c%20d';
  148. }
  149. } catch (e) {}
  150. if (hasWorkingUrl) {
  151. return;
  152. }
  153. var relative = Object.create(null);
  154. relative['ftp'] = 21;
  155. relative['file'] = 0;
  156. relative['gopher'] = 70;
  157. relative['http'] = 80;
  158. relative['https'] = 443;
  159. relative['ws'] = 80;
  160. relative['wss'] = 443;
  161. var relativePathDotMapping = Object.create(null);
  162. relativePathDotMapping['%2e'] = '.';
  163. relativePathDotMapping['.%2e'] = '..';
  164. relativePathDotMapping['%2e.'] = '..';
  165. relativePathDotMapping['%2e%2e'] = '..';
  166. function isRelativeScheme(scheme) {
  167. return relative[scheme] !== undefined;
  168. }
  169. function invalid() {
  170. clear.call(this);
  171. this._isInvalid = true;
  172. }
  173. function IDNAToASCII(h) {
  174. if (h === '') {
  175. invalid.call(this);
  176. }
  177. return h.toLowerCase();
  178. }
  179. function percentEscape(c) {
  180. var unicode = c.charCodeAt(0);
  181. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) === -1) {
  182. return c;
  183. }
  184. return encodeURIComponent(c);
  185. }
  186. function percentEscapeQuery(c) {
  187. var unicode = c.charCodeAt(0);
  188. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) === -1) {
  189. return c;
  190. }
  191. return encodeURIComponent(c);
  192. }
  193. var EOF,
  194. ALPHA = /[a-zA-Z]/,
  195. ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/;
  196. function parse(input, stateOverride, base) {
  197. function err(message) {
  198. errors.push(message);
  199. }
  200. var state = stateOverride || 'scheme start',
  201. cursor = 0,
  202. buffer = '',
  203. seenAt = false,
  204. seenBracket = false,
  205. errors = [];
  206. loop: while ((input[cursor - 1] !== EOF || cursor === 0) && !this._isInvalid) {
  207. var c = input[cursor];
  208. switch (state) {
  209. case 'scheme start':
  210. if (c && ALPHA.test(c)) {
  211. buffer += c.toLowerCase();
  212. state = 'scheme';
  213. } else if (!stateOverride) {
  214. buffer = '';
  215. state = 'no scheme';
  216. continue;
  217. } else {
  218. err('Invalid scheme.');
  219. break loop;
  220. }
  221. break;
  222. case 'scheme':
  223. if (c && ALPHANUMERIC.test(c)) {
  224. buffer += c.toLowerCase();
  225. } else if (c === ':') {
  226. this._scheme = buffer;
  227. buffer = '';
  228. if (stateOverride) {
  229. break loop;
  230. }
  231. if (isRelativeScheme(this._scheme)) {
  232. this._isRelative = true;
  233. }
  234. if (this._scheme === 'file') {
  235. state = 'relative';
  236. } else if (this._isRelative && base && base._scheme === this._scheme) {
  237. state = 'relative or authority';
  238. } else if (this._isRelative) {
  239. state = 'authority first slash';
  240. } else {
  241. state = 'scheme data';
  242. }
  243. } else if (!stateOverride) {
  244. buffer = '';
  245. cursor = 0;
  246. state = 'no scheme';
  247. continue;
  248. } else if (c === EOF) {
  249. break loop;
  250. } else {
  251. err('Code point not allowed in scheme: ' + c);
  252. break loop;
  253. }
  254. break;
  255. case 'scheme data':
  256. if (c === '?') {
  257. this._query = '?';
  258. state = 'query';
  259. } else if (c === '#') {
  260. this._fragment = '#';
  261. state = 'fragment';
  262. } else {
  263. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  264. this._schemeData += percentEscape(c);
  265. }
  266. }
  267. break;
  268. case 'no scheme':
  269. if (!base || !isRelativeScheme(base._scheme)) {
  270. err('Missing scheme.');
  271. invalid.call(this);
  272. } else {
  273. state = 'relative';
  274. continue;
  275. }
  276. break;
  277. case 'relative or authority':
  278. if (c === '/' && input[cursor + 1] === '/') {
  279. state = 'authority ignore slashes';
  280. } else {
  281. err('Expected /, got: ' + c);
  282. state = 'relative';
  283. continue;
  284. }
  285. break;
  286. case 'relative':
  287. this._isRelative = true;
  288. if (this._scheme !== 'file') {
  289. this._scheme = base._scheme;
  290. }
  291. if (c === EOF) {
  292. this._host = base._host;
  293. this._port = base._port;
  294. this._path = base._path.slice();
  295. this._query = base._query;
  296. this._username = base._username;
  297. this._password = base._password;
  298. break loop;
  299. } else if (c === '/' || c === '\\') {
  300. if (c === '\\') {
  301. err('\\ is an invalid code point.');
  302. }
  303. state = 'relative slash';
  304. } else if (c === '?') {
  305. this._host = base._host;
  306. this._port = base._port;
  307. this._path = base._path.slice();
  308. this._query = '?';
  309. this._username = base._username;
  310. this._password = base._password;
  311. state = 'query';
  312. } else if (c === '#') {
  313. this._host = base._host;
  314. this._port = base._port;
  315. this._path = base._path.slice();
  316. this._query = base._query;
  317. this._fragment = '#';
  318. this._username = base._username;
  319. this._password = base._password;
  320. state = 'fragment';
  321. } else {
  322. var nextC = input[cursor + 1];
  323. var nextNextC = input[cursor + 2];
  324. if (this._scheme !== 'file' || !ALPHA.test(c) || nextC !== ':' && nextC !== '|' || nextNextC !== EOF && nextNextC !== '/' && nextNextC !== '\\' && nextNextC !== '?' && nextNextC !== '#') {
  325. this._host = base._host;
  326. this._port = base._port;
  327. this._username = base._username;
  328. this._password = base._password;
  329. this._path = base._path.slice();
  330. this._path.pop();
  331. }
  332. state = 'relative path';
  333. continue;
  334. }
  335. break;
  336. case 'relative slash':
  337. if (c === '/' || c === '\\') {
  338. if (c === '\\') {
  339. err('\\ is an invalid code point.');
  340. }
  341. if (this._scheme === 'file') {
  342. state = 'file host';
  343. } else {
  344. state = 'authority ignore slashes';
  345. }
  346. } else {
  347. if (this._scheme !== 'file') {
  348. this._host = base._host;
  349. this._port = base._port;
  350. this._username = base._username;
  351. this._password = base._password;
  352. }
  353. state = 'relative path';
  354. continue;
  355. }
  356. break;
  357. case 'authority first slash':
  358. if (c === '/') {
  359. state = 'authority second slash';
  360. } else {
  361. err('Expected \'/\', got: ' + c);
  362. state = 'authority ignore slashes';
  363. continue;
  364. }
  365. break;
  366. case 'authority second slash':
  367. state = 'authority ignore slashes';
  368. if (c !== '/') {
  369. err('Expected \'/\', got: ' + c);
  370. continue;
  371. }
  372. break;
  373. case 'authority ignore slashes':
  374. if (c !== '/' && c !== '\\') {
  375. state = 'authority';
  376. continue;
  377. } else {
  378. err('Expected authority, got: ' + c);
  379. }
  380. break;
  381. case 'authority':
  382. if (c === '@') {
  383. if (seenAt) {
  384. err('@ already seen.');
  385. buffer += '%40';
  386. }
  387. seenAt = true;
  388. for (var i = 0; i < buffer.length; i++) {
  389. var cp = buffer[i];
  390. if (cp === '\t' || cp === '\n' || cp === '\r') {
  391. err('Invalid whitespace in authority.');
  392. continue;
  393. }
  394. if (cp === ':' && this._password === null) {
  395. this._password = '';
  396. continue;
  397. }
  398. var tempC = percentEscape(cp);
  399. if (this._password !== null) {
  400. this._password += tempC;
  401. } else {
  402. this._username += tempC;
  403. }
  404. }
  405. buffer = '';
  406. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  407. cursor -= buffer.length;
  408. buffer = '';
  409. state = 'host';
  410. continue;
  411. } else {
  412. buffer += c;
  413. }
  414. break;
  415. case 'file host':
  416. if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  417. if (buffer.length === 2 && ALPHA.test(buffer[0]) && (buffer[1] === ':' || buffer[1] === '|')) {
  418. state = 'relative path';
  419. } else if (buffer.length === 0) {
  420. state = 'relative path start';
  421. } else {
  422. this._host = IDNAToASCII.call(this, buffer);
  423. buffer = '';
  424. state = 'relative path start';
  425. }
  426. continue;
  427. } else if (c === '\t' || c === '\n' || c === '\r') {
  428. err('Invalid whitespace in file host.');
  429. } else {
  430. buffer += c;
  431. }
  432. break;
  433. case 'host':
  434. case 'hostname':
  435. if (c === ':' && !seenBracket) {
  436. this._host = IDNAToASCII.call(this, buffer);
  437. buffer = '';
  438. state = 'port';
  439. if (stateOverride === 'hostname') {
  440. break loop;
  441. }
  442. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  443. this._host = IDNAToASCII.call(this, buffer);
  444. buffer = '';
  445. state = 'relative path start';
  446. if (stateOverride) {
  447. break loop;
  448. }
  449. continue;
  450. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  451. if (c === '[') {
  452. seenBracket = true;
  453. } else if (c === ']') {
  454. seenBracket = false;
  455. }
  456. buffer += c;
  457. } else {
  458. err('Invalid code point in host/hostname: ' + c);
  459. }
  460. break;
  461. case 'port':
  462. if (/[0-9]/.test(c)) {
  463. buffer += c;
  464. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#' || stateOverride) {
  465. if (buffer !== '') {
  466. var temp = parseInt(buffer, 10);
  467. if (temp !== relative[this._scheme]) {
  468. this._port = temp + '';
  469. }
  470. buffer = '';
  471. }
  472. if (stateOverride) {
  473. break loop;
  474. }
  475. state = 'relative path start';
  476. continue;
  477. } else if (c === '\t' || c === '\n' || c === '\r') {
  478. err('Invalid code point in port: ' + c);
  479. } else {
  480. invalid.call(this);
  481. }
  482. break;
  483. case 'relative path start':
  484. if (c === '\\') {
  485. err('\'\\\' not allowed in path.');
  486. }
  487. state = 'relative path';
  488. if (c !== '/' && c !== '\\') {
  489. continue;
  490. }
  491. break;
  492. case 'relative path':
  493. if (c === EOF || c === '/' || c === '\\' || !stateOverride && (c === '?' || c === '#')) {
  494. if (c === '\\') {
  495. err('\\ not allowed in relative path.');
  496. }
  497. var tmp;
  498. if (tmp = relativePathDotMapping[buffer.toLowerCase()]) {
  499. buffer = tmp;
  500. }
  501. if (buffer === '..') {
  502. this._path.pop();
  503. if (c !== '/' && c !== '\\') {
  504. this._path.push('');
  505. }
  506. } else if (buffer === '.' && c !== '/' && c !== '\\') {
  507. this._path.push('');
  508. } else if (buffer !== '.') {
  509. if (this._scheme === 'file' && this._path.length === 0 && buffer.length === 2 && ALPHA.test(buffer[0]) && buffer[1] === '|') {
  510. buffer = buffer[0] + ':';
  511. }
  512. this._path.push(buffer);
  513. }
  514. buffer = '';
  515. if (c === '?') {
  516. this._query = '?';
  517. state = 'query';
  518. } else if (c === '#') {
  519. this._fragment = '#';
  520. state = 'fragment';
  521. }
  522. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  523. buffer += percentEscape(c);
  524. }
  525. break;
  526. case 'query':
  527. if (!stateOverride && c === '#') {
  528. this._fragment = '#';
  529. state = 'fragment';
  530. } else if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  531. this._query += percentEscapeQuery(c);
  532. }
  533. break;
  534. case 'fragment':
  535. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  536. this._fragment += c;
  537. }
  538. break;
  539. }
  540. cursor++;
  541. }
  542. }
  543. function clear() {
  544. this._scheme = '';
  545. this._schemeData = '';
  546. this._username = '';
  547. this._password = null;
  548. this._host = '';
  549. this._port = '';
  550. this._path = [];
  551. this._query = '';
  552. this._fragment = '';
  553. this._isInvalid = false;
  554. this._isRelative = false;
  555. }
  556. function JURL(url, base) {
  557. if (base !== undefined && !(base instanceof JURL)) {
  558. base = new JURL(String(base));
  559. }
  560. this._url = url;
  561. clear.call(this);
  562. var input = url.replace(/^[ \t\r\n\f]+|[ \t\r\n\f]+$/g, '');
  563. parse.call(this, input, null, base);
  564. }
  565. JURL.prototype = {
  566. toString: function toString() {
  567. return this.href;
  568. },
  569. get href() {
  570. if (this._isInvalid) {
  571. return this._url;
  572. }
  573. var authority = '';
  574. if (this._username !== '' || this._password !== null) {
  575. authority = this._username + (this._password !== null ? ':' + this._password : '') + '@';
  576. }
  577. return this.protocol + (this._isRelative ? '//' + authority + this.host : '') + this.pathname + this._query + this._fragment;
  578. },
  579. set href(value) {
  580. clear.call(this);
  581. parse.call(this, value);
  582. },
  583. get protocol() {
  584. return this._scheme + ':';
  585. },
  586. set protocol(value) {
  587. if (this._isInvalid) {
  588. return;
  589. }
  590. parse.call(this, value + ':', 'scheme start');
  591. },
  592. get host() {
  593. return this._isInvalid ? '' : this._port ? this._host + ':' + this._port : this._host;
  594. },
  595. set host(value) {
  596. if (this._isInvalid || !this._isRelative) {
  597. return;
  598. }
  599. parse.call(this, value, 'host');
  600. },
  601. get hostname() {
  602. return this._host;
  603. },
  604. set hostname(value) {
  605. if (this._isInvalid || !this._isRelative) {
  606. return;
  607. }
  608. parse.call(this, value, 'hostname');
  609. },
  610. get port() {
  611. return this._port;
  612. },
  613. set port(value) {
  614. if (this._isInvalid || !this._isRelative) {
  615. return;
  616. }
  617. parse.call(this, value, 'port');
  618. },
  619. get pathname() {
  620. return this._isInvalid ? '' : this._isRelative ? '/' + this._path.join('/') : this._schemeData;
  621. },
  622. set pathname(value) {
  623. if (this._isInvalid || !this._isRelative) {
  624. return;
  625. }
  626. this._path = [];
  627. parse.call(this, value, 'relative path start');
  628. },
  629. get search() {
  630. return this._isInvalid || !this._query || this._query === '?' ? '' : this._query;
  631. },
  632. set search(value) {
  633. if (this._isInvalid || !this._isRelative) {
  634. return;
  635. }
  636. this._query = '?';
  637. if (value[0] === '?') {
  638. value = value.slice(1);
  639. }
  640. parse.call(this, value, 'query');
  641. },
  642. get hash() {
  643. return this._isInvalid || !this._fragment || this._fragment === '#' ? '' : this._fragment;
  644. },
  645. set hash(value) {
  646. if (this._isInvalid) {
  647. return;
  648. }
  649. this._fragment = '#';
  650. if (value[0] === '#') {
  651. value = value.slice(1);
  652. }
  653. parse.call(this, value, 'fragment');
  654. },
  655. get origin() {
  656. var host;
  657. if (this._isInvalid || !this._scheme) {
  658. return '';
  659. }
  660. switch (this._scheme) {
  661. case 'data':
  662. case 'file':
  663. case 'javascript':
  664. case 'mailto':
  665. return 'null';
  666. case 'blob':
  667. try {
  668. return new JURL(this._schemeData).origin || 'null';
  669. } catch (_) {}
  670. return 'null';
  671. }
  672. host = this.host;
  673. if (!host) {
  674. return '';
  675. }
  676. return this._scheme + '://' + host;
  677. }
  678. };
  679. var OriginalURL = globalScope.URL;
  680. if (OriginalURL) {
  681. JURL.createObjectURL = function (blob) {
  682. return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
  683. };
  684. JURL.revokeObjectURL = function (url) {
  685. OriginalURL.revokeObjectURL(url);
  686. };
  687. }
  688. globalScope.URL = JURL;
  689. })();
  690. }