NodeOperations.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2022, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/DeprecatedString.h>
  7. #include <AK/Vector.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/DocumentFragment.h>
  10. #include <LibWeb/DOM/NodeOperations.h>
  11. #include <LibWeb/DOM/Text.h>
  12. namespace Web::DOM {
  13. // https://dom.spec.whatwg.org/#converting-nodes-into-a-node
  14. WebIDL::ExceptionOr<JS::NonnullGCPtr<Node>> convert_nodes_to_single_node(Vector<Variant<JS::Handle<Node>, DeprecatedString>> const& nodes, DOM::Document& document)
  15. {
  16. // 1. Let node be null.
  17. // 2. Replace each string in nodes with a new Text node whose data is the string and node document is document.
  18. // 3. If nodes contains one node, then set node to nodes[0].
  19. // 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.
  20. // 5. Return node.
  21. auto potentially_convert_string_to_text_node = [&document](Variant<JS::Handle<Node>, DeprecatedString> const& node) -> JS::NonnullGCPtr<Node> {
  22. if (node.has<JS::Handle<Node>>())
  23. return *node.get<JS::Handle<Node>>();
  24. return document.heap().allocate<DOM::Text>(document.realm(), document, MUST(String::from_deprecated_string(node.get<DeprecatedString>())));
  25. };
  26. if (nodes.size() == 1)
  27. return potentially_convert_string_to_text_node(nodes.first());
  28. auto document_fragment = document.heap().allocate<DOM::DocumentFragment>(document.realm(), document);
  29. for (auto& unconverted_node : nodes) {
  30. auto node = potentially_convert_string_to_text_node(unconverted_node);
  31. (void)TRY(document_fragment->append_child(node));
  32. }
  33. return document_fragment;
  34. }
  35. }