Ver Fonte

LibWeb: Implement value argument of URLSearchParams.delete

Shannon Booth há 11 meses atrás
pai
commit
5637dc43b2

+ 5 - 0
Tests/LibWeb/Text/expected/URL/search-params-delete.txt

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

+ 15 - 0
Tests/LibWeb/Text/input/URL/search-params-delete.html

@@ -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>

+ 15 - 5
Userland/Libraries/LibWeb/DOMURL/URLSearchParams.cpp

@@ -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 this’s 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 this’s list.
+    else {
+        m_list.remove_all_matching([&name](auto& entry) {
+            return entry.name == name;
+        });
+    }
+
     // 2. Update this.
     TRY(update());
 

+ 1 - 1
Userland/Libraries/LibWeb/DOMURL/URLSearchParams.h

@@ -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);

+ 1 - 1
Userland/Libraries/LibWeb/DOMURL/URLSearchParams.idl

@@ -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);