27void UrlPrototype::init(ExecutionEngine *engine, Object *ctor)
32 ScopedObject o(scope);
33 ctor->defineReadonlyProperty(engine->id_prototype(), (o =
this));
35 defineDefaultProperty(QStringLiteral(
"constructor"), (o = ctor));
36 defineDefaultProperty(engine->id_toString(), method_getHref);
37 defineDefaultProperty(QLatin1String(
"toJSON"), method_getHref);
39 defineAccessorProperty(QLatin1String(
"hash"), method_getHash, method_setHash);
40 defineAccessorProperty(QLatin1String(
"host"), method_getHost, method_setHost);
41 defineAccessorProperty(QLatin1String(
"hostname"), method_getHostname, method_setHostname);
42 defineAccessorProperty(QLatin1String(
"href"), method_getHref, method_setHref);
43 defineAccessorProperty(QLatin1String(
"origin"), method_getOrigin,
nullptr);
44 defineAccessorProperty(QLatin1String(
"password"), method_getPassword, method_setPassword);
45 defineAccessorProperty(QLatin1String(
"pathname"), method_getPathname, method_setPathname);
46 defineAccessorProperty(QLatin1String(
"port"), method_getPort, method_setPort);
47 defineAccessorProperty(QLatin1String(
"protocol"), method_getProtocol, method_setProtocol);
48 defineAccessorProperty(QLatin1String(
"search"), method_getSearch, method_setSearch);
49 defineAccessorProperty(QLatin1String(
"searchParams"), method_getSearchParams,
nullptr);
50 defineAccessorProperty(QLatin1String(
"username"), method_getUsername, method_setUsername);
155QString UrlObject::search()
const
157 const QUrl url = d()->url();
158 if (!url.hasQuery() || url.query().isEmpty())
161 constexpr auto options = QUrl::ComponentFormattingOption::EncodeSpaces
162 | QUrl::ComponentFormattingOption::EncodeUnicode
163 | QUrl::ComponentFormattingOption::EncodeReserved;
164 return QLatin1Char(
'?') + url.query(options);
176ReturnedValue UrlPrototype::method_getHash(
const FunctionObject *b,
const Value *thisObject,
179 ExecutionEngine *v4 = b->engine();
182 Scoped<UrlObject> r(scope, thisObject);
183 if (!checkUrlObjectType(v4, r))
184 return Encode::undefined();
186 return Encode(v4->newString(r->hash()));
189ReturnedValue UrlPrototype::method_setHash(
const FunctionObject *b,
const Value *thisObject,
190 const Value *argv,
int)
192 ExecutionEngine *v4 = b->engine();
195 ScopedValue arg(scope, argv[0]);
196 String *stringValue = arg->stringValue();
198 if (stringValue ==
nullptr)
199 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
201 Scoped<UrlObject> r(scope, thisObject);
202 if (!checkUrlObjectType(v4, r))
203 return Encode::undefined();
205 r->setHash(stringValue->toQString());
207 return Encode::undefined();
210ReturnedValue UrlPrototype::method_getHost(
const FunctionObject *b,
const Value *thisObject,
213 ExecutionEngine *v4 = b->engine();
216 Scoped<UrlObject> r(scope, thisObject);
217 if (!checkUrlObjectType(v4, r))
218 return Encode::undefined();
220 return Encode(v4->newString(r->host()));
223ReturnedValue UrlPrototype::method_setHost(
const FunctionObject *b,
const Value *thisObject,
224 const Value *argv,
int)
226 ExecutionEngine *v4 = b->engine();
229 ScopedValue arg(scope, argv[0]);
230 String *stringValue = arg->stringValue();
232 if (stringValue ==
nullptr)
233 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
235 Scoped<UrlObject> r(scope, thisObject);
236 if (!checkUrlObjectType(v4, r))
237 return Encode::undefined();
239 QString host = stringValue->toQString();
240 if (!r->setHost(host))
241 return v4->throwTypeError(QLatin1String(
"Invalid host: %1").arg(host));
243 return Encode::undefined();
246ReturnedValue UrlPrototype::method_getHostname(
const FunctionObject *b,
const Value *thisObject,
249 ExecutionEngine *v4 = b->engine();
252 Scoped<UrlObject> r(scope, thisObject);
253 if (!checkUrlObjectType(v4, r))
254 return Encode::undefined();
256 return Encode(v4->newString(r->hostname()));
259ReturnedValue UrlPrototype::method_setHostname(
const FunctionObject *b,
const Value *thisObject,
260 const Value *argv,
int)
262 ExecutionEngine *v4 = b->engine();
265 ScopedValue arg(scope, argv[0]);
266 String *stringValue = arg->stringValue();
268 if (stringValue ==
nullptr)
269 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
271 Scoped<UrlObject> r(scope, thisObject);
272 if (!checkUrlObjectType(v4, r))
273 return Encode::undefined();
275 QString hostname = stringValue->toQString();
276 if (!r->setHostname(hostname))
277 return v4->throwTypeError(QLatin1String(
"Invalid hostname: %1").arg(hostname));
279 return Encode::undefined();
282ReturnedValue UrlPrototype::method_getHref(
const FunctionObject *b,
const Value *thisObject,
285 ExecutionEngine *v4 = b->engine();
288 Scoped<UrlObject> r(scope, thisObject);
289 if (!checkUrlObjectType(v4, r))
290 return Encode::undefined();
292 return Encode(v4->newString(r->href()));
295ReturnedValue UrlPrototype::method_setHref(
const FunctionObject *b,
const Value *thisObject,
296 const Value *argv,
int)
298 ExecutionEngine *v4 = b->engine();
301 ScopedValue arg(scope, argv[0]);
302 String *stringValue = arg->stringValue();
304 if (stringValue ==
nullptr)
305 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
307 Scoped<UrlObject> r(scope, thisObject);
308 if (!checkUrlObjectType(v4, r))
309 return Encode::undefined();
311 QString href = stringValue->toQString();
312 if (!r->setHref(href))
313 return v4->throwTypeError(QLatin1String(
"Invalid URL: %1").arg(href));
315 return Encode::undefined();
318ReturnedValue UrlPrototype::method_getOrigin(
const FunctionObject *b,
const Value *thisObject,
321 ExecutionEngine *v4 = b->engine();
324 Scoped<UrlObject> r(scope, thisObject);
325 if (!checkUrlObjectType(v4, r))
326 return Encode::undefined();
328 return Encode(v4->newString(r->origin()));
331ReturnedValue UrlPrototype::method_getPassword(
const FunctionObject *b,
const Value *thisObject,
334 ExecutionEngine *v4 = b->engine();
337 Scoped<UrlObject> r(scope, thisObject);
338 if (!checkUrlObjectType(v4, r))
339 return Encode::undefined();
341 return Encode(v4->newString(r->password()));
344ReturnedValue UrlPrototype::method_setPassword(
const FunctionObject *b,
const Value *thisObject,
345 const Value *argv,
int)
347 ExecutionEngine *v4 = b->engine();
350 ScopedValue arg(scope, argv[0]);
351 String *stringValue = arg->stringValue();
353 if (stringValue ==
nullptr)
354 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
356 Scoped<UrlObject> r(scope, thisObject);
357 if (!checkUrlObjectType(v4, r))
358 return Encode::undefined();
360 r->setPassword(stringValue->toQString());
362 return Encode::undefined();
365ReturnedValue UrlPrototype::method_getPathname(
const FunctionObject *b,
const Value *thisObject,
368 ExecutionEngine *v4 = b->engine();
371 Scoped<UrlObject> r(scope, thisObject);
372 if (!checkUrlObjectType(v4, r))
373 return Encode::undefined();
375 return Encode(v4->newString(r->pathname()));
378ReturnedValue UrlPrototype::method_setPathname(
const FunctionObject *b,
const Value *thisObject,
379 const Value *argv,
int)
381 ExecutionEngine *v4 = b->engine();
384 ScopedValue arg(scope, argv[0]);
385 String *stringValue = arg->stringValue();
387 if (stringValue ==
nullptr)
388 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
390 Scoped<UrlObject> r(scope, thisObject);
391 if (!checkUrlObjectType(v4, r))
392 return Encode::undefined();
394 r->setPathname(stringValue->toQString());
396 return Encode::undefined();
399ReturnedValue UrlPrototype::method_getPort(
const FunctionObject *b,
const Value *thisObject,
402 ExecutionEngine *v4 = b->engine();
405 Scoped<UrlObject> r(scope, thisObject);
406 if (!checkUrlObjectType(v4, r))
407 return Encode::undefined();
409 return Encode(v4->newString(r->port()));
412ReturnedValue UrlPrototype::method_setPort(
const FunctionObject *b,
const Value *thisObject,
413 const Value *argv,
int)
415 ExecutionEngine *v4 = b->engine();
418 ScopedValue arg(scope, argv[0]);
419 String *stringValue = arg->stringValue();
423 if (stringValue !=
nullptr)
424 port = stringValue->toQString();
425 else if (arg->isInt32())
426 port = QString::number(arg->toInt32());
428 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
430 Scoped<UrlObject> r(scope, thisObject);
431 if (!checkUrlObjectType(v4, r))
432 return Encode::undefined();
434 if (!r->setPort(port))
435 return v4->throwTypeError(QLatin1String(
"Invalid port: %1").arg(port));
437 return Encode::undefined();
440ReturnedValue UrlPrototype::method_getProtocol(
const FunctionObject *b,
const Value *thisObject,
443 ExecutionEngine *v4 = b->engine();
446 Scoped<UrlObject> r(scope, thisObject);
447 if (!checkUrlObjectType(v4, r))
448 return Encode::undefined();
450 return Encode(v4->newString(r->protocol()));
453ReturnedValue UrlPrototype::method_setProtocol(
const FunctionObject *b,
const Value *thisObject,
454 const Value *argv,
int)
456 ExecutionEngine *v4 = b->engine();
459 ScopedValue arg(scope, argv[0]);
460 String *stringValue = arg->stringValue();
462 if (stringValue ==
nullptr)
463 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
465 Scoped<UrlObject> r(scope, thisObject);
466 if (!checkUrlObjectType(v4, r))
467 return Encode::undefined();
469 r->setProtocol(stringValue->toQString());
471 return Encode::undefined();
474ReturnedValue UrlPrototype::method_getSearch(
const FunctionObject *b,
const Value *thisObject,
477 ExecutionEngine *v4 = b->engine();
480 Scoped<UrlObject> r(scope, thisObject);
481 if (!checkUrlObjectType(v4, r))
482 return Encode::undefined();
484 return Encode(v4->newString(r->search()));
487ReturnedValue UrlPrototype::method_setSearch(
const FunctionObject *b,
const Value *thisObject,
488 const Value *argv,
int)
490 ExecutionEngine *v4 = b->engine();
493 ScopedValue arg(scope, argv[0]);
494 String *stringValue = arg->stringValue();
496 if (stringValue ==
nullptr)
497 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
499 Scoped<UrlObject> r(scope, thisObject);
500 if (!checkUrlObjectType(v4, r))
501 return Encode::undefined();
503 r->setSearch(stringValue->toQString());
505 return Encode::undefined();
508ReturnedValue UrlPrototype::method_getUsername(
const FunctionObject *b,
const Value *thisObject,
511 ExecutionEngine *v4 = b->engine();
514 Scoped<UrlObject> r(scope, thisObject);
515 if (!checkUrlObjectType(v4, r))
516 return Encode::undefined();
518 return Encode(v4->newString(r->username()));
521ReturnedValue UrlPrototype::method_setUsername(
const FunctionObject *b,
const Value *thisObject,
522 const Value *argv,
int)
524 ExecutionEngine *v4 = b->engine();
527 ScopedValue arg(scope, argv[0]);
528 String *stringValue = arg->stringValue();
530 if (stringValue ==
nullptr)
531 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
533 Scoped<UrlObject> r(scope, thisObject);
534 if (!checkUrlObjectType(v4, r))
535 return Encode::undefined();
537 r->setUsername(stringValue->toQString());
539 return Encode::undefined();
542ReturnedValue UrlPrototype::method_getSearchParams(
const FunctionObject *b,
const Value *thisObject,
545 ExecutionEngine *v4 = b->engine();
548 Scoped<UrlObject> r(scope, thisObject);
549 if (!checkUrlObjectType(v4, r))
550 return Encode::undefined();
552 Scoped<UrlSearchParamsObject> usp(scope, v4->newUrlSearchParamsObject());
554 usp->setUrlObject(thisObject->as<UrlObject>());
555 usp->initializeParams(r->search());
557 return usp->asReturnedValue();
560ReturnedValue UrlCtor::virtualCallAsConstructor(
const FunctionObject *that,
const Value *argv,
561 int argc,
const Value *newTarget)
563 ExecutionEngine *v4 = that->engine();
565 if (argc < 1 || argc > 2)
566 return v4->throwError(QLatin1String(
"Invalid amount of arguments"));
570 ScopedValue arg1(scope, argv[0]);
572 QString arg1String = arg1->toQString();
576 ScopedValue arg2(scope, argv[1]);
577 String *arg2StringValue = arg2->stringValue();
579 if (arg2StringValue ==
nullptr)
580 return v4->throwTypeError(QLatin1String(
"Invalid parameter provided"));
582 QUrl url = QUrl(arg2StringValue->toQString());
583 QUrl relativeUrl = QUrl(arg1String);
585 QString baseUrlPath = url.path();
586 QString relativePath = relativeUrl.path();
589 int lastSlash = baseUrlPath.lastIndexOf(QLatin1Char(
'/'));
591 baseUrlPath.truncate(lastSlash);
593 if (!relativePath.startsWith(QLatin1Char(
'/')))
594 relativePath = relativePath.prepend(QLatin1Char(
'/'));
596 url.setPath(baseUrlPath + relativePath);
597 url.setFragment(relativeUrl.fragment());
598 url.setQuery(relativeUrl.query());
600 urlString = url.toString();
602 urlString = arg1String;
605 ReturnedValue o = Encode(v4->newUrlObject());
610 ScopedObject obj(scope, o);
611 obj->setProtoFromNewTarget(newTarget);
613 UrlObject *urlObject = obj->as<UrlObject>();
615 if (!urlObject->setHref(urlString))
616 return v4->throwTypeError(QLatin1String(
"Invalid URL: %1").arg(urlString));
618 return obj->asReturnedValue();
627void UrlSearchParamsPrototype::init(ExecutionEngine *engine, Object *ctor)
632 ScopedObject o(scope);
633 ctor->defineReadonlyProperty(engine->id_prototype(), (o =
this));
635 defineDefaultProperty(QStringLiteral(
"constructor"), (o = ctor));
636 defineDefaultProperty(engine->id_toString(), method_toString);
637 defineDefaultProperty(QLatin1String(
"sort"), method_sort);
638 defineDefaultProperty(QLatin1String(
"append"), method_append);
639 defineDefaultProperty(QLatin1String(
"delete"), method_delete);
640 defineDefaultProperty(QLatin1String(
"has"), method_has);
641 defineDefaultProperty(QLatin1String(
"set"), method_set);
642 defineDefaultProperty(QLatin1String(
"get"), method_get);
643 defineDefaultProperty(QLatin1String(
"getAll"), method_getAll);
644 defineDefaultProperty(QLatin1String(
"forEach"), method_forEach);
645 defineDefaultProperty(QLatin1String(
"entries"), method_entries);
646 defineDefaultProperty(QLatin1String(
"keys"), method_keys);
647 defineDefaultProperty(QLatin1String(
"values"), method_values);
650ReturnedValue UrlSearchParamsCtor::virtualCallAsConstructor(
const FunctionObject *that,
const Value *argv,
651 int argc,
const Value *newTarget)
653 ExecutionEngine *v4 = that->engine();
656 return v4->throwError(QLatin1String(
"Invalid amount of arguments"));
660 ScopedValue arg(scope, argv[0]);
661 ArrayObject *argArrayObject = arg->as<ArrayObject>();
662 Object *argObject = arg->as<Object>();
664 ReturnedValue o = Encode(v4->newUrlSearchParamsObject());
669 ScopedObject obj(scope, o);
670 obj->setProtoFromNewTarget(newTarget);
672 UrlSearchParamsObject *urlSearchParamsObject = obj->as<UrlSearchParamsObject>();
674 if (argArrayObject !=
nullptr) {
675 ScopedArrayObject argArray(scope, argArrayObject);
677 uint len = argArray->getLength();
679 ScopedArrayObject pairArray(scope);
680 for (uint i = 0; i < len; i++) {
682 QV4::Value pair = Value::fromReturnedValue(argArray->get(i));
683 auto *pairArrayObject = pair.as<ArrayObject>();
685 if (pairArrayObject ==
nullptr) {
686 return v4->throwTypeError(
687 QLatin1String(
"element %1 is not a pair").arg(QString::number(i)));
691 pairArray = pairArrayObject;
694 uint pairLen = pairArray->getLength();
698 return v4->throwTypeError(QLatin1String(
"pair %1 has %2 elements instead of 2")
699 .arg(QString::number(i))
700 .arg(QString::number(pairLen)));
704 urlSearchParamsObject->initializeParams(argArray);
705 }
else if (argObject !=
nullptr) {
706 ScopedObject scopedObject(scope, argObject);
707 urlSearchParamsObject->initializeParams(scopedObject);
709 QString value = argc > 0 ? arg->toQString() : QLatin1String(
"");
710 urlSearchParamsObject->initializeParams(value);
713 return obj->asReturnedValue();
716void UrlSearchParamsObject::initializeParams()
718 auto *arrayObject = engine()->newArrayObject(0);
719 auto *keys = engine()->newArrayObject(0);
720 auto *values = engine()->newArrayObject(0);
722 d()->params.set(engine(), arrayObject);
723 d()->keys.set(engine(), keys);
724 d()->values.set(engine(), values);
727void UrlSearchParamsObject::initializeParams(QString value)
729 Q_ASSERT(d()->params ==
nullptr);
733 if (value.startsWith(QLatin1Char(
'?')))
734 value = value.mid(1);
736 const QStringList params = value.split(QLatin1Char(
'&'));
738 for (
const QString& param : params) {
744 int equalsIndex = param.indexOf(QLatin1Char(
'='));
745 if (equalsIndex != -1) {
746 key = param.left(equalsIndex);
747 value = param.mid(equalsIndex+1);
752 append(engine()->newString(key), engine()->newString(value));
756void UrlSearchParamsObject::initializeParams(ScopedArrayObject& params)
758 Q_ASSERT(d()->params ==
nullptr);
760 Scope scope(engine());
762 uint len = params->getLength();
763 auto *keys = engine()->newArrayObject(len);
764 auto *values = engine()->newArrayObject(len);
766 ScopedArrayObject scopedKeys(scope, keys);
767 ScopedArrayObject scopedValues(scope, values);
769 for (uint i = 0; i < len; i++)
774 QV4::Value pair = Value::fromReturnedValue(params->get(i));
775 auto *pairArrayObject = pair.as<ArrayObject>();
777 QV4::Value key = Value::fromReturnedValue(pairArrayObject->get(uint(0)));
778 QV4::Value value = Value::fromReturnedValue(pairArrayObject->get(uint(1)));
780 scopedKeys->put(i, key);
781 scopedValues->put(i, value);
785 d()->params.set(engine(), params->d());
786 d()->keys.set(engine(), keys);
787 d()->values.set(engine(), values);
790void UrlSearchParamsObject::initializeParams(ScopedObject& params)
792 Q_ASSERT(d()->params ==
nullptr);
796 Scope scope(engine());
797 ObjectIterator it(scope, params, ObjectIterator::EnumerableOnly);
799 ScopedValue name(scope);
800 ScopedValue val(scope);
803 name = it.nextPropertyNameAsString(val);
807 Heap::String *nameStr = name->as<String>()->d();
808 Heap::String *valStr = val->toString(engine());
810 append(nameStr, valStr);
814void UrlSearchParamsObject::setParams(
const QList<QStringList> ¶ms)
816 auto *arrayObject = engine()->newArrayObject(0);
817 auto *keys = engine()->newArrayObject(0);
818 auto *values = engine()->newArrayObject(0);
820 Scope scope(engine());
822 ScopedArrayObject scopedArray(scope, arrayObject);
824 ScopedArrayObject scopedKeys(scope, keys);
825 ScopedArrayObject scopedValues(scope, values);
826 ScopedArrayObject valuePairObject(scope);
827 ScopedValue key(scope);
828 ScopedValue value(scope);
832 for (
const QStringList& param : params) {
834 auto *valuePair = engine()->newArrayObject(2);
836 valuePairObject = valuePair;
838 key = Value::fromHeapObject(engine()->newString(param[0]));
839 value = Value::fromHeapObject(engine()->newString(param[1]));
840 valuePairObject->put(uint(0), key);
841 valuePairObject->put(uint(1), value);
843 scopedKeys->put(len, key);
844 scopedValues->put(len, value);
846 scopedArray->put(len, valuePairObject);
850 d()->params.set(engine(), arrayObject);
851 d()->keys.set(engine(), keys);
852 d()->values.set(engine(), values);
860void UrlSearchParamsObject::append(Heap::String *name, Heap::String *value)
862 Scope scope(engine());
864 ScopedArrayObject scopedArray(scope, d()->params);
865 ScopedArrayObject scopedKeys(scope, d()->keys);
866 ScopedArrayObject scopedValues(scope, d()->values);
868 auto *valuePair = engine()->newArrayObject(2);
870 ScopedArrayObject valuePairObject(scope, valuePair);
872 ScopedValue keyScoped(scope, Value::fromHeapObject(name));
873 ScopedValue valueScoped(scope, Value::fromHeapObject(value));
874 valuePairObject->put(uint(0), keyScoped);
875 valuePairObject->put(uint(1), valueScoped);
877 uint len = scopedArray->getLength();
879 scopedKeys->put(len, keyScoped);
880 scopedValues->put(len, valueScoped);
882 scopedArray->put(len, valuePairObject);
885QList<QStringList> UrlSearchParamsObject::params()
const
887 auto *arrayObject = d()->params.get();
888 Scope scope(engine());
889 ScopedArrayObject scopedArray(scope, arrayObject);
891 QList<QStringList> result;
893 uint len = scopedArray->getLength();
895 for (uint i = 0; i < len; i++) {
896 QV4::Value pair = Value::fromReturnedValue(scopedArray->get(i));
897 auto *pairArrayObject = pair.as<ArrayObject>();
899 QV4::Value key = Value::fromReturnedValue(pairArrayObject->get(uint(0)));
900 QV4::Value value = Value::fromReturnedValue(pairArrayObject->get(uint(1)));
902 result << QStringList { key.toQString(), value.toQString() };
936int UrlSearchParamsObject::indexOf(QString name,
int last)
const
938 auto *arrayObject = d()->params.get();
939 Scope scope(engine());
940 ScopedArrayObject scopedArray(scope, arrayObject);
942 int len = scopedArray->getLength();
944 for (
int i = last + 1; i < len; i++) {
946 QV4::Value pair = Value::fromReturnedValue(scopedArray->get(i));
947 auto *pairArrayObject = pair.as<ArrayObject>();
949 QV4::Value key = Value::fromReturnedValue(pairArrayObject->get(uint(0)));
951 if (key.toQString() == name)
1022PropertyKey UrlSearchParamsObjectOwnPropertyKeyIterator::next(
const QV4::Object *o, Property *pd,
1050PropertyAttributes UrlSearchParamsObject::virtualGetOwnProperty(
const Managed *m, PropertyKey id,
1053 PropertyAttributes attributes = Object::virtualGetOwnProperty(m, id, p);
1054 if (attributes != Attr_Invalid)
1057 if (id.isArrayIndex()) {
1058 const int index = id.asArrayIndex();
1059 const auto usp =
static_cast<
const UrlSearchParamsObject *>(m);
1060 if (index < usp->length()) {
1062 p->value = usp->engine()->newString(usp->nameAt(index));
1063 return Attr_NotConfigurable | Attr_NotWritable;
1067 return Object::virtualGetOwnProperty(m, id, p);
1079ReturnedValue UrlSearchParamsPrototype::method_toString(
1080 const FunctionObject *b,
const Value *thisObject,
const Value *,
int)
1082 ExecutionEngine *v4 = b->engine();
1085 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1086 if (!checkSearchParamsType(v4, o))
1087 return Encode::undefined();
1089 const auto params = o->params();
1093 for (
const QStringList &pair : params)
1094 value += QLatin1String(
"%1=%2&").arg(QString::fromUtf8(QUrl::toPercentEncoding(pair[0])),
1095 QString::fromUtf8(QUrl::toPercentEncoding(pair[1])));
1099 return Encode(v4->newString(value));
1102ReturnedValue UrlSearchParamsPrototype::method_sort(
const FunctionObject *b,
const Value *thisObject,
1105 ExecutionEngine *v4 = b->engine();
1108 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1109 if (!checkSearchParamsType(v4, o))
1110 return Encode::undefined();
1112 QList<QStringList> params = o->params();
1113 std::stable_sort(params.begin(), params.end(), [](QStringList a, QStringList b) {
return a[0] < b[0]; });
1115 o->setParams(params);
1117 return Encode::undefined();
1120ReturnedValue UrlSearchParamsPrototype::method_append(
const FunctionObject *b,
const Value *thisObject,
1121 const Value *argv,
int argc)
1123 ExecutionEngine *v4 = b->engine();
1127 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1129 ScopedValue argName(scope, argv[0]);
1130 ScopedValue argValue(scope, argv[1]);
1132 String *argNameString = argName->stringValue();
1134 if (argNameString ==
nullptr)
1135 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1137 ScopedString name(scope, argName->as<String>());
1138 ScopedString value(scope, argValue->toString(v4));
1140 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1141 if (!checkSearchParamsType(v4, o))
1142 return Encode::undefined();
1144 o->append(name->d(), value->d());
1146 return Encode::undefined();
1149ReturnedValue UrlSearchParamsPrototype::method_delete(
const FunctionObject *b,
const Value *thisObject,
1150 const Value *argv,
int argc)
1152 ExecutionEngine *v4 = b->engine();
1156 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1158 ScopedValue argName(scope, argv[0]);
1160 String *argNameString = argName->stringValue();
1162 if (argNameString ==
nullptr)
1163 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1165 QString name = argNameString->toQString();
1167 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1168 if (!checkSearchParamsType(v4, o))
1169 return Encode::undefined();
1171 QList<QStringList> params = o->params();
1172 params.removeIf([&name](
const auto &pair) {
return pair.at(0) == name; });
1174 o->setParams(params);
1176 return Encode::undefined();
1179ReturnedValue UrlSearchParamsPrototype::method_has(
const FunctionObject *b,
const Value *thisObject,
1180 const Value *argv,
int argc)
1182 ExecutionEngine *v4 = b->engine();
1186 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1188 ScopedValue argName(scope, argv[0]);
1190 String *argNameString = argName->stringValue();
1192 if (argNameString ==
nullptr)
1193 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1195 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1196 if (!checkSearchParamsType(v4, o))
1197 return Encode::undefined();
1199 QString name = argNameString->toQString();
1201 return Encode(o->indexOf(name) != -1);
1204ReturnedValue UrlSearchParamsPrototype::method_set(
const FunctionObject *b,
const Value *thisObject,
1205 const Value *argv,
int argc)
1207 ExecutionEngine *v4 = b->engine();
1211 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1213 ScopedValue argName(scope, argv[0]);
1214 ScopedValue argValue(scope, argv[1]);
1216 String *argNameString = argName->stringValue();
1218 if (argNameString ==
nullptr)
1219 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1221 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1222 if (!checkSearchParamsType(v4, o))
1223 return Encode::undefined();
1225 QString name = argNameString->toQString();
1226 QString value = argValue->toQString();
1228 auto params = o->params();
1230 bool matched =
false;
1232 for (
auto it = params.begin(); it != params.end();) {
1233 QStringList ¶m = *it;
1234 if (param[0] == name) {
1239 it = params.erase(it);
1247 params << QStringList { name, value };
1249 o->setParams(params);
1251 Scoped<UrlObject> scopedUrlObject(scope, o->d()->url.get());
1252 if (scopedUrlObject)
1253 scopedUrlObject->setSearch(o->searchString());
1255 return Encode::undefined();
1258ReturnedValue UrlSearchParamsPrototype::method_get(
const FunctionObject *b,
const Value *thisObject,
1259 const Value *argv,
int argc)
1261 ExecutionEngine *v4 = b->engine();
1265 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1267 ScopedValue argName(scope, argv[0]);
1269 String *argNameString = argName->stringValue();
1271 if (argNameString ==
nullptr)
1272 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1274 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1275 if (!checkSearchParamsType(v4, o))
1276 return Encode::undefined();
1278 QString name = argNameString->toQString();
1280 int index = o->indexOf(name);
1283 return Encode::null();
1285 return Encode(o->valueAtRaw(index));
1288ReturnedValue UrlSearchParamsPrototype::method_getAll(
const FunctionObject *b,
1289 const Value *thisObject,
const Value *argv,
1292 ExecutionEngine *v4 = b->engine();
1296 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1298 ScopedValue argName(scope, argv[0]);
1300 String *argNameString = argName->stringValue();
1302 if (argNameString ==
nullptr)
1303 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1305 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1306 if (!checkSearchParamsType(v4, o))
1307 return Encode::undefined();
1309 QString name = argNameString->toQString();
1311 auto *arrayObject = v4->newArrayObject(0);
1312 ScopedArrayObject result(scope, arrayObject);
1315 ScopedValue value(scope);
1316 for (
int index = o->indexOf(name); index != -1; index = o->indexOf(name, index)) {
1317 value = Value::fromHeapObject(o->valueAtRaw(index));
1318 result->put(i++, value);
1321 return Encode(arrayObject);
1324ReturnedValue UrlSearchParamsPrototype::method_forEach(
const FunctionObject *b,
1325 const Value *thisObject,
const Value *argv,
1328 ExecutionEngine *v4 = b->engine();
1332 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1334 ScopedValue argFunc(scope, argv[0]);
1336 FunctionObject *func = argFunc->as<FunctionObject>();
1338 if (func ==
nullptr)
1339 return v4->throwTypeError(QLatin1String(
"Invalid argument: must be a function"));
1341 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1342 if (!checkSearchParamsType(v4, o))
1343 return Encode::undefined();
1345 Scoped<String> name(scope);
1346 Scoped<String> value(scope);
1347 QV4::JSCallArguments calldata(scope, 2);
1348 for (
int i = 0; i < o->length(); i++) {
1349 name = o->nameAtRaw(i);
1350 value = o->valueAtRaw(i);
1352 calldata.args[0] = value;
1353 calldata.args[1] = name;
1355 func->call(calldata);
1358 return Encode::undefined();
1361ReturnedValue UrlSearchParamsPrototype::method_entries(
const FunctionObject *b,
1362 const Value *thisObject,
const Value *,
1365 ExecutionEngine *v4 = b->engine();
1369 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1371 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1372 if (!checkSearchParamsType(v4, o))
1373 return Encode::undefined();
1375 ScopedObject params(scope, o->d()->params.get());
1377 Scoped<ArrayIteratorObject> paramsIterator(scope, v4->newArrayIteratorObject(params));
1378 paramsIterator->d()->iterationKind = IteratorKind::KeyValueIteratorKind;
1379 return paramsIterator->asReturnedValue();
1382ReturnedValue UrlSearchParamsPrototype::method_keys(
const FunctionObject *b,
1383 const Value *thisObject,
const Value *,
1386 ExecutionEngine *v4 = b->engine();
1390 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1392 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1393 if (!checkSearchParamsType(v4, o))
1394 return Encode::undefined();
1396 ScopedObject keys(scope, o->d()->keys.get());
1398 Scoped<ArrayIteratorObject> keysIterator(scope, v4->newArrayIteratorObject(keys));
1399 keysIterator->d()->iterationKind = IteratorKind::KeyValueIteratorKind;
1400 return keysIterator->asReturnedValue();
1403ReturnedValue UrlSearchParamsPrototype::method_values(
const FunctionObject *b,
1404 const Value *thisObject,
const Value *,
1407 ExecutionEngine *v4 = b->engine();
1411 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1413 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1414 if (!checkSearchParamsType(v4, o))
1415 return Encode::undefined();
1417 ScopedObject values(scope, o->d()->values.get());
1419 Scoped<ArrayIteratorObject> valuesIterator(scope, v4->newArrayIteratorObject(values));
1420 valuesIterator->d()->iterationKind = IteratorKind::KeyValueIteratorKind;
1421 return valuesIterator->asReturnedValue();