ParentNode.cpp 8.3 KB

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