/* * Copyright (c) 2022, Luke Wilde * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include namespace Web::DOM { // https://dom.spec.whatwg.org/#converting-nodes-into-a-node 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. // 3. If nodes contains one node, then set node to nodes[0]. // 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, String> const& node) -> NonnullRefPtr { if (node.has>()) return node.get>(); return adopt_ref(*new Text(document, node.get())); }; if (nodes.size() == 1) return potentially_convert_string_to_text_node(nodes.first()); // This is NNRP instead of NNRP to be compatible with the return type. NonnullRefPtr document_fragment = adopt_ref(*new DocumentFragment(document)); for (auto& unconverted_node : nodes) { auto node = potentially_convert_string_to_text_node(unconverted_node); (void)TRY(document_fragment->append_child(node)); } return document_fragment; } }