compatibility.js 35 KB

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