source: frontend/node_modules/whatwg-url/dist/URLSearchParams.js

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

Fix frontend appearance

  • Property mode set to 100644
File size: 14.8 KB
RevLine 
[9af201e]1"use strict";
2
3const conversions = require("webidl-conversions");
4const utils = require("./utils.js");
5
6const Function = require("./Function.js");
7const implSymbol = utils.implSymbol;
8const ctorRegistrySymbol = utils.ctorRegistrySymbol;
9
10const interfaceName = "URLSearchParams";
11
12const IteratorPrototype = Object.create(utils.IteratorPrototype, {
13 next: {
14 value: function next() {
15 const internal = this && this[utils.iterInternalSymbol];
16 if (!internal) {
17 throw new TypeError("next() called on a value that is not an iterator prototype object");
18 }
19
20 const { target, kind, index } = internal;
21 const values = Array.from(target[implSymbol]);
22 const len = values.length;
23 if (index >= len) {
24 return { value: undefined, done: true };
25 }
26
27 const pair = values[index];
28 internal.index = index + 1;
29 return utils.iteratorResult(pair.map(utils.tryWrapperForImpl), kind);
30 },
31 writable: true,
32 enumerable: true,
33 configurable: true
34 },
35 [Symbol.toStringTag]: {
36 value: "URLSearchParams Iterator",
37 configurable: true
38 }
39});
40
41exports.is = value => {
42 return utils.isObject(value) && utils.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
43};
44exports.isImpl = value => {
45 return utils.isObject(value) && value instanceof Impl.implementation;
46};
47exports.convert = (value, { context = "The provided value" } = {}) => {
48 if (exports.is(value)) {
49 return utils.implForWrapper(value);
50 }
51 throw new TypeError(`${context} is not of type 'URLSearchParams'.`);
52};
53
54exports.createDefaultIterator = (target, kind) => {
55 const iterator = Object.create(IteratorPrototype);
56 Object.defineProperty(iterator, utils.iterInternalSymbol, {
57 value: { target, kind, index: 0 },
58 configurable: true
59 });
60 return iterator;
61};
62
63function makeWrapper(globalObject) {
64 if (globalObject[ctorRegistrySymbol] === undefined) {
65 throw new Error("Internal error: invalid global object");
66 }
67
68 const ctor = globalObject[ctorRegistrySymbol]["URLSearchParams"];
69 if (ctor === undefined) {
70 throw new Error("Internal error: constructor URLSearchParams is not installed on the passed global object");
71 }
72
73 return Object.create(ctor.prototype);
74}
75
76exports.create = (globalObject, constructorArgs, privateData) => {
77 const wrapper = makeWrapper(globalObject);
78 return exports.setup(wrapper, globalObject, constructorArgs, privateData);
79};
80
81exports.createImpl = (globalObject, constructorArgs, privateData) => {
82 const wrapper = exports.create(globalObject, constructorArgs, privateData);
83 return utils.implForWrapper(wrapper);
84};
85
86exports._internalSetup = (wrapper, globalObject) => {};
87
88exports.setup = (wrapper, globalObject, constructorArgs = [], privateData = {}) => {
89 privateData.wrapper = wrapper;
90
91 exports._internalSetup(wrapper, globalObject);
92 Object.defineProperty(wrapper, implSymbol, {
93 value: new Impl.implementation(globalObject, constructorArgs, privateData),
94 configurable: true
95 });
96
97 wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
98 if (Impl.init) {
99 Impl.init(wrapper[implSymbol]);
100 }
101 return wrapper;
102};
103
104exports.new = globalObject => {
105 const wrapper = makeWrapper(globalObject);
106
107 exports._internalSetup(wrapper, globalObject);
108 Object.defineProperty(wrapper, implSymbol, {
109 value: Object.create(Impl.implementation.prototype),
110 configurable: true
111 });
112
113 wrapper[implSymbol][utils.wrapperSymbol] = wrapper;
114 if (Impl.init) {
115 Impl.init(wrapper[implSymbol]);
116 }
117 return wrapper[implSymbol];
118};
119
120const exposed = new Set(["Window", "Worker"]);
121
122exports.install = (globalObject, globalNames) => {
123 if (!globalNames.some(globalName => exposed.has(globalName))) {
124 return;
125 }
126 class URLSearchParams {
127 constructor() {
128 const args = [];
129 {
130 let curArg = arguments[0];
131 if (curArg !== undefined) {
132 if (utils.isObject(curArg)) {
133 if (curArg[Symbol.iterator] !== undefined) {
134 if (!utils.isObject(curArg)) {
135 throw new TypeError(
136 "Failed to construct 'URLSearchParams': parameter 1" + " sequence" + " is not an iterable object."
137 );
138 } else {
139 const V = [];
140 const tmp = curArg;
141 for (let nextItem of tmp) {
142 if (!utils.isObject(nextItem)) {
143 throw new TypeError(
144 "Failed to construct 'URLSearchParams': parameter 1" +
145 " sequence" +
146 "'s element" +
147 " is not an iterable object."
148 );
149 } else {
150 const V = [];
151 const tmp = nextItem;
152 for (let nextItem of tmp) {
153 nextItem = conversions["USVString"](nextItem, {
154 context:
155 "Failed to construct 'URLSearchParams': parameter 1" +
156 " sequence" +
157 "'s element" +
158 "'s element"
159 });
160
161 V.push(nextItem);
162 }
163 nextItem = V;
164 }
165
166 V.push(nextItem);
167 }
168 curArg = V;
169 }
170 } else {
171 if (!utils.isObject(curArg)) {
172 throw new TypeError(
173 "Failed to construct 'URLSearchParams': parameter 1" + " record" + " is not an object."
174 );
175 } else {
176 const result = Object.create(null);
177 for (const key of Reflect.ownKeys(curArg)) {
178 const desc = Object.getOwnPropertyDescriptor(curArg, key);
179 if (desc && desc.enumerable) {
180 let typedKey = key;
181
182 typedKey = conversions["USVString"](typedKey, {
183 context: "Failed to construct 'URLSearchParams': parameter 1" + " record" + "'s key"
184 });
185
186 let typedValue = curArg[key];
187
188 typedValue = conversions["USVString"](typedValue, {
189 context: "Failed to construct 'URLSearchParams': parameter 1" + " record" + "'s value"
190 });
191
192 result[typedKey] = typedValue;
193 }
194 }
195 curArg = result;
196 }
197 }
198 } else {
199 curArg = conversions["USVString"](curArg, {
200 context: "Failed to construct 'URLSearchParams': parameter 1"
201 });
202 }
203 } else {
204 curArg = "";
205 }
206 args.push(curArg);
207 }
208 return exports.setup(Object.create(new.target.prototype), globalObject, args);
209 }
210
211 append(name, value) {
212 const esValue = this !== null && this !== undefined ? this : globalObject;
213 if (!exports.is(esValue)) {
214 throw new TypeError("'append' called on an object that is not a valid instance of URLSearchParams.");
215 }
216
217 if (arguments.length < 2) {
218 throw new TypeError(
219 "Failed to execute 'append' on 'URLSearchParams': 2 arguments required, but only " +
220 arguments.length +
221 " present."
222 );
223 }
224 const args = [];
225 {
226 let curArg = arguments[0];
227 curArg = conversions["USVString"](curArg, {
228 context: "Failed to execute 'append' on 'URLSearchParams': parameter 1"
229 });
230 args.push(curArg);
231 }
232 {
233 let curArg = arguments[1];
234 curArg = conversions["USVString"](curArg, {
235 context: "Failed to execute 'append' on 'URLSearchParams': parameter 2"
236 });
237 args.push(curArg);
238 }
239 return esValue[implSymbol].append(...args);
240 }
241
242 delete(name) {
243 const esValue = this !== null && this !== undefined ? this : globalObject;
244 if (!exports.is(esValue)) {
245 throw new TypeError("'delete' called on an object that is not a valid instance of URLSearchParams.");
246 }
247
248 if (arguments.length < 1) {
249 throw new TypeError(
250 "Failed to execute 'delete' on 'URLSearchParams': 1 argument required, but only " +
251 arguments.length +
252 " present."
253 );
254 }
255 const args = [];
256 {
257 let curArg = arguments[0];
258 curArg = conversions["USVString"](curArg, {
259 context: "Failed to execute 'delete' on 'URLSearchParams': parameter 1"
260 });
261 args.push(curArg);
262 }
263 return esValue[implSymbol].delete(...args);
264 }
265
266 get(name) {
267 const esValue = this !== null && this !== undefined ? this : globalObject;
268 if (!exports.is(esValue)) {
269 throw new TypeError("'get' called on an object that is not a valid instance of URLSearchParams.");
270 }
271
272 if (arguments.length < 1) {
273 throw new TypeError(
274 "Failed to execute 'get' on 'URLSearchParams': 1 argument required, but only " +
275 arguments.length +
276 " present."
277 );
278 }
279 const args = [];
280 {
281 let curArg = arguments[0];
282 curArg = conversions["USVString"](curArg, {
283 context: "Failed to execute 'get' on 'URLSearchParams': parameter 1"
284 });
285 args.push(curArg);
286 }
287 return esValue[implSymbol].get(...args);
288 }
289
290 getAll(name) {
291 const esValue = this !== null && this !== undefined ? this : globalObject;
292 if (!exports.is(esValue)) {
293 throw new TypeError("'getAll' called on an object that is not a valid instance of URLSearchParams.");
294 }
295
296 if (arguments.length < 1) {
297 throw new TypeError(
298 "Failed to execute 'getAll' on 'URLSearchParams': 1 argument required, but only " +
299 arguments.length +
300 " present."
301 );
302 }
303 const args = [];
304 {
305 let curArg = arguments[0];
306 curArg = conversions["USVString"](curArg, {
307 context: "Failed to execute 'getAll' on 'URLSearchParams': parameter 1"
308 });
309 args.push(curArg);
310 }
311 return utils.tryWrapperForImpl(esValue[implSymbol].getAll(...args));
312 }
313
314 has(name) {
315 const esValue = this !== null && this !== undefined ? this : globalObject;
316 if (!exports.is(esValue)) {
317 throw new TypeError("'has' called on an object that is not a valid instance of URLSearchParams.");
318 }
319
320 if (arguments.length < 1) {
321 throw new TypeError(
322 "Failed to execute 'has' on 'URLSearchParams': 1 argument required, but only " +
323 arguments.length +
324 " present."
325 );
326 }
327 const args = [];
328 {
329 let curArg = arguments[0];
330 curArg = conversions["USVString"](curArg, {
331 context: "Failed to execute 'has' on 'URLSearchParams': parameter 1"
332 });
333 args.push(curArg);
334 }
335 return esValue[implSymbol].has(...args);
336 }
337
338 set(name, value) {
339 const esValue = this !== null && this !== undefined ? this : globalObject;
340 if (!exports.is(esValue)) {
341 throw new TypeError("'set' called on an object that is not a valid instance of URLSearchParams.");
342 }
343
344 if (arguments.length < 2) {
345 throw new TypeError(
346 "Failed to execute 'set' on 'URLSearchParams': 2 arguments required, but only " +
347 arguments.length +
348 " present."
349 );
350 }
351 const args = [];
352 {
353 let curArg = arguments[0];
354 curArg = conversions["USVString"](curArg, {
355 context: "Failed to execute 'set' on 'URLSearchParams': parameter 1"
356 });
357 args.push(curArg);
358 }
359 {
360 let curArg = arguments[1];
361 curArg = conversions["USVString"](curArg, {
362 context: "Failed to execute 'set' on 'URLSearchParams': parameter 2"
363 });
364 args.push(curArg);
365 }
366 return esValue[implSymbol].set(...args);
367 }
368
369 sort() {
370 const esValue = this !== null && this !== undefined ? this : globalObject;
371 if (!exports.is(esValue)) {
372 throw new TypeError("'sort' called on an object that is not a valid instance of URLSearchParams.");
373 }
374
375 return esValue[implSymbol].sort();
376 }
377
378 toString() {
379 const esValue = this !== null && this !== undefined ? this : globalObject;
380 if (!exports.is(esValue)) {
381 throw new TypeError("'toString' called on an object that is not a valid instance of URLSearchParams.");
382 }
383
384 return esValue[implSymbol].toString();
385 }
386
387 keys() {
388 if (!exports.is(this)) {
389 throw new TypeError("'keys' called on an object that is not a valid instance of URLSearchParams.");
390 }
391 return exports.createDefaultIterator(this, "key");
392 }
393
394 values() {
395 if (!exports.is(this)) {
396 throw new TypeError("'values' called on an object that is not a valid instance of URLSearchParams.");
397 }
398 return exports.createDefaultIterator(this, "value");
399 }
400
401 entries() {
402 if (!exports.is(this)) {
403 throw new TypeError("'entries' called on an object that is not a valid instance of URLSearchParams.");
404 }
405 return exports.createDefaultIterator(this, "key+value");
406 }
407
408 forEach(callback) {
409 if (!exports.is(this)) {
410 throw new TypeError("'forEach' called on an object that is not a valid instance of URLSearchParams.");
411 }
412 if (arguments.length < 1) {
413 throw new TypeError("Failed to execute 'forEach' on 'iterable': 1 argument required, " + "but only 0 present.");
414 }
415 callback = Function.convert(callback, {
416 context: "Failed to execute 'forEach' on 'iterable': The callback provided as parameter 1"
417 });
418 const thisArg = arguments[1];
419 let pairs = Array.from(this[implSymbol]);
420 let i = 0;
421 while (i < pairs.length) {
422 const [key, value] = pairs[i].map(utils.tryWrapperForImpl);
423 callback.call(thisArg, value, key, this);
424 pairs = Array.from(this[implSymbol]);
425 i++;
426 }
427 }
428 }
429 Object.defineProperties(URLSearchParams.prototype, {
430 append: { enumerable: true },
431 delete: { enumerable: true },
432 get: { enumerable: true },
433 getAll: { enumerable: true },
434 has: { enumerable: true },
435 set: { enumerable: true },
436 sort: { enumerable: true },
437 toString: { enumerable: true },
438 keys: { enumerable: true },
439 values: { enumerable: true },
440 entries: { enumerable: true },
441 forEach: { enumerable: true },
442 [Symbol.toStringTag]: { value: "URLSearchParams", configurable: true },
443 [Symbol.iterator]: { value: URLSearchParams.prototype.entries, configurable: true, writable: true }
444 });
445 if (globalObject[ctorRegistrySymbol] === undefined) {
446 globalObject[ctorRegistrySymbol] = Object.create(null);
447 }
448 globalObject[ctorRegistrySymbol][interfaceName] = URLSearchParams;
449
450 Object.defineProperty(globalObject, interfaceName, {
451 configurable: true,
452 writable: true,
453 value: URLSearchParams
454 });
455};
456
457const Impl = require("./URLSearchParams-impl.js");
Note: See TracBrowser for help on using the repository browser.