2022-01-29 20:23:48 +00:00
|
|
|
|
/*
|
|
|
|
|
|
* Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
|
2023-09-11 18:36:23 +12:00
|
|
|
|
* Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
|
2025-12-11 16:45:01 +00:00
|
|
|
|
* Copyright (c) 2025, Sam Atkins <sam@ladybird.org>
|
2022-01-29 20:23:48 +00:00
|
|
|
|
*
|
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-07-24 12:05:52 -04:00
|
|
|
|
#include <AK/Utf16String.h>
|
2022-01-29 20:23:48 +00:00
|
|
|
|
#include <AK/Vector.h>
|
2022-08-28 13:42:07 +02:00
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2022-01-29 20:23:48 +00:00
|
|
|
|
#include <LibWeb/DOM/DocumentFragment.h>
|
|
|
|
|
|
#include <LibWeb/DOM/NodeOperations.h>
|
|
|
|
|
|
#include <LibWeb/DOM/Text.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace Web::DOM {
|
|
|
|
|
|
|
2025-12-11 16:45:01 +00:00
|
|
|
|
// https://dom.spec.whatwg.org/#convert-nodes-into-a-node
|
|
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<Node>> convert_nodes_to_single_node(Vector<Variant<GC::Root<Node>, Utf16String>> const& nodes, Document& document)
|
2022-01-29 20:23:48 +00:00
|
|
|
|
{
|
2025-12-11 16:45:01 +00:00
|
|
|
|
// 1. Replace each string of nodes with a new Text node whose data is the string and node document is document.
|
2025-07-24 12:05:52 -04:00
|
|
|
|
auto potentially_convert_string_to_text_node = [&document](Variant<GC::Root<Node>, Utf16String> const& node) -> GC::Ref<Node> {
|
2024-11-15 04:01:23 +13:00
|
|
|
|
if (node.has<GC::Root<Node>>())
|
|
|
|
|
|
return *node.get<GC::Root<Node>>();
|
2022-01-29 20:23:48 +00:00
|
|
|
|
|
2025-12-11 16:45:01 +00:00
|
|
|
|
return document.realm().create<Text>(document, node.get<Utf16String>());
|
2022-01-29 20:23:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-12-11 16:45:01 +00:00
|
|
|
|
// 2. If nodes’s size is 1, then return nodes[0].
|
2022-01-29 20:23:48 +00:00
|
|
|
|
if (nodes.size() == 1)
|
2023-08-13 13:05:26 +02:00
|
|
|
|
return potentially_convert_string_to_text_node(nodes.first());
|
2022-01-29 20:23:48 +00:00
|
|
|
|
|
2025-12-11 16:45:01 +00:00
|
|
|
|
// 3. Let fragment be a new DocumentFragment node whose node document is document.
|
|
|
|
|
|
auto fragment = document.realm().create<DocumentFragment>(document);
|
|
|
|
|
|
|
|
|
|
|
|
// 4. For each node of nodes: append node to fragment.
|
2023-09-11 18:36:23 +12:00
|
|
|
|
for (auto const& unconverted_node : nodes) {
|
2023-08-13 13:05:26 +02:00
|
|
|
|
auto node = potentially_convert_string_to_text_node(unconverted_node);
|
2025-12-11 16:45:01 +00:00
|
|
|
|
TRY(fragment->append_child(node));
|
2022-01-29 20:23:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-11 16:45:01 +00:00
|
|
|
|
// 5. Return fragment.
|
|
|
|
|
|
return fragment;
|
2022-01-29 20:23:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|