/* * Copyright (c) 2022, Luke Wilde * Copyright (c) 2023, Shannon Booth * Copyright (c) 2025, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include namespace Web::DOM { // https://dom.spec.whatwg.org/#convert-nodes-into-a-node WebIDL::ExceptionOr> convert_nodes_to_single_node(Vector, Utf16String>> const& nodes, Document& document) { // 1. Replace each string of nodes with a new Text node whose data is the string and node document is document. auto potentially_convert_string_to_text_node = [&document](Variant, Utf16String> const& node) -> GC::Ref { if (node.has>()) return *node.get>(); return document.realm().create(document, node.get()); }; // 2. If nodes’s size is 1, then return nodes[0]. if (nodes.size() == 1) return potentially_convert_string_to_text_node(nodes.first()); // 3. Let fragment be a new DocumentFragment node whose node document is document. auto fragment = document.realm().create(document); // 4. For each node of nodes: append node to fragment. for (auto const& unconverted_node : nodes) { auto node = potentially_convert_string_to_text_node(unconverted_node); TRY(fragment->append_child(node)); } // 5. Return fragment. return fragment; } }