compatibility.js 21 KB

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