ParentNode.cpp 6.2 KB

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