compatibility.js 22 KB

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