2
0

compatibility.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  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 isAndroidPre5 = /Android\s[0-4][^\d]/.test(userAgent);
  22. var isChrome = userAgent.indexOf('Chrom') >= 0;
  23. var isIOSChrome = userAgent.indexOf('CriOS') >= 0;
  24. var isIE = userAgent.indexOf('Trident') >= 0;
  25. var isIOS = /\b(iPad|iPhone|iPod)(?=;)/.test(userAgent);
  26. var isOpera = userAgent.indexOf('Opera') >= 0;
  27. var isSafari = /Safari\//.test(userAgent) && !/(Chrome\/|Android\s)/.test(userAgent);
  28. var hasDOM = (typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && (typeof document === 'undefined' ? 'undefined' : _typeof(document)) === 'object';
  29. if (typeof PDFJS === 'undefined') {
  30. globalScope.PDFJS = {};
  31. }
  32. PDFJS.compatibilityChecked = true;
  33. (function normalizeURLObject() {
  34. if (!globalScope.URL) {
  35. globalScope.URL = globalScope.webkitURL;
  36. }
  37. })();
  38. (function checkObjectDefinePropertyCompatibility() {
  39. if (typeof Object.defineProperty !== 'undefined') {
  40. var definePropertyPossible = true;
  41. try {
  42. if (hasDOM) {
  43. Object.defineProperty(new Image(), 'id', { value: 'test' });
  44. }
  45. var Test = function Test() {};
  46. Test.prototype = {
  47. get id() {}
  48. };
  49. Object.defineProperty(new Test(), 'id', {
  50. value: '',
  51. configurable: true,
  52. enumerable: true,
  53. writable: false
  54. });
  55. } catch (e) {
  56. definePropertyPossible = false;
  57. }
  58. if (definePropertyPossible) {
  59. return;
  60. }
  61. }
  62. Object.defineProperty = function objectDefineProperty(obj, name, def) {
  63. delete obj[name];
  64. if ('get' in def) {
  65. obj.__defineGetter__(name, def['get']);
  66. }
  67. if ('set' in def) {
  68. obj.__defineSetter__(name, def['set']);
  69. }
  70. if ('value' in def) {
  71. obj.__defineSetter__(name, function objectDefinePropertySetter(value) {
  72. this.__defineGetter__(name, function objectDefinePropertyGetter() {
  73. return value;
  74. });
  75. return value;
  76. });
  77. obj[name] = def.value;
  78. }
  79. };
  80. })();
  81. (function checkXMLHttpRequestResponseCompatibility() {
  82. if (typeof XMLHttpRequest === 'undefined') {
  83. return;
  84. }
  85. var xhrPrototype = XMLHttpRequest.prototype;
  86. var xhr = new XMLHttpRequest();
  87. if (!('overrideMimeType' in xhr)) {
  88. Object.defineProperty(xhrPrototype, 'overrideMimeType', {
  89. value: function xmlHttpRequestOverrideMimeType(mimeType) {}
  90. });
  91. }
  92. if ('responseType' in xhr) {
  93. return;
  94. }
  95. Object.defineProperty(xhrPrototype, 'responseType', {
  96. get: function xmlHttpRequestGetResponseType() {
  97. return this._responseType || 'text';
  98. },
  99. set: function xmlHttpRequestSetResponseType(value) {
  100. if (value === 'text' || value === 'arraybuffer') {
  101. this._responseType = value;
  102. if (value === 'arraybuffer' && typeof this.overrideMimeType === 'function') {
  103. this.overrideMimeType('text/plain; charset=x-user-defined');
  104. }
  105. }
  106. }
  107. });
  108. if (typeof VBArray !== 'undefined') {
  109. Object.defineProperty(xhrPrototype, 'response', {
  110. get: function xmlHttpRequestResponseGet() {
  111. if (this.responseType === 'arraybuffer') {
  112. return new Uint8Array(new VBArray(this.responseBody).toArray());
  113. }
  114. return this.responseText;
  115. }
  116. });
  117. return;
  118. }
  119. Object.defineProperty(xhrPrototype, 'response', {
  120. get: function xmlHttpRequestResponseGet() {
  121. if (this.responseType !== 'arraybuffer') {
  122. return this.responseText;
  123. }
  124. var text = this.responseText;
  125. var i,
  126. n = text.length;
  127. var result = new Uint8Array(n);
  128. for (i = 0; i < n; ++i) {
  129. result[i] = text.charCodeAt(i) & 0xFF;
  130. }
  131. return result.buffer;
  132. }
  133. });
  134. })();
  135. (function checkWindowBtoaCompatibility() {
  136. if ('btoa' in globalScope) {
  137. return;
  138. }
  139. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  140. globalScope.btoa = function (chars) {
  141. var buffer = '';
  142. var i, n;
  143. for (i = 0, n = chars.length; i < n; i += 3) {
  144. var b1 = chars.charCodeAt(i) & 0xFF;
  145. var b2 = chars.charCodeAt(i + 1) & 0xFF;
  146. var b3 = chars.charCodeAt(i + 2) & 0xFF;
  147. var d1 = b1 >> 2,
  148. d2 = (b1 & 3) << 4 | b2 >> 4;
  149. var d3 = i + 1 < n ? (b2 & 0xF) << 2 | b3 >> 6 : 64;
  150. var d4 = i + 2 < n ? b3 & 0x3F : 64;
  151. buffer += digits.charAt(d1) + digits.charAt(d2) + digits.charAt(d3) + digits.charAt(d4);
  152. }
  153. return buffer;
  154. };
  155. })();
  156. (function checkWindowAtobCompatibility() {
  157. if ('atob' in globalScope) {
  158. return;
  159. }
  160. var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  161. globalScope.atob = function (input) {
  162. input = input.replace(/=+$/, '');
  163. if (input.length % 4 === 1) {
  164. throw new Error('bad atob input');
  165. }
  166. 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) {
  167. buffer = digits.indexOf(buffer);
  168. }
  169. return output;
  170. };
  171. })();
  172. (function checkFunctionPrototypeBindCompatibility() {
  173. if (typeof Function.prototype.bind !== 'undefined') {
  174. return;
  175. }
  176. Function.prototype.bind = function functionPrototypeBind(obj) {
  177. var fn = this,
  178. headArgs = Array.prototype.slice.call(arguments, 1);
  179. var bound = function functionPrototypeBindBound() {
  180. var args = headArgs.concat(Array.prototype.slice.call(arguments));
  181. return fn.apply(obj, args);
  182. };
  183. return bound;
  184. };
  185. })();
  186. (function checkDatasetProperty() {
  187. if (!hasDOM) {
  188. return;
  189. }
  190. var div = document.createElement('div');
  191. if ('dataset' in div) {
  192. return;
  193. }
  194. Object.defineProperty(HTMLElement.prototype, 'dataset', {
  195. get: function get() {
  196. if (this._dataset) {
  197. return this._dataset;
  198. }
  199. var dataset = {};
  200. for (var j = 0, jj = this.attributes.length; j < jj; j++) {
  201. var attribute = this.attributes[j];
  202. if (attribute.name.substring(0, 5) !== 'data-') {
  203. continue;
  204. }
  205. var key = attribute.name.substring(5).replace(/\-([a-z])/g, function (all, ch) {
  206. return ch.toUpperCase();
  207. });
  208. dataset[key] = attribute.value;
  209. }
  210. Object.defineProperty(this, '_dataset', {
  211. value: dataset,
  212. writable: false,
  213. enumerable: false
  214. });
  215. return dataset;
  216. },
  217. enumerable: true
  218. });
  219. })();
  220. (function checkClassListProperty() {
  221. function changeList(element, itemName, add, remove) {
  222. var s = element.className || '';
  223. var list = s.split(/\s+/g);
  224. if (list[0] === '') {
  225. list.shift();
  226. }
  227. var index = list.indexOf(itemName);
  228. if (index < 0 && add) {
  229. list.push(itemName);
  230. }
  231. if (index >= 0 && remove) {
  232. list.splice(index, 1);
  233. }
  234. element.className = list.join(' ');
  235. return index >= 0;
  236. }
  237. if (!hasDOM) {
  238. return;
  239. }
  240. var div = document.createElement('div');
  241. if ('classList' in div) {
  242. return;
  243. }
  244. var classListPrototype = {
  245. add: function add(name) {
  246. changeList(this.element, name, true, false);
  247. },
  248. contains: function contains(name) {
  249. return changeList(this.element, name, false, false);
  250. },
  251. remove: function remove(name) {
  252. changeList(this.element, name, false, true);
  253. },
  254. toggle: function toggle(name) {
  255. changeList(this.element, name, true, true);
  256. }
  257. };
  258. Object.defineProperty(HTMLElement.prototype, 'classList', {
  259. get: function get() {
  260. if (this._classList) {
  261. return this._classList;
  262. }
  263. var classList = Object.create(classListPrototype, {
  264. element: {
  265. value: this,
  266. writable: false,
  267. enumerable: true
  268. }
  269. });
  270. Object.defineProperty(this, '_classList', {
  271. value: classList,
  272. writable: false,
  273. enumerable: false
  274. });
  275. return classList;
  276. },
  277. enumerable: true
  278. });
  279. })();
  280. (function checkWorkerConsoleCompatibility() {
  281. if (typeof importScripts === 'undefined' || 'console' in globalScope) {
  282. return;
  283. }
  284. var consoleTimer = {};
  285. var workerConsole = {
  286. log: function log() {
  287. var args = Array.prototype.slice.call(arguments);
  288. globalScope.postMessage({
  289. targetName: 'main',
  290. action: 'console_log',
  291. data: args
  292. });
  293. },
  294. error: function error() {
  295. var args = Array.prototype.slice.call(arguments);
  296. globalScope.postMessage({
  297. targetName: 'main',
  298. action: 'console_error',
  299. data: args
  300. });
  301. },
  302. time: function time(name) {
  303. consoleTimer[name] = Date.now();
  304. },
  305. timeEnd: function timeEnd(name) {
  306. var time = consoleTimer[name];
  307. if (!time) {
  308. throw new Error('Unknown timer name ' + name);
  309. }
  310. this.log('Timer:', name, Date.now() - time);
  311. }
  312. };
  313. globalScope.console = workerConsole;
  314. })();
  315. (function checkConsoleCompatibility() {
  316. if (!hasDOM) {
  317. return;
  318. }
  319. if (!('console' in window)) {
  320. window.console = {
  321. log: function log() {},
  322. error: function error() {},
  323. warn: function warn() {}
  324. };
  325. return;
  326. }
  327. if (!('bind' in console.log)) {
  328. console.log = function (fn) {
  329. return function (msg) {
  330. return fn(msg);
  331. };
  332. }(console.log);
  333. console.error = function (fn) {
  334. return function (msg) {
  335. return fn(msg);
  336. };
  337. }(console.error);
  338. console.warn = function (fn) {
  339. return function (msg) {
  340. return fn(msg);
  341. };
  342. }(console.warn);
  343. return;
  344. }
  345. })();
  346. (function checkOnClickCompatibility() {
  347. function ignoreIfTargetDisabled(event) {
  348. if (isDisabled(event.target)) {
  349. event.stopPropagation();
  350. }
  351. }
  352. function isDisabled(node) {
  353. return node.disabled || node.parentNode && isDisabled(node.parentNode);
  354. }
  355. if (isOpera) {
  356. document.addEventListener('click', ignoreIfTargetDisabled, true);
  357. }
  358. })();
  359. (function checkOnBlobSupport() {
  360. if (isIE || isIOSChrome) {
  361. PDFJS.disableCreateObjectURL = true;
  362. }
  363. })();
  364. (function checkNavigatorLanguage() {
  365. if (typeof navigator === 'undefined') {
  366. return;
  367. }
  368. if ('language' in navigator) {
  369. return;
  370. }
  371. PDFJS.locale = navigator.userLanguage || 'en-US';
  372. })();
  373. (function checkRangeRequests() {
  374. if (isSafari || isIOS) {
  375. PDFJS.disableRange = true;
  376. PDFJS.disableStream = true;
  377. }
  378. })();
  379. (function checkSetPresenceInImageData() {
  380. if (!hasDOM) {
  381. return;
  382. }
  383. if (window.CanvasPixelArray) {
  384. if (typeof window.CanvasPixelArray.prototype.set !== 'function') {
  385. window.CanvasPixelArray.prototype.set = function (arr) {
  386. for (var i = 0, ii = this.length; i < ii; i++) {
  387. this[i] = arr[i];
  388. }
  389. };
  390. }
  391. } else {
  392. var polyfill = false,
  393. versionMatch;
  394. if (isChrome) {
  395. versionMatch = userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
  396. polyfill = versionMatch && parseInt(versionMatch[2]) < 21;
  397. } else if (isAndroid) {
  398. polyfill = isAndroidPre5;
  399. } else if (isSafari) {
  400. versionMatch = userAgent.match(/Version\/([0-9]+)\.([0-9]+)\.([0-9]+) Safari\//);
  401. polyfill = versionMatch && parseInt(versionMatch[1]) < 6;
  402. }
  403. if (polyfill) {
  404. var contextPrototype = window.CanvasRenderingContext2D.prototype;
  405. var createImageData = contextPrototype.createImageData;
  406. contextPrototype.createImageData = function (w, h) {
  407. var imageData = createImageData.call(this, w, h);
  408. imageData.data.set = function (arr) {
  409. for (var i = 0, ii = this.length; i < ii; i++) {
  410. this[i] = arr[i];
  411. }
  412. };
  413. return imageData;
  414. };
  415. contextPrototype = null;
  416. }
  417. }
  418. })();
  419. (function checkRequestAnimationFrame() {
  420. function installFakeAnimationFrameFunctions() {
  421. window.requestAnimationFrame = function (callback) {
  422. return window.setTimeout(callback, 20);
  423. };
  424. window.cancelAnimationFrame = function (timeoutID) {
  425. window.clearTimeout(timeoutID);
  426. };
  427. }
  428. if (!hasDOM) {
  429. return;
  430. }
  431. if (isIOS) {
  432. installFakeAnimationFrameFunctions();
  433. return;
  434. }
  435. if ('requestAnimationFrame' in window) {
  436. return;
  437. }
  438. window.requestAnimationFrame = window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame;
  439. if (window.requestAnimationFrame) {
  440. return;
  441. }
  442. installFakeAnimationFrameFunctions();
  443. })();
  444. (function checkCanvasSizeLimitation() {
  445. if (isIOS || isAndroid) {
  446. PDFJS.maxCanvasPixels = 5242880;
  447. }
  448. })();
  449. (function checkFullscreenSupport() {
  450. if (!hasDOM) {
  451. return;
  452. }
  453. if (isIE && window.parent !== window) {
  454. PDFJS.disableFullscreen = true;
  455. }
  456. })();
  457. (function checkCurrentScript() {
  458. if (!hasDOM) {
  459. return;
  460. }
  461. if ('currentScript' in document) {
  462. return;
  463. }
  464. Object.defineProperty(document, 'currentScript', {
  465. get: function get() {
  466. var scripts = document.getElementsByTagName('script');
  467. return scripts[scripts.length - 1];
  468. },
  469. enumerable: true,
  470. configurable: true
  471. });
  472. })();
  473. (function checkInputTypeNumberAssign() {
  474. if (!hasDOM) {
  475. return;
  476. }
  477. var el = document.createElement('input');
  478. try {
  479. el.type = 'number';
  480. } catch (ex) {
  481. var inputProto = el.constructor.prototype;
  482. var typeProperty = Object.getOwnPropertyDescriptor(inputProto, 'type');
  483. Object.defineProperty(inputProto, 'type', {
  484. get: function get() {
  485. return typeProperty.get.call(this);
  486. },
  487. set: function set(value) {
  488. typeProperty.set.call(this, value === 'number' ? 'text' : value);
  489. },
  490. enumerable: true,
  491. configurable: true
  492. });
  493. }
  494. })();
  495. (function checkDocumentReadyState() {
  496. if (!hasDOM) {
  497. return;
  498. }
  499. if (!document.attachEvent) {
  500. return;
  501. }
  502. var documentProto = document.constructor.prototype;
  503. var readyStateProto = Object.getOwnPropertyDescriptor(documentProto, 'readyState');
  504. Object.defineProperty(documentProto, 'readyState', {
  505. get: function get() {
  506. var value = readyStateProto.get.call(this);
  507. return value === 'interactive' ? 'loading' : value;
  508. },
  509. set: function set(value) {
  510. readyStateProto.set.call(this, value);
  511. },
  512. enumerable: true,
  513. configurable: true
  514. });
  515. })();
  516. (function checkChildNodeRemove() {
  517. if (!hasDOM) {
  518. return;
  519. }
  520. if (typeof Element.prototype.remove !== 'undefined') {
  521. return;
  522. }
  523. Element.prototype.remove = function () {
  524. if (this.parentNode) {
  525. this.parentNode.removeChild(this);
  526. }
  527. };
  528. })();
  529. (function checkObjectValues() {
  530. if (Object.values) {
  531. return;
  532. }
  533. Object.values = require('core-js/fn/object/values');
  534. })();
  535. (function checkArrayIncludes() {
  536. if (Array.prototype.includes) {
  537. return;
  538. }
  539. Array.prototype.includes = require('core-js/fn/array/includes');
  540. })();
  541. (function checkNumberIsNaN() {
  542. if (Number.isNaN) {
  543. return;
  544. }
  545. Number.isNaN = require('core-js/fn/number/is-nan');
  546. })();
  547. (function checkNumberIsInteger() {
  548. if (Number.isInteger) {
  549. return;
  550. }
  551. Number.isInteger = require('core-js/fn/number/is-integer');
  552. })();
  553. (function checkPromise() {
  554. if (globalScope.Promise) {
  555. return;
  556. }
  557. globalScope.Promise = require('core-js/fn/promise');
  558. })();
  559. (function checkWeakMap() {
  560. if (globalScope.WeakMap) {
  561. return;
  562. }
  563. globalScope.WeakMap = require('core-js/fn/weak-map');
  564. })();
  565. (function checkURLConstructor() {
  566. var hasWorkingUrl = false;
  567. try {
  568. if (typeof URL === 'function' && _typeof(URL.prototype) === 'object' && 'origin' in URL.prototype) {
  569. var u = new URL('b', 'http://a');
  570. u.pathname = 'c%20d';
  571. hasWorkingUrl = u.href === 'http://a/c%20d';
  572. }
  573. } catch (e) {}
  574. if (hasWorkingUrl) {
  575. return;
  576. }
  577. var relative = Object.create(null);
  578. relative['ftp'] = 21;
  579. relative['file'] = 0;
  580. relative['gopher'] = 70;
  581. relative['http'] = 80;
  582. relative['https'] = 443;
  583. relative['ws'] = 80;
  584. relative['wss'] = 443;
  585. var relativePathDotMapping = Object.create(null);
  586. relativePathDotMapping['%2e'] = '.';
  587. relativePathDotMapping['.%2e'] = '..';
  588. relativePathDotMapping['%2e.'] = '..';
  589. relativePathDotMapping['%2e%2e'] = '..';
  590. function isRelativeScheme(scheme) {
  591. return relative[scheme] !== undefined;
  592. }
  593. function invalid() {
  594. clear.call(this);
  595. this._isInvalid = true;
  596. }
  597. function IDNAToASCII(h) {
  598. if (h === '') {
  599. invalid.call(this);
  600. }
  601. return h.toLowerCase();
  602. }
  603. function percentEscape(c) {
  604. var unicode = c.charCodeAt(0);
  605. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) === -1) {
  606. return c;
  607. }
  608. return encodeURIComponent(c);
  609. }
  610. function percentEscapeQuery(c) {
  611. var unicode = c.charCodeAt(0);
  612. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) === -1) {
  613. return c;
  614. }
  615. return encodeURIComponent(c);
  616. }
  617. var EOF,
  618. ALPHA = /[a-zA-Z]/,
  619. ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/;
  620. function parse(input, stateOverride, base) {
  621. function err(message) {
  622. errors.push(message);
  623. }
  624. var state = stateOverride || 'scheme start',
  625. cursor = 0,
  626. buffer = '',
  627. seenAt = false,
  628. seenBracket = false,
  629. errors = [];
  630. loop: while ((input[cursor - 1] !== EOF || cursor === 0) && !this._isInvalid) {
  631. var c = input[cursor];
  632. switch (state) {
  633. case 'scheme start':
  634. if (c && ALPHA.test(c)) {
  635. buffer += c.toLowerCase();
  636. state = 'scheme';
  637. } else if (!stateOverride) {
  638. buffer = '';
  639. state = 'no scheme';
  640. continue;
  641. } else {
  642. err('Invalid scheme.');
  643. break loop;
  644. }
  645. break;
  646. case 'scheme':
  647. if (c && ALPHANUMERIC.test(c)) {
  648. buffer += c.toLowerCase();
  649. } else if (c === ':') {
  650. this._scheme = buffer;
  651. buffer = '';
  652. if (stateOverride) {
  653. break loop;
  654. }
  655. if (isRelativeScheme(this._scheme)) {
  656. this._isRelative = true;
  657. }
  658. if (this._scheme === 'file') {
  659. state = 'relative';
  660. } else if (this._isRelative && base && base._scheme === this._scheme) {
  661. state = 'relative or authority';
  662. } else if (this._isRelative) {
  663. state = 'authority first slash';
  664. } else {
  665. state = 'scheme data';
  666. }
  667. } else if (!stateOverride) {
  668. buffer = '';
  669. cursor = 0;
  670. state = 'no scheme';
  671. continue;
  672. } else if (c === EOF) {
  673. break loop;
  674. } else {
  675. err('Code point not allowed in scheme: ' + c);
  676. break loop;
  677. }
  678. break;
  679. case 'scheme data':
  680. if (c === '?') {
  681. this._query = '?';
  682. state = 'query';
  683. } else if (c === '#') {
  684. this._fragment = '#';
  685. state = 'fragment';
  686. } else {
  687. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  688. this._schemeData += percentEscape(c);
  689. }
  690. }
  691. break;
  692. case 'no scheme':
  693. if (!base || !isRelativeScheme(base._scheme)) {
  694. err('Missing scheme.');
  695. invalid.call(this);
  696. } else {
  697. state = 'relative';
  698. continue;
  699. }
  700. break;
  701. case 'relative or authority':
  702. if (c === '/' && input[cursor + 1] === '/') {
  703. state = 'authority ignore slashes';
  704. } else {
  705. err('Expected /, got: ' + c);
  706. state = 'relative';
  707. continue;
  708. }
  709. break;
  710. case 'relative':
  711. this._isRelative = true;
  712. if (this._scheme !== 'file') {
  713. this._scheme = base._scheme;
  714. }
  715. if (c === EOF) {
  716. this._host = base._host;
  717. this._port = base._port;
  718. this._path = base._path.slice();
  719. this._query = base._query;
  720. this._username = base._username;
  721. this._password = base._password;
  722. break loop;
  723. } else if (c === '/' || c === '\\') {
  724. if (c === '\\') {
  725. err('\\ is an invalid code point.');
  726. }
  727. state = 'relative slash';
  728. } else if (c === '?') {
  729. this._host = base._host;
  730. this._port = base._port;
  731. this._path = base._path.slice();
  732. this._query = '?';
  733. this._username = base._username;
  734. this._password = base._password;
  735. state = 'query';
  736. } else if (c === '#') {
  737. this._host = base._host;
  738. this._port = base._port;
  739. this._path = base._path.slice();
  740. this._query = base._query;
  741. this._fragment = '#';
  742. this._username = base._username;
  743. this._password = base._password;
  744. state = 'fragment';
  745. } else {
  746. var nextC = input[cursor + 1];
  747. var nextNextC = input[cursor + 2];
  748. if (this._scheme !== 'file' || !ALPHA.test(c) || nextC !== ':' && nextC !== '|' || nextNextC !== EOF && nextNextC !== '/' && nextNextC !== '\\' && nextNextC !== '?' && nextNextC !== '#') {
  749. this._host = base._host;
  750. this._port = base._port;
  751. this._username = base._username;
  752. this._password = base._password;
  753. this._path = base._path.slice();
  754. this._path.pop();
  755. }
  756. state = 'relative path';
  757. continue;
  758. }
  759. break;
  760. case 'relative slash':
  761. if (c === '/' || c === '\\') {
  762. if (c === '\\') {
  763. err('\\ is an invalid code point.');
  764. }
  765. if (this._scheme === 'file') {
  766. state = 'file host';
  767. } else {
  768. state = 'authority ignore slashes';
  769. }
  770. } else {
  771. if (this._scheme !== 'file') {
  772. this._host = base._host;
  773. this._port = base._port;
  774. this._username = base._username;
  775. this._password = base._password;
  776. }
  777. state = 'relative path';
  778. continue;
  779. }
  780. break;
  781. case 'authority first slash':
  782. if (c === '/') {
  783. state = 'authority second slash';
  784. } else {
  785. err('Expected \'/\', got: ' + c);
  786. state = 'authority ignore slashes';
  787. continue;
  788. }
  789. break;
  790. case 'authority second slash':
  791. state = 'authority ignore slashes';
  792. if (c !== '/') {
  793. err('Expected \'/\', got: ' + c);
  794. continue;
  795. }
  796. break;
  797. case 'authority ignore slashes':
  798. if (c !== '/' && c !== '\\') {
  799. state = 'authority';
  800. continue;
  801. } else {
  802. err('Expected authority, got: ' + c);
  803. }
  804. break;
  805. case 'authority':
  806. if (c === '@') {
  807. if (seenAt) {
  808. err('@ already seen.');
  809. buffer += '%40';
  810. }
  811. seenAt = true;
  812. for (var i = 0; i < buffer.length; i++) {
  813. var cp = buffer[i];
  814. if (cp === '\t' || cp === '\n' || cp === '\r') {
  815. err('Invalid whitespace in authority.');
  816. continue;
  817. }
  818. if (cp === ':' && this._password === null) {
  819. this._password = '';
  820. continue;
  821. }
  822. var tempC = percentEscape(cp);
  823. if (this._password !== null) {
  824. this._password += tempC;
  825. } else {
  826. this._username += tempC;
  827. }
  828. }
  829. buffer = '';
  830. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  831. cursor -= buffer.length;
  832. buffer = '';
  833. state = 'host';
  834. continue;
  835. } else {
  836. buffer += c;
  837. }
  838. break;
  839. case 'file host':
  840. if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  841. if (buffer.length === 2 && ALPHA.test(buffer[0]) && (buffer[1] === ':' || buffer[1] === '|')) {
  842. state = 'relative path';
  843. } else if (buffer.length === 0) {
  844. state = 'relative path start';
  845. } else {
  846. this._host = IDNAToASCII.call(this, buffer);
  847. buffer = '';
  848. state = 'relative path start';
  849. }
  850. continue;
  851. } else if (c === '\t' || c === '\n' || c === '\r') {
  852. err('Invalid whitespace in file host.');
  853. } else {
  854. buffer += c;
  855. }
  856. break;
  857. case 'host':
  858. case 'hostname':
  859. if (c === ':' && !seenBracket) {
  860. this._host = IDNAToASCII.call(this, buffer);
  861. buffer = '';
  862. state = 'port';
  863. if (stateOverride === 'hostname') {
  864. break loop;
  865. }
  866. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  867. this._host = IDNAToASCII.call(this, buffer);
  868. buffer = '';
  869. state = 'relative path start';
  870. if (stateOverride) {
  871. break loop;
  872. }
  873. continue;
  874. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  875. if (c === '[') {
  876. seenBracket = true;
  877. } else if (c === ']') {
  878. seenBracket = false;
  879. }
  880. buffer += c;
  881. } else {
  882. err('Invalid code point in host/hostname: ' + c);
  883. }
  884. break;
  885. case 'port':
  886. if (/[0-9]/.test(c)) {
  887. buffer += c;
  888. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#' || stateOverride) {
  889. if (buffer !== '') {
  890. var temp = parseInt(buffer, 10);
  891. if (temp !== relative[this._scheme]) {
  892. this._port = temp + '';
  893. }
  894. buffer = '';
  895. }
  896. if (stateOverride) {
  897. break loop;
  898. }
  899. state = 'relative path start';
  900. continue;
  901. } else if (c === '\t' || c === '\n' || c === '\r') {
  902. err('Invalid code point in port: ' + c);
  903. } else {
  904. invalid.call(this);
  905. }
  906. break;
  907. case 'relative path start':
  908. if (c === '\\') {
  909. err('\'\\\' not allowed in path.');
  910. }
  911. state = 'relative path';
  912. if (c !== '/' && c !== '\\') {
  913. continue;
  914. }
  915. break;
  916. case 'relative path':
  917. if (c === EOF || c === '/' || c === '\\' || !stateOverride && (c === '?' || c === '#')) {
  918. if (c === '\\') {
  919. err('\\ not allowed in relative path.');
  920. }
  921. var tmp;
  922. if (tmp = relativePathDotMapping[buffer.toLowerCase()]) {
  923. buffer = tmp;
  924. }
  925. if (buffer === '..') {
  926. this._path.pop();
  927. if (c !== '/' && c !== '\\') {
  928. this._path.push('');
  929. }
  930. } else if (buffer === '.' && c !== '/' && c !== '\\') {
  931. this._path.push('');
  932. } else if (buffer !== '.') {
  933. if (this._scheme === 'file' && this._path.length === 0 && buffer.length === 2 && ALPHA.test(buffer[0]) && buffer[1] === '|') {
  934. buffer = buffer[0] + ':';
  935. }
  936. this._path.push(buffer);
  937. }
  938. buffer = '';
  939. if (c === '?') {
  940. this._query = '?';
  941. state = 'query';
  942. } else if (c === '#') {
  943. this._fragment = '#';
  944. state = 'fragment';
  945. }
  946. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  947. buffer += percentEscape(c);
  948. }
  949. break;
  950. case 'query':
  951. if (!stateOverride && c === '#') {
  952. this._fragment = '#';
  953. state = 'fragment';
  954. } else if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  955. this._query += percentEscapeQuery(c);
  956. }
  957. break;
  958. case 'fragment':
  959. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  960. this._fragment += c;
  961. }
  962. break;
  963. }
  964. cursor++;
  965. }
  966. }
  967. function clear() {
  968. this._scheme = '';
  969. this._schemeData = '';
  970. this._username = '';
  971. this._password = null;
  972. this._host = '';
  973. this._port = '';
  974. this._path = [];
  975. this._query = '';
  976. this._fragment = '';
  977. this._isInvalid = false;
  978. this._isRelative = false;
  979. }
  980. function JURL(url, base) {
  981. if (base !== undefined && !(base instanceof JURL)) {
  982. base = new JURL(String(base));
  983. }
  984. this._url = url;
  985. clear.call(this);
  986. var input = url.replace(/^[ \t\r\n\f]+|[ \t\r\n\f]+$/g, '');
  987. parse.call(this, input, null, base);
  988. }
  989. JURL.prototype = {
  990. toString: function toString() {
  991. return this.href;
  992. },
  993. get href() {
  994. if (this._isInvalid) {
  995. return this._url;
  996. }
  997. var authority = '';
  998. if (this._username !== '' || this._password !== null) {
  999. authority = this._username + (this._password !== null ? ':' + this._password : '') + '@';
  1000. }
  1001. return this.protocol + (this._isRelative ? '//' + authority + this.host : '') + this.pathname + this._query + this._fragment;
  1002. },
  1003. set href(value) {
  1004. clear.call(this);
  1005. parse.call(this, value);
  1006. },
  1007. get protocol() {
  1008. return this._scheme + ':';
  1009. },
  1010. set protocol(value) {
  1011. if (this._isInvalid) {
  1012. return;
  1013. }
  1014. parse.call(this, value + ':', 'scheme start');
  1015. },
  1016. get host() {
  1017. return this._isInvalid ? '' : this._port ? this._host + ':' + this._port : this._host;
  1018. },
  1019. set host(value) {
  1020. if (this._isInvalid || !this._isRelative) {
  1021. return;
  1022. }
  1023. parse.call(this, value, 'host');
  1024. },
  1025. get hostname() {
  1026. return this._host;
  1027. },
  1028. set hostname(value) {
  1029. if (this._isInvalid || !this._isRelative) {
  1030. return;
  1031. }
  1032. parse.call(this, value, 'hostname');
  1033. },
  1034. get port() {
  1035. return this._port;
  1036. },
  1037. set port(value) {
  1038. if (this._isInvalid || !this._isRelative) {
  1039. return;
  1040. }
  1041. parse.call(this, value, 'port');
  1042. },
  1043. get pathname() {
  1044. return this._isInvalid ? '' : this._isRelative ? '/' + this._path.join('/') : this._schemeData;
  1045. },
  1046. set pathname(value) {
  1047. if (this._isInvalid || !this._isRelative) {
  1048. return;
  1049. }
  1050. this._path = [];
  1051. parse.call(this, value, 'relative path start');
  1052. },
  1053. get search() {
  1054. return this._isInvalid || !this._query || this._query === '?' ? '' : this._query;
  1055. },
  1056. set search(value) {
  1057. if (this._isInvalid || !this._isRelative) {
  1058. return;
  1059. }
  1060. this._query = '?';
  1061. if (value[0] === '?') {
  1062. value = value.slice(1);
  1063. }
  1064. parse.call(this, value, 'query');
  1065. },
  1066. get hash() {
  1067. return this._isInvalid || !this._fragment || this._fragment === '#' ? '' : this._fragment;
  1068. },
  1069. set hash(value) {
  1070. if (this._isInvalid) {
  1071. return;
  1072. }
  1073. this._fragment = '#';
  1074. if (value[0] === '#') {
  1075. value = value.slice(1);
  1076. }
  1077. parse.call(this, value, 'fragment');
  1078. },
  1079. get origin() {
  1080. var host;
  1081. if (this._isInvalid || !this._scheme) {
  1082. return '';
  1083. }
  1084. switch (this._scheme) {
  1085. case 'data':
  1086. case 'file':
  1087. case 'javascript':
  1088. case 'mailto':
  1089. return 'null';
  1090. case 'blob':
  1091. try {
  1092. return new JURL(this._schemeData).origin || 'null';
  1093. } catch (_) {}
  1094. return 'null';
  1095. }
  1096. host = this.host;
  1097. if (!host) {
  1098. return '';
  1099. }
  1100. return this._scheme + '://' + host;
  1101. }
  1102. };
  1103. var OriginalURL = globalScope.URL;
  1104. if (OriginalURL) {
  1105. JURL.createObjectURL = function (blob) {
  1106. return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
  1107. };
  1108. JURL.revokeObjectURL = function (url) {
  1109. OriginalURL.revokeObjectURL(url);
  1110. };
  1111. }
  1112. globalScope.URL = JURL;
  1113. })();
  1114. }