瀏覽代碼

LibWeb: Add String versions for some functions in ParentNode

These functions are required in the porting of the DocumentFragment
interface from DeprecatedString to String. Unfortunately since
ParentNode is used by Document, we can't fully remove the deprecated
versions of these functions yet.
Shannon Booth 1 年之前
父節點
當前提交
bfc0773285

+ 3 - 3
Userland/Libraries/LibWeb/DOM/ChildNode.h

@@ -31,7 +31,7 @@ public:
         auto viable_previous_sibling = viable_previous_sibling_for_insertion(nodes);
         auto viable_previous_sibling = viable_previous_sibling_for_insertion(nodes);
 
 
         // 4. Let node be the result of converting nodes into a node, given nodes and this’s node document.
         // 4. Let node be the result of converting nodes into a node, given nodes and this’s node document.
-        auto node_to_insert = TRY(convert_nodes_to_single_node(nodes, node->document()));
+        auto node_to_insert = TRY(convert_nodes_to_single_node(from_deprecated_nodes(nodes), node->document()));
 
 
         // 5. If viablePreviousSibling is null, then set it to parent’s first child; otherwise to viablePreviousSibling’s next sibling.
         // 5. If viablePreviousSibling is null, then set it to parent’s first child; otherwise to viablePreviousSibling’s next sibling.
         if (!viable_previous_sibling)
         if (!viable_previous_sibling)
@@ -61,7 +61,7 @@ public:
         auto viable_next_sibling = viable_nest_sibling_for_insertion(nodes);
         auto viable_next_sibling = viable_nest_sibling_for_insertion(nodes);
 
 
         // 4. Let node be the result of converting nodes into a node, given nodes and this’s node document.
         // 4. Let node be the result of converting nodes into a node, given nodes and this’s node document.
-        auto node_to_insert = TRY(convert_nodes_to_single_node(nodes, node->document()));
+        auto node_to_insert = TRY(convert_nodes_to_single_node(from_deprecated_nodes(nodes), node->document()));
 
 
         // 5. Pre-insert node into parent before viableNextSibling.
         // 5. Pre-insert node into parent before viableNextSibling.
         (void)TRY(parent->pre_insert(node_to_insert, viable_next_sibling));
         (void)TRY(parent->pre_insert(node_to_insert, viable_next_sibling));
@@ -85,7 +85,7 @@ public:
         auto viable_next_sibling = viable_nest_sibling_for_insertion(nodes);
         auto viable_next_sibling = viable_nest_sibling_for_insertion(nodes);
 
 
         // 4. Let node be the result of converting nodes into a node, given nodes and this’s node document.
         // 4. Let node be the result of converting nodes into a node, given nodes and this’s node document.
-        auto node_to_insert = TRY(convert_nodes_to_single_node(nodes, node->document()));
+        auto node_to_insert = TRY(convert_nodes_to_single_node(from_deprecated_nodes(nodes), node->document()));
 
 
         // 5. If this’s parent is parent, replace this with node within parent.
         // 5. If this’s parent is parent, replace this with node within parent.
         // Note: This could have been inserted into node.
         // Note: This could have been inserted into node.

