LibWeb: Implement value argument of URLSearchParams.delete

This commit is contained in:
Shannon Booth 2024-08-10 23:35:14 +12:00 committed by Tim Ledbetter
commit 5637dc43b2
Notes: github-actions[bot] 2024-08-12 22:02:50 +00:00
5 changed files with 37 additions and 7 deletions

View file

@ -0,0 +1,5 @@
animal=octopus&place=ocean
animal=octopus&place=ocean
place=ocean
place=ocean

View file

@ -0,0 +1,15 @@
<script src="../include.js"></script>
<script>
test(() => {
const params = new URLSearchParams('animal=octopus&place=ocean');
println(params.toString());
params.delete('non-existing-key');
println(params.toString());
params.delete('animal');
println(params.toString());
params.delete('place', 'non-existing-value');
println(params.toString());
params.delete('place', 'ocean');
println(params.toString());
});
</script>

View file

@ -231,12 +231,22 @@ WebIDL::ExceptionOr<void> URLSearchParams::update()
return {};
}
WebIDL::ExceptionOr<void> URLSearchParams::delete_(String const& name)
// https://url.spec.whatwg.org/#dom-urlsearchparams-delete
WebIDL::ExceptionOr<void> URLSearchParams::delete_(String const& name, Optional<String> const& value)
{
// 1. Remove all name-value pairs whose name is name from list.
m_list.remove_all_matching([&name](auto& entry) {
return entry.name == name;
});
// 1. If value is given, then remove all tuples whose name is name and value is value from thiss list.
if (value.has_value()) {
m_list.remove_all_matching([&name, &value](auto& entry) {
return entry.name == name && entry.value == value.value();
});
}
// 2. Otherwise, remove all tuples whose name is name from thiss list.
else {
m_list.remove_all_matching([&name](auto& entry) {
return entry.name == name;
});
}
// 2. Update this.
TRY(update());

View file

@ -32,7 +32,7 @@ public:
size_t size() const;
WebIDL::ExceptionOr<void> append(String const& name, String const& value);
WebIDL::ExceptionOr<void> delete_(String const& name);
WebIDL::ExceptionOr<void> delete_(String const& name, Optional<String> const& value = {});
Optional<String> get(String const& name);
WebIDL::ExceptionOr<Vector<String>> get_all(String const& name);
bool has(String const& name);

View file

@ -7,7 +7,7 @@ interface URLSearchParams {
readonly attribute unsigned long size;
undefined append(USVString name, USVString value);
undefined delete(USVString name);
undefined delete(USVString name, optional USVString value);
USVString? get(USVString name);
sequence<USVString> getAll(USVString name);
boolean has(USVString name);