1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:38:11 +00:00

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.
This commit is contained in:
Shannon Booth 2023-09-11 18:36:23 +12:00 committed by Sam Atkins
parent 3e0849eb4f
commit bfc0773285
5 changed files with 49 additions and 10 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
* Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
*
* 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<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.
// 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.
// 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>>())
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)
return potentially_convert_string_to_text_node(nodes.first());
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);
(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;
}
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;
}
}