ParentNode.cpp 7.9 KB

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