ParentNode.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/ParentNode.h>
  10. #include <LibWeb/DOM/StaticNodeList.h>
  11. #include <LibWeb/Dump.h>
  12. #include <LibWeb/Namespace.h>
  13. namespace Web::DOM {
  14. ExceptionOr<RefPtr<Element>> ParentNode::query_selector(StringView selector_text)
  15. {
  16. auto maybe_selectors = parse_selector(CSS::ParsingContext(*this), selector_text);
  17. if (!maybe_selectors.has_value())
  18. return DOM::SyntaxError::create("Failed to parse selector");
  19. auto selectors = maybe_selectors.value();
  20. RefPtr<Element> result;
  21. for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  22. for (auto& selector : selectors) {
  23. if (SelectorEngine::matches(selector, element)) {
  24. result = element;
  25. return IterationDecision::Break;
  26. }
  27. }
  28. return IterationDecision::Continue;
  29. });
  30. return result;
  31. }
  32. ExceptionOr<NonnullRefPtr<NodeList>> ParentNode::query_selector_all(StringView selector_text)
  33. {
  34. auto maybe_selectors = parse_selector(CSS::ParsingContext(*this), selector_text);
  35. if (!maybe_selectors.has_value())
  36. return DOM::SyntaxError::create("Failed to parse selector");
  37. auto selectors = maybe_selectors.value();
  38. NonnullRefPtrVector<Node> elements;
  39. for_each_in_inclusive_subtree_of_type<Element>([&](auto& element) {
  40. for (auto& selector : selectors) {
  41. if (SelectorEngine::matches(selector, element)) {
  42. elements.append(element);
  43. }
  44. }
  45. return IterationDecision::Continue;
  46. });
  47. return StaticNodeList::create(move(elements));
  48. }
  49. RefPtr<Element> ParentNode::first_element_child()
  50. {
  51. return first_child_of_type<Element>();
  52. }
  53. RefPtr<Element> ParentNode::last_element_child()
  54. {
  55. return last_child_of_type<Element>();
  56. }
  57. // https://dom.spec.whatwg.org/#dom-parentnode-childelementcount
  58. u32 ParentNode::child_element_count() const
  59. {
  60. u32 count = 0;
  61. for (auto* child = first_child(); child; child = child->next_sibling()) {
  62. if (is<Element>(child))
  63. ++count;
  64. }
  65. return count;
  66. }
  67. // https://dom.spec.whatwg.org/#dom-parentnode-children
  68. NonnullRefPtr<HTMLCollection> ParentNode::children()
  69. {
  70. // The children getter steps are to return an HTMLCollection collection rooted at this matching only element children.
  71. // FIXME: This should return the same HTMLCollection object every time,
  72. // but that would cause a reference cycle since HTMLCollection refs the root.
  73. return HTMLCollection::create(*this, [this](Element const& element) {
  74. return is_parent_of(element);
  75. });
  76. }
  77. // https://dom.spec.whatwg.org/#concept-getelementsbytagname
  78. // NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
  79. NonnullRefPtr<HTMLCollection> ParentNode::get_elements_by_tag_name(FlyString const& qualified_name)
  80. {
  81. // 1. If qualifiedName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches only descendant elements.
  82. if (qualified_name == "*") {
  83. return HTMLCollection::create(*this, [this](Element const& element) {
  84. return element.is_descendant_of(*this);
  85. });
  86. }
  87. // 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:
  88. // (It is currently always a HTML document)
  89. return HTMLCollection::create(*this, [this, qualified_name](Element const& element) {
  90. if (!element.is_descendant_of(*this))
  91. return false;
  92. // - Whose namespace is the HTML namespace and whose qualified name is qualifiedName, in ASCII lowercase.
  93. if (element.namespace_() == Namespace::HTML)
  94. return element.qualified_name().to_lowercase() == qualified_name.to_lowercase();
  95. // - Whose namespace is not the HTML namespace and whose qualified name is qualifiedName.
  96. return element.qualified_name() == qualified_name;
  97. });
  98. // FIXME: 3. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose qualified name is qualifiedName.
  99. }
  100. // https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
  101. // NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
  102. NonnullRefPtr<HTMLCollection> ParentNode::get_elements_by_tag_name_ns(FlyString const& nullable_namespace, FlyString const& local_name)
  103. {
  104. // 1. If namespace is the empty string, set it to null.
  105. String namespace_ = nullable_namespace;
  106. if (namespace_.is_empty())
  107. namespace_ = {};
  108. // 2. If both namespace and localName are "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements.
  109. if (namespace_ == "*" && local_name == "*") {
  110. return HTMLCollection::create(*this, [this](Element const& element) {
  111. return element.is_descendant_of(*this);
  112. });
  113. }
  114. // 3. Otherwise, if namespace is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose local name is localName.
  115. if (namespace_ == "*") {
  116. return HTMLCollection::create(*this, [this, local_name](Element const& element) {
  117. return element.is_descendant_of(*this) && element.local_name() == local_name;
  118. });
  119. }
  120. // 4. Otherwise, if localName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace.
  121. if (local_name == "*") {
  122. return HTMLCollection::create(*this, [this, namespace_](Element const& element) {
  123. return element.is_descendant_of(*this) && element.namespace_() == namespace_;
  124. });
  125. }
  126. // 5. Otherwise, return a HTMLCollection rooted at root, whose filter matches descendant elements whose namespace is namespace and local name is localName.
  127. return HTMLCollection::create(*this, [this, namespace_, local_name](Element const& element) {
  128. return element.is_descendant_of(*this) && element.namespace_() == namespace_ && element.local_name() == local_name;
  129. });
  130. }
  131. }