compatibility.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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 isSafari = /Safari\//.test(userAgent) && !/(Chrome\/|Android\s)/.test(userAgent);
  27. var hasDOM = (typeof window === 'undefined' ? 'undefined' : _typeof(window)) === 'object' && (typeof document === 'undefined' ? 'undefined' : _typeof(document)) === 'object';
  28. if (typeof PDFJS === 'undefined') {
  29. globalScope.PDFJS = {};
  30. }
  31. PDFJS.compatibilityChecked = true;
  32. (function normalizeURLObject() {
  33. if (!globalScope.URL) {
  34. globalScope.URL = globalScope.webkitURL;
  35. }
  36. })();
  37. (function checkXMLHttpRequestResponseCompatibility() {
  38. if (typeof XMLHttpRequest === 'undefined') {
  39. return;
  40. }
  41. var xhrPrototype = XMLHttpRequest.prototype;
  42. var xhr = new XMLHttpRequest();
  43. if (!('overrideMimeType' in xhr)) {
  44. Object.defineProperty(xhrPrototype, 'overrideMimeType', {
  45. value: function xmlHttpRequestOverrideMimeType(mimeType) {}
  46. });
  47. }
  48. if ('responseType' in xhr) {
  49. return;
  50. }
  51. Object.defineProperty(xhrPrototype, 'responseType', {
  52. get: function xmlHttpRequestGetResponseType() {
  53. return this._responseType || 'text';
  54. },
  55. set: function xmlHttpRequestSetResponseType(value) {
  56. if (value === 'text' || value === 'arraybuffer') {
  57. this._responseType = value;
  58. if (value === 'arraybuffer' && typeof this.overrideMimeType === 'function') {
  59. this.overrideMimeType('text/plain; charset=x-user-defined');
  60. }
  61. }
  62. }
  63. });
  64. Object.defineProperty(xhrPrototype, 'response', {
  65. get: function xmlHttpRequestResponseGet() {
  66. if (this.responseType !== 'arraybuffer') {
  67. return this.responseText;
  68. }
  69. var text = this.responseText;
  70. var i,
  71. n = text.length;
  72. var result = new Uint8Array(n);
  73. for (i = 0; i < n; ++i) {
  74. result[i] = text.charCodeAt(i) & 0xFF;
  75. }
  76. return result.buffer;
  77. }
  78. });
  79. })();
  80. (function checkDatasetProperty() {
  81. if (!hasDOM) {
  82. return;
  83. }
  84. var div = document.createElement('div');
  85. if ('dataset' in div) {
  86. return;
  87. }
  88. Object.defineProperty(HTMLElement.prototype, 'dataset', {
  89. get: function get() {
  90. if (this._dataset) {
  91. return this._dataset;
  92. }
  93. var dataset = {};
  94. for (var j = 0, jj = this.attributes.length; j < jj; j++) {
  95. var attribute = this.attributes[j];
  96. if (attribute.name.substring(0, 5) !== 'data-') {
  97. continue;
  98. }
  99. var key = attribute.name.substring(5).replace(/\-([a-z])/g, function (all, ch) {
  100. return ch.toUpperCase();
  101. });
  102. dataset[key] = attribute.value;
  103. }
  104. Object.defineProperty(this, '_dataset', {
  105. value: dataset,
  106. writable: false,
  107. enumerable: false
  108. });
  109. return dataset;
  110. },
  111. enumerable: true
  112. });
  113. })();
  114. (function checkOnBlobSupport() {
  115. if (isIE || isIOSChrome) {
  116. PDFJS.disableCreateObjectURL = true;
  117. }
  118. })();
  119. (function checkNavigatorLanguage() {
  120. if (typeof navigator === 'undefined') {
  121. return;
  122. }
  123. if ('language' in navigator) {
  124. return;
  125. }
  126. PDFJS.locale = navigator.userLanguage || 'en-US';
  127. })();
  128. (function checkRangeRequests() {
  129. if (isSafari || isIOS) {
  130. PDFJS.disableRange = true;
  131. PDFJS.disableStream = true;
  132. }
  133. })();
  134. (function checkSetPresenceInImageData() {
  135. if (!hasDOM) {
  136. return;
  137. }
  138. if (window.CanvasPixelArray) {
  139. if (typeof window.CanvasPixelArray.prototype.set !== 'function') {
  140. window.CanvasPixelArray.prototype.set = function (arr) {
  141. for (var i = 0, ii = this.length; i < ii; i++) {
  142. this[i] = arr[i];
  143. }
  144. };
  145. }
  146. } else {
  147. var polyfill = false,
  148. versionMatch;
  149. if (isChrome) {
  150. versionMatch = userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
  151. polyfill = versionMatch && parseInt(versionMatch[2]) < 21;
  152. } else if (isAndroid) {
  153. polyfill = isAndroidPre5;
  154. } else if (isSafari) {
  155. versionMatch = userAgent.match(/Version\/([0-9]+)\.([0-9]+)\.([0-9]+) Safari\//);
  156. polyfill = versionMatch && parseInt(versionMatch[1]) < 6;
  157. }
  158. if (polyfill) {
  159. var contextPrototype = window.CanvasRenderingContext2D.prototype;
  160. var createImageData = contextPrototype.createImageData;
  161. contextPrototype.createImageData = function (w, h) {
  162. var imageData = createImageData.call(this, w, h);
  163. imageData.data.set = function (arr) {
  164. for (var i = 0, ii = this.length; i < ii; i++) {
  165. this[i] = arr[i];
  166. }
  167. };
  168. return imageData;
  169. };
  170. contextPrototype = null;
  171. }
  172. }
  173. })();
  174. (function checkCanvasSizeLimitation() {
  175. if (isIOS || isAndroid) {
  176. PDFJS.maxCanvasPixels = 5242880;
  177. }
  178. })();
  179. (function checkFullscreenSupport() {
  180. if (!hasDOM) {
  181. return;
  182. }
  183. if (isIE && window.parent !== window) {
  184. PDFJS.disableFullscreen = true;
  185. }
  186. })();
  187. (function checkCurrentScript() {
  188. if (!hasDOM) {
  189. return;
  190. }
  191. if ('currentScript' in document) {
  192. return;
  193. }
  194. Object.defineProperty(document, 'currentScript', {
  195. get: function get() {
  196. var scripts = document.getElementsByTagName('script');
  197. return scripts[scripts.length - 1];
  198. },
  199. enumerable: true,
  200. configurable: true
  201. });
  202. })();
  203. (function checkInputTypeNumberAssign() {
  204. if (!hasDOM) {
  205. return;
  206. }
  207. var el = document.createElement('input');
  208. try {
  209. el.type = 'number';
  210. } catch (ex) {
  211. var inputProto = el.constructor.prototype;
  212. var typeProperty = Object.getOwnPropertyDescriptor(inputProto, 'type');
  213. Object.defineProperty(inputProto, 'type', {
  214. get: function get() {
  215. return typeProperty.get.call(this);
  216. },
  217. set: function set(value) {
  218. typeProperty.set.call(this, value === 'number' ? 'text' : value);
  219. },
  220. enumerable: true,
  221. configurable: true
  222. });
  223. }
  224. })();
  225. (function checkDocumentReadyState() {
  226. if (!hasDOM) {
  227. return;
  228. }
  229. if (!document.attachEvent) {
  230. return;
  231. }
  232. var documentProto = document.constructor.prototype;
  233. var readyStateProto = Object.getOwnPropertyDescriptor(documentProto, 'readyState');
  234. Object.defineProperty(documentProto, 'readyState', {
  235. get: function get() {
  236. var value = readyStateProto.get.call(this);
  237. return value === 'interactive' ? 'loading' : value;
  238. },
  239. set: function set(value) {
  240. readyStateProto.set.call(this, value);
  241. },
  242. enumerable: true,
  243. configurable: true
  244. });
  245. })();
  246. (function checkChildNodeRemove() {
  247. if (!hasDOM) {
  248. return;
  249. }
  250. if (typeof Element.prototype.remove !== 'undefined') {
  251. return;
  252. }
  253. Element.prototype.remove = function () {
  254. if (this.parentNode) {
  255. this.parentNode.removeChild(this);
  256. }
  257. };
  258. })();
  259. (function checkObjectValues() {
  260. if (Object.values) {
  261. return;
  262. }
  263. Object.values = require('core-js/fn/object/values');
  264. })();
  265. (function checkArrayIncludes() {
  266. if (Array.prototype.includes) {
  267. return;
  268. }
  269. Array.prototype.includes = require('core-js/fn/array/includes');
  270. })();
  271. (function checkNumberIsNaN() {
  272. if (Number.isNaN) {
  273. return;
  274. }
  275. Number.isNaN = require('core-js/fn/number/is-nan');
  276. })();
  277. (function checkNumberIsInteger() {
  278. if (Number.isInteger) {
  279. return;
  280. }
  281. Number.isInteger = require('core-js/fn/number/is-integer');
  282. })();
  283. (function checkPromise() {
  284. if (globalScope.Promise) {
  285. return;
  286. }
  287. globalScope.Promise = require('core-js/fn/promise');
  288. })();
  289. (function checkWeakMap() {
  290. if (globalScope.WeakMap) {
  291. return;
  292. }
  293. globalScope.WeakMap = require('core-js/fn/weak-map');
  294. })();
  295. (function checkURLConstructor() {
  296. var hasWorkingUrl = false;
  297. try {
  298. if (typeof URL === 'function' && _typeof(URL.prototype) === 'object' && 'origin' in URL.prototype) {
  299. var u = new URL('b', 'http://a');
  300. u.pathname = 'c%20d';
  301. hasWorkingUrl = u.href === 'http://a/c%20d';
  302. }
  303. } catch (e) {}
  304. if (hasWorkingUrl) {
  305. return;
  306. }
  307. var relative = Object.create(null);
  308. relative['ftp'] = 21;
  309. relative['file'] = 0;
  310. relative['gopher'] = 70;
  311. relative['http'] = 80;
  312. relative['https'] = 443;
  313. relative['ws'] = 80;
  314. relative['wss'] = 443;
  315. var relativePathDotMapping = Object.create(null);
  316. relativePathDotMapping['%2e'] = '.';
  317. relativePathDotMapping['.%2e'] = '..';
  318. relativePathDotMapping['%2e.'] = '..';
  319. relativePathDotMapping['%2e%2e'] = '..';
  320. function isRelativeScheme(scheme) {
  321. return relative[scheme] !== undefined;
  322. }
  323. function invalid() {
  324. clear.call(this);
  325. this._isInvalid = true;
  326. }
  327. function IDNAToASCII(h) {
  328. if (h === '') {
  329. invalid.call(this);
  330. }
  331. return h.toLowerCase();
  332. }
  333. function percentEscape(c) {
  334. var unicode = c.charCodeAt(0);
  335. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) === -1) {
  336. return c;
  337. }
  338. return encodeURIComponent(c);
  339. }
  340. function percentEscapeQuery(c) {
  341. var unicode = c.charCodeAt(0);
  342. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) === -1) {
  343. return c;
  344. }
  345. return encodeURIComponent(c);
  346. }
  347. var EOF,
  348. ALPHA = /[a-zA-Z]/,
  349. ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/;
  350. function parse(input, stateOverride, base) {
  351. function err(message) {
  352. errors.push(message);
  353. }
  354. var state = stateOverride || 'scheme start',
  355. cursor = 0,
  356. buffer = '',
  357. seenAt = false,
  358. seenBracket = false,
  359. errors = [];
  360. loop: while ((input[cursor - 1] !== EOF || cursor === 0) && !this._isInvalid) {
  361. var c = input[cursor];
  362. switch (state) {
  363. case 'scheme start':
  364. if (c && ALPHA.test(c)) {
  365. buffer += c.toLowerCase();
  366. state = 'scheme';
  367. } else if (!stateOverride) {
  368. buffer = '';
  369. state = 'no scheme';
  370. continue;
  371. } else {
  372. err('Invalid scheme.');
  373. break loop;
  374. }
  375. break;
  376. case 'scheme':
  377. if (c && ALPHANUMERIC.test(c)) {
  378. buffer += c.toLowerCase();
  379. } else if (c === ':') {
  380. this._scheme = buffer;
  381. buffer = '';
  382. if (stateOverride) {
  383. break loop;
  384. }
  385. if (isRelativeScheme(this._scheme)) {
  386. this._isRelative = true;
  387. }
  388. if (this._scheme === 'file') {
  389. state = 'relative';
  390. } else if (this._isRelative && base && base._scheme === this._scheme) {
  391. state = 'relative or authority';
  392. } else if (this._isRelative) {
  393. state = 'authority first slash';
  394. } else {
  395. state = 'scheme data';
  396. }
  397. } else if (!stateOverride) {
  398. buffer = '';
  399. cursor = 0;
  400. state = 'no scheme';
  401. continue;
  402. } else if (c === EOF) {
  403. break loop;
  404. } else {
  405. err('Code point not allowed in scheme: ' + c);
  406. break loop;
  407. }
  408. break;
  409. case 'scheme data':
  410. if (c === '?') {
  411. this._query = '?';
  412. state = 'query';
  413. } else if (c === '#') {
  414. this._fragment = '#';
  415. state = 'fragment';
  416. } else {
  417. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  418. this._schemeData += percentEscape(c);
  419. }
  420. }
  421. break;
  422. case 'no scheme':
  423. if (!base || !isRelativeScheme(base._scheme)) {
  424. err('Missing scheme.');
  425. invalid.call(this);
  426. } else {
  427. state = 'relative';
  428. continue;
  429. }
  430. break;
  431. case 'relative or authority':
  432. if (c === '/' && input[cursor + 1] === '/') {
  433. state = 'authority ignore slashes';
  434. } else {
  435. err('Expected /, got: ' + c);
  436. state = 'relative';
  437. continue;
  438. }
  439. break;
  440. case 'relative':
  441. this._isRelative = true;
  442. if (this._scheme !== 'file') {
  443. this._scheme = base._scheme;
  444. }
  445. if (c === EOF) {
  446. this._host = base._host;
  447. this._port = base._port;
  448. this._path = base._path.slice();
  449. this._query = base._query;
  450. this._username = base._username;
  451. this._password = base._password;
  452. break loop;
  453. } else if (c === '/' || c === '\\') {
  454. if (c === '\\') {
  455. err('\\ is an invalid code point.');
  456. }
  457. state = 'relative slash';
  458. } else if (c === '?') {
  459. this._host = base._host;
  460. this._port = base._port;
  461. this._path = base._path.slice();
  462. this._query = '?';
  463. this._username = base._username;
  464. this._password = base._password;
  465. state = 'query';
  466. } else if (c === '#') {
  467. this._host = base._host;
  468. this._port = base._port;
  469. this._path = base._path.slice();
  470. this._query = base._query;
  471. this._fragment = '#';
  472. this._username = base._username;
  473. this._password = base._password;
  474. state = 'fragment';
  475. } else {
  476. var nextC = input[cursor + 1];
  477. var nextNextC = input[cursor + 2];
  478. if (this._scheme !== 'file' || !ALPHA.test(c) || nextC !== ':' && nextC !== '|' || nextNextC !== EOF && nextNextC !== '/' && nextNextC !== '\\' && nextNextC !== '?' && nextNextC !== '#') {
  479. this._host = base._host;
  480. this._port = base._port;
  481. this._username = base._username;
  482. this._password = base._password;
  483. this._path = base._path.slice();
  484. this._path.pop();
  485. }
  486. state = 'relative path';
  487. continue;
  488. }
  489. break;
  490. case 'relative slash':
  491. if (c === '/' || c === '\\') {
  492. if (c === '\\') {
  493. err('\\ is an invalid code point.');
  494. }
  495. if (this._scheme === 'file') {
  496. state = 'file host';
  497. } else {
  498. state = 'authority ignore slashes';
  499. }
  500. } else {
  501. if (this._scheme !== 'file') {
  502. this._host = base._host;
  503. this._port = base._port;
  504. this._username = base._username;
  505. this._password = base._password;
  506. }
  507. state = 'relative path';
  508. continue;
  509. }
  510. break;
  511. case 'authority first slash':
  512. if (c === '/') {
  513. state = 'authority second slash';
  514. } else {
  515. err('Expected \'/\', got: ' + c);
  516. state = 'authority ignore slashes';
  517. continue;
  518. }
  519. break;
  520. case 'authority second slash':
  521. state = 'authority ignore slashes';
  522. if (c !== '/') {
  523. err('Expected \'/\', got: ' + c);
  524. continue;
  525. }
  526. break;
  527. case 'authority ignore slashes':
  528. if (c !== '/' && c !== '\\') {
  529. state = 'authority';
  530. continue;
  531. } else {
  532. err('Expected authority, got: ' + c);
  533. }
  534. break;
  535. case 'authority':
  536. if (c === '@') {
  537. if (seenAt) {
  538. err('@ already seen.');
  539. buffer += '%40';
  540. }
  541. seenAt = true;
  542. for (var i = 0; i < buffer.length; i++) {
  543. var cp = buffer[i];
  544. if (cp === '\t' || cp === '\n' || cp === '\r') {
  545. err('Invalid whitespace in authority.');
  546. continue;
  547. }
  548. if (cp === ':' && this._password === null) {
  549. this._password = '';
  550. continue;
  551. }
  552. var tempC = percentEscape(cp);
  553. if (this._password !== null) {
  554. this._password += tempC;
  555. } else {
  556. this._username += tempC;
  557. }
  558. }
  559. buffer = '';
  560. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  561. cursor -= buffer.length;
  562. buffer = '';
  563. state = 'host';
  564. continue;
  565. } else {
  566. buffer += c;
  567. }
  568. break;
  569. case 'file host':
  570. if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  571. if (buffer.length === 2 && ALPHA.test(buffer[0]) && (buffer[1] === ':' || buffer[1] === '|')) {
  572. state = 'relative path';
  573. } else if (buffer.length === 0) {
  574. state = 'relative path start';
  575. } else {
  576. this._host = IDNAToASCII.call(this, buffer);
  577. buffer = '';
  578. state = 'relative path start';
  579. }
  580. continue;
  581. } else if (c === '\t' || c === '\n' || c === '\r') {
  582. err('Invalid whitespace in file host.');
  583. } else {
  584. buffer += c;
  585. }
  586. break;
  587. case 'host':
  588. case 'hostname':
  589. if (c === ':' && !seenBracket) {
  590. this._host = IDNAToASCII.call(this, buffer);
  591. buffer = '';
  592. state = 'port';
  593. if (stateOverride === 'hostname') {
  594. break loop;
  595. }
  596. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  597. this._host = IDNAToASCII.call(this, buffer);
  598. buffer = '';
  599. state = 'relative path start';
  600. if (stateOverride) {
  601. break loop;
  602. }
  603. continue;
  604. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  605. if (c === '[') {
  606. seenBracket = true;
  607. } else if (c === ']') {
  608. seenBracket = false;
  609. }
  610. buffer += c;
  611. } else {
  612. err('Invalid code point in host/hostname: ' + c);
  613. }
  614. break;
  615. case 'port':
  616. if (/[0-9]/.test(c)) {
  617. buffer += c;
  618. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#' || stateOverride) {
  619. if (buffer !== '') {
  620. var temp = parseInt(buffer, 10);
  621. if (temp !== relative[this._scheme]) {
  622. this._port = temp + '';
  623. }
  624. buffer = '';
  625. }
  626. if (stateOverride) {
  627. break loop;
  628. }
  629. state = 'relative path start';
  630. continue;
  631. } else if (c === '\t' || c === '\n' || c === '\r') {
  632. err('Invalid code point in port: ' + c);
  633. } else {
  634. invalid.call(this);
  635. }
  636. break;
  637. case 'relative path start':
  638. if (c === '\\') {
  639. err('\'\\\' not allowed in path.');
  640. }
  641. state = 'relative path';
  642. if (c !== '/' && c !== '\\') {
  643. continue;
  644. }
  645. break;
  646. case 'relative path':
  647. if (c === EOF || c === '/' || c === '\\' || !stateOverride && (c === '?' || c === '#')) {
  648. if (c === '\\') {
  649. err('\\ not allowed in relative path.');
  650. }
  651. var tmp;
  652. if (tmp = relativePathDotMapping[buffer.toLowerCase()]) {
  653. buffer = tmp;
  654. }
  655. if (buffer === '..') {
  656. this._path.pop();
  657. if (c !== '/' && c !== '\\') {
  658. this._path.push('');
  659. }
  660. } else if (buffer === '.' && c !== '/' && c !== '\\') {
  661. this._path.push('');
  662. } else if (buffer !== '.') {
  663. if (this._scheme === 'file' && this._path.length === 0 && buffer.length === 2 && ALPHA.test(buffer[0]) && buffer[1] === '|') {
  664. buffer = buffer[0] + ':';
  665. }
  666. this._path.push(buffer);
  667. }
  668. buffer = '';
  669. if (c === '?') {
  670. this._query = '?';
  671. state = 'query';
  672. } else if (c === '#') {
  673. this._fragment = '#';
  674. state = 'fragment';
  675. }
  676. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  677. buffer += percentEscape(c);
  678. }
  679. break;
  680. case 'query':
  681. if (!stateOverride && c === '#') {
  682. this._fragment = '#';
  683. state = 'fragment';
  684. } else if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  685. this._query += percentEscapeQuery(c);
  686. }
  687. break;
  688. case 'fragment':
  689. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  690. this._fragment += c;
  691. }
  692. break;
  693. }
  694. cursor++;
  695. }
  696. }
  697. function clear() {
  698. this._scheme = '';
  699. this._schemeData = '';
  700. this._username = '';
  701. this._password = null;
  702. this._host = '';
  703. this._port = '';
  704. this._path = [];
  705. this._query = '';
  706. this._fragment = '';
  707. this._isInvalid = false;
  708. this._isRelative = false;
  709. }
  710. function JURL(url, base) {
  711. if (base !== undefined && !(base instanceof JURL)) {
  712. base = new JURL(String(base));
  713. }
  714. this._url = url;
  715. clear.call(this);
  716. var input = url.replace(/^[ \t\r\n\f]+|[ \t\r\n\f]+$/g, '');
  717. parse.call(this, input, null, base);
  718. }
  719. JURL.prototype = {
  720. toString: function toString() {
  721. return this.href;
  722. },
  723. get href() {
  724. if (this._isInvalid) {
  725. return this._url;
  726. }
  727. var authority = '';
  728. if (this._username !== '' || this._password !== null) {
  729. authority = this._username + (this._password !== null ? ':' + this._password : '') + '@';
  730. }
  731. return this.protocol + (this._isRelative ? '//' + authority + this.host : '') + this.pathname + this._query + this._fragment;
  732. },
  733. set href(value) {
  734. clear.call(this);
  735. parse.call(this, value);
  736. },
  737. get protocol() {
  738. return this._scheme + ':';
  739. },
  740. set protocol(value) {
  741. if (this._isInvalid) {
  742. return;
  743. }
  744. parse.call(this, value + ':', 'scheme start');
  745. },
  746. get host() {
  747. return this._isInvalid ? '' : this._port ? this._host + ':' + this._port : this._host;
  748. },
  749. set host(value) {
  750. if (this._isInvalid || !this._isRelative) {
  751. return;
  752. }
  753. parse.call(this, value, 'host');
  754. },
  755. get hostname() {
  756. return this._host;
  757. },
  758. set hostname(value) {
  759. if (this._isInvalid || !this._isRelative) {
  760. return;
  761. }
  762. parse.call(this, value, 'hostname');
  763. },
  764. get port() {
  765. return this._port;
  766. },
  767. set port(value) {
  768. if (this._isInvalid || !this._isRelative) {
  769. return;
  770. }
  771. parse.call(this, value, 'port');
  772. },
  773. get pathname() {
  774. return this._isInvalid ? '' : this._isRelative ? '/' + this._path.join('/') : this._schemeData;
  775. },
  776. set pathname(value) {
  777. if (this._isInvalid || !this._isRelative) {
  778. return;
  779. }
  780. this._path = [];
  781. parse.call(this, value, 'relative path start');
  782. },
  783. get search() {
  784. return this._isInvalid || !this._query || this._query === '?' ? '' : this._query;
  785. },
  786. set search(value) {
  787. if (this._isInvalid || !this._isRelative) {
  788. return;
  789. }
  790. this._query = '?';
  791. if (value[0] === '?') {
  792. value = value.slice(1);
  793. }
  794. parse.call(this, value, 'query');
  795. },
  796. get hash() {
  797. return this._isInvalid || !this._fragment || this._fragment === '#' ? '' : this._fragment;
  798. },
  799. set hash(value) {
  800. if (this._isInvalid) {
  801. return;
  802. }
  803. this._fragment = '#';
  804. if (value[0] === '#') {
  805. value = value.slice(1);
  806. }
  807. parse.call(this, value, 'fragment');
  808. },
  809. get origin() {
  810. var host;
  811. if (this._isInvalid || !this._scheme) {
  812. return '';
  813. }
  814. switch (this._scheme) {
  815. case 'data':
  816. case 'file':
  817. case 'javascript':
  818. case 'mailto':
  819. return 'null';
  820. case 'blob':
  821. try {
  822. return new JURL(this._schemeData).origin || 'null';
  823. } catch (_) {}
  824. return 'null';
  825. }
  826. host = this.host;
  827. if (!host) {
  828. return '';
  829. }
  830. return this._scheme + '://' + host;
  831. }
  832. };
  833. var OriginalURL = globalScope.URL;
  834. if (OriginalURL) {
  835. JURL.createObjectURL = function (blob) {
  836. return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
  837. };
  838. JURL.revokeObjectURL = function (url) {
  839. OriginalURL.revokeObjectURL(url);
  840. };
  841. }
  842. globalScope.URL = JURL;
  843. })();
  844. }