+ 21 - 4
Userland/Libraries/LibWeb/DOM/NodeOperations.cpp

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
+ * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
@@ -14,7 +15,7 @@
 namespace Web::DOM {
 namespace Web::DOM {
 
 
 // https://dom.spec.whatwg.org/#converting-nodes-into-a-node
 // https://dom.spec.whatwg.org/#converting-nodes-into-a-node
-WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> convert_nodes_to_single_node(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes, DOM::Document& document)
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> convert_nodes_to_single_node(Vector<Variant<JS::Handle<Node>, String>> const& nodes, DOM::Document& document)
 {
 {
     // 1. Let node be null.
     // 1. Let node be null.
     // 2. Replace each string in nodes with a new Text node whose data is the string and node document is document.
     // 2. Replace each string in nodes with a new Text node whose data is the string and node document is document.
@@ -22,18 +23,18 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> convert_nodes_to_single_node(Vector<
     // 4. Otherwise, set node to a new DocumentFragment node whose node document is document, and then append each node in nodes, if any, to it.
     // 4. Otherwise, set node to a new DocumentFragment node whose node document is document, and then append each node in nodes, if any, to it.
     // 5. Return node.
     // 5. Return node.
 
 
-    auto potentially_convert_string_to_text_node = [&document](Variant<JS::Handle<Node>, DeprecatedString> const& node) -> JS::NonnullGCPtr<Node> {
+    auto potentially_convert_string_to_text_node = [&document](Variant<JS::Handle<Node>, String> const& node) -> JS::NonnullGCPtr<Node> {
         if (node.has<JS::Handle<Node>>())
         if (node.has<JS::Handle<Node>>())
             return *node.get<JS::Handle<Node>>();
             return *node.get<JS::Handle<Node>>();
 
 
-        return document.heap().allocate<DOM::Text>(document.realm(), document, MUST(String::from_deprecated_string(node.get<DeprecatedString>())));
+        return document.heap().allocate<DOM::Text>(document.realm(), document, node.get<String>());
     };
     };
 
 
     if (nodes.size() == 1)
     if (nodes.size() == 1)
         return potentially_convert_string_to_text_node(nodes.first());
         return potentially_convert_string_to_text_node(nodes.first());
 
 
     auto document_fragment = document.heap().allocate<DOM::DocumentFragment>(document.realm(), document);
     auto document_fragment = document.heap().allocate<DOM::DocumentFragment>(document.realm(), document);
-    for (auto& unconverted_node : nodes) {
+    for (auto const& unconverted_node : nodes) {
         auto node = potentially_convert_string_to_text_node(unconverted_node);
         auto node = potentially_convert_string_to_text_node(unconverted_node);
         (void)TRY(document_fragment->append_child(node));
         (void)TRY(document_fragment->append_child(node));
     }
     }
@@ -41,4 +42,20 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> convert_nodes_to_single_node(Vector<
     return document_fragment;
     return document_fragment;
 }
 }
 
 
+Vector<Variant<JS::Handle<Node>, String>> from_deprecated_nodes(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& deprecated_nodes)
+{
+    Vector<Variant<JS::Handle<Node>, String>> nodes;
+    nodes.ensure_capacity(deprecated_nodes.size());
+    for (auto const& deprecated_node : deprecated_nodes) {
+        deprecated_node.visit(
+            [&nodes](JS::Handle<Node> node) {
+                nodes.unchecked_append(node);
+            },
+            [&nodes](DeprecatedString const& deprecated_node) {
+                nodes.unchecked_append(MUST(String::from_deprecated_string(deprecated_node)));
+            });
+    }
+    return nodes;
+}
+
 }
 }

+ 4 - 1
Userland/Libraries/LibWeb/DOM/NodeOperations.h

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
+ * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
@@ -12,6 +13,8 @@
 
 
 namespace Web::DOM {
 namespace Web::DOM {
 
 
-WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> convert_nodes_to_single_node(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes, DOM::Document& document);
+WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> convert_nodes_to_single_node(Vector<Variant<JS::Handle<Node>, String>> const& nodes, DOM::Document& document);
+
+Vector<Variant<JS::Handle<Node>, String>> from_deprecated_nodes(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& deprecated_nodes);
 
 
 }
 }

+ 17 - 2
Userland/Libraries/LibWeb/DOM/ParentNode.cpp

@@ -187,7 +187,7 @@ JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_tag_name_ns(Depreca
 }
 }
 
 
 // https://dom.spec.whatwg.org/#dom-parentnode-prepend
 // https://dom.spec.whatwg.org/#dom-parentnode-prepend
-WebIDL::ExceptionOr<void> ParentNode::prepend(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes)
+WebIDL::ExceptionOr<void> ParentNode::prepend(Vector<Variant<JS::Handle<Node>, String>> const& nodes)
 {
 {
     // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
     // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
     auto node = TRY(convert_nodes_to_single_node(nodes, document()));
     auto node = TRY(convert_nodes_to_single_node(nodes, document()));
@@ -198,7 +198,22 @@ WebIDL::ExceptionOr<void> ParentNode::prepend(Vector<Variant<JS::Handle<Node>, D
     return {};
     return {};
 }
 }
 
 
+WebIDL::ExceptionOr<void> ParentNode::prepend(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes)
+{
+    return prepend(from_deprecated_nodes(nodes));
+}
+
 WebIDL::ExceptionOr<void> ParentNode::append(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes)
 WebIDL::ExceptionOr<void> ParentNode::append(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes)
+{
+    return append(from_deprecated_nodes(nodes));
+}
+
+WebIDL::ExceptionOr<void> ParentNode::replace_children(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes)
+{
+    return replace_children(from_deprecated_nodes(nodes));
+}
+
+WebIDL::ExceptionOr<void> ParentNode::append(Vector<Variant<JS::Handle<Node>, String>> const& nodes)
 {
 {
     // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
     // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
     auto node = TRY(convert_nodes_to_single_node(nodes, document()));
     auto node = TRY(convert_nodes_to_single_node(nodes, document()));
@@ -209,7 +224,7 @@ WebIDL::ExceptionOr<void> ParentNode::append(Vector<Variant<JS::Handle<Node>, De
     return {};
     return {};
 }
 }
 
 
-WebIDL::ExceptionOr<void> ParentNode::replace_children(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes)
+WebIDL::ExceptionOr<void> ParentNode::replace_children(Vector<Variant<JS::Handle<Node>, String>> const& nodes)
 {
 {
     // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
     // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
     auto node = TRY(convert_nodes_to_single_node(nodes, document()));
     auto node = TRY(convert_nodes_to_single_node(nodes, document()));

+ 4 - 0
Userland/Libraries/LibWeb/DOM/ParentNode.h

@@ -31,6 +31,10 @@ public:
     JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name(DeprecatedFlyString const&);
     JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name(DeprecatedFlyString const&);
     JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name_ns(DeprecatedFlyString const&, DeprecatedFlyString const&);
     JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name_ns(DeprecatedFlyString const&, DeprecatedFlyString const&);
 
 
+    WebIDL::ExceptionOr<void> prepend(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
+    WebIDL::ExceptionOr<void> append(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
+    WebIDL::ExceptionOr<void> replace_children(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
+
     WebIDL::ExceptionOr<void> prepend(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
     WebIDL::ExceptionOr<void> prepend(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
     WebIDL::ExceptionOr<void> append(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
     WebIDL::ExceptionOr<void> append(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
     WebIDL::ExceptionOr<void> replace_children(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);
     WebIDL::ExceptionOr<void> replace_children(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes);