diff --git a/src/ecmascript/spidermonkey/urlsearchparams.cpp b/src/ecmascript/spidermonkey/urlsearchparams.cpp index 64e5a78c3..50ee38bd0 100644 --- a/src/ecmascript/spidermonkey/urlsearchparams.cpp +++ b/src/ecmascript/spidermonkey/urlsearchparams.cpp @@ -11,9 +11,11 @@ #include "elinks.h" #include "ecmascript/spidermonkey/util.h" +#include #include #include #include +#include #include "bfu/dialog.h" #include "cache/cache.h" @@ -146,17 +148,72 @@ urlSearchParams_constructor(JSContext* ctx, unsigned argc, JS::Value* vp) return false; } JS::SetReservedSlot(newObj, 0, JS::PrivateValue(u)); + JS::RootedObject r(ctx, JS::NewMapObject(ctx)); if (argc > 0) { - char *urlstring = jsval_to_string(ctx, args[0]); + if (args[0].isObject()) { + JS::RootedObject ar(ctx, &args[0].toObject()); + uint32_t len = 0; + JS::GetArrayLength(ctx, ar, &len); - if (!urlstring) { - return false; + if (len > 0) { + for (int i = 0; i < len; i++) { + JS::RootedValue val(ctx); + + if (!JS_GetElement(ctx, ar, i, &val)) { + continue; + } + JS::RootedObject ar2(ctx, &val.toObject()); + uint32_t len2 = 0; + JS::GetArrayLength(ctx, ar2, &len2); + + if (len2 != 2) { + continue; + } + JS::RootedValue k(ctx); + JS::RootedValue v(ctx); + + if (!JS_GetElement(ctx, ar2, 0, &k)) { + continue; + } + + if (!JS_GetElement(ctx, ar2, 1, &v)) { + continue; + } + JS::MapSet(ctx, r, k, v); + } + u->map = r; + } else { + JS::Rooted props(ctx, JS::IdVector(ctx)); + JS_Enumerate(ctx, ar, &props); + uint32_t len = props.length(); + + for (int i = 0; i < len; i++) { + JS::RootedValue v(ctx); + JS::RootedString rst(ctx, props[i].toString()); + JS::UniqueChars utf8chars = JS_EncodeStringToUTF8(ctx, rst); + char *name = utf8chars.get(); + + if (name) { + if (!JS_GetProperty(ctx, ar, name, &v)) { + continue; + } + JS::RootedValue k(ctx, JS::StringValue(props[i].toString())); + JS::MapSet(ctx, r, k, v); + } + } + u->map = r; + } + } else { + char *urlstring = jsval_to_string(ctx, args[0]); + + if (!urlstring) { + return false; + } + parse_text(ctx, r, urlstring); + u->map = r; + mem_free(urlstring); } - JS::RootedObject r(ctx, JS::NewMapObject(ctx)); - u->map = r; - parse_text(ctx, r, urlstring); - mem_free(urlstring); } args.rval().setObject(*newObj); diff --git a/test/ecmascript/assert/URLSearchParams.html b/test/ecmascript/assert/URLSearchParams.html index 8a760dcc6..8ca889eeb 100644 --- a/test/ecmascript/assert/URLSearchParams.html +++ b/test/ecmascript/assert/URLSearchParams.html @@ -11,5 +11,22 @@ console.assert(param.has('cc'), 'cc'); console.assert(param.has('bar', '2'), 'bar=2'); console.assert(param.get('bar') === '2', 'bar = 2'); console.assert(param.toString() === 'foo=1&bar=2&cc=3', 'without ?'); + +var params3 = new URLSearchParams([ + ["foo", "a1"], + ["bar", "a2"], + ["baz", "a3"], +]); +console.assert(params3.get('bar') === 'a2', 'bar = a2'); + +var params4 = new URLSearchParams({ + foo : "c1", + bar : "c2", + baz : "c3", +}); +console.assert(params4.get('bar') === 'c2', params4.get('bar')); + + + console.exit();