web.url-search-params.js 13 KB

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