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