ParentNode.cpp 7.8 KB

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