compatibility.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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 checkMathLog2() {
  272. if (Math.log2) {
  273. return;
  274. }
  275. Math.log2 = require('core-js/fn/math/log2');
  276. })();
  277. (function checkNumberIsNaN() {
  278. if (Number.isNaN) {
  279. return;
  280. }
  281. Number.isNaN = require('core-js/fn/number/is-nan');
  282. })();
  283. (function checkNumberIsInteger() {
  284. if (Number.isInteger) {
  285. return;
  286. }
  287. Number.isInteger = require('core-js/fn/number/is-integer');
  288. })();
  289. (function checkPromise() {
  290. if (globalScope.Promise) {
  291. return;
  292. }
  293. globalScope.Promise = require('core-js/fn/promise');
  294. })();
  295. (function checkWeakMap() {
  296. if (globalScope.WeakMap) {
  297. return;
  298. }
  299. globalScope.WeakMap = require('core-js/fn/weak-map');
  300. })();
  301. (function checkURLConstructor() {
  302. var hasWorkingUrl = false;
  303. try {
  304. if (typeof URL === 'function' && _typeof(URL.prototype) === 'object' && 'origin' in URL.prototype) {
  305. var u = new URL('b', 'http://a');
  306. u.pathname = 'c%20d';
  307. hasWorkingUrl = u.href === 'http://a/c%20d';
  308. }
  309. } catch (e) {}
  310. if (hasWorkingUrl) {
  311. return;
  312. }
  313. var relative = Object.create(null);
  314. relative['ftp'] = 21;
  315. relative['file'] = 0;
  316. relative['gopher'] = 70;
  317. relative['http'] = 80;
  318. relative['https'] = 443;
  319. relative['ws'] = 80;
  320. relative['wss'] = 443;
  321. var relativePathDotMapping = Object.create(null);
  322. relativePathDotMapping['%2e'] = '.';
  323. relativePathDotMapping['.%2e'] = '..';
  324. relativePathDotMapping['%2e.'] = '..';
  325. relativePathDotMapping['%2e%2e'] = '..';
  326. function isRelativeScheme(scheme) {
  327. return relative[scheme] !== undefined;
  328. }
  329. function invalid() {
  330. clear.call(this);
  331. this._isInvalid = true;
  332. }
  333. function IDNAToASCII(h) {
  334. if (h === '') {
  335. invalid.call(this);
  336. }
  337. return h.toLowerCase();
  338. }
  339. function percentEscape(c) {
  340. var unicode = c.charCodeAt(0);
  341. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x3F, 0x60].indexOf(unicode) === -1) {
  342. return c;
  343. }
  344. return encodeURIComponent(c);
  345. }
  346. function percentEscapeQuery(c) {
  347. var unicode = c.charCodeAt(0);
  348. if (unicode > 0x20 && unicode < 0x7F && [0x22, 0x23, 0x3C, 0x3E, 0x60].indexOf(unicode) === -1) {
  349. return c;
  350. }
  351. return encodeURIComponent(c);
  352. }
  353. var EOF,
  354. ALPHA = /[a-zA-Z]/,
  355. ALPHANUMERIC = /[a-zA-Z0-9\+\-\.]/;
  356. function parse(input, stateOverride, base) {
  357. function err(message) {
  358. errors.push(message);
  359. }
  360. var state = stateOverride || 'scheme start',
  361. cursor = 0,
  362. buffer = '',
  363. seenAt = false,
  364. seenBracket = false,
  365. errors = [];
  366. loop: while ((input[cursor - 1] !== EOF || cursor === 0) && !this._isInvalid) {
  367. var c = input[cursor];
  368. switch (state) {
  369. case 'scheme start':
  370. if (c && ALPHA.test(c)) {
  371. buffer += c.toLowerCase();
  372. state = 'scheme';
  373. } else if (!stateOverride) {
  374. buffer = '';
  375. state = 'no scheme';
  376. continue;
  377. } else {
  378. err('Invalid scheme.');
  379. break loop;
  380. }
  381. break;
  382. case 'scheme':
  383. if (c && ALPHANUMERIC.test(c)) {
  384. buffer += c.toLowerCase();
  385. } else if (c === ':') {
  386. this._scheme = buffer;
  387. buffer = '';
  388. if (stateOverride) {
  389. break loop;
  390. }
  391. if (isRelativeScheme(this._scheme)) {
  392. this._isRelative = true;
  393. }
  394. if (this._scheme === 'file') {
  395. state = 'relative';
  396. } else if (this._isRelative && base && base._scheme === this._scheme) {
  397. state = 'relative or authority';
  398. } else if (this._isRelative) {
  399. state = 'authority first slash';
  400. } else {
  401. state = 'scheme data';
  402. }
  403. } else if (!stateOverride) {
  404. buffer = '';
  405. cursor = 0;
  406. state = 'no scheme';
  407. continue;
  408. } else if (c === EOF) {
  409. break loop;
  410. } else {
  411. err('Code point not allowed in scheme: ' + c);
  412. break loop;
  413. }
  414. break;
  415. case 'scheme data':
  416. if (c === '?') {
  417. this._query = '?';
  418. state = 'query';
  419. } else if (c === '#') {
  420. this._fragment = '#';
  421. state = 'fragment';
  422. } else {
  423. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  424. this._schemeData += percentEscape(c);
  425. }
  426. }
  427. break;
  428. case 'no scheme':
  429. if (!base || !isRelativeScheme(base._scheme)) {
  430. err('Missing scheme.');
  431. invalid.call(this);
  432. } else {
  433. state = 'relative';
  434. continue;
  435. }
  436. break;
  437. case 'relative or authority':
  438. if (c === '/' && input[cursor + 1] === '/') {
  439. state = 'authority ignore slashes';
  440. } else {
  441. err('Expected /, got: ' + c);
  442. state = 'relative';
  443. continue;
  444. }
  445. break;
  446. case 'relative':
  447. this._isRelative = true;
  448. if (this._scheme !== 'file') {
  449. this._scheme = base._scheme;
  450. }
  451. if (c === EOF) {
  452. this._host = base._host;
  453. this._port = base._port;
  454. this._path = base._path.slice();
  455. this._query = base._query;
  456. this._username = base._username;
  457. this._password = base._password;
  458. break loop;
  459. } else if (c === '/' || c === '\\') {
  460. if (c === '\\') {
  461. err('\\ is an invalid code point.');
  462. }
  463. state = 'relative slash';
  464. } else if (c === '?') {
  465. this._host = base._host;
  466. this._port = base._port;
  467. this._path = base._path.slice();
  468. this._query = '?';
  469. this._username = base._username;
  470. this._password = base._password;
  471. state = 'query';
  472. } else if (c === '#') {
  473. this._host = base._host;
  474. this._port = base._port;
  475. this._path = base._path.slice();
  476. this._query = base._query;
  477. this._fragment = '#';
  478. this._username = base._username;
  479. this._password = base._password;
  480. state = 'fragment';
  481. } else {
  482. var nextC = input[cursor + 1];
  483. var nextNextC = input[cursor + 2];
  484. if (this._scheme !== 'file' || !ALPHA.test(c) || nextC !== ':' && nextC !== '|' || nextNextC !== EOF && nextNextC !== '/' && nextNextC !== '\\' && nextNextC !== '?' && nextNextC !== '#') {
  485. this._host = base._host;
  486. this._port = base._port;
  487. this._username = base._username;
  488. this._password = base._password;
  489. this._path = base._path.slice();
  490. this._path.pop();
  491. }
  492. state = 'relative path';
  493. continue;
  494. }
  495. break;
  496. case 'relative slash':
  497. if (c === '/' || c === '\\') {
  498. if (c === '\\') {
  499. err('\\ is an invalid code point.');
  500. }
  501. if (this._scheme === 'file') {
  502. state = 'file host';
  503. } else {
  504. state = 'authority ignore slashes';
  505. }
  506. } else {
  507. if (this._scheme !== 'file') {
  508. this._host = base._host;
  509. this._port = base._port;
  510. this._username = base._username;
  511. this._password = base._password;
  512. }
  513. state = 'relative path';
  514. continue;
  515. }
  516. break;
  517. case 'authority first slash':
  518. if (c === '/') {
  519. state = 'authority second slash';
  520. } else {
  521. err('Expected \'/\', got: ' + c);
  522. state = 'authority ignore slashes';
  523. continue;
  524. }
  525. break;
  526. case 'authority second slash':
  527. state = 'authority ignore slashes';
  528. if (c !== '/') {
  529. err('Expected \'/\', got: ' + c);
  530. continue;
  531. }
  532. break;
  533. case 'authority ignore slashes':
  534. if (c !== '/' && c !== '\\') {
  535. state = 'authority';
  536. continue;
  537. } else {
  538. err('Expected authority, got: ' + c);
  539. }
  540. break;
  541. case 'authority':
  542. if (c === '@') {
  543. if (seenAt) {
  544. err('@ already seen.');
  545. buffer += '%40';
  546. }
  547. seenAt = true;
  548. for (var i = 0; i < buffer.length; i++) {
  549. var cp = buffer[i];
  550. if (cp === '\t' || cp === '\n' || cp === '\r') {
  551. err('Invalid whitespace in authority.');
  552. continue;
  553. }
  554. if (cp === ':' && this._password === null) {
  555. this._password = '';
  556. continue;
  557. }
  558. var tempC = percentEscape(cp);
  559. if (this._password !== null) {
  560. this._password += tempC;
  561. } else {
  562. this._username += tempC;
  563. }
  564. }
  565. buffer = '';
  566. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  567. cursor -= buffer.length;
  568. buffer = '';
  569. state = 'host';
  570. continue;
  571. } else {
  572. buffer += c;
  573. }
  574. break;
  575. case 'file host':
  576. if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  577. if (buffer.length === 2 && ALPHA.test(buffer[0]) && (buffer[1] === ':' || buffer[1] === '|')) {
  578. state = 'relative path';
  579. } else if (buffer.length === 0) {
  580. state = 'relative path start';
  581. } else {
  582. this._host = IDNAToASCII.call(this, buffer);
  583. buffer = '';
  584. state = 'relative path start';
  585. }
  586. continue;
  587. } else if (c === '\t' || c === '\n' || c === '\r') {
  588. err('Invalid whitespace in file host.');
  589. } else {
  590. buffer += c;
  591. }
  592. break;
  593. case 'host':
  594. case 'hostname':
  595. if (c === ':' && !seenBracket) {
  596. this._host = IDNAToASCII.call(this, buffer);
  597. buffer = '';
  598. state = 'port';
  599. if (stateOverride === 'hostname') {
  600. break loop;
  601. }
  602. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#') {
  603. this._host = IDNAToASCII.call(this, buffer);
  604. buffer = '';
  605. state = 'relative path start';
  606. if (stateOverride) {
  607. break loop;
  608. }
  609. continue;
  610. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  611. if (c === '[') {
  612. seenBracket = true;
  613. } else if (c === ']') {
  614. seenBracket = false;
  615. }
  616. buffer += c;
  617. } else {
  618. err('Invalid code point in host/hostname: ' + c);
  619. }
  620. break;
  621. case 'port':
  622. if (/[0-9]/.test(c)) {
  623. buffer += c;
  624. } else if (c === EOF || c === '/' || c === '\\' || c === '?' || c === '#' || stateOverride) {
  625. if (buffer !== '') {
  626. var temp = parseInt(buffer, 10);
  627. if (temp !== relative[this._scheme]) {
  628. this._port = temp + '';
  629. }
  630. buffer = '';
  631. }
  632. if (stateOverride) {
  633. break loop;
  634. }
  635. state = 'relative path start';
  636. continue;
  637. } else if (c === '\t' || c === '\n' || c === '\r') {
  638. err('Invalid code point in port: ' + c);
  639. } else {
  640. invalid.call(this);
  641. }
  642. break;
  643. case 'relative path start':
  644. if (c === '\\') {
  645. err('\'\\\' not allowed in path.');
  646. }
  647. state = 'relative path';
  648. if (c !== '/' && c !== '\\') {
  649. continue;
  650. }
  651. break;
  652. case 'relative path':
  653. if (c === EOF || c === '/' || c === '\\' || !stateOverride && (c === '?' || c === '#')) {
  654. if (c === '\\') {
  655. err('\\ not allowed in relative path.');
  656. }
  657. var tmp;
  658. if (tmp = relativePathDotMapping[buffer.toLowerCase()]) {
  659. buffer = tmp;
  660. }
  661. if (buffer === '..') {
  662. this._path.pop();
  663. if (c !== '/' && c !== '\\') {
  664. this._path.push('');
  665. }
  666. } else if (buffer === '.' && c !== '/' && c !== '\\') {
  667. this._path.push('');
  668. } else if (buffer !== '.') {
  669. if (this._scheme === 'file' && this._path.length === 0 && buffer.length === 2 && ALPHA.test(buffer[0]) && buffer[1] === '|') {
  670. buffer = buffer[0] + ':';
  671. }
  672. this._path.push(buffer);
  673. }
  674. buffer = '';
  675. if (c === '?') {
  676. this._query = '?';
  677. state = 'query';
  678. } else if (c === '#') {
  679. this._fragment = '#';
  680. state = 'fragment';
  681. }
  682. } else if (c !== '\t' && c !== '\n' && c !== '\r') {
  683. buffer += percentEscape(c);
  684. }
  685. break;
  686. case 'query':
  687. if (!stateOverride && c === '#') {
  688. this._fragment = '#';
  689. state = 'fragment';
  690. } else if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  691. this._query += percentEscapeQuery(c);
  692. }
  693. break;
  694. case 'fragment':
  695. if (c !== EOF && c !== '\t' && c !== '\n' && c !== '\r') {
  696. this._fragment += c;
  697. }
  698. break;
  699. }
  700. cursor++;
  701. }
  702. }
  703. function clear() {
  704. this._scheme = '';
  705. this._schemeData = '';
  706. this._username = '';
  707. this._password = null;
  708. this._host = '';
  709. this._port = '';
  710. this._path = [];
  711. this._query = '';
  712. this._fragment = '';
  713. this._isInvalid = false;
  714. this._isRelative = false;
  715. }
  716. function JURL(url, base) {
  717. if (base !== undefined && !(base instanceof JURL)) {
  718. base = new JURL(String(base));
  719. }
  720. this._url = url;
  721. clear.call(this);
  722. var input = url.replace(/^[ \t\r\n\f]+|[ \t\r\n\f]+$/g, '');
  723. parse.call(this, input, null, base);
  724. }
  725. JURL.prototype = {
  726. toString: function toString() {
  727. return this.href;
  728. },
  729. get href() {
  730. if (this._isInvalid) {
  731. return this._url;
  732. }
  733. var authority = '';
  734. if (this._username !== '' || this._password !== null) {
  735. authority = this._username + (this._password !== null ? ':' + this._password : '') + '@';
  736. }
  737. return this.protocol + (this._isRelative ? '//' + authority + this.host : '') + this.pathname + this._query + this._fragment;
  738. },
  739. set href(value) {
  740. clear.call(this);
  741. parse.call(this, value);
  742. },
  743. get protocol() {
  744. return this._scheme + ':';
  745. },
  746. set protocol(value) {
  747. if (this._isInvalid) {
  748. return;
  749. }
  750. parse.call(this, value + ':', 'scheme start');
  751. },
  752. get host() {
  753. return this._isInvalid ? '' : this._port ? this._host + ':' + this._port : this._host;
  754. },
  755. set host(value) {
  756. if (this._isInvalid || !this._isRelative) {
  757. return;
  758. }
  759. parse.call(this, value, 'host');
  760. },
  761. get hostname() {
  762. return this._host;
  763. },
  764. set hostname(value) {
  765. if (this._isInvalid || !this._isRelative) {
  766. return;
  767. }
  768. parse.call(this, value, 'hostname');
  769. },
  770. get port() {
  771. return this._port;
  772. },
  773. set port(value) {
  774. if (this._isInvalid || !this._isRelative) {
  775. return;
  776. }
  777. parse.call(this, value, 'port');
  778. },
  779. get pathname() {
  780. return this._isInvalid ? '' : this._isRelative ? '/' + this._path.join('/') : this._schemeData;
  781. },
  782. set pathname(value) {
  783. if (this._isInvalid || !this._isRelative) {
  784. return;
  785. }
  786. this._path = [];
  787. parse.call(this, value, 'relative path start');
  788. },
  789. get search() {
  790. return this._isInvalid || !this._query || this._query === '?' ? '' : this._query;
  791. },
  792. set search(value) {
  793. if (this._isInvalid || !this._isRelative) {
  794. return;
  795. }
  796. this._query = '?';
  797. if (value[0] === '?') {
  798. value = value.slice(1);
  799. }
  800. parse.call(this, value, 'query');
  801. },
  802. get hash() {
  803. return this._isInvalid || !this._fragment || this._fragment === '#' ? '' : this._fragment;
  804. },
  805. set hash(value) {
  806. if (this._isInvalid) {
  807. return;
  808. }
  809. this._fragment = '#';
  810. if (value[0] === '#') {
  811. value = value.slice(1);
  812. }
  813. parse.call(this, value, 'fragment');
  814. },
  815. get origin() {
  816. var host;
  817. if (this._isInvalid || !this._scheme) {
  818. return '';
  819. }
  820. switch (this._scheme) {
  821. case 'data':
  822. case 'file':
  823. case 'javascript':
  824. case 'mailto':
  825. return 'null';
  826. case 'blob':
  827. try {
  828. return new JURL(this._schemeData).origin || 'null';
  829. } catch (_) {}
  830. return 'null';
  831. }
  832. host = this.host;
  833. if (!host) {
  834. return '';
  835. }
  836. return this._scheme + '://' + host;
  837. }
  838. };
  839. var OriginalURL = globalScope.URL;
  840. if (OriginalURL) {
  841. JURL.createObjectURL = function (blob) {
  842. return OriginalURL.createObjectURL.apply(OriginalURL, arguments);
  843. };
  844. JURL.revokeObjectURL = function (url) {
  845. OriginalURL.revokeObjectURL(url);
  846. };
  847. }
  848. globalScope.URL = JURL;
  849. })();
  850. }