ParentNode.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2020, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibWeb/CSS/Parser/Parser.h>
  9. #include <LibWeb/CSS/SelectorEngine.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/DOM/HTMLCollection.h>
  12. #include <LibWeb/DOM/NodeOperations.h>
  13. #include <LibWeb/DOM/ParentNode.h>
  14. #include <LibWeb/DOM/StaticNodeList.h>
  15. #include <LibWeb/Dump.h>
  16. #include <LibWeb/Infra/Strings.h>
  17. #include <LibWeb/Namespace.h>
  18. namespace Web::DOM {
  19. // https://dom.spec.whatwg.org/#dom-parentnode-queryselector
  20. WebIDL::ExceptionOr<JS::GCPtr<Element>> ParentNode::query_selector(StringView selector_text)
  21. {
  22. // The querySelector(selectors) method steps are to return the first result of running scope-match a selectors string selectors against this,
  23. // if the result is not an empty list; otherwise null.
  24. // https://dom.spec.whatwg.org/#scope-match-a-selectors-string
  25. // To scope-match a selectors string selectors against a node, run these steps:
  26. // 1. Let s be the result of parse a selector selectors.
  27. auto maybe_selectors = parse_selector(CSS::Parser::ParsingContext(*this), selector_text);
  28. // 2. If s is failure, then throw a "SyntaxError" DOMException.
  29. if (!maybe_selectors.has_value())
  30. return WebIDL::SyntaxError::create(realm(), "Failed to parse selector"_fly_string);
  31. auto selectors = maybe_selectors.value();
  32. // 3. Return the result of match a selector against a tree with s and node’s root using scoping root node.
  33. JS::GCPtr<Element> result;
  34. // FIXME: This should be shadow-including. https://drafts.csswg.org/selectors-4/#match-a-selector-against-a-tree
  35. for_each_in_subtree_of_type<Element>([&](auto& element) {
  36. for (auto& selector : selectors) {
  37. if (SelectorEngine::matches(selector, {}, element, {}, this)) {
  38. result = &element;
  39. return IterationDecision::Break;
  40. }
  41. }
  42. return IterationDecision::Continue;
  43. });
  44. return result;
  45. }
  46. // https://dom.spec.whatwg.org/#dom-parentnode-queryselectorall
  47. WebIDL::ExceptionOr<JS::NonnullGCPtr<NodeList>> ParentNode::query_selector_all(StringView selector_text)
  48. {
  49. // The querySelectorAll(selectors) method steps are to return the static result of running scope-match a selectors string selectors against this.
  50. // https://dom.spec.whatwg.org/#scope-match-a-selectors-string
  51. // To scope-match a selectors string selectors against a node, run these steps:
  52. // 1. Let s be the result of parse a selector selectors.
  53. auto maybe_selectors = parse_selector(CSS::Parser::ParsingContext(*this), selector_text);
  54. // 2. If s is failure, then throw a "SyntaxError" DOMException.
  55. if (!maybe_selectors.has_value())
  56. return WebIDL::SyntaxError::create(realm(), "Failed to parse selector"_fly_string);
  57. auto selectors = maybe_selectors.value();
  58. // 3. Return the result of match a selector against a tree with s and node’s root using scoping root node.
  59. Vector<JS::Handle<Node>> elements;
  60. // FIXME: This should be shadow-including. https://drafts.csswg.org/selectors-4/#match-a-selector-against-a-tree
  61. for_each_in_subtree_of_type<Element>([&](auto& element) {
  62. for (auto& selector : selectors) {
  63. if (SelectorEngine::matches(selector, {}, element, {}, this)) {
  64. elements.append(&element);
  65. }
  66. }
  67. return IterationDecision::Continue;
  68. });
  69. return StaticNodeList::create(realm(), move(elements));
  70. }
  71. JS::GCPtr<Element> ParentNode::first_element_child()
  72. {
  73. return first_child_of_type<Element>();
  74. }
  75. JS::GCPtr<Element> ParentNode::last_element_child()
  76. {
  77. return last_child_of_type<Element>();
  78. }
  79. // https://dom.spec.whatwg.org/#dom-parentnode-childelementcount
  80. u32 ParentNode::child_element_count() const
  81. {
  82. u32 count = 0;
  83. for (auto* child = first_child(); child; child = child->next_sibling()) {
  84. if (is<Element>(child))
  85. ++count;
  86. }
  87. return count;
  88. }
  89. void ParentNode::visit_edges(Cell::Visitor& visitor)
  90. {
  91. Base::visit_edges(visitor);
  92. visitor.visit(m_children);
  93. }
  94. // https://dom.spec.whatwg.org/#dom-parentnode-children
  95. JS::NonnullGCPtr<HTMLCollection> ParentNode::children()
  96. {
  97. // The children getter steps are to return an HTMLCollection collection rooted at this matching only element children.
  98. if (!m_children) {
  99. m_children = HTMLCollection::create(*this, HTMLCollection::Scope::Children, [](Element const&) {
  100. return true;
  101. });
  102. }
  103. return *m_children;
  104. }
  105. // https://dom.spec.whatwg.org/#concept-getelementsbytagname
  106. // NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
  107. JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_tag_name(FlyString const& qualified_name)
  108. {
  109. // 1. If qualifiedName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches only descendant elements.
  110. if (qualified_name == "*") {
  111. return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [](Element const&) {
  112. return true;
  113. });
  114. }
  115. // 2. Otherwise, if root’s node document is an HTML document, return a HTMLCollection rooted at root, whose filter matches the following descendant elements:
  116. if (root().document().document_type() == Document::Type::HTML) {
  117. FlyString qualified_name_in_ascii_lowercase = MUST(Infra::to_ascii_lowercase(qualified_name));
  118. return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [qualified_name, qualified_name_in_ascii_lowercase](Element const& element) {
  119. // - Whose namespace is the HTML namespace and whose qualified name is qualifiedName, in ASCII lowercase.
  120. if (element.namespace_uri() == Namespace::HTML)
  121. return element.qualified_name() == qualified_name_in_ascii_lowercase;
  122. // - Whose namespace is not the HTML namespace and whose qualified name is qualifiedName.
  123. return element.qualified_name() == qualified_name;
  124. });
  125. }
  126. // 3. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose qualified name is qualifiedName.
  127. return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [qualified_name](Element const& element) {
  128. return element.qualified_name() == qualified_name;
  129. });
  130. }
  131. // https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
  132. // NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
  133. JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_tag_name_ns(Optional<String> const& nullable_namespace, FlyString const& local_name)
  134. {
  135. // 1. If namespace is the empty string, set it to null.
  136. Optional<FlyString> namespace_;
  137. if (nullable_namespace.has_value() && !nullable_namespace->is_empty())
  138. namespace_ = nullable_namespace.value();
  139. // 2. If both namespace and localName are "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements.
  140. if (namespace_ == "*" && local_name == "*") {
  141. return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [](Element const&) {
  142. return true;
  143. });
  144. }
  145. // 3. Otherwise, if namespace is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose local name is localName.
  146. if (namespace_ == "*") {
  147. return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [local_name](Element const& element) {
  148. return element.local_name() == local_name;
  149. });
  150. }
  151. // 4. Otherwise, if localName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace.
  152. if (local_name == "*") {
  153. return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [namespace_](Element const& element) {
  154. return element.namespace_uri() == namespace_;
  155. });
  156. }
  157. // 5. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace and local name is localName.
  158. return HTMLCollection::create(*this, HTMLCollection::Scope::Descendants, [namespace_, local_name](Element const& element) {
  159. return element.namespace_uri() == namespace_ && element.local_name() == local_name;
  160. });
  161. }
  162. // https://dom.spec.whatwg.org/#dom-parentnode-prepend
  163. WebIDL::ExceptionOr<void> ParentNode::prepend(Vector<Variant<JS::Handle<Node>, String>> const& nodes)
  164. {
  165. // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
  166. auto node = TRY(convert_nodes_to_single_node(nodes, document()));
  167. // 2. Pre-insert node into this before this’s first child.
  168. (void)TRY(pre_insert(node, first_child()));
  169. return {};
  170. }
  171. WebIDL::ExceptionOr<void> ParentNode::append(Vector<Variant<JS::Handle<Node>, String>> const& nodes)
  172. {
  173. // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
  174. auto node = TRY(convert_nodes_to_single_node(nodes, document()));
  175. // 2. Append node to this.
  176. (void)TRY(append_child(node));
  177. return {};
  178. }
  179. WebIDL::ExceptionOr<void> ParentNode::replace_children(Vector<Variant<JS::Handle<Node>, String>> const& nodes)
  180. {
  181. // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
  182. auto node = TRY(convert_nodes_to_single_node(nodes, document()));
  183. // 2. Ensure pre-insertion validity of node into this before null.
  184. TRY(ensure_pre_insertion_validity(node, nullptr));
  185. // 3. Replace all with node within this.
  186. replace_all(*node);
  187. return {};
  188. }
  189. }