source: frontend/node_modules/core-js-pure/modules/web.url-search-params.constructor.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 17.5 KB
Line 
1'use strict';
2// TODO: in core-js@4, move /modules/ dependencies to public entries for better optimization by tools like `preset-env`
3require('../modules/es.array.iterator');
4require('../modules/es.string.from-code-point');
5var $ = require('../internals/export');
6var globalThis = require('../internals/global-this');
7var safeGetBuiltIn = require('../internals/safe-get-built-in');
8var getBuiltIn = require('../internals/get-built-in');
9var call = require('../internals/function-call');
10var uncurryThis = require('../internals/function-uncurry-this');
11var DESCRIPTORS = require('../internals/descriptors');
12var USE_NATIVE_URL = require('../internals/url-constructor-detection');
13var defineBuiltIn = require('../internals/define-built-in');
14var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
15var defineBuiltIns = require('../internals/define-built-ins');
16var setToStringTag = require('../internals/set-to-string-tag');
17var createIteratorConstructor = require('../internals/iterator-create-constructor');
18var InternalStateModule = require('../internals/internal-state');
19var anInstance = require('../internals/an-instance');
20var isCallable = require('../internals/is-callable');
21var hasOwn = require('../internals/has-own-property');
22var bind = require('../internals/function-bind-context');
23var classof = require('../internals/classof');
24var anObject = require('../internals/an-object');
25var isObject = require('../internals/is-object');
26var $toString = require('../internals/to-string');
27var create = require('../internals/object-create');
28var createPropertyDescriptor = require('../internals/create-property-descriptor');
29var getIterator = require('../internals/get-iterator');
30var getIteratorMethod = require('../internals/get-iterator-method');
31var createIterResultObject = require('../internals/create-iter-result-object');
32var validateArgumentsLength = require('../internals/validate-arguments-length');
33var wellKnownSymbol = require('../internals/well-known-symbol');
34var arraySort = require('../internals/array-sort');
35
36var ITERATOR = wellKnownSymbol('iterator');
37var URL_SEARCH_PARAMS = 'URLSearchParams';
38var URL_SEARCH_PARAMS_ITERATOR = URL_SEARCH_PARAMS + 'Iterator';
39var setInternalState = InternalStateModule.set;
40var getInternalParamsState = InternalStateModule.getterFor(URL_SEARCH_PARAMS);
41var getInternalIteratorState = InternalStateModule.getterFor(URL_SEARCH_PARAMS_ITERATOR);
42
43var nativeFetch = safeGetBuiltIn('fetch');
44var NativeRequest = safeGetBuiltIn('Request');
45var Headers = safeGetBuiltIn('Headers');
46var RequestPrototype = NativeRequest && NativeRequest.prototype;
47var HeadersPrototype = Headers && Headers.prototype;
48var TypeError = globalThis.TypeError;
49var encodeURIComponent = globalThis.encodeURIComponent;
50var fromCharCode = String.fromCharCode;
51var fromCodePoint = getBuiltIn('String', 'fromCodePoint');
52var $parseInt = parseInt;
53var charAt = uncurryThis(''.charAt);
54var join = uncurryThis([].join);
55var push = uncurryThis([].push);
56var replace = uncurryThis(''.replace);
57var shift = uncurryThis([].shift);
58var splice = uncurryThis([].splice);
59var split = uncurryThis(''.split);
60var stringSlice = uncurryThis(''.slice);
61var exec = uncurryThis(/./.exec);
62
63var plus = /\+/g;
64var FALLBACK_REPLACER = '\uFFFD';
65var VALID_HEX = /^[0-9a-f]+$/i;
66
67var parseHexOctet = function (string, start) {
68 var substr = stringSlice(string, start, start + 2);
69 if (!exec(VALID_HEX, substr)) return NaN;
70
71 return $parseInt(substr, 16);
72};
73
74var getLeadingOnes = function (octet) {
75 var count = 0;
76 for (var mask = 0x80; mask > 0 && (octet & mask) !== 0; mask >>= 1) {
77 count++;
78 }
79 return count;
80};
81
82var utf8Decode = function (octets) {
83 var codePoint = null;
84 var length = octets.length;
85
86 switch (length) {
87 case 1:
88 codePoint = octets[0];
89 break;
90 case 2:
91 codePoint = (octets[0] & 0x1F) << 6 | (octets[1] & 0x3F);
92 break;
93 case 3:
94 codePoint = (octets[0] & 0x0F) << 12 | (octets[1] & 0x3F) << 6 | (octets[2] & 0x3F);
95 break;
96 case 4:
97 codePoint = (octets[0] & 0x07) << 18 | (octets[1] & 0x3F) << 12 | (octets[2] & 0x3F) << 6 | (octets[3] & 0x3F);
98 break;
99 }
100
101 // reject surrogates, overlong encodings, and out-of-range codepoints
102 if (codePoint === null
103 || codePoint > 0x10FFFF
104 || (codePoint >= 0xD800 && codePoint <= 0xDFFF)
105 || codePoint < (length > 3 ? 0x10000 : length > 2 ? 0x800 : length > 1 ? 0x80 : 0)
106 ) return null;
107
108 return codePoint;
109};
110
111/* eslint-disable max-statements, max-depth -- ok */
112var decode = function (input) {
113 input = replace(input, plus, ' ');
114 var length = input.length;
115 var result = '';
116 var i = 0;
117
118 while (i < length) {
119 var decodedChar = charAt(input, i);
120
121 if (decodedChar === '%') {
122 if (charAt(input, i + 1) === '%' || i + 3 > length) {
123 result += '%';
124 i++;
125 continue;
126 }
127
128 var octet = parseHexOctet(input, i + 1);
129
130 // eslint-disable-next-line no-self-compare -- NaN check
131 if (octet !== octet) {
132 result += decodedChar;
133 i++;
134 continue;
135 }
136
137 i += 2;
138 var byteSequenceLength = getLeadingOnes(octet);
139
140 if (byteSequenceLength === 0) {
141 decodedChar = fromCharCode(octet);
142 } else {
143 if (byteSequenceLength === 1 || byteSequenceLength > 4) {
144 result += FALLBACK_REPLACER;
145 i++;
146 continue;
147 }
148
149 var octets = [octet];
150 var sequenceIndex = 1;
151
152 while (sequenceIndex < byteSequenceLength) {
153 i++;
154 if (i + 3 > length || charAt(input, i) !== '%') break;
155
156 var nextByte = parseHexOctet(input, i + 1);
157
158 // eslint-disable-next-line no-self-compare -- NaN check
159 if (nextByte !== nextByte || nextByte > 191 || nextByte < 128) break;
160
161 // https://encoding.spec.whatwg.org/#utf-8-decoder - position-specific byte ranges
162 if (sequenceIndex === 1) {
163 if (octet === 0xE0 && nextByte < 0xA0) break;
164 if (octet === 0xED && nextByte > 0x9F) break;
165 if (octet === 0xF0 && nextByte < 0x90) break;
166 if (octet === 0xF4 && nextByte > 0x8F) break;
167 }
168
169 push(octets, nextByte);
170 i += 2;
171 sequenceIndex++;
172 }
173
174 if (octets.length !== byteSequenceLength) {
175 result += FALLBACK_REPLACER;
176 continue;
177 }
178
179 var codePoint = utf8Decode(octets);
180 if (codePoint === null) {
181 for (var replacement = 0; replacement < byteSequenceLength; replacement++) result += FALLBACK_REPLACER;
182 i++;
183 continue;
184 } else {
185 decodedChar = fromCodePoint(codePoint);
186 }
187 }
188 }
189
190 result += decodedChar;
191 i++;
192 }
193
194 return result;
195};
196/* eslint-enable max-statements, max-depth -- ok */
197
198var find = /[!'()~]|%20/g;
199
200var replacements = {
201 '!': '%21',
202 "'": '%27',
203 '(': '%28',
204 ')': '%29',
205 '~': '%7E',
206 '%20': '+'
207};
208
209var replacer = function (match) {
210 return replacements[match];
211};
212
213var serialize = function (it) {
214 return replace(encodeURIComponent(it), find, replacer);
215};
216
217var URLSearchParamsIterator = createIteratorConstructor(function Iterator(params, kind) {
218 setInternalState(this, {
219 type: URL_SEARCH_PARAMS_ITERATOR,
220 target: getInternalParamsState(params).entries,
221 index: 0,
222 kind: kind
223 });
224}, URL_SEARCH_PARAMS, function next() {
225 var state = getInternalIteratorState(this);
226 var target = state.target;
227 var index = state.index++;
228 if (!target || index >= target.length) {
229 state.target = null;
230 return createIterResultObject(undefined, true);
231 }
232 var entry = target[index];
233 switch (state.kind) {
234 case 'keys': return createIterResultObject(entry.key, false);
235 case 'values': return createIterResultObject(entry.value, false);
236 } return createIterResultObject([entry.key, entry.value], false);
237}, true);
238
239var URLSearchParamsState = function (init) {
240 this.entries = [];
241 this.url = null;
242
243 if (init !== undefined) {
244 if (isObject(init)) this.parseObject(init);
245 else this.parseQuery(typeof init == 'string' ? charAt(init, 0) === '?' ? stringSlice(init, 1) : init : $toString(init));
246 }
247};
248
249URLSearchParamsState.prototype = {
250 type: URL_SEARCH_PARAMS,
251 bindURL: function (url) {
252 this.url = url;
253 this.update();
254 },
255 parseObject: function (object) {
256 var entries = this.entries;
257 var iteratorMethod = getIteratorMethod(object);
258 var iterator, next, step, entryIterator, entryNext, first, second;
259
260 if (iteratorMethod) {
261 iterator = getIterator(object, iteratorMethod);
262 next = iterator.next;
263 while (!(step = call(next, iterator)).done) {
264 entryIterator = getIterator(anObject(step.value));
265 entryNext = entryIterator.next;
266 if (
267 (first = call(entryNext, entryIterator)).done ||
268 (second = call(entryNext, entryIterator)).done ||
269 !call(entryNext, entryIterator).done
270 ) throw new TypeError('Expected sequence with length 2');
271 push(entries, { key: $toString(first.value), value: $toString(second.value) });
272 }
273 } else for (var key in object) if (hasOwn(object, key)) {
274 push(entries, { key: key, value: $toString(object[key]) });
275 }
276 },
277 parseQuery: function (query) {
278 if (query) {
279 var entries = this.entries;
280 var attributes = split(query, '&');
281 var index = 0;
282 var attribute, entry;
283 while (index < attributes.length) {
284 attribute = attributes[index++];
285 if (attribute.length) {
286 entry = split(attribute, '=');
287 push(entries, {
288 key: decode(shift(entry)),
289 value: decode(join(entry, '='))
290 });
291 }
292 }
293 }
294 },
295 serialize: function () {
296 var entries = this.entries;
297 var result = [];
298 var index = 0;
299 var entry;
300 while (index < entries.length) {
301 entry = entries[index++];
302 push(result, serialize(entry.key) + '=' + serialize(entry.value));
303 } return join(result, '&');
304 },
305 update: function () {
306 this.entries.length = 0;
307 this.parseQuery(this.url.query);
308 },
309 updateURL: function () {
310 if (this.url) this.url.update();
311 }
312};
313
314// `URLSearchParams` constructor
315// https://url.spec.whatwg.org/#interface-urlsearchparams
316var URLSearchParamsConstructor = function URLSearchParams(/* init */) {
317 anInstance(this, URLSearchParamsPrototype);
318 var init = arguments.length > 0 ? arguments[0] : undefined;
319 var state = setInternalState(this, new URLSearchParamsState(init));
320 if (!DESCRIPTORS) this.size = state.entries.length;
321};
322
323var URLSearchParamsPrototype = URLSearchParamsConstructor.prototype;
324
325defineBuiltIns(URLSearchParamsPrototype, {
326 // `URLSearchParams.prototype.append` method
327 // https://url.spec.whatwg.org/#dom-urlsearchparams-append
328 append: function append(name, value) {
329 var state = getInternalParamsState(this);
330 validateArgumentsLength(arguments.length, 2);
331 push(state.entries, { key: $toString(name), value: $toString(value) });
332 if (!DESCRIPTORS) this.size++;
333 state.updateURL();
334 },
335 // `URLSearchParams.prototype.delete` method
336 // https://url.spec.whatwg.org/#dom-urlsearchparams-delete
337 'delete': function (name /* , value */) {
338 var state = getInternalParamsState(this);
339 var length = validateArgumentsLength(arguments.length, 1);
340 var entries = state.entries;
341 var key = $toString(name);
342 var $value = length < 2 ? undefined : arguments[1];
343 var value = $value === undefined ? $value : $toString($value);
344 var index = 0;
345 while (index < entries.length) {
346 var entry = entries[index];
347 if (entry.key === key && (value === undefined || entry.value === value)) {
348 splice(entries, index, 1);
349 } else index++;
350 }
351 if (!DESCRIPTORS) this.size = entries.length;
352 state.updateURL();
353 },
354 // `URLSearchParams.prototype.get` method
355 // https://url.spec.whatwg.org/#dom-urlsearchparams-get
356 get: function get(name) {
357 var entries = getInternalParamsState(this).entries;
358 validateArgumentsLength(arguments.length, 1);
359 var key = $toString(name);
360 var index = 0;
361 for (; index < entries.length; index++) {
362 if (entries[index].key === key) return entries[index].value;
363 }
364 return null;
365 },
366 // `URLSearchParams.prototype.getAll` method
367 // https://url.spec.whatwg.org/#dom-urlsearchparams-getall
368 getAll: function getAll(name) {
369 var entries = getInternalParamsState(this).entries;
370 validateArgumentsLength(arguments.length, 1);
371 var key = $toString(name);
372 var result = [];
373 var index = 0;
374 for (; index < entries.length; index++) {
375 if (entries[index].key === key) push(result, entries[index].value);
376 }
377 return result;
378 },
379 // `URLSearchParams.prototype.has` method
380 // https://url.spec.whatwg.org/#dom-urlsearchparams-has
381 has: function has(name /* , value */) {
382 var entries = getInternalParamsState(this).entries;
383 var length = validateArgumentsLength(arguments.length, 1);
384 var key = $toString(name);
385 var $value = length < 2 ? undefined : arguments[1];
386 var value = $value === undefined ? $value : $toString($value);
387 var index = 0;
388 while (index < entries.length) {
389 var entry = entries[index++];
390 if (entry.key === key && (value === undefined || entry.value === value)) return true;
391 }
392 return false;
393 },
394 // `URLSearchParams.prototype.set` method
395 // https://url.spec.whatwg.org/#dom-urlsearchparams-set
396 set: function set(name, value) {
397 var state = getInternalParamsState(this);
398 validateArgumentsLength(arguments.length, 2);
399 var entries = state.entries;
400 var found = false;
401 var key = $toString(name);
402 var val = $toString(value);
403 var index = 0;
404 var entry;
405 for (; index < entries.length; index++) {
406 entry = entries[index];
407 if (entry.key === key) {
408 if (found) splice(entries, index--, 1);
409 else {
410 found = true;
411 entry.value = val;
412 }
413 }
414 }
415 if (!found) push(entries, { key: key, value: val });
416 if (!DESCRIPTORS) this.size = entries.length;
417 state.updateURL();
418 },
419 // `URLSearchParams.prototype.sort` method
420 // https://url.spec.whatwg.org/#dom-urlsearchparams-sort
421 sort: function sort() {
422 var state = getInternalParamsState(this);
423 arraySort(state.entries, function (a, b) {
424 return a.key > b.key ? 1 : -1;
425 });
426 state.updateURL();
427 },
428 // `URLSearchParams.prototype.forEach` method
429 forEach: function forEach(callback /* , thisArg */) {
430 var entries = getInternalParamsState(this).entries;
431 var boundFunction = bind(callback, arguments.length > 1 ? arguments[1] : undefined);
432 var index = 0;
433 var entry;
434 while (index < entries.length) {
435 entry = entries[index++];
436 boundFunction(entry.value, entry.key, this);
437 }
438 },
439 // `URLSearchParams.prototype.keys` method
440 keys: function keys() {
441 return new URLSearchParamsIterator(this, 'keys');
442 },
443 // `URLSearchParams.prototype.values` method
444 values: function values() {
445 return new URLSearchParamsIterator(this, 'values');
446 },
447 // `URLSearchParams.prototype.entries` method
448 entries: function entries() {
449 return new URLSearchParamsIterator(this, 'entries');
450 }
451}, { enumerable: true });
452
453// `URLSearchParams.prototype[@@iterator]` method
454defineBuiltIn(URLSearchParamsPrototype, ITERATOR, URLSearchParamsPrototype.entries, { name: 'entries' });
455
456// `URLSearchParams.prototype.toString` method
457// https://url.spec.whatwg.org/#urlsearchparams-stringification-behavior
458defineBuiltIn(URLSearchParamsPrototype, 'toString', function toString() {
459 return getInternalParamsState(this).serialize();
460}, { enumerable: true });
461
462// `URLSearchParams.prototype.size` getter
463// https://url.spec.whatwg.org/#dom-urlsearchparams-size
464if (DESCRIPTORS) defineBuiltInAccessor(URLSearchParamsPrototype, 'size', {
465 get: function size() {
466 return getInternalParamsState(this).entries.length;
467 },
468 configurable: true,
469 enumerable: true
470});
471
472setToStringTag(URLSearchParamsConstructor, URL_SEARCH_PARAMS);
473
474$({ global: true, constructor: true, forced: !USE_NATIVE_URL }, {
475 URLSearchParams: URLSearchParamsConstructor
476});
477
478// Wrap `fetch` and `Request` for correct work with polyfilled `URLSearchParams`
479if (!USE_NATIVE_URL && isCallable(Headers)) {
480 var headersHas = uncurryThis(HeadersPrototype.has);
481 var headersSet = uncurryThis(HeadersPrototype.set);
482
483 var wrapRequestOptions = function (init) {
484 if (isObject(init)) {
485 var body = init.body;
486 var headers;
487 if (classof(body) === URL_SEARCH_PARAMS) {
488 headers = init.headers ? new Headers(init.headers) : new Headers();
489 if (!headersHas(headers, 'content-type')) {
490 headersSet(headers, 'content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
491 }
492 return create(init, {
493 body: createPropertyDescriptor(0, $toString(body)),
494 headers: createPropertyDescriptor(0, headers)
495 });
496 }
497 } return init;
498 };
499
500 if (isCallable(nativeFetch)) {
501 $({ global: true, enumerable: true, dontCallGetSet: true, forced: true }, {
502 fetch: function fetch(input /* , init */) {
503 return nativeFetch(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
504 }
505 });
506 }
507
508 if (isCallable(NativeRequest)) {
509 var RequestConstructor = function Request(input /* , init */) {
510 anInstance(this, RequestPrototype);
511 return new NativeRequest(input, arguments.length > 1 ? wrapRequestOptions(arguments[1]) : {});
512 };
513
514 RequestPrototype.constructor = RequestConstructor;
515 RequestConstructor.prototype = RequestPrototype;
516
517 $({ global: true, constructor: true, dontCallGetSet: true, forced: true }, {
518 Request: RequestConstructor
519 });
520 }
521}
522
523module.exports = {
524 URLSearchParams: URLSearchParamsConstructor,
525 getState: getInternalParamsState
526};
Note: See TracBrowser for help on using the repository browser.