web.url.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. 'use strict';
  2. // TODO: in core-js@4, move /modules/ dependencies to public entries for better optimization by tools like `preset-env`
  3. require('../modules/es.string.iterator');
  4. var $ = require('../internals/export');
  5. var DESCRIPTORS = require('../internals/descriptors');
  6. var USE_NATIVE_URL = require('../internals/native-url');
  7. var global = require('../internals/global');
  8. var defineProperties = require('../internals/object-define-properties');
  9. var redefine = require('../internals/redefine');
  10. var anInstance = require('../internals/an-instance');
  11. var has = require('../internals/has');
  12. var assign = require('../internals/object-assign');
  13. var arrayFrom = require('../internals/array-from');
  14. var codeAt = require('../internals/string-multibyte').codeAt;
  15. var toASCII = require('../internals/string-punycode-to-ascii');
  16. var setToStringTag = require('../internals/set-to-string-tag');
  17. var URLSearchParamsModule = require('../modules/web.url-search-params');
  18. var InternalStateModule = require('../internals/internal-state');
  19. var NativeURL = global.URL;
  20. var URLSearchParams = URLSearchParamsModule.URLSearchParams;
  21. var getInternalSearchParamsState = URLSearchParamsModule.getState;
  22. var setInternalState = InternalStateModule.set;
  23. var getInternalURLState = InternalStateModule.getterFor('URL');
  24. var floor = Math.floor;
  25. var pow = Math.pow;
  26. var INVALID_AUTHORITY = 'Invalid authority';
  27. var INVALID_SCHEME = 'Invalid scheme';
  28. var INVALID_HOST = 'Invalid host';
  29. var INVALID_PORT = 'Invalid port';
  30. var ALPHA = /[A-Za-z]/;
  31. var ALPHANUMERIC = /[\d+-.A-Za-z]/;
  32. var DIGIT = /\d/;
  33. var HEX_START = /^(0x|0X)/;
  34. var OCT = /^[0-7]+$/;
  35. var DEC = /^\d+$/;
  36. var HEX = /^[\dA-Fa-f]+$/;
  37. // eslint-disable-next-line no-control-regex
  38. var FORBIDDEN_HOST_CODE_POINT = /[\u0000\u0009\u000A\u000D #%/:?@[\\]]/;
  39. // eslint-disable-next-line no-control-regex
  40. var FORBIDDEN_HOST_CODE_POINT_EXCLUDING_PERCENT = /[\u0000\u0009\u000A\u000D #/:?@[\\]]/;
  41. // eslint-disable-next-line no-control-regex
  42. var LEADING_AND_TRAILING_C0_CONTROL_OR_SPACE = /^[\u0000-\u001F ]+|[\u0000-\u001F ]+$/g;
  43. // eslint-disable-next-line no-control-regex
  44. var TAB_AND_NEW_LINE = /[\u0009\u000A\u000D]/g;
  45. var EOF;
  46. var parseHost = function (url, input) {
  47. var result, codePoints, index;
  48. if (input.charAt(0) == '[') {
  49. if (input.charAt(input.length - 1) != ']') return INVALID_HOST;
  50. result = parseIPv6(input.slice(1, -1));
  51. if (!result) return INVALID_HOST;
  52. url.host = result;
  53. // opaque host
  54. } else if (!isSpecial(url)) {
  55. if (FORBIDDEN_HOST_CODE_POINT_EXCLUDING_PERCENT.test(input)) return INVALID_HOST;
  56. result = '';
  57. codePoints = arrayFrom(input);
  58. for (index = 0; index < codePoints.length; index++) {
  59. result += percentEncode(codePoints[index], C0ControlPercentEncodeSet);
  60. }
  61. url.host = result;
  62. } else {
  63. input = toASCII(input);
  64. if (FORBIDDEN_HOST_CODE_POINT.test(input)) return INVALID_HOST;
  65. result = parseIPv4(input);
  66. if (result === null) return INVALID_HOST;
  67. url.host = result;
  68. }
  69. };
  70. var parseIPv4 = function (input) {
  71. var parts = input.split('.');
  72. var partsLength, numbers, index, part, radix, number, ipv4;
  73. if (parts.length && parts[parts.length - 1] == '') {
  74. parts.pop();
  75. }
  76. partsLength = parts.length;
  77. if (partsLength > 4) return input;
  78. numbers = [];
  79. for (index = 0; index < partsLength; index++) {
  80. part = parts[index];
  81. if (part == '') return input;
  82. radix = 10;
  83. if (part.length > 1 && part.charAt(0) == '0') {
  84. radix = HEX_START.test(part) ? 16 : 8;
  85. part = part.slice(radix == 8 ? 1 : 2);
  86. }
  87. if (part === '') {
  88. number = 0;
  89. } else {
  90. if (!(radix == 10 ? DEC : radix == 8 ? OCT : HEX).test(part)) return input;
  91. number = parseInt(part, radix);
  92. }
  93. numbers.push(number);
  94. }
  95. for (index = 0; index < partsLength; index++) {
  96. number = numbers[index];
  97. if (index == partsLength - 1) {
  98. if (number >= pow(256, 5 - partsLength)) return null;
  99. } else if (number > 255) return null;
  100. }
  101. ipv4 = numbers.pop();
  102. for (index = 0; index < numbers.length; index++) {
  103. ipv4 += numbers[index] * pow(256, 3 - index);
  104. }
  105. return ipv4;
  106. };
  107. // eslint-disable-next-line max-statements
  108. var parseIPv6 = function (input) {
  109. var address = [0, 0, 0, 0, 0, 0, 0, 0];
  110. var pieceIndex = 0;
  111. var compress = null;
  112. var pointer = 0;
  113. var value, length, numbersSeen, ipv4Piece, number, swaps, swap;
  114. var char = function () {
  115. return input.charAt(pointer);
  116. };
  117. if (char() == ':') {
  118. if (input.charAt(1) != ':') return;
  119. pointer += 2;
  120. pieceIndex++;
  121. compress = pieceIndex;
  122. }
  123. while (char()) {
  124. if (pieceIndex == 8) return;
  125. if (char() == ':') {
  126. if (compress !== null) return;
  127. pointer++;
  128. pieceIndex++;
  129. compress = pieceIndex;
  130. continue;
  131. }
  132. value = length = 0;
  133. while (length < 4 && HEX.test(char())) {
  134. value = value * 16 + parseInt(char(), 16);
  135. pointer++;
  136. length++;
  137. }
  138. if (char() == '.') {
  139. if (length == 0) return;
  140. pointer -= length;
  141. if (pieceIndex > 6) return;
  142. numbersSeen = 0;
  143. while (char()) {
  144. ipv4Piece = null;
  145. if (numbersSeen > 0) {
  146. if (char() == '.' && numbersSeen < 4) pointer++;
  147. else return;
  148. }
  149. if (!DIGIT.test(char())) return;
  150. while (DIGIT.test(char())) {
  151. number = parseInt(char(), 10);
  152. if (ipv4Piece === null) ipv4Piece = number;
  153. else if (ipv4Piece == 0) return;
  154. else ipv4Piece = ipv4Piece * 10 + number;
  155. if (ipv4Piece > 255) return;
  156. pointer++;
  157. }
  158. address[pieceIndex] = address[pieceIndex] * 256 + ipv4Piece;
  159. numbersSeen++;
  160. if (numbersSeen == 2 || numbersSeen == 4) pieceIndex++;
  161. }
  162. if (numbersSeen != 4) return;
  163. break;
  164. } else if (char() == ':') {
  165. pointer++;
  166. if (!char()) return;
  167. } else if (char()) return;
  168. address[pieceIndex++] = value;
  169. }
  170. if (compress !== null) {
  171. swaps = pieceIndex - compress;
  172. pieceIndex = 7;
  173. while (pieceIndex != 0 && swaps > 0) {
  174. swap = address[pieceIndex];
  175. address[pieceIndex--] = address[compress + swaps - 1];
  176. address[compress + --swaps] = swap;
  177. }
  178. } else if (pieceIndex != 8) return;
  179. return address;
  180. };
  181. var findLongestZeroSequence = function (ipv6) {
  182. var maxIndex = null;
  183. var maxLength = 1;
  184. var currStart = null;
  185. var currLength = 0;
  186. var index = 0;
  187. for (; index < 8; index++) {
  188. if (ipv6[index] !== 0) {
  189. if (currLength > maxLength) {
  190. maxIndex = currStart;
  191. maxLength = currLength;
  192. }
  193. currStart = null;
  194. currLength = 0;
  195. } else {
  196. if (currStart === null) currStart = index;
  197. ++currLength;
  198. }
  199. }
  200. if (currLength > maxLength) {
  201. maxIndex = currStart;
  202. maxLength = currLength;
  203. }
  204. return maxIndex;
  205. };
  206. var serializeHost = function (host) {
  207. var result, index, compress, ignore0;
  208. // ipv4
  209. if (typeof host == 'number') {
  210. result = [];
  211. for (index = 0; index < 4; index++) {
  212. result.unshift(host % 256);
  213. host = floor(host / 256);
  214. } return result.join('.');
  215. // ipv6
  216. } else if (typeof host == 'object') {
  217. result = '';
  218. compress = findLongestZeroSequence(host);
  219. for (index = 0; index < 8; index++) {
  220. if (ignore0 && host[index] === 0) continue;
  221. if (ignore0) ignore0 = false;
  222. if (compress === index) {
  223. result += index ? ':' : '::';
  224. ignore0 = true;
  225. } else {
  226. result += host[index].toString(16);
  227. if (index < 7) result += ':';
  228. }
  229. }
  230. return '[' + result + ']';
  231. } return host;
  232. };
  233. var C0ControlPercentEncodeSet = {};
  234. var fragmentPercentEncodeSet = assign({}, C0ControlPercentEncodeSet, {
  235. ' ': 1, '"': 1, '<': 1, '>': 1, '`': 1
  236. });
  237. var pathPercentEncodeSet = assign({}, fragmentPercentEncodeSet, {
  238. '#': 1, '?': 1, '{': 1, '}': 1
  239. });
  240. var userinfoPercentEncodeSet = assign({}, pathPercentEncodeSet, {
  241. '/': 1, ':': 1, ';': 1, '=': 1, '@': 1, '[': 1, '\\': 1, ']': 1, '^': 1, '|': 1
  242. });
  243. var percentEncode = function (char, set) {
  244. var code = codeAt(char, 0);
  245. return code > 0x20 && code < 0x7F && !has(set, char) ? char : encodeURIComponent(char);
  246. };
  247. var specialSchemes = {
  248. ftp: 21,
  249. file: null,
  250. http: 80,
  251. https: 443,
  252. ws: 80,
  253. wss: 443
  254. };
  255. var isSpecial = function (url) {
  256. return has(specialSchemes, url.scheme);
  257. };
  258. var includesCredentials = function (url) {
  259. return url.username != '' || url.password != '';
  260. };
  261. var cannotHaveUsernamePasswordPort = function (url) {
  262. return !url.host || url.cannotBeABaseURL || url.scheme == 'file';
  263. };
  264. var isWindowsDriveLetter = function (string, normalized) {
  265. var second;
  266. return string.length == 2 && ALPHA.test(string.charAt(0))
  267. && ((second = string.charAt(1)) == ':' || (!normalized && second == '|'));
  268. };
  269. var startsWithWindowsDriveLetter = function (string) {
  270. var third;
  271. return string.length > 1 && isWindowsDriveLetter(string.slice(0, 2)) && (
  272. string.length == 2 ||
  273. ((third = string.charAt(2)) === '/' || third === '\\' || third === '?' || third === '#')
  274. );
  275. };
  276. var shortenURLsPath = function (url) {
  277. var path = url.path;
  278. var pathSize = path.length;
  279. if (pathSize && (url.scheme != 'file' || pathSize != 1 || !isWindowsDriveLetter(path[0], true))) {
  280. path.pop();
  281. }
  282. };
  283. var isSingleDot = function (segment) {
  284. return segment === '.' || segment.toLowerCase() === '%2e';
  285. };
  286. var isDoubleDot = function (segment) {
  287. segment = segment.toLowerCase();
  288. return segment === '..' || segment === '%2e.' || segment === '.%2e' || segment === '%2e%2e';
  289. };
  290. // States:
  291. var SCHEME_START = {};
  292. var SCHEME = {};
  293. var NO_SCHEME = {};
  294. var SPECIAL_RELATIVE_OR_AUTHORITY = {};
  295. var PATH_OR_AUTHORITY = {};
  296. var RELATIVE = {};
  297. var RELATIVE_SLASH = {};
  298. var SPECIAL_AUTHORITY_SLASHES = {};
  299. var SPECIAL_AUTHORITY_IGNORE_SLASHES = {};
  300. var AUTHORITY = {};
  301. var HOST = {};
  302. var HOSTNAME = {};
  303. var PORT = {};
  304. var FILE = {};
  305. var FILE_SLASH = {};
  306. var FILE_HOST = {};
  307. var PATH_START = {};
  308. var PATH = {};
  309. var CANNOT_BE_A_BASE_URL_PATH = {};
  310. var QUERY = {};
  311. var FRAGMENT = {};
  312. // eslint-disable-next-line max-statements
  313. var parseURL = function (url, input, stateOverride, base) {
  314. var state = stateOverride || SCHEME_START;
  315. var pointer = 0;
  316. var buffer = '';
  317. var seenAt = false;
  318. var seenBracket = false;
  319. var seenPasswordToken = false;
  320. var codePoints, char, bufferCodePoints, failure;
  321. if (!stateOverride) {
  322. url.scheme = '';
  323. url.username = '';
  324. url.password = '';
  325. url.host = null;
  326. url.port = null;
  327. url.path = [];
  328. url.query = null;
  329. url.fragment = null;
  330. url.cannotBeABaseURL = false;
  331. input = input.replace(LEADING_AND_TRAILING_C0_CONTROL_OR_SPACE, '');
  332. }
  333. input = input.replace(TAB_AND_NEW_LINE, '');
  334. codePoints = arrayFrom(input);
  335. while (pointer <= codePoints.length) {
  336. char = codePoints[pointer];
  337. switch (state) {
  338. case SCHEME_START:
  339. if (char && ALPHA.test(char)) {
  340. buffer += char.toLowerCase();
  341. state = SCHEME;
  342. } else if (!stateOverride) {
  343. state = NO_SCHEME;
  344. continue;
  345. } else return INVALID_SCHEME;
  346. break;
  347. case SCHEME:
  348. if (char && (ALPHANUMERIC.test(char) || char == '+' || char == '-' || char == '.')) {
  349. buffer += char.toLowerCase();
  350. } else if (char == ':') {
  351. if (stateOverride && (
  352. (isSpecial(url) != has(specialSchemes, buffer)) ||
  353. (buffer == 'file' && (includesCredentials(url) || url.port !== null)) ||
  354. (url.scheme == 'file' && !url.host)
  355. )) return;
  356. url.scheme = buffer;
  357. if (stateOverride) {
  358. if (isSpecial(url) && specialSchemes[url.scheme] == url.port) url.port = null;
  359. return;
  360. }
  361. buffer = '';
  362. if (url.scheme == 'file') {
  363. state = FILE;
  364. } else if (isSpecial(url) && base && base.scheme == url.scheme) {
  365. state = SPECIAL_RELATIVE_OR_AUTHORITY;
  366. } else if (isSpecial(url)) {
  367. state = SPECIAL_AUTHORITY_SLASHES;
  368. } else if (codePoints[pointer + 1] == '/') {
  369. state = PATH_OR_AUTHORITY;
  370. pointer++;
  371. } else {
  372. url.cannotBeABaseURL = true;
  373. url.path.push('');
  374. state = CANNOT_BE_A_BASE_URL_PATH;
  375. }
  376. } else if (!stateOverride) {
  377. buffer = '';
  378. state = NO_SCHEME;
  379. pointer = 0;
  380. continue;
  381. } else return INVALID_SCHEME;
  382. break;
  383. case NO_SCHEME:
  384. if (!base || (base.cannotBeABaseURL && char != '#')) return INVALID_SCHEME;
  385. if (base.cannotBeABaseURL && char == '#') {
  386. url.scheme = base.scheme;
  387. url.path = base.path.slice();
  388. url.query = base.query;
  389. url.fragment = '';
  390. url.cannotBeABaseURL = true;
  391. state = FRAGMENT;
  392. break;
  393. }
  394. state = base.scheme == 'file' ? FILE : RELATIVE;
  395. continue;
  396. case SPECIAL_RELATIVE_OR_AUTHORITY:
  397. if (char == '/' && codePoints[pointer + 1] == '/') {
  398. state = SPECIAL_AUTHORITY_IGNORE_SLASHES;
  399. pointer++;
  400. } else {
  401. state = RELATIVE;
  402. continue;
  403. } break;
  404. case PATH_OR_AUTHORITY:
  405. if (char == '/') {
  406. state = AUTHORITY;
  407. break;
  408. } else {
  409. state = PATH;
  410. continue;
  411. }
  412. case RELATIVE:
  413. url.scheme = base.scheme;
  414. if (char == EOF) {
  415. url.username = base.username;
  416. url.password = base.password;
  417. url.host = base.host;
  418. url.port = base.port;
  419. url.path = base.path.slice();
  420. url.query = base.query;
  421. } else if (char == '/' || (char == '\\' && isSpecial(url))) {
  422. state = RELATIVE_SLASH;
  423. } else if (char == '?') {
  424. url.username = base.username;
  425. url.password = base.password;
  426. url.host = base.host;
  427. url.port = base.port;
  428. url.path = base.path.slice();
  429. url.query = '';
  430. state = QUERY;
  431. } else if (char == '#') {
  432. url.username = base.username;
  433. url.password = base.password;
  434. url.host = base.host;
  435. url.port = base.port;
  436. url.path = base.path.slice();
  437. url.query = base.query;
  438. url.fragment = '';
  439. state = FRAGMENT;
  440. } else {
  441. url.username = base.username;
  442. url.password = base.password;
  443. url.host = base.host;
  444. url.port = base.port;
  445. url.path = base.path.slice();
  446. url.path.pop();
  447. state = PATH;
  448. continue;
  449. } break;
  450. case RELATIVE_SLASH:
  451. if (isSpecial(url) && (char == '/' || char == '\\')) {
  452. state = SPECIAL_AUTHORITY_IGNORE_SLASHES;
  453. } else if (char == '/') {
  454. state = AUTHORITY;
  455. } else {
  456. url.username = base.username;
  457. url.password = base.password;
  458. url.host = base.host;
  459. url.port = base.port;
  460. state = PATH;
  461. continue;
  462. } break;
  463. case SPECIAL_AUTHORITY_SLASHES:
  464. state = SPECIAL_AUTHORITY_IGNORE_SLASHES;
  465. if (char != '/' || buffer.charAt(pointer + 1) != '/') continue;
  466. pointer++;
  467. break;
  468. case SPECIAL_AUTHORITY_IGNORE_SLASHES:
  469. if (char != '/' && char != '\\') {
  470. state = AUTHORITY;
  471. continue;
  472. } break;
  473. case AUTHORITY:
  474. if (char == '@') {
  475. if (seenAt) buffer = '%40' + buffer;
  476. seenAt = true;
  477. bufferCodePoints = arrayFrom(buffer);
  478. for (var i = 0; i < bufferCodePoints.length; i++) {
  479. var codePoint = bufferCodePoints[i];
  480. if (codePoint == ':' && !seenPasswordToken) {
  481. seenPasswordToken = true;
  482. continue;
  483. }
  484. var encodedCodePoints = percentEncode(codePoint, userinfoPercentEncodeSet);
  485. if (seenPasswordToken) url.password += encodedCodePoints;
  486. else url.username += encodedCodePoints;
  487. }
  488. buffer = '';
  489. } else if (
  490. char == EOF || char == '/' || char == '?' || char == '#' ||
  491. (char == '\\' && isSpecial(url))
  492. ) {
  493. if (seenAt && buffer == '') return INVALID_AUTHORITY;
  494. pointer -= arrayFrom(buffer).length + 1;
  495. buffer = '';
  496. state = HOST;
  497. } else buffer += char;
  498. break;
  499. case HOST:
  500. case HOSTNAME:
  501. if (stateOverride && url.scheme == 'file') {
  502. state = FILE_HOST;
  503. continue;
  504. } else if (char == ':' && !seenBracket) {
  505. if (buffer == '') return INVALID_HOST;
  506. failure = parseHost(url, buffer);
  507. if (failure) return failure;
  508. buffer = '';
  509. state = PORT;
  510. if (stateOverride == HOSTNAME) return;
  511. } else if (
  512. char == EOF || char == '/' || char == '?' || char == '#' ||
  513. (char == '\\' && isSpecial(url))
  514. ) {
  515. if (isSpecial(url) && buffer == '') return INVALID_HOST;
  516. if (stateOverride && buffer == '' && (includesCredentials(url) || url.port !== null)) return;
  517. failure = parseHost(url, buffer);
  518. if (failure) return failure;
  519. buffer = '';
  520. state = PATH_START;
  521. if (stateOverride) return;
  522. continue;
  523. } else {
  524. if (char == '[') seenBracket = true;
  525. else if (char == ']') seenBracket = false;
  526. buffer += char;
  527. } break;
  528. case PORT:
  529. if (DIGIT.test(char)) {
  530. buffer += char;
  531. } else if (
  532. char == EOF || char == '/' || char == '?' || char == '#' ||
  533. (char == '\\' && isSpecial(url)) ||
  534. stateOverride
  535. ) {
  536. if (buffer != '') {
  537. var port = parseInt(buffer, 10);
  538. if (port > 0xFFFF) return INVALID_PORT;
  539. url.port = (isSpecial(url) && port === specialSchemes[url.scheme]) ? null : port;
  540. buffer = '';
  541. }
  542. if (stateOverride) return;
  543. state = PATH_START;
  544. continue;
  545. } else return INVALID_PORT;
  546. break;
  547. case FILE:
  548. url.scheme = 'file';
  549. if (char == '/' || char == '\\') state = FILE_SLASH;
  550. else if (base && base.scheme == 'file') {
  551. if (char == EOF) {
  552. url.host = base.host;
  553. url.path = base.path.slice();
  554. url.query = base.query;
  555. } else if (char == '?') {
  556. url.host = base.host;
  557. url.path = base.path.slice();
  558. url.query = '';
  559. state = QUERY;
  560. } else if (char == '#') {
  561. url.host = base.host;
  562. url.path = base.path.slice();
  563. url.query = base.query;
  564. url.fragment = '';
  565. state = FRAGMENT;
  566. } else {
  567. if (!startsWithWindowsDriveLetter(codePoints.slice(pointer).join(''))) {
  568. url.host = base.host;
  569. url.path = base.path.slice();
  570. shortenURLsPath(url);
  571. }
  572. state = PATH;
  573. continue;
  574. }
  575. } else {
  576. state = PATH;
  577. continue;
  578. } break;
  579. case FILE_SLASH:
  580. if (char == '/' || char == '\\') {
  581. state = FILE_HOST;
  582. break;
  583. }
  584. if (base && base.scheme == 'file' && !startsWithWindowsDriveLetter(codePoints.slice(pointer).join(''))) {
  585. if (isWindowsDriveLetter(base.path[0], true)) url.path.push(base.path[0]);
  586. else url.host = base.host;
  587. }
  588. state = PATH;
  589. continue;
  590. case FILE_HOST:
  591. if (char == EOF || char == '/' || char == '\\' || char == '?' || char == '#') {
  592. if (!stateOverride && isWindowsDriveLetter(buffer)) {
  593. state = PATH;
  594. } else if (buffer == '') {
  595. url.host = '';
  596. if (stateOverride) return;
  597. state = PATH_START;
  598. } else {
  599. failure = parseHost(url, buffer);
  600. if (failure) return failure;
  601. if (url.host == 'localhost') url.host = '';
  602. if (stateOverride) return;
  603. buffer = '';
  604. state = PATH_START;
  605. } continue;
  606. } else buffer += char;
  607. break;
  608. case PATH_START:
  609. if (isSpecial(url)) {
  610. state = PATH;
  611. if (char != '/' && char != '\\') continue;
  612. } else if (!stateOverride && char == '?') {
  613. url.query = '';
  614. state = QUERY;
  615. } else if (!stateOverride && char == '#') {
  616. url.fragment = '';
  617. state = FRAGMENT;
  618. } else if (char != EOF) {
  619. state = PATH;
  620. if (char != '/') continue;
  621. } break;
  622. case PATH:
  623. if (
  624. char == EOF || char == '/' ||
  625. (char == '\\' && isSpecial(url)) ||
  626. (!stateOverride && (char == '?' || char == '#'))
  627. ) {
  628. if (isDoubleDot(buffer)) {
  629. shortenURLsPath(url);
  630. if (char != '/' && !(char == '\\' && isSpecial(url))) {
  631. url.path.push('');
  632. }
  633. } else if (isSingleDot(buffer)) {
  634. if (char != '/' && !(char == '\\' && isSpecial(url))) {
  635. url.path.push('');
  636. }
  637. } else {
  638. if (url.scheme == 'file' && !url.path.length && isWindowsDriveLetter(buffer)) {
  639. if (url.host) url.host = '';
  640. buffer = buffer.charAt(0) + ':'; // normalize windows drive letter
  641. }
  642. url.path.push(buffer);
  643. }
  644. buffer = '';
  645. if (url.scheme == 'file' && (char == EOF || char == '?' || char == '#')) {
  646. while (url.path.length > 1 && url.path[0] === '') {
  647. url.path.shift();
  648. }
  649. }
  650. if (char == '?') {
  651. url.query = '';
  652. state = QUERY;
  653. } else if (char == '#') {
  654. url.fragment = '';
  655. state = FRAGMENT;
  656. }
  657. } else {
  658. buffer += percentEncode(char, pathPercentEncodeSet);
  659. } break;
  660. case CANNOT_BE_A_BASE_URL_PATH:
  661. if (char == '?') {
  662. url.query = '';
  663. state = QUERY;
  664. } else if (char == '#') {
  665. url.fragment = '';
  666. state = FRAGMENT;
  667. } else if (char != EOF) {
  668. url.path[0] += percentEncode(char, C0ControlPercentEncodeSet);
  669. } break;
  670. case QUERY:
  671. if (!stateOverride && char == '#') {
  672. url.fragment = '';
  673. state = FRAGMENT;
  674. } else if (char != EOF) {
  675. if (char == "'" && isSpecial(url)) url.query += '%27';
  676. else if (char == '#') url.query += '%23';
  677. else url.query += percentEncode(char, C0ControlPercentEncodeSet);
  678. } break;
  679. case FRAGMENT:
  680. if (char != EOF) url.fragment += percentEncode(char, fragmentPercentEncodeSet);
  681. break;
  682. }
  683. pointer++;
  684. }
  685. };
  686. // `URL` constructor
  687. // https://url.spec.whatwg.org/#url-class
  688. var URLConstructor = function URL(url /* , base */) {
  689. var that = anInstance(this, URLConstructor, 'URL');
  690. var base = arguments.length > 1 ? arguments[1] : undefined;
  691. var urlString = String(url);
  692. var state = setInternalState(that, { type: 'URL' });
  693. var baseState, failure;
  694. if (base !== undefined) {
  695. if (base instanceof URLConstructor) baseState = getInternalURLState(base);
  696. else {
  697. failure = parseURL(baseState = {}, String(base));
  698. if (failure) throw TypeError(failure);
  699. }
  700. }
  701. failure = parseURL(state, urlString, null, baseState);
  702. if (failure) throw TypeError(failure);
  703. var searchParams = state.searchParams = new URLSearchParams();
  704. var searchParamsState = getInternalSearchParamsState(searchParams);
  705. searchParamsState.updateSearchParams(state.query);
  706. searchParamsState.updateURL = function () {
  707. state.query = String(searchParams) || null;
  708. };
  709. if (!DESCRIPTORS) {
  710. that.href = serializeURL.call(that);
  711. that.origin = getOrigin.call(that);
  712. that.protocol = getProtocol.call(that);
  713. that.username = getUsername.call(that);
  714. that.password = getPassword.call(that);
  715. that.host = getHost.call(that);
  716. that.hostname = getHostname.call(that);
  717. that.port = getPort.call(that);
  718. that.pathname = getPathname.call(that);
  719. that.search = getSearch.call(that);
  720. that.searchParams = getSearchParams.call(that);
  721. that.hash = getHash.call(that);
  722. }
  723. };
  724. var URLPrototype = URLConstructor.prototype;
  725. var serializeURL = function () {
  726. var url = getInternalURLState(this);
  727. var scheme = url.scheme;
  728. var username = url.username;
  729. var password = url.password;
  730. var host = url.host;
  731. var port = url.port;
  732. var path = url.path;
  733. var query = url.query;
  734. var fragment = url.fragment;
  735. var output = scheme + ':';
  736. if (host !== null) {
  737. output += '//';
  738. if (includesCredentials(url)) {
  739. output += username + (password ? ':' + password : '') + '@';
  740. }
  741. output += serializeHost(host);
  742. if (port !== null) output += ':' + port;
  743. } else if (scheme == 'file') output += '//';
  744. output += url.cannotBeABaseURL ? path[0] : path.length ? '/' + path.join('/') : '';
  745. if (query !== null) output += '?' + query;
  746. if (fragment !== null) output += '#' + fragment;
  747. return output;
  748. };
  749. var getOrigin = function () {
  750. var url = getInternalURLState(this);
  751. var scheme = url.scheme;
  752. var port = url.port;
  753. if (scheme == 'blob') try {
  754. return new URL(scheme.path[0]).origin;
  755. } catch (error) {
  756. return 'null';
  757. }
  758. if (scheme == 'file' || !isSpecial(url)) return 'null';
  759. return scheme + '://' + serializeHost(url.host) + (port !== null ? ':' + port : '');
  760. };
  761. var getProtocol = function () {
  762. return getInternalURLState(this).scheme + ':';
  763. };
  764. var getUsername = function () {
  765. return getInternalURLState(this).username;
  766. };
  767. var getPassword = function () {
  768. return getInternalURLState(this).password;
  769. };
  770. var getHost = function () {
  771. var url = getInternalURLState(this);
  772. var host = url.host;
  773. var port = url.port;
  774. return host === null ? ''
  775. : port === null ? serializeHost(host)
  776. : serializeHost(host) + ':' + port;
  777. };
  778. var getHostname = function () {
  779. var host = getInternalURLState(this).host;
  780. return host === null ? '' : serializeHost(host);
  781. };
  782. var getPort = function () {
  783. var port = getInternalURLState(this).port;
  784. return port === null ? '' : String(port);
  785. };
  786. var getPathname = function () {
  787. var url = getInternalURLState(this);
  788. var path = url.path;
  789. return url.cannotBeABaseURL ? path[0] : path.length ? '/' + path.join('/') : '';
  790. };
  791. var getSearch = function () {
  792. var query = getInternalURLState(this).query;
  793. return query ? '?' + query : '';
  794. };
  795. var getSearchParams = function () {
  796. return getInternalURLState(this).searchParams;
  797. };
  798. var getHash = function () {
  799. var fragment = getInternalURLState(this).fragment;
  800. return fragment ? '#' + fragment : '';
  801. };
  802. var accessorDescriptor = function (getter, setter) {
  803. return { get: getter, set: setter, configurable: true, enumerable: true };
  804. };
  805. if (DESCRIPTORS) {
  806. defineProperties(URLPrototype, {
  807. // `URL.prototype.href` accessors pair
  808. // https://url.spec.whatwg.org/#dom-url-href
  809. href: accessorDescriptor(serializeURL, function (href) {
  810. var url = getInternalURLState(this);
  811. var urlString = String(href);
  812. var failure = parseURL(url, urlString);
  813. if (failure) throw TypeError(failure);
  814. getInternalSearchParamsState(url.searchParams).updateSearchParams(url.query);
  815. }),
  816. // `URL.prototype.origin` getter
  817. // https://url.spec.whatwg.org/#dom-url-origin
  818. origin: accessorDescriptor(getOrigin),
  819. // `URL.prototype.protocol` accessors pair
  820. // https://url.spec.whatwg.org/#dom-url-protocol
  821. protocol: accessorDescriptor(getProtocol, function (protocol) {
  822. var url = getInternalURLState(this);
  823. parseURL(url, String(protocol) + ':', SCHEME_START);
  824. }),
  825. // `URL.prototype.username` accessors pair
  826. // https://url.spec.whatwg.org/#dom-url-username
  827. username: accessorDescriptor(getUsername, function (username) {
  828. var url = getInternalURLState(this);
  829. var codePoints = arrayFrom(String(username));
  830. if (cannotHaveUsernamePasswordPort(url)) return;
  831. url.username = '';
  832. for (var i = 0; i < codePoints.length; i++) {
  833. url.username += percentEncode(codePoints[i], userinfoPercentEncodeSet);
  834. }
  835. }),
  836. // `URL.prototype.password` accessors pair
  837. // https://url.spec.whatwg.org/#dom-url-password
  838. password: accessorDescriptor(getPassword, function (password) {
  839. var url = getInternalURLState(this);
  840. var codePoints = arrayFrom(String(password));
  841. if (cannotHaveUsernamePasswordPort(url)) return;
  842. url.password = '';
  843. for (var i = 0; i < codePoints.length; i++) {
  844. url.password += percentEncode(codePoints[i], userinfoPercentEncodeSet);
  845. }
  846. }),
  847. // `URL.prototype.host` accessors pair
  848. // https://url.spec.whatwg.org/#dom-url-host
  849. host: accessorDescriptor(getHost, function (host) {
  850. var url = getInternalURLState(this);
  851. if (url.cannotBeABaseURL) return;
  852. parseURL(url, String(host), HOST);
  853. }),
  854. // `URL.prototype.hostname` accessors pair
  855. // https://url.spec.whatwg.org/#dom-url-hostname
  856. hostname: accessorDescriptor(getHostname, function (hostname) {
  857. var url = getInternalURLState(this);
  858. if (url.cannotBeABaseURL) return;
  859. parseURL(url, String(hostname), HOSTNAME);
  860. }),
  861. // `URL.prototype.port` accessors pair
  862. // https://url.spec.whatwg.org/#dom-url-port
  863. port: accessorDescriptor(getPort, function (port) {
  864. var url = getInternalURLState(this);
  865. if (cannotHaveUsernamePasswordPort(url)) return;
  866. port = String(port);
  867. if (port == '') url.port = null;
  868. else parseURL(url, port, PORT);
  869. }),
  870. // `URL.prototype.pathname` accessors pair
  871. // https://url.spec.whatwg.org/#dom-url-pathname
  872. pathname: accessorDescriptor(getPathname, function (pathname) {
  873. var url = getInternalURLState(this);
  874. if (url.cannotBeABaseURL) return;
  875. url.path = [];
  876. parseURL(url, pathname + '', PATH_START);
  877. }),
  878. // `URL.prototype.search` accessors pair
  879. // https://url.spec.whatwg.org/#dom-url-search
  880. search: accessorDescriptor(getSearch, function (search) {
  881. var url = getInternalURLState(this);
  882. search = String(search);
  883. if (search == '') {
  884. url.query = null;
  885. } else {
  886. if ('?' == search.charAt(0)) search = search.slice(1);
  887. url.query = '';
  888. parseURL(url, search, QUERY);
  889. }
  890. getInternalSearchParamsState(url.searchParams).updateSearchParams(url.query);
  891. }),
  892. // `URL.prototype.searchParams` getter
  893. // https://url.spec.whatwg.org/#dom-url-searchparams
  894. searchParams: accessorDescriptor(getSearchParams),
  895. // `URL.prototype.hash` accessors pair
  896. // https://url.spec.whatwg.org/#dom-url-hash
  897. hash: accessorDescriptor(getHash, function (hash) {
  898. var url = getInternalURLState(this);
  899. hash = String(hash);
  900. if (hash == '') {
  901. url.fragment = null;
  902. return;
  903. }
  904. if ('#' == hash.charAt(0)) hash = hash.slice(1);
  905. url.fragment = '';
  906. parseURL(url, hash, FRAGMENT);
  907. })
  908. });
  909. }
  910. // `URL.prototype.toJSON` method
  911. // https://url.spec.whatwg.org/#dom-url-tojson
  912. redefine(URLPrototype, 'toJSON', function toJSON() {
  913. return serializeURL.call(this);
  914. }, { enumerable: true });
  915. // `URL.prototype.toString` method
  916. // https://url.spec.whatwg.org/#URL-stringification-behavior
  917. redefine(URLPrototype, 'toString', function toString() {
  918. return serializeURL.call(this);
  919. }, { enumerable: true });
  920. if (NativeURL) {
  921. var nativeCreateObjectURL = NativeURL.createObjectURL;
  922. var nativeRevokeObjectURL = NativeURL.revokeObjectURL;
  923. // `URL.createObjectURL` method
  924. // https://developer.mozilla.org/en-US/docs/Web/API/URL/createObjectURL
  925. // eslint-disable-next-line no-unused-vars
  926. if (nativeCreateObjectURL) redefine(URLConstructor, 'createObjectURL', function createObjectURL(blob) {
  927. return nativeCreateObjectURL.apply(NativeURL, arguments);
  928. });
  929. // `URL.revokeObjectURL` method
  930. // https://developer.mozilla.org/en-US/docs/Web/API/URL/revokeObjectURL
  931. // eslint-disable-next-line no-unused-vars
  932. if (nativeRevokeObjectURL) redefine(URLConstructor, 'revokeObjectURL', function revokeObjectURL(url) {
  933. return nativeRevokeObjectURL.apply(NativeURL, arguments);
  934. });
  935. }
  936. setToStringTag(URLConstructor, 'URL');
  937. $({ global: true, forced: !USE_NATIVE_URL, sham: !DESCRIPTORS }, {
  938. URL: URLConstructor
  939. });