Pārlūkot izejas kodu

LibWeb: Implement Element.getAttributeNodeNS

Shannon Booth 1 gadu atpakaļ
vecāks
revīzija
7a26a889cb

+ 4 - 0
Tests/LibWeb/Text/expected/DOM/Element-getAttributeNodeNS.txt

@@ -0,0 +1,4 @@
+     xlink:href getAttributeNode = 'null'
+xlink:href getAttributeNodeNS = 'Attr': name = xlink:href value = test
+href getAttributeNode = 'Attr': name = href value = test
+href getAttributeNodeNS = 'null'

+ 23 - 0
Tests/LibWeb/Text/input/DOM/Element-getAttributeNodeNS.html

@@ -0,0 +1,23 @@
+<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">
+    function dumpAttr(description, attr) {
+        if (attr === null) {
+            println(`${description} = 'null'`);
+            return;
+        }
+        println(`${description} = '${attr.constructor.name}': name = ${attr.name} value = ${attr.value}`);
+    }
+    test(() => {
+        const xlinkNS = document.getElementById("with-xlink-href");
+        dumpAttr('xlink:href getAttributeNode', xlinkNS.getAttributeNode("href"));
+        dumpAttr('xlink:href getAttributeNodeNS', xlinkNS.getAttributeNodeNS("http://www.w3.org/1999/xlink", "href"));
+
+        const noNS = document.getElementById("no-xlink-href");
+        dumpAttr('href getAttributeNode', noNS.getAttributeNode("href"));
+        dumpAttr('href getAttributeNodeNS', noNS.getAttributeNodeNS("http://www.w3.org/1999/xlink", "href"));
+    });
+</script>

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

@@ -164,6 +164,13 @@ JS::GCPtr<Attr> Element::get_attribute_node(FlyString const& name) const
     return m_attributes->get_attribute(name);
     return m_attributes->get_attribute(name);
 }
 }
 
 
+// https://dom.spec.whatwg.org/#dom-element-getattributenodens
+JS::GCPtr<Attr> Element::get_attribute_node_ns(Optional<FlyString> const& namespace_, FlyString const& name) const
+{
+    // The getAttributeNodeNS(namespace, localName) method steps are to return the result of getting an attribute given namespace, localName, and this.
+    return m_attributes->get_attribute_ns(namespace_, name);
+}
+
 // https://dom.spec.whatwg.org/#dom-element-setattribute
 // https://dom.spec.whatwg.org/#dom-element-setattribute
 WebIDL::ExceptionOr<void> Element::set_attribute(FlyString const& name, String const& value)
 WebIDL::ExceptionOr<void> Element::set_attribute(FlyString const& name, String const& value)
 {
 {

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

@@ -119,6 +119,7 @@ public:
     Vector<String> get_attribute_names() const;
     Vector<String> get_attribute_names() const;
 
 
     JS::GCPtr<Attr> get_attribute_node(FlyString const& name) const;
     JS::GCPtr<Attr> get_attribute_node(FlyString const& name) const;
+    JS::GCPtr<Attr> get_attribute_node_ns(Optional<FlyString> const& namespace_, FlyString const& name) const;
 
 
     DOMTokenList* class_list();
     DOMTokenList* class_list();
 
 

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

@@ -43,7 +43,8 @@ interface Element : Node {
     [SameObject] readonly attribute NamedNodeMap attributes;
     [SameObject] readonly attribute NamedNodeMap attributes;
     sequence<DOMString> getAttributeNames();
     sequence<DOMString> getAttributeNames();
 
 
-    Attr? getAttributeNode(DOMString qualifiedName);
+    Attr? getAttributeNode([FlyString] DOMString qualifiedName);
+    Attr? getAttributeNodeNS([FlyString] DOMString? namespace, [FlyString] DOMString localName);
 
 
     HTMLCollection getElementsByTagName(DOMString tagName);
     HTMLCollection getElementsByTagName(DOMString tagName);
     HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);
     HTMLCollection getElementsByTagNameNS(DOMString? namespace, DOMString localName);