compatibility.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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 checkCanvasSizeLimitation() {
  420. if (isIOS || isAndroid) {
  421. PDFJS.maxCanvasPixels = 5242880;
  422. }
  423. })();
  424. (function checkFullscreenSupport() {
  425. if (!hasDOM) {
  426. return;
  427. }
  428. if (isIE && window.parent !== window) {
  429. PDFJS.disableFullscreen = true;
  430. }
  431. })();
  432. (function checkCurrentScript() {
  433. if (!hasDOM) {
  434. return;
  435. }
  436. if ('currentScript' in document) {
  437. return;
  438. }
  439. Object.defineProperty(document, 'currentScript', {
  440. get: function get() {
  441. var scripts = document.getElementsByTagName('script');
  442. return scripts[scripts.length - 1];
  443. },
  444. enumerable: true,
  445. configurable: true
  446. });
  447. })();
  448. (function checkInputTypeNumberAssign() {
  449. if (!hasDOM) {
  450. return;
  451. }
  452. var el = document.createElement('input');
  453. try {
  454. el.type = 'number';
  455. } catch (ex) {
  456. var inputProto = el.constructor.prototype;
  457. var typeProperty = Object.getOwnPropertyDescriptor(inputProto, 'type');
  458. Object.defineProperty(inputProto, 'type', {
  459. get: function get() {
  460. return typeProperty.get.call(this);
  461. },
  462. set: function set(value) {
  463. typeProperty.set.call(this, value === 'number' ? 'text' : value);
  464. },
  465. enumerable: true,
  466. configurable: true
  467. });
  468. }
  469. })();
  470. (function checkDocumentReadyState() {
  471. if (!hasDOM) {
  472. return;
  473. }
  474. if (!document.attachEvent) {
  475. return;
  476. }
  477. var documentProto = document.constructor.prototype;
  478. var readyStateProto = Object.getOwnPropertyDescriptor(documentProto, 'readyState');
  479. Object.defineProperty(documentProto, 'readyState', {
  480. get: function get() {
  481. var value = readyStateProto.get.call(this);
  482. return value === 'interactive' ? 'loading' : value;
  483. },
  484. set: function set(value) {
  485. readyStateProto.set.call(this, value);
  486. },
  487. enumerable: true,
  488. configurable: true
  489. });
  490. })();
  491. (function checkChildNodeRemove() {
  492. if (!hasDOM) {
  493. return;
  494. }
  495. if (typeof Element.prototype.remove !== 'undefined') {
  496. return;
  497. }
  498. Element.prototype.remove = function () {
  499. if (this.parentNode) {
  500. this.parentNode.removeChild(this);
  501. }
  502. };
  503. })();
  504. (function checkObjectValues() {
  505. if (Object.values) {
  506. return;
  507. }
  508. Object.values = require('core-js/fn/object/values');
  509. })();
  510. (function checkArrayIncludes() {
  511. if (Array.prototype.includes) {
  512. return;
  513. }
  514. Array.prototype.includes = require('core-js/fn/array/includes');
  515. })();
  516. (function checkNumberIsNaN() {
  517. if (Number.isNaN) {
  518. return;
  519. }
  520. Number.isNaN = require('core-js/fn/number/is-nan');
  521. })();
  522. (function checkNumberIsInteger() {
  523. if (Number.isInteger) {
  524. return;
  525. }
  526. Number.isInteger = require('core-js/fn/number/is-integer');
  527. })();
  528. (function checkPromise() {
  529. if (globalScope.Promise) {
  530. return;
  531. }
  532. globalScope.Promise = require('core-js/fn/promise');
  533. })();
  534. (function checkWeakMap() {
  535. if (globalScope.WeakMap) {
  536. return;
  537. }
  538. globalScope.WeakMap = require('core-js/fn/weak-map');
  539. })();
  540. (function checkURLConstructor() {
  541. var hasWorkingUrl = false;
  542. try {
  543. if (typeof URL === 'function' && _typeof(URL.prototype) === 'object' && 'origin' in URL.prototype) {
  544. var u = new URL('b', 'http://a');
  545. u.pathname = 'c%20d';
  546. hasWorkingUrl = u.href === 'http://a/c%20d';
  547. }
  548. } catch (e) {}
  549. if (hasWorkingUrl) {
  550. return;
  551. }
  552. var relative = Object.create(null);
  553. relative['ftp'] = 21;
  554. relative['file'] = 0;
  555. relative['gopher'] = 70;
  556. relative['http'] = 80;
  557. relative['https'] = 443;
  558. relative['ws'] = 80;
  559. relative['wss'] = 443;
  560. var relativePathDotMapping = Object.create(null);
  561. relativePathDotMapping['%2e'] = '.';
  562. relativePathDotMapping['.%2e'] = '..';
  563. relativePathDotMapping['%2e.'] = '..';
  564. relativePathDotMapping['%2e%2e'] = '..';
  565. function isRelativeScheme(scheme) {
  566. return relative[scheme] !== undefined;
  567. }
  568. function invalid() {
  569. clear.call(this);
  570. this._isInvalid = true;
  571. }
  572. function IDNAToASCII(h) {
  573. if (h === '') {
  574. invalid.call(this);
  575. }
  576. return h.toLowerCase();
  577. }
  578. function percentEscape(c) {
  579. var unicode = c.charCodeAt(0);
  580. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) === -1) {
  581. return c;
  582. }
  583. return encodeURIComponent(c);
  584. }
  585. function percentEscapeQuery(c) {
  586. var unicode = c.charCodeAt(0);
  587. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) === -1) {
  588. return c;
  589. }
  590. return encodeURIComponent(c);
  591. }
  592. var EOF,
  593. ALPHA = /[a-zA-Z]/,
  594. ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/;
  595. function parse(input, stateOverride, base) {
  596. function err(message) {
  597. errors.push(message);
  598. }
  599. var state = stateOverride || 'scheme start',
  600. cursor = 0,
  601. buffer = '',
  602. seenAt = false,
  603. seenBracket = false,
  604. errors = [];
  605. loop: while ((input[cursor - 1] !== EOF || cursor === 0) && !this._isInvalid) {
  606. var c = input[cursor];
  607. switch (state) {
  608. case 'scheme start':
  609. if (c && ALPHA.test(c)) {
  610. buffer += c.toLowerCase();
  611. state = 'scheme';
  612. } else if (!stateOverride) {
  613. buffer = '';
  614. state = 'no scheme';
  615. continue;
  616. } else {
  617. err('Invalid scheme.');
  618. break loop;
  619. }
  620. break;
  621. case 'scheme':
  622. if (c && ALPHANUMERIC.test(c)) {
  623. buffer += c.toLowerCase();
  624. } else if (c === ':') {
  625. this._scheme = buffer;
  626. buffer = '';
  627. if (stateOverride) {
  628. break loop;
  629. }
  630. if (isRelativeScheme(this._scheme)) {
  631. this._isRelative = true;
  632. }
  633. if (this._scheme === 'file') {
  634. state = 'relative';
  635. } else if (this._isRelative && base && base._scheme === this._scheme) {
  636. state = 'relative or authority';
  637. } else if (this._isRelative) {
  638. state = 'authority first slash';
  639. } else {
  640. state = 'scheme data';
  641. }
  642. } else if (!stateOverride) {
  643. buffer = '';
  644. cursor = 0;
  645. state = 'no scheme';
  646. continue;
  647. } else if (c === EOF) {
  648. break loop;
  649. } else {
  650. err('Code point not allowed in scheme: ' + c);
  651. break loop;
  652. }
  653. break;
  654. case 'scheme data':
  655. if (c === '?') {
  656. this._query = '?';
  657. state = 'query';
  658. } else if (c === '#') {
  659. this._fragment = '#';
  660. state = 'fragment';
  661. } else {
  662. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  663. this._schemeData += percentEscape(c);
  664. }
  665. }
  666. break;
  667. case 'no scheme':
  668. if (!base || !isRelativeScheme(base._scheme)) {
  669. err('Missing scheme.');
  670. invalid.call(this);
  671. } else {
  672. state = 'relative';
  673. continue;
  674. }
  675. break;
  676. case 'relative or authority':
  677. if (c === '/' && input[cursor + 1] === '/') {
  678. state = 'authority ignore slashes';
  679. } else {
  680. err('Expected /, got: ' + c);
  681. state = 'relative';
  682. continue;
  683. }
  684. break;
  685. case 'relative':
  686. this._isRelative = true;
  687. if (this._scheme !== 'file') {
  688. this._scheme = base._scheme;
  689. }
  690. if (c === EOF) {
  691. this._host = base._host;
  692. this._port = base._port;
  693. this._path = base._path.slice();
  694. this._query = base._query;
  695. this._username = base._username;
  696. this._password = base._password;
  697. break loop;
  698. } else if (c === '/' || c === '\\') {
  699. if (c === '\\') {
  700. err('\\ is an invalid code point.');
  701. }
  702. state = 'relative slash';
  703. } else if (c === '?') {
  704. this._host = base._host;
  705. this._port = base._port;
  706. this._path = base._path.slice();
  707. this._query = '?';
  708. this._username = base._username;
  709. this._password = base._password;
  710. state = 'query';
  711. } else if (c === '#') {
  712. this._host = base._host;
  713. this._port = base._port;
  714. this._path = base._path.slice();
  715. this._query = base._query;
  716. this._fragment = '#';
  717. this._username = base._username;
  718. this._password = base._password;
  719. state = 'fragment';
  720. } else {
  721. var nextC = input[cursor + 1];
  722. var nextNextC = input[cursor + 2];
  723. if (this._scheme !== 'file' || !ALPHA.test(c) || nextC !== ':' && nextC !== '|' || nextNextC !== EOF && nextNextC !== '/' && nextNextC !== '\\' && nextNextC !== '?' && nextNextC !== '#') {
  724. this._host = base._host;
  725. this._port = base._port;
  726. this._username = base._username;
  727. this._password = base._password;
  728. this._path = base._path.slice();
  729. this._path.pop();
  730. }
  731. state = 'relative path';
  732. continue;
  733. }
  734. break;
  735. case 'relative slash':
  736. if (c === '/' || c === '\\') {
  737. if (c === '\\') {
  738. err('\\ is an invalid code point.');
  739. }
  740. if (this._scheme === 'file') {
  741. state = 'file host';
  742. } else {
  743. state = 'authority ignore slashes';
  744. }
  745. } else {
  746. if (this._scheme !== 'file') {
  747. this._host = base._host;
  748. this._port = base._port;
  749. this._username = base._username;
  750. this._password = base._password;
  751. }
  752. state = 'relative path';
  753. continue;
  754. }
  755. break;
  756. case 'authority first slash':
  757. if (c === '/') {
  758. state = 'authority second slash';
  759. } else {
  760. err('Expected \'/\', got: ' + c);
  761. state = 'authority ignore slashes';
  762. continue;
  763. }
  764. break;
  765. case 'authority second slash':
  766. state = 'authority ignore slashes';
  767. if (c !== '/') {
  768. err('Expected \'/\', got: ' + c);
  769. continue;
  770. }
  771. break;
  772. case 'authority ignore slashes':
  773. if (c !== '/' && c !== '\\') {
  774. state = 'authority';
  775. continue;
  776. } else {
  777. err('Expected authority, got: ' + c);
  778. }
  779. break;
  780. case 'authority':
  781. if (c === '@') {
  782. if (seenAt) {
  783. err('@ already seen.');
  784. buffer += '%40';
  785. }
  786. seenAt = true;
  787. for (var i = 0; i < buffer.length; i++) {
  788. var cp = buffer[i];
  789. if (cp === '\t' || cp === '\n' || cp === '\r') {
  790. err('Invalid whitespace in authority.');
  791. continue;
  792. }
  793. if (cp === ':' && this._password === null) {
  794. this._password = '';
  795. continue;
  796. }
  797. var tempC = percentEscape(cp);
  798. if (this._password !== null) {
  799. this._password += tempC;
  800. } else {
  801. this._username += tempC;
  802. }
  803. }
  804. buffer = '';
  805. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  806. cursor -= buffer.length;
  807. buffer = '';
  808. state = 'host';
  809. continue;
  810. } else {
  811. buffer += c;
  812. }
  813. break;
  814. case 'file host':
  815. if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  816. if (buffer.length === 2 && ALPHA.test(buffer[0]) && (buffer[1] === ':' || buffer[1] === '|')) {
  817. state = 'relative path';
  818. } else if (buffer.length === 0) {
  819. state = 'relative path start';
  820. } else {
  821. this._host = IDNAToASCII.call(this, buffer);
  822. buffer = '';
  823. state = 'relative path start';
  824. }
  825. continue;
  826. } else if (c === '\t' || c === '\n' || c === '\r') {
  827. err('Invalid whitespace in file host.');
  828. } else {
  829. buffer += c;
  830. }
  831. break;
  832. case 'host':
  833. case 'hostname':
  834. if (c === ':' && !seenBracket) {
  835. this._host = IDNAToASCII.call(this, buffer);
  836. buffer = '';
  837. state = 'port';
  838. if (stateOverride === 'hostname') {
  839. break loop;
  840. }
  841. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  842. this._host = IDNAToASCII.call(this, buffer);
  843. buffer = '';
  844. state = 'relative path start';
  845. if (stateOverride) {
  846. break loop;
  847. }
  848. continue;
  849. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  850. if (c === '[') {
  851. seenBracket = true;
  852. } else if (c === ']') {
  853. seenBracket = false;
  854. }
  855. buffer += c;
  856. } else {
  857. err('Invalid code point in host/hostname: ' + c);
  858. }
  859. break;
  860. case 'port':
  861. if (/[0-9]/.test(c)) {
  862. buffer += c;
  863. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#' || stateOverride) {
  864. if (buffer !== '') {
  865. var temp = parseInt(buffer, 10);
  866. if (temp !== relative[this._scheme]) {
  867. this._port = temp + '';
  868. }
  869. buffer = '';
  870. }
  871. if (stateOverride) {
  872. break loop;
  873. }
  874. state = 'relative path start';
  875. continue;
  876. } else if (c === '\t' || c === '\n' || c === '\r') {
  877. err('Invalid code point in port: ' + c);
  878. } else {
  879. invalid.call(this);
  880. }
  881. break;
  882. case 'relative path start':
  883. if (c === '\\') {
  884. err('\'\\\' not allowed in path.');
  885. }
  886. state = 'relative path';
  887. if (c !== '/' && c !== '\\') {
  888. continue;
  889. }
  890. break;
  891. case 'relative path':
  892. if (c === EOF || c === '/' || c === '\\' || !stateOverride && (c === '?' || c === '#')) {
  893. if (c === '\\') {
  894. err('\\ not allowed in relative path.');
  895. }
  896. var tmp;
  897. if (tmp = relativePathDotMapping[buffer.toLowerCase()]) {
  898. buffer = tmp;
  899. }
  900. if (buffer === '..') {
  901. this._path.pop();
  902. if (c !== '/' && c !== '\\') {
  903. this._path.push('');
  904. }
  905. } else if (buffer === '.' && c !== '/' && c !== '\\') {
  906. this._path.push('');
  907. } else if (buffer !== '.') {
  908. if (this._scheme === 'file' && this._path.length === 0 && buffer.length === 2 && ALPHA.test(buffer[0]) && buffer[1] === '|') {
  909. buffer = buffer[0] + ':';
  910. }
  911. this._path.push(buffer);
  912. }
  913. buffer = '';
  914. if (c === '?') {
  915. this._query = '?';
  916. state = 'query';
  917. } else if (c === '#') {
  918. this._fragment = '#';
  919. state = 'fragment';
  920. }
  921. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  922. buffer += percentEscape(c);
  923. }
  924. break;
  925. case 'query':
  926. if (!stateOverride && c === '#') {
  927. this._fragment = '#';
  928. state = 'fragment';
  929. } else if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  930. this._query += percentEscapeQuery(c);
  931. }
  932. break;
  933. case 'fragment':
  934. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  935. this._fragment += c;
  936. }
  937. break;
  938. }
  939. cursor++;
  940. }
  941. }
  942. function clear() {
  943. this._scheme = '';
  944. this._schemeData = '';
  945. this._username = '';
  946. this._password = null;
  947. this._host = '';
  948. this._port = '';
  949. this._path = [];
  950. this._query = '';
  951. this._fragment = '';
  952. this._isInvalid = false;
  953. this._isRelative = false;
  954. }
  955. function JURL(url, base) {
  956. if (base !== undefined && !(base instanceof JURL)) {
  957. base = new JURL(String(base));
  958. }
  959. this._url = url;
  960. clear.call(this);
  961. var input = url.replace(/^[ \t\r\n\f]+|[ \t\r\n\f]+$/g, '');
  962. parse.call(this, input, null, base);
  963. }
  964. JURL.prototype = {
  965. toString: function toString() {
  966. return this.href;
  967. },
  968. get href() {
  969. if (this._isInvalid) {
  970. return this._url;
  971. }
  972. var authority = '';
  973. if (this._username !== '' || this._password !== null) {
  974. authority = this._username + (this._password !== null ? ':' + this._password : '') + '@';
  975. }
  976. return this.protocol + (this._isRelative ? '//' + authority + this.host : '') + this.pathname + this._query + this._fragment;
  977. },
  978. set href(value) {
  979. clear.call(this);
  980. parse.call(this, value);
  981. },
  982. get protocol() {
  983. return this._scheme + ':';
  984. },
  985. set protocol(value) {
  986. if (this._isInvalid) {
  987. return;
  988. }
  989. parse.call(this, value + ':', 'scheme start');
  990. },
  991. get host() {
  992. return this._isInvalid ? '' : this._port ? this._host + ':' + this._port : this._host;
  993. },
  994. set host(value) {
  995. if (this._isInvalid || !this._isRelative) {
  996. return;
  997. }
  998. parse.call(this, value, 'host');
  999. },
  1000. get hostname() {
  1001. return this._host;
  1002. },
  1003. set hostname(value) {
  1004. if (this._isInvalid || !this._isRelative) {
  1005. return;
  1006. }
  1007. parse.call(this, value, 'hostname');
  1008. },
  1009. get port() {
  1010. return this._port;
  1011. },
  1012. set port(value) {
  1013. if (this._isInvalid || !this._isRelative) {
  1014. return;
  1015. }
  1016. parse.call(this, value, 'port');
  1017. },
  1018. get pathname() {
  1019. return this._isInvalid ? '' : this._isRelative ? '/' + this._path.join('/') : this._schemeData;
  1020. },
  1021. set pathname(value) {
  1022. if (this._isInvalid || !this._isRelative) {
  1023. return;
  1024. }
  1025. this._path = [];
  1026. parse.call(this, value, 'relative path start');
  1027. },
  1028. get search() {
  1029. return this._isInvalid || !this._query || this._query === '?' ? '' : this._query;
  1030. },
  1031. set search(value) {
  1032. if (this._isInvalid || !this._isRelative) {
  1033. return;
  1034. }
  1035. this._query = '?';
  1036. if (value[0] === '?') {
  1037. value = value.slice(1);
  1038. }
  1039. parse.call(this, value, 'query');
  1040. },
  1041. get hash() {
  1042. return this._isInvalid || !this._fragment || this._fragment === '#' ? '' : this._fragment;
  1043. },
  1044. set hash(value) {
  1045. if (this._isInvalid) {
  1046. return;
  1047. }
  1048. this._fragment = '#';
  1049. if (value[0] === '#') {
  1050. value = value.slice(1);
  1051. }
  1052. parse.call(this, value, 'fragment');
  1053. },
  1054. get origin() {
  1055. var host;
  1056. if (this._isInvalid || !this._scheme) {
  1057. return '';
  1058. }
  1059. switch (this._scheme) {
  1060. case 'data':
  1061. case 'file':
  1062. case 'javascript':
  1063. case 'mailto':
  1064. return 'null';
  1065. case 'blob':
  1066. try {
  1067. return new JURL(this._schemeData).origin || 'null';
  1068. } catch (_) {}
  1069. return 'null';
  1070. }
  1071. host = this.host;
  1072. if (!host) {
  1073. return '';
  1074. }
  1075. return this._scheme + '://' + host;
  1076. }
  1077. };
  1078. var OriginalURL = globalScope.URL;
  1079. if (OriginalURL) {
  1080. JURL.createObjectURL = function (blob) {
  1081. return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
  1082. };
  1083. JURL.revokeObjectURL = function (url) {
  1084. OriginalURL.revokeObjectURL(url);
  1085. };
  1086. }
  1087. globalScope.URL = JURL;
  1088. })();
  1089. }