From b305ee86924911754836a03f8e9fa0b53cca1e46 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Mon, 14 Feb 2022 06:38:03 +0000 Subject: [PATCH] LibWeb: Add support for the record variant of URLSearchParams --- .../Libraries/LibWeb/URL/URLSearchParams.cpp | 16 ++++++++++++++-- Userland/Libraries/LibWeb/URL/URLSearchParams.h | 2 +- .../Libraries/LibWeb/URL/URLSearchParams.idl | 3 +-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibWeb/URL/URLSearchParams.cpp b/Userland/Libraries/LibWeb/URL/URLSearchParams.cpp index 48cb919917..34ca58a0e9 100644 --- a/Userland/Libraries/LibWeb/URL/URLSearchParams.cpp +++ b/Userland/Libraries/LibWeb/URL/URLSearchParams.cpp @@ -66,7 +66,9 @@ Vector url_decode(StringView input) return output; } -DOM::ExceptionOr> URLSearchParams::create_with_global_object(Bindings::WindowObject&, Variant>, String> const& init) +// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams +// https://url.spec.whatwg.org/#urlsearchparams-initialize +DOM::ExceptionOr> URLSearchParams::create_with_global_object(Bindings::WindowObject&, Variant>, OrderedHashMap, String> const& init) { // 1. If init is a string and starts with U+003F (?), then remove the first code point from init. // NOTE: We do this when we know that it's a string on step 3 of initialization. @@ -94,8 +96,18 @@ DOM::ExceptionOr> URLSearchParams::create_with_gl return URLSearchParams::create(move(list)); } - // TODO // 2. Otherwise, if init is a record, then for each name → value of init, append a new name-value pair whose name is name and value is value, to query’s list. + if (init.has>()) { + auto const& init_record = init.get>(); + + Vector list; + list.ensure_capacity(init_record.size()); + + for (auto const& pair : init_record) + list.append(QueryParam { .name = pair.key, .value = pair.value }); + + return URLSearchParams::create(move(list)); + } // 3. Otherwise: // a. Assert: init is a string. diff --git a/Userland/Libraries/LibWeb/URL/URLSearchParams.h b/Userland/Libraries/LibWeb/URL/URLSearchParams.h index 22f44a5a97..7fe3ea8114 100644 --- a/Userland/Libraries/LibWeb/URL/URLSearchParams.h +++ b/Userland/Libraries/LibWeb/URL/URLSearchParams.h @@ -30,7 +30,7 @@ public: return adopt_ref(*new URLSearchParams(move(list))); } - static DOM::ExceptionOr> create_with_global_object(Bindings::WindowObject&, Variant>, String> const& init); + static DOM::ExceptionOr> create_with_global_object(Bindings::WindowObject&, Variant>, OrderedHashMap, String> const& init); void append(String const& name, String const& value); void delete_(String const& name); diff --git a/Userland/Libraries/LibWeb/URL/URLSearchParams.idl b/Userland/Libraries/LibWeb/URL/URLSearchParams.idl index e552a248b4..b5030d004b 100644 --- a/Userland/Libraries/LibWeb/URL/URLSearchParams.idl +++ b/Userland/Libraries/LibWeb/URL/URLSearchParams.idl @@ -1,7 +1,6 @@ interface URLSearchParams { - // FIXME: the real type of init is (sequence> or record or USVString) - constructor(optional (sequence> or USVString) init = ""); + constructor(optional (sequence> or record or USVString) init = ""); undefined append(USVString name, USVString value); undefined delete(USVString name);