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