From bfc07732855ddaea17ab65a376d2e34bc349516e Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Mon, 11 Sep 2023 18:36:23 +1200 Subject: [PATCH] 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. --- Userland/Libraries/LibWeb/DOM/ChildNode.h | 6 ++--- .../Libraries/LibWeb/DOM/NodeOperations.cpp | 25 ++++++++++++++++--- .../Libraries/LibWeb/DOM/NodeOperations.h | 5 +++- Userland/Libraries/LibWeb/DOM/ParentNode.cpp | 19 ++++++++++++-- Userland/Libraries/LibWeb/DOM/ParentNode.h | 4 +++ 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/ChildNode.h b/Userland/Libraries/LibWeb/DOM/ChildNode.h index 1de358115e..734ef52922 100644 --- a/Userland/Libraries/LibWeb/DOM/ChildNode.h +++ b/Userland/Libraries/LibWeb/DOM/ChildNode.h @@ -31,7 +31,7 @@ public: 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. - 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. if (!viable_previous_sibling) @@ -61,7 +61,7 @@ public: 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. - 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. (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); // 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. // Note: This could have been inserted into node. diff --git a/Userland/Libraries/LibWeb/DOM/NodeOperations.cpp b/Userland/Libraries/LibWeb/DOM/NodeOperations.cpp index 576d9d113e..4fbcea0da7 100644 --- a/Userland/Libraries/LibWeb/DOM/NodeOperations.cpp +++ b/Userland/Libraries/LibWeb/DOM/NodeOperations.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2022, Luke Wilde + * Copyright (c) 2023, Shannon Booth * * SPDX-License-Identifier: BSD-2-Clause */ @@ -14,7 +15,7 @@ namespace Web::DOM { // https://dom.spec.whatwg.org/#converting-nodes-into-a-node -WebIDL::ExceptionOr> convert_nodes_to_single_node(Vector, DeprecatedString>> const& nodes, DOM::Document& document) +WebIDL::ExceptionOr> convert_nodes_to_single_node(Vector, String>> const& nodes, DOM::Document& document) { // 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. @@ -22,18 +23,18 @@ WebIDL::ExceptionOr> 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. // 5. Return node. - auto potentially_convert_string_to_text_node = [&document](Variant, DeprecatedString> const& node) -> JS::NonnullGCPtr { + auto potentially_convert_string_to_text_node = [&document](Variant, String> const& node) -> JS::NonnullGCPtr { if (node.has>()) return *node.get>(); - return document.heap().allocate(document.realm(), document, MUST(String::from_deprecated_string(node.get()))); + return document.heap().allocate(document.realm(), document, node.get()); }; if (nodes.size() == 1) return potentially_convert_string_to_text_node(nodes.first()); auto document_fragment = document.heap().allocate(document.realm(), document); - for (auto& unconverted_node : nodes) { + for (auto const& unconverted_node : nodes) { auto node = potentially_convert_string_to_text_node(unconverted_node); (void)TRY(document_fragment->append_child(node)); } @@ -41,4 +42,20 @@ WebIDL::ExceptionOr> convert_nodes_to_single_node(Vector< return document_fragment; } +Vector, String>> from_deprecated_nodes(Vector, DeprecatedString>> const& deprecated_nodes) +{ + Vector, String>> nodes; + nodes.ensure_capacity(deprecated_nodes.size()); + for (auto const& deprecated_node : deprecated_nodes) { + deprecated_node.visit( + [&nodes](JS::Handle node) { + nodes.unchecked_append(node); + }, + [&nodes](DeprecatedString const& deprecated_node) { + nodes.unchecked_append(MUST(String::from_deprecated_string(deprecated_node))); + }); + } + return nodes; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/NodeOperations.h b/Userland/Libraries/LibWeb/DOM/NodeOperations.h index 2d65b77d74..175ad34430 100644 --- a/Userland/Libraries/LibWeb/DOM/NodeOperations.h +++ b/Userland/Libraries/LibWeb/DOM/NodeOperations.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2022, Luke Wilde + * Copyright (c) 2023, Shannon Booth * * SPDX-License-Identifier: BSD-2-Clause */ @@ -12,6 +13,8 @@ namespace Web::DOM { -WebIDL::ExceptionOr> convert_nodes_to_single_node(Vector, DeprecatedString>> const& nodes, DOM::Document& document); +WebIDL::ExceptionOr> convert_nodes_to_single_node(Vector, String>> const& nodes, DOM::Document& document); + +Vector, String>> from_deprecated_nodes(Vector, DeprecatedString>> const& deprecated_nodes); } diff --git a/Userland/Libraries/LibWeb/DOM/ParentNode.cpp b/Userland/Libraries/LibWeb/DOM/ParentNode.cpp index fff262d2cb..b99e73f8ed 100644 --- a/Userland/Libraries/LibWeb/DOM/ParentNode.cpp +++ b/Userland/Libraries/LibWeb/DOM/ParentNode.cpp @@ -187,7 +187,7 @@ JS::NonnullGCPtr ParentNode::get_elements_by_tag_name_ns(Depreca } // https://dom.spec.whatwg.org/#dom-parentnode-prepend -WebIDL::ExceptionOr ParentNode::prepend(Vector, DeprecatedString>> const& nodes) +WebIDL::ExceptionOr ParentNode::prepend(Vector, String>> const& nodes) { // 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())); @@ -198,7 +198,22 @@ WebIDL::ExceptionOr ParentNode::prepend(Vector, D return {}; } +WebIDL::ExceptionOr ParentNode::prepend(Vector, DeprecatedString>> const& nodes) +{ + return prepend(from_deprecated_nodes(nodes)); +} + WebIDL::ExceptionOr ParentNode::append(Vector, DeprecatedString>> const& nodes) +{ + return append(from_deprecated_nodes(nodes)); +} + +WebIDL::ExceptionOr ParentNode::replace_children(Vector, DeprecatedString>> const& nodes) +{ + return replace_children(from_deprecated_nodes(nodes)); +} + +WebIDL::ExceptionOr ParentNode::append(Vector, String>> const& nodes) { // 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())); @@ -209,7 +224,7 @@ WebIDL::ExceptionOr ParentNode::append(Vector, De return {}; } -WebIDL::ExceptionOr ParentNode::replace_children(Vector, DeprecatedString>> const& nodes) +WebIDL::ExceptionOr ParentNode::replace_children(Vector, String>> const& nodes) { // 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())); diff --git a/Userland/Libraries/LibWeb/DOM/ParentNode.h b/Userland/Libraries/LibWeb/DOM/ParentNode.h index 0761d23fb1..98a7f75bf3 100644 --- a/Userland/Libraries/LibWeb/DOM/ParentNode.h +++ b/Userland/Libraries/LibWeb/DOM/ParentNode.h @@ -31,6 +31,10 @@ public: JS::NonnullGCPtr get_elements_by_tag_name(DeprecatedFlyString const&); JS::NonnullGCPtr get_elements_by_tag_name_ns(DeprecatedFlyString const&, DeprecatedFlyString const&); + WebIDL::ExceptionOr prepend(Vector, String>> const& nodes); + WebIDL::ExceptionOr append(Vector, String>> const& nodes); + WebIDL::ExceptionOr replace_children(Vector, String>> const& nodes); + WebIDL::ExceptionOr prepend(Vector, DeprecatedString>> const& nodes); WebIDL::ExceptionOr append(Vector, DeprecatedString>> const& nodes); WebIDL::ExceptionOr replace_children(Vector, DeprecatedString>> const& nodes);