From f3f2170ac6c217fb88346f4834524168bac85a50 Mon Sep 17 00:00:00 2001 From: Luke Date: Mon, 5 Jul 2021 05:30:24 +0100 Subject: [PATCH] LibWeb: Add the cloning steps in clone_node This will be used in HTMLTemplateElement later to clone template contents. This makes the clone functions non-const in the process, as the cloning steps can have side effects. --- Userland/Libraries/LibWeb/DOM/Node.cpp | 8 +++++--- Userland/Libraries/LibWeb/DOM/Node.h | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp index d2f70f3d72..29cf612567 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.cpp +++ b/Userland/Libraries/LibWeb/DOM/Node.cpp @@ -406,7 +406,7 @@ ExceptionOr> Node::replace_child(NonnullRefPtr node, N } // https://dom.spec.whatwg.org/#concept-node-clone -NonnullRefPtr Node::clone_node(Document* document, bool clone_children) const +NonnullRefPtr Node::clone_node(Document* document, bool clone_children) { if (!document) document = m_document; @@ -454,7 +454,9 @@ NonnullRefPtr Node::clone_node(Document* document, bool clone_children) co TODO(); } // FIXME: 4. Set copy’s node document and document to copy, if copy is a document, and set copy’s node document to document otherwise. - // FIXME: 5. Run any cloning steps defined for node in other applicable specifications and pass copy, node, document and the clone children flag if set, as parameters. + + cloned(*copy, clone_children); + if (clone_children) { for_each_child([&](auto& child) { copy->append_child(child.clone_node(document, true)); @@ -464,7 +466,7 @@ NonnullRefPtr Node::clone_node(Document* document, bool clone_children) co } // https://dom.spec.whatwg.org/#dom-node-clonenode -ExceptionOr> Node::clone_node_binding(bool deep) const +ExceptionOr> Node::clone_node_binding(bool deep) { if (is(*this)) return NotSupportedError::create("Cannot clone shadow root"); diff --git a/Userland/Libraries/LibWeb/DOM/Node.h b/Userland/Libraries/LibWeb/DOM/Node.h index 63d9b122b6..0c21d6cd64 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.h +++ b/Userland/Libraries/LibWeb/DOM/Node.h @@ -85,8 +85,8 @@ public: ExceptionOr> replace_child(NonnullRefPtr node, NonnullRefPtr child); - NonnullRefPtr clone_node(Document* document = nullptr, bool clone_children = false) const; - ExceptionOr> clone_node_binding(bool deep) const; + NonnullRefPtr clone_node(Document* document = nullptr, bool clone_children = false); + ExceptionOr> clone_node_binding(bool deep); // NOTE: This is intended for the JS bindings. bool has_child_nodes() const { return has_children(); } @@ -134,6 +134,7 @@ public: virtual void removed_from(Node*) { } virtual void children_changed() { } virtual void adopted_from(const Document&) { } + virtual void cloned(Node&, bool) {}; const Layout::Node* layout_node() const { return m_layout_node; } Layout::Node* layout_node() { return m_layout_node; }