Kaynağa Gözat

LibWeb: Implement Node.cloneNode for Attr nodes

Andreas Kling 2 yıl önce
ebeveyn
işleme
a004d3043f

+ 8 - 3
Userland/Libraries/LibWeb/DOM/Attr.cpp

@@ -15,12 +15,17 @@ namespace Web::DOM {
 
 JS::NonnullGCPtr<Attr> Attr::create(Document& document, FlyString local_name, DeprecatedString value, Element const* owner_element)
 {
-    return *document.heap().allocate<Attr>(document.realm(), document, move(local_name), move(value), owner_element);
+    return *document.heap().allocate<Attr>(document.realm(), document, QualifiedName(move(local_name), {}, {}), move(value), owner_element);
 }
 
-Attr::Attr(Document& document, FlyString local_name, DeprecatedString value, Element const* owner_element)
+JS::NonnullGCPtr<Attr> Attr::clone(Document& document)
+{
+    return *heap().allocate<Attr>(realm(), document, m_qualified_name, m_value, nullptr);
+}
+
+Attr::Attr(Document& document, QualifiedName qualified_name, DeprecatedString value, Element const* owner_element)
     : Node(document, NodeType::ATTRIBUTE_NODE)
-    , m_qualified_name(move(local_name), {}, {})
+    , m_qualified_name(move(qualified_name))
     , m_value(move(value))
     , m_owner_element(owner_element)
 {

+ 2 - 1
Userland/Libraries/LibWeb/DOM/Attr.h

@@ -19,6 +19,7 @@ class Attr final : public Node {
 
 public:
     static JS::NonnullGCPtr<Attr> create(Document&, FlyString local_name, DeprecatedString value, Element const* = nullptr);
+    JS::NonnullGCPtr<Attr> clone(Document&);
 
     virtual ~Attr() override = default;
 
@@ -42,7 +43,7 @@ public:
     void handle_attribute_changes(Element&, DeprecatedString const& old_value, DeprecatedString const& new_value);
 
 private:
-    Attr(Document&, FlyString local_name, DeprecatedString value, Element const*);
+    Attr(Document&, QualifiedName, DeprecatedString value, Element const*);
 
     virtual void visit_edges(Cell::Visitor&) override;
 

+ 2 - 2
Userland/Libraries/LibWeb/DOM/Node.cpp

@@ -750,10 +750,10 @@ JS::NonnullGCPtr<Node> Node::clone_node(Document* document, bool clone_children)
         document_type_copy->set_system_id(document_type->system_id());
         copy = move(document_type_copy);
     } else if (is<Attr>(this)) {
-        // FIXME:
         // Attr
         // Set copy’s namespace, namespace prefix, local name, and value to those of node.
-        dbgln("clone_node() not implemented for Attribute");
+        auto& attr = static_cast<Attr&>(*this);
+        copy = attr.clone(*document);
     } else if (is<Text>(this)) {
         // Text
         auto text = verify_cast<Text>(this);