diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp index 90ccf5ef2d..c4ff744ece 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.cpp +++ b/Userland/Libraries/LibWeb/DOM/Node.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Linus Groh * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -43,6 +44,7 @@ #include #include #include +#include namespace Web::DOM { @@ -363,6 +365,70 @@ void Node::remove(bool suppress_observers) parent->children_changed(); } +// https://dom.spec.whatwg.org/#concept-node-clone +NonnullRefPtr Node::clone_node(Document* document, bool clone_children) const +{ + if (!document) + document = m_document; + RefPtr copy; + if (is(this)) { + auto& element = *downcast(this); + auto qualified_name = QualifiedName(element.local_name(), element.prefix(), element.namespace_()); + auto element_copy = adopt(*new Element(*document, move(qualified_name))); + element.for_each_attribute([&](auto& name, auto& value) { + element_copy->set_attribute(name, value); + }); + copy = move(element_copy); + } else if (is(this)) { + auto document_ = downcast(this); + auto document_copy = Document::create(document_->url()); + document_copy->set_encoding(document_->encoding()); + document_copy->set_content_type(document_->content_type()); + document_copy->set_origin(document_->origin()); + document_copy->set_quirks_mode(document_->mode()); + // FIXME: Set type ("xml" or "html") + copy = move(document_copy); + } else if (is(this)) { + auto document_type = downcast(this); + auto document_type_copy = adopt(*new DocumentType(*document)); + document_type_copy->set_name(document_type->name()); + document_type_copy->set_public_id(document_type->public_id()); + document_type_copy->set_system_id(document_type->system_id()); + copy = move(document_type_copy); + } else if (is(this)) { + auto text = downcast(this); + auto text_copy = adopt(*new Text(*document, text->data())); + copy = move(text_copy); + } else if (is(this)) { + auto comment = downcast(this); + auto comment_copy = adopt(*new Comment(*document, comment->data())); + copy = move(comment_copy); + } else if (is(this)) { + auto processing_instruction = downcast(this); + auto processing_instruction_copy = adopt(*new ProcessingInstruction(*document, processing_instruction->data(), processing_instruction->target())); + copy = move(processing_instruction_copy); + } else { + dbgln("clone_node() not implemented for NodeType {}", (u16)m_type); + 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. + if (clone_children) { + for_each_child([&](auto& child) { + copy->append_child(child.clone_node(document, true)); + }); + } + return copy.release_nonnull(); +} + +// https://dom.spec.whatwg.org/#dom-node-clonenode +ExceptionOr> Node::clone_node_binding(bool deep) const +{ + if (is(*this)) + return NotSupportedError::create("Cannot clone shadow root"); + return clone_node(nullptr, deep); +} + void Node::set_document(Badge, Document& document) { if (m_document == &document) diff --git a/Userland/Libraries/LibWeb/DOM/Node.h b/Userland/Libraries/LibWeb/DOM/Node.h index e4a2927c51..0dd64b5bf7 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.h +++ b/Userland/Libraries/LibWeb/DOM/Node.h @@ -102,6 +102,9 @@ public: void remove_all_children(bool suppress_observers = false); u16 compare_document_position(RefPtr other); + NonnullRefPtr clone_node(Document* document = nullptr, bool clone_children = false) const; + ExceptionOr> clone_node_binding(bool deep) const; + // NOTE: This is intended for the JS bindings. bool has_child_nodes() const { return has_children(); } NonnullRefPtrVector child_nodes() const; diff --git a/Userland/Libraries/LibWeb/DOM/Node.idl b/Userland/Libraries/LibWeb/DOM/Node.idl index 34de218746..7c17fc8bad 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.idl +++ b/Userland/Libraries/LibWeb/DOM/Node.idl @@ -18,6 +18,7 @@ interface Node : EventTarget { Node appendChild(Node node); [ImplementedAs=pre_insert] Node insertBefore(Node node, Node? child); [ImplementedAs=pre_remove] Node removeChild(Node child); + [ImplementedAs=clone_node_binding] Node cloneNode(optional boolean deep = false); const unsigned short ELEMENT_NODE = 1; const unsigned short ATTRIBUTE_NODE = 2; diff --git a/Userland/Libraries/LibWeb/DOM/ProcessingInstruction.h b/Userland/Libraries/LibWeb/DOM/ProcessingInstruction.h index 0ff536994d..cd433a88de 100644 --- a/Userland/Libraries/LibWeb/DOM/ProcessingInstruction.h +++ b/Userland/Libraries/LibWeb/DOM/ProcessingInstruction.h @@ -35,7 +35,7 @@ class ProcessingInstruction final : public CharacterData { public: using WrapperType = Bindings::ProcessingInstructionWrapper; - ProcessingInstruction(Document&, const String&, const String&); + ProcessingInstruction(Document&, const String& data, const String& target); virtual ~ProcessingInstruction() override; virtual FlyString node_name() const override { return m_target; }