ParentNode.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/HTML/Window.h>
  16. #include <LibWeb/Namespace.h>
  17. namespace Web::DOM {
  18. WebIDL::ExceptionOr<JS::GCPtr<Element>> ParentNode::query_selector(StringView selector_text)
  19. {
  20. auto maybe_selectors = parse_selector(CSS::Parser::ParsingContext(*this), selector_text);
  21. if (!maybe_selectors.has_value())
  22. return DOM::SyntaxError::create(global_object(), "Failed to parse selector");
  23. auto selectors = maybe_selectors.value();
  24. JS::GCPtr<Element> result;
  25. // FIXME: This should be shadow-including. https://drafts.csswg.org/selectors-4/#match-a-selector-against-a-tree
  26. for_each_in_subtree_of_type<Element>([&](auto& element) {
  27. for (auto& selector : selectors) {
  28. if (SelectorEngine::matches(selector, element)) {
  29. result = &element;
  30. return IterationDecision::Break;
  31. }
  32. }
  33. return IterationDecision::Continue;
  34. });
  35. return result;
  36. }
  37. WebIDL::ExceptionOr<JS::NonnullGCPtr<NodeList>> ParentNode::query_selector_all(StringView selector_text)
  38. {
  39. auto maybe_selectors = parse_selector(CSS::Parser::ParsingContext(*this), selector_text);
  40. if (!maybe_selectors.has_value())
  41. return DOM::SyntaxError::create(global_object(), "Failed to parse selector");
  42. auto selectors = maybe_selectors.value();
  43. Vector<JS::Handle<Node>> elements;
  44. // FIXME: This should be shadow-including. https://drafts.csswg.org/selectors-4/#match-a-selector-against-a-tree
  45. for_each_in_subtree_of_type<Element>([&](auto& element) {
  46. for (auto& selector : selectors) {
  47. if (SelectorEngine::matches(selector, element)) {
  48. elements.append(&element);
  49. }
  50. }
  51. return IterationDecision::Continue;
  52. });
  53. return StaticNodeList::create(window(), move(elements));
  54. }
  55. JS::GCPtr<Element> ParentNode::first_element_child()
  56. {
  57. return first_child_of_type<Element>();
  58. }
  59. JS::GCPtr<Element> ParentNode::last_element_child()
  60. {
  61. return last_child_of_type<Element>();
  62. }
  63. // https://dom.spec.whatwg.org/#dom-parentnode-childelementcount
  64. u32 ParentNode::child_element_count() const
  65. {
  66. u32 count = 0;
  67. for (auto* child = first_child(); child; child = child->next_sibling()) {
  68. if (is<Element>(child))
  69. ++count;
  70. }
  71. return count;
  72. }
  73. void ParentNode::visit_edges(Cell::Visitor& visitor)
  74. {
  75. Base::visit_edges(visitor);
  76. visitor.visit(m_children);
  77. }
  78. // https://dom.spec.whatwg.org/#dom-parentnode-children
  79. JS::NonnullGCPtr<HTMLCollection> ParentNode::children()
  80. {
  81. // The children getter steps are to return an HTMLCollection collection rooted at this matching only element children.
  82. if (!m_children) {
  83. m_children = HTMLCollection::create(*this, [this](Element const& element) {
  84. return is_parent_of(element);
  85. });
  86. }
  87. return *m_children;
  88. }
  89. // https://dom.spec.whatwg.org/#concept-getelementsbytagname
  90. // NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
  91. JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_tag_name(FlyString const& qualified_name)
  92. {
  93. // 1. If qualifiedName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches only descendant elements.
  94. if (qualified_name == "*") {
  95. return HTMLCollection::create(*this, [](Element const&) {
  96. return true;
  97. });
  98. }
  99. // 2. Otherwise, if root’s node document is an HTML document, return a HTMLCollection rooted at root, whose filter matches the following descendant elements:
  100. if (root().document().document_type() == Document::Type::HTML) {
  101. return HTMLCollection::create(*this, [qualified_name](Element const& element) {
  102. // - Whose namespace is the HTML namespace and whose qualified name is qualifiedName, in ASCII lowercase.
  103. if (element.namespace_() == Namespace::HTML)
  104. return element.qualified_name().to_lowercase() == qualified_name.to_lowercase();
  105. // - Whose namespace is not the HTML namespace and whose qualified name is qualifiedName.
  106. return element.qualified_name() == qualified_name;
  107. });
  108. }
  109. // 3. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose qualified name is qualifiedName.
  110. return HTMLCollection::create(*this, [qualified_name](Element const& element) {
  111. return element.qualified_name() == qualified_name;
  112. });
  113. }
  114. // https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
  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_ns(FlyString const& nullable_namespace, FlyString const& local_name)
  117. {
  118. // 1. If namespace is the empty string, set it to null.
  119. String namespace_ = nullable_namespace;
  120. if (namespace_.is_empty())
  121. namespace_ = {};
  122. // 2. If both namespace and localName are "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements.
  123. if (namespace_ == "*" && local_name == "*") {
  124. return HTMLCollection::create(*this, [](Element const&) {
  125. return true;
  126. });
  127. }
  128. // 3. Otherwise, if namespace is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose local name is localName.
  129. if (namespace_ == "*") {
  130. return HTMLCollection::create(*this, [local_name](Element const& element) {
  131. return element.local_name() == local_name;
  132. });
  133. }
  134. // 4. Otherwise, if localName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace.
  135. if (local_name == "*") {
  136. return HTMLCollection::create(*this, [namespace_](Element const& element) {
  137. return element.namespace_() == namespace_;
  138. });
  139. }
  140. // 5. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace and local name is localName.
  141. return HTMLCollection::create(*this, [namespace_, local_name](Element const& element) {
  142. return element.namespace_() == namespace_ && element.local_name() == local_name;
  143. });
  144. }
  145. // https://dom.spec.whatwg.org/#dom-parentnode-prepend
  146. WebIDL::ExceptionOr<void> ParentNode::prepend(Vector<Variant<JS::Handle<Node>, String>> const& nodes)
  147. {
  148. // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
  149. auto node = TRY(convert_nodes_to_single_node(nodes, document()));
  150. // 2. Pre-insert node into this before this’s first child.
  151. (void)TRY(pre_insert(node, first_child()));
  152. return {};
  153. }
  154. WebIDL::ExceptionOr<void> ParentNode::append(Vector<Variant<JS::Handle<Node>, String>> const& nodes)
  155. {
  156. // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
  157. auto node = TRY(convert_nodes_to_single_node(nodes, document()));
  158. // 2. Append node to this.
  159. (void)TRY(append_child(node));
  160. return {};
  161. }
  162. WebIDL::ExceptionOr<void> ParentNode::replace_children(Vector<Variant<JS::Handle<Node>, String>> const& nodes)
  163. {
  164. // 1. Let node be the result of converting nodes into a node given nodes and this’s node document.
  165. auto node = TRY(convert_nodes_to_single_node(nodes, document()));
  166. // 2. Ensure pre-insertion validity of node into this before null.
  167. TRY(ensure_pre_insertion_validity(node, nullptr));
  168. // 3. Replace all with node within this.
  169. replace_all(*node);
  170. return {};
  171. }
  172. }