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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 new 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.size = 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. var state = getInternalParamsState(this);
  199. validateArgumentsLength(arguments.length, 2);
  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 /* , value */) {
  207. var state = getInternalParamsState(this);
  208. var length = validateArgumentsLength(arguments.length, 1);
  209. var entries = state.entries;
  210. var key = $toString(name);
  211. var $value = length < 2 ? undefined : arguments[1];
  212. var value = $value === undefined ? $value : $toString($value);
  213. var index = 0;
  214. while (index < entries.length) {
  215. var entry = entries[index];
  216. if (entry.key === key && (value === undefined || entry.value === value)) {
  217. splice(entries, index, 1);
  218. if (value !== undefined) break;
  219. } else index++;
  220. }
  221. if (!DESCRIPTORS) this.size = entries.length;
  222. state.updateURL();
  223. },
  224. // `URLSearchParams.prototype.get` method
  225. // https://url.spec.whatwg.org/#dom-urlsearchparams-get
  226. get: function get(name) {
  227. var entries = getInternalParamsState(this).entries;
  228. validateArgumentsLength(arguments.length, 1);
  229. var key = $toString(name);
  230. var index = 0;
  231. for (; index < entries.length; index++) {
  232. if (entries[index].key === key) return entries[index].value;
  233. }
  234. return null;
  235. },
  236. // `URLSearchParams.prototype.getAll` method
  237. // https://url.spec.whatwg.org/#dom-urlsearchparams-getall
  238. getAll: function getAll(name) {
  239. var entries = getInternalParamsState(this).entries;
  240. validateArgumentsLength(arguments.length, 1);
  241. var key = $toString(name);
  242. var result = [];
  243. var index = 0;
  244. for (; index < entries.length; index++) {
  245. if (entries[index].key === key) push(result, entries[index].value);
  246. }
  247. return result;
  248. },
  249. // `URLSearchParams.prototype.has` method
  250. // https://url.spec.whatwg.org/#dom-urlsearchparams-has
  251. has: function has(name /* , value */) {
  252. var entries = getInternalParamsState(this).entries;
  253. var length = validateArgumentsLength(arguments.length, 1);
  254. var key = $toString(name);
  255. var $value = length < 2 ? undefined : arguments[1];
  256. var value = $value === undefined ? $value : $toString($value);
  257. var index = 0;
  258. while (index < entries.length) {
  259. var entry = entries[index++];
  260. if (entry.key === key && (value === undefined || entry.value === value)) return true;
  261. }
  262. return false;
  263. },
  264. // `URLSearchParams.prototype.set` method
  265. // https://url.spec.whatwg.org/#dom-urlsearchparams-set
  266. set: function set(name, value) {
  267. var state = getInternalParamsState(this);
  268. validateArgumentsLength(arguments.length, 1);
  269. var entries = state.entries;
  270. var found = false;
  271. var key = $toString(name);
  272. var val = $toString(value);
  273. var index = 0;
  274. var entry;
  275. for (; index < entries.length; index++) {
  276. entry = entries[index];
  277. if (entry.key === key) {
  278. if (found) splice(entries, index--, 1);
  279. else {
  280. found = true;
  281. entry.value = val;
  282. }
  283. }
  284. }
  285. if (!found) push(entries, { key: key, value: val });
  286. if (!DESCRIPTORS) this.size = entries.length;
  287. state.updateURL();
  288. },
  289. // `URLSearchParams.prototype.sort` method
  290. // https://url.spec.whatwg.org/#dom-urlsearchparams-sort
  291. sort: function sort() {
  292. var state = getInternalParamsState(this);
  293. arraySort(state.entries, function (a, b) {
  294. return a.key > b.key ? 1 : -1;
  295. });
  296. state.updateURL();
  297. },
  298. // `URLSearchParams.prototype.forEach` method
  299. forEach: function forEach(callback /* , thisArg */) {
  300. var entries = getInternalParamsState(this).entries;
  301. var boundFunction = bind(callback, arguments.length > 1 ? arguments[1] : undefined);
  302. var index = 0;
  303. var entry;
  304. while (index < entries.length) {
  305. entry = entries[index++];
  306. boundFunction(entry.value, entry.key, this);
  307. }
  308. },
  309. // `URLSearchParams.prototype.keys` method
  310. keys: function keys() {
  311. return new URLSearchParamsIterator(this, 'keys');
  312. },
  313. // `URLSearchParams.prototype.values` method
  314. values: function values() {
  315. return new URLSearchParamsIterator(this, 'values');
  316. },
  317. // `URLSearchParams.prototype.entries` method
  318. entries: function entries() {
  319. return new URLSearchParamsIterator(this, 'entries');
  320. }
  321. }, { enumerable: true });
  322. // `URLSearchParams.prototype[@@iterator]` method
  323. defineBuiltIn(URLSearchParamsPrototype, ITERATOR, URLSearchParamsPrototype.entries, { name: 'entries' });
  324. // `URLSearchParams.prototype.toString` method
  325. // https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
  326. defineBuiltIn(URLSearchParamsPrototype, 'toString', function toString() {
  327. return getInternalParamsState(this).serialize();
  328. }, { enumerable: true });
  329. // `URLSearchParams.prototype.size` getter
  330. // https://github.com/whatwg/url/pull/734
  331. if (DESCRIPTORS) defineBuiltInAccessor(URLSearchParamsPrototype, 'size', {
  332. get: function size() {
  333. return getInternalParamsState(this).entries.length;
  334. },
  335. configurable: true,
  336. enumerable: true
  337. });
  338. setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS);
  339. $({ global: true, constructor: true, forced: !USE_NATIVE_URL }, {
  340. URLSearchParams: URLSearchParamsConstructor
  341. });
  342. // Wrap `fetch` and `Request` for correct work with polyfilled `URLSearchParams`
  343. if (!USE_NATIVE_URL && isCallable(Headers)) {
  344. var headersHas = uncurryThis(HeadersPrototype.has);
  345. var headersSet = uncurryThis(HeadersPrototype.set);
  346. var wrapRequestOptions = function (init) {
  347. if (isObject(init)) {
  348. var body = init.body;
  349. var headers;
  350. if (classof(body) === URL_SEARCH_PARAMS) {
  351. headers = init.headers ? new Headers(init.headers) : new Headers();
  352. if (!headersHas(headers, 'content-type')) {
  353. headersSet(headers, 'content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
  354. }
  355. return create(init, {
  356. body: createPropertyDescriptor(0, $toString(body)),
  357. headers: createPropertyDescriptor(0, headers)
  358. });
  359. }
  360. } return init;
  361. };
  362. if (isCallable(nativeFetch)) {
  363. $({ global: true, enumerable: true, dontCallGetSet: true, forced: true }, {
  364. fetch: function fetch(input /* , init */) {
  365. return nativeFetch(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
  366. }
  367. });
  368. }
  369. if (isCallable(NativeRequest)) {
  370. var RequestConstructor = function Request(input /* , init */) {
  371. anInstance(this, RequestPrototype);
  372. return new NativeRequest(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
  373. };
  374. RequestPrototype.constructor = RequestConstructor;
  375. RequestConstructor.prototype = RequestPrototype;
  376. $({ global: true, constructor: true, dontCallGetSet: true, forced: true }, {
  377. Request: RequestConstructor
  378. });
  379. }
  380. }
  381. module.exports = {
  382. URLSearchParams: URLSearchParamsConstructor,
  383. getState: getInternalParamsState
  384. };