Browse Source

LibWeb: Implement Element.removeAttributeNS

Shannon Booth 1 year ago
parent
commit
3910efb80b

+ 12 - 0
Tests/LibWeb/Text/expected/DOM/Element-removeAttributeNS.txt

@@ -0,0 +1,12 @@
+     Original values
+xlink:href getAttributeNS = 'test'
+href getAttribute = 'test'
+Non-matching namespace
+xlink:href getAttributeNS = 'test'
+href getAttribute = 'test'
+Non-matching name
+xlink:href getAttributeNS = 'test'
+href getAttribute = 'test'
+Matching
+xlink:href getAttributeNS = 'null'
+href getAttribute = 'null'

+ 37 - 0
Tests/LibWeb/Text/input/DOM/Element-removeAttributeNS.html

@@ -0,0 +1,37 @@
+<script src="../include.js"></script>
+<svg xmlns="http://www.w3.org/2000/svg">
+    <script id="with-xlink-href" xlink:href="test"></script>
+    <script id="no-xlink-href" href="test"></script>
+</svg>
+<script id="svg-script-element">
+    test(() => {
+        const namespace = "http://www.w3.org/1999/xlink"
+        const xlinkNS = document.getElementById("with-xlink-href");
+        const noNS = document.getElementById("no-xlink-href");
+
+        println("Original values");
+        println(`xlink:href getAttributeNS = '${xlinkNS.getAttributeNS(namespace, "href")}'`);
+        println(`href getAttribute = '${noNS.getAttribute("href")}'`);
+
+        println("Non-matching namespace");
+        xlinkNS.removeAttributeNS(null, "href");
+        noNS.removeAttributeNS(namespace, "href");
+
+        println(`xlink:href getAttributeNS = '${xlinkNS.getAttributeNS(namespace, "href")}'`);
+        println(`href getAttribute = '${noNS.getAttribute("href")}'`);
+
+        println("Non-matching name");
+        xlinkNS.removeAttributeNS(namespace, "thing");
+        noNS.removeAttributeNS(null, "thing");
+
+        println(`xlink:href getAttributeNS = '${xlinkNS.getAttributeNS(namespace, "href")}'`);
+        println(`href getAttribute = '${noNS.getAttribute("href")}'`);
+
+        println("Matching");
+        xlinkNS.removeAttributeNS(namespace, "href");
+        noNS.removeAttributeNS(null, "href");
+
+        println(`xlink:href getAttributeNS = '${xlinkNS.getAttributeNS(namespace, "href")}'`);
+        println(`href getAttribute = '${noNS.getAttribute("href")}'`);
+    });
+</script>

+ 7 - 0
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -305,6 +305,13 @@ void Element::remove_attribute(FlyString const& name)
     m_attributes->remove_attribute(name);
     m_attributes->remove_attribute(name);
 }
 }
 
 
+// https://dom.spec.whatwg.org/#dom-element-removeattributens
+void Element::remove_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& name)
+{
+    // The removeAttributeNS(namespace, localName) method steps are to remove an attribute given namespace, localName, and this, and then return undefined.
+    m_attributes->remove_attribute_ns(namespace_, name);
+}
+
 // https://dom.spec.whatwg.org/#dom-element-hasattribute
 // https://dom.spec.whatwg.org/#dom-element-hasattribute
 bool Element::has_attribute(FlyString const& name) const
 bool Element::has_attribute(FlyString const& name) const
 {
 {

+ 1 - 0
Userland/Libraries/LibWeb/DOM/Element.h

@@ -112,6 +112,7 @@ public:
 
 
     void append_attribute(Attr&);
     void append_attribute(Attr&);
     void remove_attribute(FlyString const& name);
     void remove_attribute(FlyString const& name);
+    void remove_attribute_ns(Optional<FlyString> const& namespace_, FlyString const& name);
 
 
     WebIDL::ExceptionOr<bool> toggle_attribute(FlyString const& name, Optional<bool> force);
     WebIDL::ExceptionOr<bool> toggle_attribute(FlyString const& name, Optional<bool> force);
     size_t attribute_list_size() const;
     size_t attribute_list_size() const;

+ 2 - 1
Userland/Libraries/LibWeb/DOM/Element.idl

@@ -35,7 +35,8 @@ interface Element : Node {
     [CEReactions] Attr? setAttributeNode(Attr attr);
     [CEReactions] Attr? setAttributeNode(Attr attr);
     [CEReactions] Attr? setAttributeNodeNS(Attr attr);
     [CEReactions] Attr? setAttributeNodeNS(Attr attr);
 
 
-    [CEReactions] undefined removeAttribute(DOMString qualifiedName);
+    [CEReactions] undefined removeAttribute([FlyString] DOMString qualifiedName);
+    [CEReactions] undefined removeAttributeNS([FlyString] DOMString? namespace, [FlyString] DOMString localName);
     [CEReactions] boolean toggleAttribute(DOMString qualifiedName, optional boolean force);
     [CEReactions] boolean toggleAttribute(DOMString qualifiedName, optional boolean force);
     boolean hasAttribute(DOMString qualifiedName);
     boolean hasAttribute(DOMString qualifiedName);
     boolean hasAttributeNS([FlyString] DOMString? namespace, [FlyString] DOMString localName);
     boolean hasAttributeNS([FlyString] DOMString? namespace, [FlyString] DOMString localName);