diff --git a/Tests/LibWeb/Text/expected/URL/search-params-has.txt b/Tests/LibWeb/Text/expected/URL/search-params-has.txt new file mode 100644 index 00000000000..cacfb4af0af --- /dev/null +++ b/Tests/LibWeb/Text/expected/URL/search-params-has.txt @@ -0,0 +1,5 @@ +false +true +false +true +false diff --git a/Tests/LibWeb/Text/input/URL/search-params-has.html b/Tests/LibWeb/Text/input/URL/search-params-has.html new file mode 100644 index 00000000000..d393eff766e --- /dev/null +++ b/Tests/LibWeb/Text/input/URL/search-params-has.html @@ -0,0 +1,11 @@ + + diff --git a/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp b/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp index 89e35682315..138952ffa99 100644 --- a/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp +++ b/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp @@ -278,13 +278,30 @@ WebIDL::ExceptionOr> URLSearchParams::get_all(String const& name) return values; } -bool URLSearchParams::has(String const& name) +// https://url.spec.whatwg.org/#dom-urlsearchparams-has +bool URLSearchParams::has(String const& name, Optional const& value) { - // return true if there is a name-value pair whose name is name in this’s list, and false otherwise. - return !m_list.find_if([&name](auto& entry) { - return entry.name == name; - }) - .is_end(); + // 1. If value is given and there is a tuple whose name is name and value is value in this’s list, then return true. + if (value.has_value()) { + if (!m_list.find_if([&name, &value](auto& entry) { + return entry.name == name && entry.value == value.value(); + }) + .is_end()) { + return true; + } + } + // 2. If value is not given and there is a tuple whose name is name in this’s list, then return true. + else { + if (!m_list.find_if([&name](auto& entry) { + return entry.name == name; + }) + .is_end()) { + return true; + } + } + + // 3. Return false. + return false; } WebIDL::ExceptionOr URLSearchParams::set(String const& name, String const& value) diff --git a/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.h b/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.h index e7440d6a273..c9f5436b79c 100644 --- a/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.h +++ b/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.h @@ -35,7 +35,7 @@ public: WebIDL::ExceptionOr delete_(String const& name, Optional const& value = {}); Optional get(String const& name); WebIDL::ExceptionOr> get_all(String const& name); - bool has(String const& name); + bool has(String const& name, Optional const& value = {}); WebIDL::ExceptionOr set(String const& name, String const& value); WebIDL::ExceptionOr sort(); diff --git a/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.idl b/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.idl index 5226134f360..921fc0852d2 100644 --- a/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.idl +++ b/Userland/Libraries/LibWeb/DOMURL/URLSearchParams.idl @@ -10,7 +10,7 @@ interface URLSearchParams { undefined delete(USVString name, optional USVString value); USVString? get(USVString name); sequence getAll(USVString name); - boolean has(USVString name); + boolean has(USVString name, optional USVString value); undefined set(USVString name, USVString value); undefined sort();