2
0

compatibility.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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 checkOnClickCompatibility() {
  281. function ignoreIfTargetDisabled(event) {
  282. if (isDisabled(event.target)) {
  283. event.stopPropagation();
  284. }
  285. }
  286. function isDisabled(node) {
  287. return node.disabled || node.parentNode && isDisabled(node.parentNode);
  288. }
  289. if (isOpera) {
  290. document.addEventListener('click', ignoreIfTargetDisabled, true);
  291. }
  292. })();
  293. (function checkOnBlobSupport() {
  294. if (isIE || isIOSChrome) {
  295. PDFJS.disableCreateObjectURL = true;
  296. }
  297. })();
  298. (function checkNavigatorLanguage() {
  299. if (typeof navigator === 'undefined') {
  300. return;
  301. }
  302. if ('language' in navigator) {
  303. return;
  304. }
  305. PDFJS.locale = navigator.userLanguage || 'en-US';
  306. })();
  307. (function checkRangeRequests() {
  308. if (isSafari || isIOS) {
  309. PDFJS.disableRange = true;
  310. PDFJS.disableStream = true;
  311. }
  312. })();
  313. (function checkSetPresenceInImageData() {
  314. if (!hasDOM) {
  315. return;
  316. }
  317. if (window.CanvasPixelArray) {
  318. if (typeof window.CanvasPixelArray.prototype.set !== 'function') {
  319. window.CanvasPixelArray.prototype.set = function (arr) {
  320. for (var i = 0, ii = this.length; i < ii; i++) {
  321. this[i] = arr[i];
  322. }
  323. };
  324. }
  325. } else {
  326. var polyfill = false,
  327. versionMatch;
  328. if (isChrome) {
  329. versionMatch = userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
  330. polyfill = versionMatch && parseInt(versionMatch[2]) < 21;
  331. } else if (isAndroid) {
  332. polyfill = isAndroidPre5;
  333. } else if (isSafari) {
  334. versionMatch = userAgent.match(/Version\/([0-9]+)\.([0-9]+)\.([0-9]+) Safari\//);
  335. polyfill = versionMatch && parseInt(versionMatch[1]) < 6;
  336. }
  337. if (polyfill) {
  338. var contextPrototype = window.CanvasRenderingContext2D.prototype;
  339. var createImageData = contextPrototype.createImageData;
  340. contextPrototype.createImageData = function (w, h) {
  341. var imageData = createImageData.call(this, w, h);
  342. imageData.data.set = function (arr) {
  343. for (var i = 0, ii = this.length; i < ii; i++) {
  344. this[i] = arr[i];
  345. }
  346. };
  347. return imageData;
  348. };
  349. contextPrototype = null;
  350. }
  351. }
  352. })();
  353. (function checkCanvasSizeLimitation() {
  354. if (isIOS || isAndroid) {
  355. PDFJS.maxCanvasPixels = 5242880;
  356. }
  357. })();
  358. (function checkFullscreenSupport() {
  359. if (!hasDOM) {
  360. return;
  361. }
  362. if (isIE && window.parent !== window) {
  363. PDFJS.disableFullscreen = true;
  364. }
  365. })();
  366. (function checkCurrentScript() {
  367. if (!hasDOM) {
  368. return;
  369. }
  370. if ('currentScript' in document) {
  371. return;
  372. }
  373. Object.defineProperty(document, 'currentScript', {
  374. get: function get() {
  375. var scripts = document.getElementsByTagName('script');
  376. return scripts[scripts.length - 1];
  377. },
  378. enumerable: true,
  379. configurable: true
  380. });
  381. })();
  382. (function checkInputTypeNumberAssign() {
  383. if (!hasDOM) {
  384. return;
  385. }
  386. var el = document.createElement('input');
  387. try {
  388. el.type = 'number';
  389. } catch (ex) {
  390. var inputProto = el.constructor.prototype;
  391. var typeProperty = Object.getOwnPropertyDescriptor(inputProto, 'type');
  392. Object.defineProperty(inputProto, 'type', {
  393. get: function get() {
  394. return typeProperty.get.call(this);
  395. },
  396. set: function set(value) {
  397. typeProperty.set.call(this, value === 'number' ? 'text' : value);
  398. },
  399. enumerable: true,
  400. configurable: true
  401. });
  402. }
  403. })();
  404. (function checkDocumentReadyState() {
  405. if (!hasDOM) {
  406. return;
  407. }
  408. if (!document.attachEvent) {
  409. return;
  410. }
  411. var documentProto = document.constructor.prototype;
  412. var readyStateProto = Object.getOwnPropertyDescriptor(documentProto, 'readyState');
  413. Object.defineProperty(documentProto, 'readyState', {
  414. get: function get() {
  415. var value = readyStateProto.get.call(this);
  416. return value === 'interactive' ? 'loading' : value;
  417. },
  418. set: function set(value) {
  419. readyStateProto.set.call(this, value);
  420. },
  421. enumerable: true,
  422. configurable: true
  423. });
  424. })();
  425. (function checkChildNodeRemove() {
  426. if (!hasDOM) {
  427. return;
  428. }
  429. if (typeof Element.prototype.remove !== 'undefined') {
  430. return;
  431. }
  432. Element.prototype.remove = function () {
  433. if (this.parentNode) {
  434. this.parentNode.removeChild(this);
  435. }
  436. };
  437. })();
  438. (function checkObjectValues() {
  439. if (Object.values) {
  440. return;
  441. }
  442. Object.values = require('core-js/fn/object/values');
  443. })();
  444. (function checkArrayIncludes() {
  445. if (Array.prototype.includes) {
  446. return;
  447. }
  448. Array.prototype.includes = require('core-js/fn/array/includes');
  449. })();
  450. (function checkNumberIsNaN() {
  451. if (Number.isNaN) {
  452. return;
  453. }
  454. Number.isNaN = require('core-js/fn/number/is-nan');
  455. })();
  456. (function checkNumberIsInteger() {
  457. if (Number.isInteger) {
  458. return;
  459. }
  460. Number.isInteger = require('core-js/fn/number/is-integer');
  461. })();
  462. (function checkPromise() {
  463. if (globalScope.Promise) {
  464. return;
  465. }
  466. globalScope.Promise = require('core-js/fn/promise');
  467. })();
  468. (function checkWeakMap() {
  469. if (globalScope.WeakMap) {
  470. return;
  471. }
  472. globalScope.WeakMap = require('core-js/fn/weak-map');
  473. })();
  474. (function checkURLConstructor() {
  475. var hasWorkingUrl = false;
  476. try {
  477. if (typeof URL === 'function' && _typeof(URL.prototype) === 'object' && 'origin' in URL.prototype) {
  478. var u = new URL('b', 'http://a');
  479. u.pathname = 'c%20d';
  480. hasWorkingUrl = u.href === 'http://a/c%20d';
  481. }
  482. } catch (e) {}
  483. if (hasWorkingUrl) {
  484. return;
  485. }
  486. var relative = Object.create(null);
  487. relative['ftp'] = 21;
  488. relative['file'] = 0;
  489. relative['gopher'] = 70;
  490. relative['http'] = 80;
  491. relative['https'] = 443;
  492. relative['ws'] = 80;
  493. relative['wss'] = 443;
  494. var relativePathDotMapping = Object.create(null);
  495. relativePathDotMapping['%2e'] = '.';
  496. relativePathDotMapping['.%2e'] = '..';
  497. relativePathDotMapping['%2e.'] = '..';
  498. relativePathDotMapping['%2e%2e'] = '..';
  499. function isRelativeScheme(scheme) {
  500. return relative[scheme] !== undefined;
  501. }
  502. function invalid() {
  503. clear.call(this);
  504. this._isInvalid = true;
  505. }
  506. function IDNAToASCII(h) {
  507. if (h === '') {
  508. invalid.call(this);
  509. }
  510. return h.toLowerCase();
  511. }
  512. function percentEscape(c) {
  513. var unicode = c.charCodeAt(0);
  514. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) === -1) {
  515. return c;
  516. }
  517. return encodeURIComponent(c);
  518. }
  519. function percentEscapeQuery(c) {
  520. var unicode = c.charCodeAt(0);
  521. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) === -1) {
  522. return c;
  523. }
  524. return encodeURIComponent(c);
  525. }
  526. var EOF,
  527. ALPHA = /[a-zA-Z]/,
  528. ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/;
  529. function parse(input, stateOverride, base) {
  530. function err(message) {
  531. errors.push(message);
  532. }
  533. var state = stateOverride || 'scheme start',
  534. cursor = 0,
  535. buffer = '',
  536. seenAt = false,
  537. seenBracket = false,
  538. errors = [];
  539. loop: while ((input[cursor - 1] !== EOF || cursor === 0) && !this._isInvalid) {
  540. var c = input[cursor];
  541. switch (state) {
  542. case 'scheme start':
  543. if (c && ALPHA.test(c)) {
  544. buffer += c.toLowerCase();
  545. state = 'scheme';
  546. } else if (!stateOverride) {
  547. buffer = '';
  548. state = 'no scheme';
  549. continue;
  550. } else {
  551. err('Invalid scheme.');
  552. break loop;
  553. }
  554. break;
  555. case 'scheme':
  556. if (c && ALPHANUMERIC.test(c)) {
  557. buffer += c.toLowerCase();
  558. } else if (c === ':') {
  559. this._scheme = buffer;
  560. buffer = '';
  561. if (stateOverride) {
  562. break loop;
  563. }
  564. if (isRelativeScheme(this._scheme)) {
  565. this._isRelative = true;
  566. }
  567. if (this._scheme === 'file') {
  568. state = 'relative';
  569. } else if (this._isRelative && base && base._scheme === this._scheme) {
  570. state = 'relative or authority';
  571. } else if (this._isRelative) {
  572. state = 'authority first slash';
  573. } else {
  574. state = 'scheme data';
  575. }
  576. } else if (!stateOverride) {
  577. buffer = '';
  578. cursor = 0;
  579. state = 'no scheme';
  580. continue;
  581. } else if (c === EOF) {
  582. break loop;
  583. } else {
  584. err('Code point not allowed in scheme: ' + c);
  585. break loop;
  586. }
  587. break;
  588. case 'scheme data':
  589. if (c === '?') {
  590. this._query = '?';
  591. state = 'query';
  592. } else if (c === '#') {
  593. this._fragment = '#';
  594. state = 'fragment';
  595. } else {
  596. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  597. this._schemeData += percentEscape(c);
  598. }
  599. }
  600. break;
  601. case 'no scheme':
  602. if (!base || !isRelativeScheme(base._scheme)) {
  603. err('Missing scheme.');
  604. invalid.call(this);
  605. } else {
  606. state = 'relative';
  607. continue;
  608. }
  609. break;
  610. case 'relative or authority':
  611. if (c === '/' && input[cursor + 1] === '/') {
  612. state = 'authority ignore slashes';
  613. } else {
  614. err('Expected /, got: ' + c);
  615. state = 'relative';
  616. continue;
  617. }
  618. break;
  619. case 'relative':
  620. this._isRelative = true;
  621. if (this._scheme !== 'file') {
  622. this._scheme = base._scheme;
  623. }
  624. if (c === EOF) {
  625. this._host = base._host;
  626. this._port = base._port;
  627. this._path = base._path.slice();
  628. this._query = base._query;
  629. this._username = base._username;
  630. this._password = base._password;
  631. break loop;
  632. } else if (c === '/' || c === '\\') {
  633. if (c === '\\') {
  634. err('\\ is an invalid code point.');
  635. }
  636. state = 'relative slash';
  637. } else if (c === '?') {
  638. this._host = base._host;
  639. this._port = base._port;
  640. this._path = base._path.slice();
  641. this._query = '?';
  642. this._username = base._username;
  643. this._password = base._password;
  644. state = 'query';
  645. } else if (c === '#') {
  646. this._host = base._host;
  647. this._port = base._port;
  648. this._path = base._path.slice();
  649. this._query = base._query;
  650. this._fragment = '#';
  651. this._username = base._username;
  652. this._password = base._password;
  653. state = 'fragment';
  654. } else {
  655. var nextC = input[cursor + 1];
  656. var nextNextC = input[cursor + 2];
  657. if (this._scheme !== 'file' || !ALPHA.test(c) || nextC !== ':' && nextC !== '|' || nextNextC !== EOF && nextNextC !== '/' && nextNextC !== '\\' && nextNextC !== '?' && nextNextC !== '#') {
  658. this._host = base._host;
  659. this._port = base._port;
  660. this._username = base._username;
  661. this._password = base._password;
  662. this._path = base._path.slice();
  663. this._path.pop();
  664. }
  665. state = 'relative path';
  666. continue;
  667. }
  668. break;
  669. case 'relative slash':
  670. if (c === '/' || c === '\\') {
  671. if (c === '\\') {
  672. err('\\ is an invalid code point.');
  673. }
  674. if (this._scheme === 'file') {
  675. state = 'file host';
  676. } else {
  677. state = 'authority ignore slashes';
  678. }
  679. } else {
  680. if (this._scheme !== 'file') {
  681. this._host = base._host;
  682. this._port = base._port;
  683. this._username = base._username;
  684. this._password = base._password;
  685. }
  686. state = 'relative path';
  687. continue;
  688. }
  689. break;
  690. case 'authority first slash':
  691. if (c === '/') {
  692. state = 'authority second slash';
  693. } else {
  694. err('Expected \'/\', got: ' + c);
  695. state = 'authority ignore slashes';
  696. continue;
  697. }
  698. break;
  699. case 'authority second slash':
  700. state = 'authority ignore slashes';
  701. if (c !== '/') {
  702. err('Expected \'/\', got: ' + c);
  703. continue;
  704. }
  705. break;
  706. case 'authority ignore slashes':
  707. if (c !== '/' && c !== '\\') {
  708. state = 'authority';
  709. continue;
  710. } else {
  711. err('Expected authority, got: ' + c);
  712. }
  713. break;
  714. case 'authority':
  715. if (c === '@') {
  716. if (seenAt) {
  717. err('@ already seen.');
  718. buffer += '%40';
  719. }
  720. seenAt = true;
  721. for (var i = 0; i < buffer.length; i++) {
  722. var cp = buffer[i];
  723. if (cp === '\t' || cp === '\n' || cp === '\r') {
  724. err('Invalid whitespace in authority.');
  725. continue;
  726. }
  727. if (cp === ':' && this._password === null) {
  728. this._password = '';
  729. continue;
  730. }
  731. var tempC = percentEscape(cp);
  732. if (this._password !== null) {
  733. this._password += tempC;
  734. } else {
  735. this._username += tempC;
  736. }
  737. }
  738. buffer = '';
  739. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  740. cursor -= buffer.length;
  741. buffer = '';
  742. state = 'host';
  743. continue;
  744. } else {
  745. buffer += c;
  746. }
  747. break;
  748. case 'file host':
  749. if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  750. if (buffer.length === 2 && ALPHA.test(buffer[0]) && (buffer[1] === ':' || buffer[1] === '|')) {
  751. state = 'relative path';
  752. } else if (buffer.length === 0) {
  753. state = 'relative path start';
  754. } else {
  755. this._host = IDNAToASCII.call(this, buffer);
  756. buffer = '';
  757. state = 'relative path start';
  758. }
  759. continue;
  760. } else if (c === '\t' || c === '\n' || c === '\r') {
  761. err('Invalid whitespace in file host.');
  762. } else {
  763. buffer += c;
  764. }
  765. break;
  766. case 'host':
  767. case 'hostname':
  768. if (c === ':' && !seenBracket) {
  769. this._host = IDNAToASCII.call(this, buffer);
  770. buffer = '';
  771. state = 'port';
  772. if (stateOverride === 'hostname') {
  773. break loop;
  774. }
  775. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  776. this._host = IDNAToASCII.call(this, buffer);
  777. buffer = '';
  778. state = 'relative path start';
  779. if (stateOverride) {
  780. break loop;
  781. }
  782. continue;
  783. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  784. if (c === '[') {
  785. seenBracket = true;
  786. } else if (c === ']') {
  787. seenBracket = false;
  788. }
  789. buffer += c;
  790. } else {
  791. err('Invalid code point in host/hostname: ' + c);
  792. }
  793. break;
  794. case 'port':
  795. if (/[0-9]/.test(c)) {
  796. buffer += c;
  797. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#' || stateOverride) {
  798. if (buffer !== '') {
  799. var temp = parseInt(buffer, 10);
  800. if (temp !== relative[this._scheme]) {
  801. this._port = temp + '';
  802. }
  803. buffer = '';
  804. }
  805. if (stateOverride) {
  806. break loop;
  807. }
  808. state = 'relative path start';
  809. continue;
  810. } else if (c === '\t' || c === '\n' || c === '\r') {
  811. err('Invalid code point in port: ' + c);
  812. } else {
  813. invalid.call(this);
  814. }
  815. break;
  816. case 'relative path start':
  817. if (c === '\\') {
  818. err('\'\\\' not allowed in path.');
  819. }
  820. state = 'relative path';
  821. if (c !== '/' && c !== '\\') {
  822. continue;
  823. }
  824. break;
  825. case 'relative path':
  826. if (c === EOF || c === '/' || c === '\\' || !stateOverride && (c === '?' || c === '#')) {
  827. if (c === '\\') {
  828. err('\\ not allowed in relative path.');
  829. }
  830. var tmp;
  831. if (tmp = relativePathDotMapping[buffer.toLowerCase()]) {
  832. buffer = tmp;
  833. }
  834. if (buffer === '..') {
  835. this._path.pop();
  836. if (c !== '/' && c !== '\\') {
  837. this._path.push('');
  838. }
  839. } else if (buffer === '.' && c !== '/' && c !== '\\') {
  840. this._path.push('');
  841. } else if (buffer !== '.') {
  842. if (this._scheme === 'file' && this._path.length === 0 && buffer.length === 2 && ALPHA.test(buffer[0]) && buffer[1] === '|') {
  843. buffer = buffer[0] + ':';
  844. }
  845. this._path.push(buffer);
  846. }
  847. buffer = '';
  848. if (c === '?') {
  849. this._query = '?';
  850. state = 'query';
  851. } else if (c === '#') {
  852. this._fragment = '#';
  853. state = 'fragment';
  854. }
  855. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  856. buffer += percentEscape(c);
  857. }
  858. break;
  859. case 'query':
  860. if (!stateOverride && c === '#') {
  861. this._fragment = '#';
  862. state = 'fragment';
  863. } else if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  864. this._query += percentEscapeQuery(c);
  865. }
  866. break;
  867. case 'fragment':
  868. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  869. this._fragment += c;
  870. }
  871. break;
  872. }
  873. cursor++;
  874. }
  875. }
  876. function clear() {
  877. this._scheme = '';
  878. this._schemeData = '';
  879. this._username = '';
  880. this._password = null;
  881. this._host = '';
  882. this._port = '';
  883. this._path = [];
  884. this._query = '';
  885. this._fragment = '';
  886. this._isInvalid = false;
  887. this._isRelative = false;
  888. }
  889. function JURL(url, base) {
  890. if (base !== undefined && !(base instanceof JURL)) {
  891. base = new JURL(String(base));
  892. }
  893. this._url = url;
  894. clear.call(this);
  895. var input = url.replace(/^[ \t\r\n\f]+|[ \t\r\n\f]+$/g, '');
  896. parse.call(this, input, null, base);
  897. }
  898. JURL.prototype = {
  899. toString: function toString() {
  900. return this.href;
  901. },
  902. get href() {
  903. if (this._isInvalid) {
  904. return this._url;
  905. }
  906. var authority = '';
  907. if (this._username !== '' || this._password !== null) {
  908. authority = this._username + (this._password !== null ? ':' + this._password : '') + '@';
  909. }
  910. return this.protocol + (this._isRelative ? '//' + authority + this.host : '') + this.pathname + this._query + this._fragment;
  911. },
  912. set href(value) {
  913. clear.call(this);
  914. parse.call(this, value);
  915. },
  916. get protocol() {
  917. return this._scheme + ':';
  918. },
  919. set protocol(value) {
  920. if (this._isInvalid) {
  921. return;
  922. }
  923. parse.call(this, value + ':', 'scheme start');
  924. },
  925. get host() {
  926. return this._isInvalid ? '' : this._port ? this._host + ':' + this._port : this._host;
  927. },
  928. set host(value) {
  929. if (this._isInvalid || !this._isRelative) {
  930. return;
  931. }
  932. parse.call(this, value, 'host');
  933. },
  934. get hostname() {
  935. return this._host;
  936. },
  937. set hostname(value) {
  938. if (this._isInvalid || !this._isRelative) {
  939. return;
  940. }
  941. parse.call(this, value, 'hostname');
  942. },
  943. get port() {
  944. return this._port;
  945. },
  946. set port(value) {
  947. if (this._isInvalid || !this._isRelative) {
  948. return;
  949. }
  950. parse.call(this, value, 'port');
  951. },
  952. get pathname() {
  953. return this._isInvalid ? '' : this._isRelative ? '/' + this._path.join('/') : this._schemeData;
  954. },
  955. set pathname(value) {
  956. if (this._isInvalid || !this._isRelative) {
  957. return;
  958. }
  959. this._path = [];
  960. parse.call(this, value, 'relative path start');
  961. },
  962. get search() {
  963. return this._isInvalid || !this._query || this._query === '?' ? '' : this._query;
  964. },
  965. set search(value) {
  966. if (this._isInvalid || !this._isRelative) {
  967. return;
  968. }
  969. this._query = '?';
  970. if (value[0] === '?') {
  971. value = value.slice(1);
  972. }
  973. parse.call(this, value, 'query');
  974. },
  975. get hash() {
  976. return this._isInvalid || !this._fragment || this._fragment === '#' ? '' : this._fragment;
  977. },
  978. set hash(value) {
  979. if (this._isInvalid) {
  980. return;
  981. }
  982. this._fragment = '#';
  983. if (value[0] === '#') {
  984. value = value.slice(1);
  985. }
  986. parse.call(this, value, 'fragment');
  987. },
  988. get origin() {
  989. var host;
  990. if (this._isInvalid || !this._scheme) {
  991. return '';
  992. }
  993. switch (this._scheme) {
  994. case 'data':
  995. case 'file':
  996. case 'javascript':
  997. case 'mailto':
  998. return 'null';
  999. case 'blob':
  1000. try {
  1001. return new JURL(this._schemeData).origin || 'null';
  1002. } catch (_) {}
  1003. return 'null';
  1004. }
  1005. host = this.host;
  1006. if (!host) {
  1007. return '';
  1008. }
  1009. return this._scheme + '://' + host;
  1010. }
  1011. };
  1012. var OriginalURL = globalScope.URL;
  1013. if (OriginalURL) {
  1014. JURL.createObjectURL = function (blob) {
  1015. return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
  1016. };
  1017. JURL.revokeObjectURL = function (url) {
  1018. OriginalURL.revokeObjectURL(url);
  1019. };
  1020. }
  1021. globalScope.URL = JURL;
  1022. })();
  1023. }