ParentNode.cpp 11 KB

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