Selaa lähdekoodia

LibWeb: Implement Document.importNode

Luke Wilde 3 vuotta sitten
vanhempi
commit
27dfd0170e

+ 11 - 0
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -814,6 +814,17 @@ NonnullRefPtrVector<HTML::HTMLScriptElement> Document::take_scripts_to_execute_a
     return move(m_scripts_to_execute_as_soon_as_possible);
 }
 
+// https://dom.spec.whatwg.org/#dom-document-importnode
+ExceptionOr<NonnullRefPtr<Node>> Document::import_node(NonnullRefPtr<Node> node, bool deep)
+{
+    // 1. If node is a document or shadow root, then throw a "NotSupportedError" DOMException.
+    if (is<Document>(*node) || is<ShadowRoot>(*node))
+        return DOM::NotSupportedError::create("Cannot import a document or shadow root.");
+
+    // 2. Return a clone of node, with this and the clone children flag set if deep is true.
+    return node->clone_node(this, deep);
+}
+
 // https://dom.spec.whatwg.org/#concept-node-adopt
 void Document::adopt_node(Node& node)
 {

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

@@ -202,6 +202,7 @@ public:
     bool in_quirks_mode() const { return m_quirks_mode == QuirksMode::Yes; }
     void set_quirks_mode(QuirksMode mode) { m_quirks_mode = mode; }
 
+    ExceptionOr<NonnullRefPtr<Node>> import_node(NonnullRefPtr<Node> node, bool deep);
     void adopt_node(Node&);
     ExceptionOr<NonnullRefPtr<Node>> adopt_node_binding(NonnullRefPtr<Node>);
 

+ 1 - 0
Userland/Libraries/LibWeb/DOM/Document.idl

@@ -41,6 +41,7 @@ interface Document : Node {
     Range createRange();
     Event createEvent(DOMString interface);
 
+    [CEReactions, NewObject] Node importNode(Node node, optional boolean deep = false);
     [CEReactions, ImplementedAs=adopt_node_binding] Node adoptNode(Node node);
 
     [ImplementedAs=style_sheets_for_bindings] readonly attribute StyleSheetList styleSheets;