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

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