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 for (uint i = 0; i < len; i++) {
681 QV4::Value pair = Value::fromReturnedValue(argArray->get(i));
682 auto *pairArrayObject = pair.as<ArrayObject>();
684 if (pairArrayObject ==
nullptr) {
685 return v4->throwTypeError(
686 QLatin1String(
"element %1 is not a pair").arg(QString::number(i)));
690 ScopedArrayObject pairArray(scope, pairArrayObject);
693 uint pairLen = pairArray->getLength();
697 return v4->throwTypeError(QLatin1String(
"pair %1 has %2 elements instead of 2")
698 .arg(QString::number(i))
699 .arg(QString::number(pairLen)));
703 urlSearchParamsObject->initializeParams(argArray);
704 }
else if (argObject !=
nullptr) {
705 ScopedObject scopedObject(scope, argObject);
706 urlSearchParamsObject->initializeParams(scopedObject);
708 QString value = argc > 0 ? arg->toQString() : QLatin1String(
"");
709 urlSearchParamsObject->initializeParams(value);
712 return obj->asReturnedValue();
715void UrlSearchParamsObject::initializeParams()
717 auto *arrayObject = engine()->newArrayObject(0);
718 auto *keys = engine()->newArrayObject(0);
719 auto *values = engine()->newArrayObject(0);
721 d()->params.set(engine(), arrayObject);
722 d()->keys.set(engine(), keys);
723 d()->values.set(engine(), values);
726void UrlSearchParamsObject::initializeParams(QString value)
728 Q_ASSERT(d()->params ==
nullptr);
732 if (value.startsWith(QLatin1Char(
'?')))
733 value = value.mid(1);
735 const QStringList params = value.split(QLatin1Char(
'&'));
737 for (
const QString& param : params) {
743 int equalsIndex = param.indexOf(QLatin1Char(
'='));
744 if (equalsIndex != -1) {
745 key = param.left(equalsIndex);
746 value = param.mid(equalsIndex+1);
751 append(engine()->newString(key), engine()->newString(value));
755void UrlSearchParamsObject::initializeParams(ScopedArrayObject& params)
757 Q_ASSERT(d()->params ==
nullptr);
759 Scope scope(engine());
761 uint len = params->getLength();
762 auto *keys = engine()->newArrayObject(len);
763 auto *values = engine()->newArrayObject(len);
765 ScopedArrayObject scopedKeys(scope, keys);
766 ScopedArrayObject scopedValues(scope, values);
768 for (uint i = 0; i < len; i++)
773 QV4::Value pair = Value::fromReturnedValue(params->get(i));
774 auto *pairArrayObject = pair.as<ArrayObject>();
776 QV4::Value key = Value::fromReturnedValue(pairArrayObject->get(uint(0)));
777 QV4::Value value = Value::fromReturnedValue(pairArrayObject->get(uint(1)));
779 scopedKeys->put(i, key);
780 scopedValues->put(i, value);
784 d()->params.set(engine(), params->d());
785 d()->keys.set(engine(), keys);
786 d()->values.set(engine(), values);
789void UrlSearchParamsObject::initializeParams(ScopedObject& params)
791 Q_ASSERT(d()->params ==
nullptr);
795 Scope scope(engine());
796 ObjectIterator it(scope, params, ObjectIterator::EnumerableOnly);
798 ScopedValue name(scope);
799 ScopedValue val(scope);
802 name = it.nextPropertyNameAsString(val);
806 Heap::String *nameStr = name->as<String>()->d();
807 Heap::String *valStr = val->toString(engine());
809 append(nameStr, valStr);
813void UrlSearchParamsObject::setParams(QList<QStringList> params)
815 auto *arrayObject = engine()->newArrayObject(0);
816 auto *keys = engine()->newArrayObject(0);
817 auto *values = engine()->newArrayObject(0);
819 Scope scope(engine());
821 ScopedArrayObject scopedArray(scope, arrayObject);
823 ScopedArrayObject scopedKeys(scope, keys);
824 ScopedArrayObject scopedValues(scope, values);
828 for (
const QStringList& param : params) {
830 auto *valuePair = engine()->newArrayObject(2);
832 ScopedArrayObject valuePairObject(scope, valuePair);
834 ScopedValue key(scope, Value::fromHeapObject(engine()->newString(param[0])));
835 ScopedValue value(scope, Value::fromHeapObject(engine()->newString(param[1])));
836 valuePairObject->put(uint(0), key);
837 valuePairObject->put(uint(1), value);
839 scopedKeys->put(len, key);
840 scopedValues->put(len, value);
842 scopedArray->put(len, valuePairObject);
846 d()->params.set(engine(), arrayObject);
847 d()->keys.set(engine(), keys);
848 d()->values.set(engine(), values);
856void UrlSearchParamsObject::append(Heap::String *name, Heap::String *value)
858 Scope scope(engine());
860 ScopedArrayObject scopedArray(scope, d()->params);
861 ScopedArrayObject scopedKeys(scope, d()->keys);
862 ScopedArrayObject scopedValues(scope, d()->values);
864 auto *valuePair = engine()->newArrayObject(2);
866 ScopedArrayObject valuePairObject(scope, valuePair);
868 ScopedValue keyScoped(scope, Value::fromHeapObject(name));
869 ScopedValue valueScoped(scope, Value::fromHeapObject(value));
870 valuePairObject->put(uint(0), keyScoped);
871 valuePairObject->put(uint(1), valueScoped);
873 uint len = scopedArray->getLength();
875 scopedKeys->put(len, keyScoped);
876 scopedValues->put(len, valueScoped);
878 scopedArray->put(len, valuePairObject);
881QList<QStringList> UrlSearchParamsObject::params()
const
883 auto *arrayObject = d()->params.get();
884 Scope scope(engine());
885 ScopedArrayObject scopedArray(scope, arrayObject);
887 QList<QStringList> result;
889 uint len = scopedArray->getLength();
891 for (uint i = 0; i < len; i++) {
892 QV4::Value pair = Value::fromReturnedValue(scopedArray->get(i));
893 auto *pairArrayObject = pair.as<ArrayObject>();
895 QV4::Value key = Value::fromReturnedValue(pairArrayObject->get(uint(0)));
896 QV4::Value value = Value::fromReturnedValue(pairArrayObject->get(uint(1)));
898 result << QStringList { key.toQString(), value.toQString() };
932int UrlSearchParamsObject::indexOf(QString name,
int last)
const
934 auto *arrayObject = d()->params.get();
935 Scope scope(engine());
936 ScopedArrayObject scopedArray(scope, arrayObject);
938 int len = scopedArray->getLength();
940 for (
int i = last + 1; i < len; i++) {
942 QV4::Value pair = Value::fromReturnedValue(scopedArray->get(i));
943 auto *pairArrayObject = pair.as<ArrayObject>();
945 QV4::Value key = Value::fromReturnedValue(pairArrayObject->get(uint(0)));
947 if (key.toQString() == name)
1018PropertyKey UrlSearchParamsObjectOwnPropertyKeyIterator::next(
const QV4::Object *o, Property *pd,
1046PropertyAttributes UrlSearchParamsObject::virtualGetOwnProperty(
const Managed *m, PropertyKey id,
1049 PropertyAttributes attributes = Object::virtualGetOwnProperty(m, id, p);
1050 if (attributes != Attr_Invalid)
1053 if (id.isArrayIndex()) {
1054 const int index = id.asArrayIndex();
1055 const auto usp =
static_cast<
const UrlSearchParamsObject *>(m);
1056 if (index < usp->length()) {
1058 p->value = usp->engine()->newString(usp->nameAt(index));
1059 return Attr_NotConfigurable | Attr_NotWritable;
1063 return Object::virtualGetOwnProperty(m, id, p);
1075ReturnedValue UrlSearchParamsPrototype::method_toString(
1076 const FunctionObject *b,
const Value *thisObject,
const Value *,
int)
1078 ExecutionEngine *v4 = b->engine();
1081 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1082 if (!checkSearchParamsType(v4, o))
1083 return Encode::undefined();
1085 auto params = o->params();
1089 for (
const QStringList &pair : params)
1090 value += QLatin1String(
"%1=%2&").arg(QString::fromUtf8(QUrl::toPercentEncoding(pair[0])),
1091 QString::fromUtf8(QUrl::toPercentEncoding(pair[1])));
1095 return Encode(v4->newString(value));
1098ReturnedValue UrlSearchParamsPrototype::method_sort(
const FunctionObject *b,
const Value *thisObject,
1101 ExecutionEngine *v4 = b->engine();
1104 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1105 if (!checkSearchParamsType(v4, o))
1106 return Encode::undefined();
1108 QList<QStringList> params = o->params();
1109 std::stable_sort(params.begin(), params.end(), [](QStringList a, QStringList b) {
return a[0] < b[0]; });
1111 o->setParams(params);
1113 return Encode::undefined();
1116ReturnedValue UrlSearchParamsPrototype::method_append(
const FunctionObject *b,
const Value *thisObject,
1117 const Value *argv,
int argc)
1119 ExecutionEngine *v4 = b->engine();
1123 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1125 ScopedValue argName(scope, argv[0]);
1126 ScopedValue argValue(scope, argv[1]);
1128 String *argNameString = argName->stringValue();
1130 if (argNameString ==
nullptr)
1131 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1133 ScopedString name(scope, argName->as<String>());
1134 ScopedString value(scope, argValue->toString(v4));
1136 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1137 if (!checkSearchParamsType(v4, o))
1138 return Encode::undefined();
1140 o->append(name->d(), value->d());
1142 return Encode::undefined();
1145ReturnedValue UrlSearchParamsPrototype::method_delete(
const FunctionObject *b,
const Value *thisObject,
1146 const Value *argv,
int argc)
1148 ExecutionEngine *v4 = b->engine();
1152 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1154 ScopedValue argName(scope, argv[0]);
1156 String *argNameString = argName->stringValue();
1158 if (argNameString ==
nullptr)
1159 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1161 QString name = argNameString->toQString();
1163 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1164 if (!checkSearchParamsType(v4, o))
1165 return Encode::undefined();
1167 QList<QStringList> params = o->params();
1168 params.removeIf([&name](
const auto &pair) {
return pair.at(0) == name; });
1170 o->setParams(params);
1172 return Encode::undefined();
1175ReturnedValue UrlSearchParamsPrototype::method_has(
const FunctionObject *b,
const Value *thisObject,
1176 const Value *argv,
int argc)
1178 ExecutionEngine *v4 = b->engine();
1182 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1184 ScopedValue argName(scope, argv[0]);
1186 String *argNameString = argName->stringValue();
1188 if (argNameString ==
nullptr)
1189 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1191 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1192 if (!checkSearchParamsType(v4, o))
1193 return Encode::undefined();
1195 QString name = argNameString->toQString();
1197 return Encode(o->indexOf(name) != -1);
1200ReturnedValue UrlSearchParamsPrototype::method_set(
const FunctionObject *b,
const Value *thisObject,
1201 const Value *argv,
int argc)
1203 ExecutionEngine *v4 = b->engine();
1207 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1209 ScopedValue argName(scope, argv[0]);
1210 ScopedValue argValue(scope, argv[1]);
1212 String *argNameString = argName->stringValue();
1214 if (argNameString ==
nullptr)
1215 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1217 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1218 if (!checkSearchParamsType(v4, o))
1219 return Encode::undefined();
1221 QString name = argNameString->toQString();
1222 QString value = argValue->toQString();
1224 auto params = o->params();
1226 bool matched =
false;
1228 for (
auto it = params.begin(); it != params.end();) {
1229 QStringList ¶m = *it;
1230 if (param[0] == name) {
1235 it = params.erase(it);
1243 params << QStringList { name, value };
1245 o->setParams(params);
1247 Scoped<UrlObject> scopedUrlObject(scope, o->d()->url.get());
1248 if (scopedUrlObject)
1249 scopedUrlObject->setSearch(o->searchString());
1251 return Encode::undefined();
1254ReturnedValue UrlSearchParamsPrototype::method_get(
const FunctionObject *b,
const Value *thisObject,
1255 const Value *argv,
int argc)
1257 ExecutionEngine *v4 = b->engine();
1261 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1263 ScopedValue argName(scope, argv[0]);
1265 String *argNameString = argName->stringValue();
1267 if (argNameString ==
nullptr)
1268 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1270 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1271 if (!checkSearchParamsType(v4, o))
1272 return Encode::undefined();
1274 QString name = argNameString->toQString();
1276 int index = o->indexOf(name);
1279 return Encode::null();
1281 return Encode(o->valueAtRaw(index));
1284ReturnedValue UrlSearchParamsPrototype::method_getAll(
const FunctionObject *b,
1285 const Value *thisObject,
const Value *argv,
1288 ExecutionEngine *v4 = b->engine();
1292 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1294 ScopedValue argName(scope, argv[0]);
1296 String *argNameString = argName->stringValue();
1298 if (argNameString ==
nullptr)
1299 return v4->throwTypeError(QLatin1String(
"Invalid argument provided"));
1301 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1302 if (!checkSearchParamsType(v4, o))
1303 return Encode::undefined();
1305 QString name = argNameString->toQString();
1307 auto *arrayObject = v4->newArrayObject(0);
1308 ScopedArrayObject result(scope, arrayObject);
1311 for (
int index = o->indexOf(name); index != -1; index = o->indexOf(name, index)) {
1312 ScopedValue value(scope, Value::fromHeapObject(o->valueAtRaw(index)));
1313 result->put(i++, value);
1316 return Encode(arrayObject);
1319ReturnedValue UrlSearchParamsPrototype::method_forEach(
const FunctionObject *b,
1320 const Value *thisObject,
const Value *argv,
1323 ExecutionEngine *v4 = b->engine();
1327 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1329 ScopedValue argFunc(scope, argv[0]);
1331 FunctionObject *func = argFunc->as<FunctionObject>();
1333 if (func ==
nullptr)
1334 return v4->throwTypeError(QLatin1String(
"Invalid argument: must be a function"));
1336 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1337 if (!checkSearchParamsType(v4, o))
1338 return Encode::undefined();
1340 for (
int i = 0; i < o->length(); i++) {
1341 Scoped<String> name(scope, o->nameAtRaw(i));
1342 Scoped<String> value(scope, o->valueAtRaw(i));
1344 QV4::JSCallArguments calldata(scope, 2);
1346 calldata.args[0] = value;
1347 calldata.args[1] = name;
1349 func->call(calldata);
1352 return Encode::undefined();
1355ReturnedValue UrlSearchParamsPrototype::method_entries(
const FunctionObject *b,
1356 const Value *thisObject,
const Value *,
1359 ExecutionEngine *v4 = b->engine();
1363 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1365 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1366 if (!checkSearchParamsType(v4, o))
1367 return Encode::undefined();
1369 ScopedObject params(scope, o->d()->params.get());
1371 Scoped<ArrayIteratorObject> paramsIterator(scope, v4->newArrayIteratorObject(params));
1372 paramsIterator->d()->iterationKind = IteratorKind::KeyValueIteratorKind;
1373 return paramsIterator->asReturnedValue();
1376ReturnedValue UrlSearchParamsPrototype::method_keys(
const FunctionObject *b,
1377 const Value *thisObject,
const Value *,
1380 ExecutionEngine *v4 = b->engine();
1384 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1386 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1387 if (!checkSearchParamsType(v4, o))
1388 return Encode::undefined();
1390 ScopedObject keys(scope, o->d()->keys.get());
1392 Scoped<ArrayIteratorObject> keysIterator(scope, v4->newArrayIteratorObject(keys));
1393 keysIterator->d()->iterationKind = IteratorKind::KeyValueIteratorKind;
1394 return keysIterator->asReturnedValue();
1397ReturnedValue UrlSearchParamsPrototype::method_values(
const FunctionObject *b,
1398 const Value *thisObject,
const Value *,
1401 ExecutionEngine *v4 = b->engine();
1405 return v4->throwError(QLatin1String(
"Bad amount of arguments"));
1407 Scoped<UrlSearchParamsObject> o(scope, thisObject);
1408 if (!checkSearchParamsType(v4, o))
1409 return Encode::undefined();
1411 ScopedObject values(scope, o->d()->values.get());
1413 Scoped<ArrayIteratorObject> valuesIterator(scope, v4->newArrayIteratorObject(values));
1414 valuesIterator->d()->iterationKind = IteratorKind::KeyValueIteratorKind;
1415 return valuesIterator->asReturnedValue();