web.url-search-params.constructor.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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.array.iterator');
  4. var $ = require('../internals/export');
  5. var global = require('../internals/global');
  6. var call = require('../internals/function-call');
  7. var uncurryThis = require('../internals/function-uncurry-this');
  8. var DESCRIPTORS = require('../internals/descriptors');
  9. var USE_NATIVE_URL = require('../internals/url-constructor-detection');
  10. var defineBuiltIn = require('../internals/define-built-in');
  11. var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
  12. var defineBuiltIns = require('../internals/define-built-ins');
  13. var setToStringTag = require('../internals/set-to-string-tag');
  14. var createIteratorConstructor = require('../internals/iterator-create-constructor');
  15. var InternalStateModule = require('../internals/internal-state');
  16. var anInstance = require('../internals/an-instance');
  17. var isCallable = require('../internals/is-callable');
  18. var hasOwn = require('../internals/has-own-property');
  19. var bind = require('../internals/function-bind-context');
  20. var classof = require('../internals/classof');
  21. var anObject = require('../internals/an-object');
  22. var isObject = require('../internals/is-object');
  23. var $toString = require('../internals/to-string');
  24. var create = require('../internals/object-create');
  25. var createPropertyDescriptor = require('../internals/create-property-descriptor');
  26. var getIterator = require('../internals/get-iterator');
  27. var getIteratorMethod = require('../internals/get-iterator-method');
  28. var validateArgumentsLength = require('../internals/validate-arguments-length');
  29. var wellKnownSymbol = require('../internals/well-known-symbol');
  30. var arraySort = require('../internals/array-sort');
  31. var ITERATOR = wellKnownSymbol('iterator');
  32. var URL_SEARCH_PARAMS = 'URLSearchParams';
  33. var URL_SEARCH_PARAMS_ITERATOR = URL_SEARCH_PARAMS + 'Iterator';
  34. var setInternalState = InternalStateModule.set;
  35. var getInternalParamsState = InternalStateModule.getterFor(URL_SEARCH_PARAMS);
  36. var getInternalIteratorState = InternalStateModule.getterFor(URL_SEARCH_PARAMS_ITERATOR);
  37. // eslint-disable-next-line es/no-object-getownpropertydescriptor -- safe
  38. var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
  39. // Avoid NodeJS experimental warning
  40. var safeGetBuiltIn = function (name) {
  41. if (!DESCRIPTORS) return global[name];
  42. var descriptor = getOwnPropertyDescriptor(global, name);
  43. return descriptor && descriptor.value;
  44. };
  45. var nativeFetch = safeGetBuiltIn('fetch');
  46. var NativeRequest = safeGetBuiltIn('Request');
  47. var Headers = safeGetBuiltIn('Headers');
  48. var RequestPrototype = NativeRequest && NativeRequest.prototype;
  49. var HeadersPrototype = Headers && Headers.prototype;
  50. var RegExp = global.RegExp;
  51. var TypeError = global.TypeError;
  52. var decodeURIComponent = global.decodeURIComponent;
  53. var encodeURIComponent = global.encodeURIComponent;
  54. var charAt = uncurryThis(''.charAt);
  55. var join = uncurryThis([].join);
  56. var push = uncurryThis([].push);
  57. var replace = uncurryThis(''.replace);
  58. var shift = uncurryThis([].shift);
  59. var splice = uncurryThis([].splice);
  60. var split = uncurryThis(''.split);
  61. var stringSlice = uncurryThis(''.slice);
  62. var plus = /\+/g;
  63. var sequences = Array(4);
  64. var percentSequence = function (bytes) {
  65. return sequences[bytes - 1] || (sequences[bytes - 1] = RegExp('((?:%[\\da-f]{2}){' + bytes + '})', 'gi'));
  66. };
  67. var percentDecode = function (sequence) {
  68. try {
  69. return decodeURIComponent(sequence);
  70. } catch (error) {
  71. return sequence;
  72. }
  73. };
  74. var deserialize = function (it) {
  75. var result = replace(it, plus, ' ');
  76. var bytes = 4;
  77. try {
  78. return decodeURIComponent(result);
  79. } catch (error) {
  80. while (bytes) {
  81. result = replace(result, percentSequence(bytes--), percentDecode);
  82. }
  83. return result;
  84. }
  85. };
  86. var find = /[!'()~]|%20/g;
  87. var replacements = {
  88. '!': '%21',
  89. "'": '%27',
  90. '(': '%28',
  91. ')': '%29',
  92. '~': '%7E',
  93. '%20': '+'
  94. };
  95. var replacer = function (match) {
  96. return replacements[match];
  97. };
  98. var serialize = function (it) {
  99. return replace(encodeURIComponent(it), find, replacer);
  100. };
  101. var URLSearchParamsIterator = createIteratorConstructor(function Iterator(params, kind) {
  102. setInternalState(this, {
  103. type: URL_SEARCH_PARAMS_ITERATOR,
  104. iterator: getIterator(getInternalParamsState(params).entries),
  105. kind: kind
  106. });
  107. }, 'Iterator', function next() {
  108. var state = getInternalIteratorState(this);
  109. var kind = state.kind;
  110. var step = state.iterator.next();
  111. var entry = step.value;
  112. if (!step.done) {
  113. step.value = kind === 'keys' ? entry.key : kind === 'values' ? entry.value : [entry.key, entry.value];
  114. } return step;
  115. }, true);
  116. var URLSearchParamsState = function (init) {
  117. this.entries = [];
  118. this.url = null;
  119. if (init !== undefined) {
  120. if (isObject(init)) this.parseObject(init);
  121. else this.parseQuery(typeof init == 'string' ? charAt(init, 0) === '?' ? stringSlice(init, 1) : init : $toString(init));
  122. }
  123. };
  124. URLSearchParamsState.prototype = {
  125. type: URL_SEARCH_PARAMS,
  126. bindURL: function (url) {
  127. this.url = url;
  128. this.update();
  129. },
  130. parseObject: function (object) {
  131. var iteratorMethod = getIteratorMethod(object);
  132. var iterator, next, step, entryIterator, entryNext, first, second;
  133. if (iteratorMethod) {
  134. iterator = getIterator(object, iteratorMethod);
  135. next = iterator.next;
  136. while (!(step = call(next, iterator)).done) {
  137. entryIterator = getIterator(anObject(step.value));
  138. entryNext = entryIterator.next;
  139. if (
  140. (first = call(entryNext, entryIterator)).done ||
  141. (second = call(entryNext, entryIterator)).done ||
  142. !call(entryNext, entryIterator).done
  143. ) throw TypeError('Expected sequence with length 2');
  144. push(this.entries, { key: $toString(first.value), value: $toString(second.value) });
  145. }
  146. } else for (var key in object) if (hasOwn(object, key)) {
  147. push(this.entries, { key: key, value: $toString(object[key]) });
  148. }
  149. },
  150. parseQuery: function (query) {
  151. if (query) {
  152. var attributes = split(query, '&');
  153. var index = 0;
  154. var attribute, entry;
  155. while (index < attributes.length) {
  156. attribute = attributes[index++];
  157. if (attribute.length) {
  158. entry = split(attribute, '=');
  159. push(this.entries, {
  160. key: deserialize(shift(entry)),
  161. value: deserialize(join(entry, '='))
  162. });
  163. }
  164. }
  165. }
  166. },
  167. serialize: function () {
  168. var entries = this.entries;
  169. var result = [];
  170. var index = 0;
  171. var entry;
  172. while (index < entries.length) {
  173. entry = entries[index++];
  174. push(result, serialize(entry.key) + '=' + serialize(entry.value));
  175. } return join(result, '&');
  176. },
  177. update: function () {
  178. this.entries.length = 0;
  179. this.parseQuery(this.url.query);
  180. },
  181. updateURL: function () {
  182. if (this.url) this.url.update();
  183. }
  184. };
  185. // `URLSearchParams` constructor
  186. // https://url.spec.whatwg.org/#interface-urlsearchparams
  187. var URLSearchParamsConstructor = function URLSearchParams(/* init */) {
  188. anInstance(this, URLSearchParamsPrototype);
  189. var init = arguments.length > 0 ? arguments[0] : undefined;
  190. var state = setInternalState(this, new URLSearchParamsState(init));
  191. if (!DESCRIPTORS) this.length = state.entries.length;
  192. };
  193. var URLSearchParamsPrototype = URLSearchParamsConstructor.prototype;
  194. defineBuiltIns(URLSearchParamsPrototype, {
  195. // `URLSearchParams.prototype.append` method
  196. // https://url.spec.whatwg.org/#dom-urlsearchparams-append
  197. append: function append(name, value) {
  198. validateArgumentsLength(arguments.length, 2);
  199. var state = getInternalParamsState(this);
  200. push(state.entries, { key: $toString(name), value: $toString(value) });
  201. if (!DESCRIPTORS) this.length++;
  202. state.updateURL();
  203. },
  204. // `URLSearchParams.prototype.delete` method
  205. // https://url.spec.whatwg.org/#dom-urlsearchparams-delete
  206. 'delete': function (name) {
  207. validateArgumentsLength(arguments.length, 1);
  208. var state = getInternalParamsState(this);
  209. var entries = state.entries;
  210. var key = $toString(name);
  211. var index = 0;
  212. while (index < entries.length) {
  213. if (entries[index].key === key) splice(entries, index, 1);
  214. else index++;
  215. }
  216. if (!DESCRIPTORS) this.length = entries.length;
  217. state.updateURL();
  218. },
  219. // `URLSearchParams.prototype.get` method
  220. // https://url.spec.whatwg.org/#dom-urlsearchparams-get
  221. get: function get(name) {
  222. validateArgumentsLength(arguments.length, 1);
  223. var entries = getInternalParamsState(this).entries;
  224. var key = $toString(name);
  225. var index = 0;
  226. for (; index < entries.length; index++) {
  227. if (entries[index].key === key) return entries[index].value;
  228. }
  229. return null;
  230. },
  231. // `URLSearchParams.prototype.getAll` method
  232. // https://url.spec.whatwg.org/#dom-urlsearchparams-getall
  233. getAll: function getAll(name) {
  234. validateArgumentsLength(arguments.length, 1);
  235. var entries = getInternalParamsState(this).entries;
  236. var key = $toString(name);
  237. var result = [];
  238. var index = 0;
  239. for (; index < entries.length; index++) {
  240. if (entries[index].key === key) push(result, entries[index].value);
  241. }
  242. return result;
  243. },
  244. // `URLSearchParams.prototype.has` method
  245. // https://url.spec.whatwg.org/#dom-urlsearchparams-has
  246. has: function has(name) {
  247. validateArgumentsLength(arguments.length, 1);
  248. var entries = getInternalParamsState(this).entries;
  249. var key = $toString(name);
  250. var index = 0;
  251. while (index < entries.length) {
  252. if (entries[index++].key === key) return true;
  253. }
  254. return false;
  255. },
  256. // `URLSearchParams.prototype.set` method
  257. // https://url.spec.whatwg.org/#dom-urlsearchparams-set
  258. set: function set(name, value) {
  259. validateArgumentsLength(arguments.length, 1);
  260. var state = getInternalParamsState(this);
  261. var entries = state.entries;
  262. var found = false;
  263. var key = $toString(name);
  264. var val = $toString(value);
  265. var index = 0;
  266. var entry;
  267. for (; index < entries.length; index++) {
  268. entry = entries[index];
  269. if (entry.key === key) {
  270. if (found) splice(entries, index--, 1);
  271. else {
  272. found = true;
  273. entry.value = val;
  274. }
  275. }
  276. }
  277. if (!found) push(entries, { key: key, value: val });
  278. if (!DESCRIPTORS) this.length = entries.length;
  279. state.updateURL();
  280. },
  281. // `URLSearchParams.prototype.sort` method
  282. // https://url.spec.whatwg.org/#dom-urlsearchparams-sort
  283. sort: function sort() {
  284. var state = getInternalParamsState(this);
  285. arraySort(state.entries, function (a, b) {
  286. return a.key > b.key ? 1 : -1;
  287. });
  288. state.updateURL();
  289. },
  290. // `URLSearchParams.prototype.forEach` method
  291. forEach: function forEach(callback /* , thisArg */) {
  292. var entries = getInternalParamsState(this).entries;
  293. var boundFunction = bind(callback, arguments.length > 1 ? arguments[1] : undefined);
  294. var index = 0;
  295. var entry;
  296. while (index < entries.length) {
  297. entry = entries[index++];
  298. boundFunction(entry.value, entry.key, this);
  299. }
  300. },
  301. // `URLSearchParams.prototype.keys` method
  302. keys: function keys() {
  303. return new URLSearchParamsIterator(this, 'keys');
  304. },
  305. // `URLSearchParams.prototype.values` method
  306. values: function values() {
  307. return new URLSearchParamsIterator(this, 'values');
  308. },
  309. // `URLSearchParams.prototype.entries` method
  310. entries: function entries() {
  311. return new URLSearchParamsIterator(this, 'entries');
  312. }
  313. }, { enumerable: true });
  314. // `URLSearchParams.prototype[@@iterator]` method
  315. defineBuiltIn(URLSearchParamsPrototype, ITERATOR, URLSearchParamsPrototype.entries, { name: 'entries' });
  316. // `URLSearchParams.prototype.toString` method
  317. // https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
  318. defineBuiltIn(URLSearchParamsPrototype, 'toString', function toString() {
  319. return getInternalParamsState(this).serialize();
  320. }, { enumerable: true });
  321. // `URLSearchParams.prototype.size` getter
  322. // https://github.com/whatwg/url/pull/734
  323. if (DESCRIPTORS) defineBuiltInAccessor(URLSearchParamsPrototype, 'size', {
  324. get: function size() {
  325. return getInternalParamsState(this).entries.length;
  326. },
  327. configurable: true,
  328. enumerable: true
  329. });
  330. setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS);
  331. $({ global: true, constructor: true, forced: !USE_NATIVE_URL }, {
  332. URLSearchParams: URLSearchParamsConstructor
  333. });
  334. // Wrap `fetch` and `Request` for correct work with polyfilled `URLSearchParams`
  335. if (!USE_NATIVE_URL && isCallable(Headers)) {
  336. var headersHas = uncurryThis(HeadersPrototype.has);
  337. var headersSet = uncurryThis(HeadersPrototype.set);
  338. var wrapRequestOptions = function (init) {
  339. if (isObject(init)) {
  340. var body = init.body;
  341. var headers;
  342. if (classof(body) === URL_SEARCH_PARAMS) {
  343. headers = init.headers ? new Headers(init.headers) : new Headers();
  344. if (!headersHas(headers, 'content-type')) {
  345. headersSet(headers, 'content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
  346. }
  347. return create(init, {
  348. body: createPropertyDescriptor(0, $toString(body)),
  349. headers: createPropertyDescriptor(0, headers)
  350. });
  351. }
  352. } return init;
  353. };
  354. if (isCallable(nativeFetch)) {
  355. $({ global: true, enumerable: true, dontCallGetSet: true, forced: true }, {
  356. fetch: function fetch(input /* , init */) {
  357. return nativeFetch(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
  358. }
  359. });
  360. }
  361. if (isCallable(NativeRequest)) {
  362. var RequestConstructor = function Request(input /* , init */) {
  363. anInstance(this, RequestPrototype);
  364. return new NativeRequest(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
  365. };
  366. RequestPrototype.constructor = RequestConstructor;
  367. RequestConstructor.prototype = RequestPrototype;
  368. $({ global: true, constructor: true, dontCallGetSet: true, forced: true }, {
  369. Request: RequestConstructor
  370. });
  371. }
  372. }
  373. module.exports = {
  374. URLSearchParams: URLSearchParamsConstructor,
  375. getState: getInternalParamsState
  376. };