SelectorEngine.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/SelectorEngine.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/DOM/Element.h>
  9. #include <LibWeb/DOM/Text.h>
  10. #include <LibWeb/HTML/AttributeNames.h>
  11. #include <LibWeb/HTML/HTMLElement.h>
  12. namespace Web::SelectorEngine {
  13. static bool matches_hover_pseudo_class(const DOM::Element& element)
  14. {
  15. auto* hovered_node = element.document().hovered_node();
  16. if (!hovered_node)
  17. return false;
  18. if (&element == hovered_node)
  19. return true;
  20. return element.is_ancestor_of(*hovered_node);
  21. }
  22. static bool matches(const CSS::Selector::SimpleSelector& component, const DOM::Element& element)
  23. {
  24. switch (component.pseudo_element) {
  25. case CSS::Selector::SimpleSelector::PseudoElement::None:
  26. break;
  27. default:
  28. // FIXME: Implement pseudo-elements.
  29. return false;
  30. }
  31. switch (component.pseudo_class) {
  32. case CSS::Selector::SimpleSelector::PseudoClass::None:
  33. break;
  34. case CSS::Selector::SimpleSelector::PseudoClass::Link:
  35. if (!element.is_link())
  36. return false;
  37. break;
  38. case CSS::Selector::SimpleSelector::PseudoClass::Visited:
  39. // FIXME: Maybe match this selector sometimes?
  40. return false;
  41. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  42. if (!matches_hover_pseudo_class(element))
  43. return false;
  44. break;
  45. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  46. // FIXME: Implement matches_focus_pseudo_class(element)
  47. return false;
  48. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  49. if (element.previous_element_sibling())
  50. return false;
  51. break;
  52. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  53. if (element.next_element_sibling())
  54. return false;
  55. break;
  56. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  57. if (element.previous_element_sibling() || element.next_element_sibling())
  58. return false;
  59. break;
  60. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  61. if (element.first_child_of_type<DOM::Element>() || element.first_child_of_type<DOM::Text>())
  62. return false;
  63. break;
  64. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  65. if (!is<HTML::HTMLElement>(element))
  66. return false;
  67. break;
  68. case CSS::Selector::SimpleSelector::PseudoClass::FirstOfType:
  69. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  70. if (sibling->tag_name() == element.tag_name())
  71. return false;
  72. }
  73. break;
  74. case CSS::Selector::SimpleSelector::PseudoClass::LastOfType:
  75. for (auto* sibling = element.next_element_sibling(); sibling; sibling = sibling->next_element_sibling()) {
  76. if (sibling->tag_name() == element.tag_name())
  77. return false;
  78. }
  79. break;
  80. }
  81. switch (component.attribute_match_type) {
  82. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  83. if (!element.has_attribute(component.attribute_name))
  84. return false;
  85. break;
  86. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  87. if (element.attribute(component.attribute_name) != component.attribute_value)
  88. return false;
  89. break;
  90. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  91. if (!element.attribute(component.attribute_name).split(' ').contains_slow(component.attribute_value))
  92. return false;
  93. break;
  94. default:
  95. break;
  96. }
  97. switch (component.type) {
  98. case CSS::Selector::SimpleSelector::Type::Universal:
  99. return true;
  100. case CSS::Selector::SimpleSelector::Type::Id:
  101. return component.value == element.attribute(HTML::AttributeNames::id);
  102. case CSS::Selector::SimpleSelector::Type::Class:
  103. return element.has_class(component.value);
  104. case CSS::Selector::SimpleSelector::Type::TagName:
  105. return component.value == element.local_name();
  106. default:
  107. VERIFY_NOT_REACHED();
  108. }
  109. }
  110. static bool matches(const CSS::Selector& selector, int component_list_index, const DOM::Element& element)
  111. {
  112. auto& component_list = selector.complex_selectors()[component_list_index];
  113. for (auto& component : component_list.compound_selector) {
  114. if (!matches(component, element))
  115. return false;
  116. }
  117. switch (component_list.relation) {
  118. case CSS::Selector::ComplexSelector::Relation::None:
  119. return true;
  120. case CSS::Selector::ComplexSelector::Relation::Descendant:
  121. VERIFY(component_list_index != 0);
  122. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  123. if (!is<DOM::Element>(*ancestor))
  124. continue;
  125. if (matches(selector, component_list_index - 1, downcast<DOM::Element>(*ancestor)))
  126. return true;
  127. }
  128. return false;
  129. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  130. VERIFY(component_list_index != 0);
  131. if (!element.parent() || !is<DOM::Element>(*element.parent()))
  132. return false;
  133. return matches(selector, component_list_index - 1, downcast<DOM::Element>(*element.parent()));
  134. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  135. VERIFY(component_list_index != 0);
  136. if (auto* sibling = element.previous_element_sibling())
  137. return matches(selector, component_list_index - 1, *sibling);
  138. return false;
  139. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  140. VERIFY(component_list_index != 0);
  141. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  142. if (matches(selector, component_list_index - 1, *sibling))
  143. return true;
  144. }
  145. return false;
  146. case CSS::Selector::ComplexSelector::Relation::Column:
  147. TODO();
  148. }
  149. VERIFY_NOT_REACHED();
  150. }
  151. bool matches(const CSS::Selector& selector, const DOM::Element& element)
  152. {
  153. VERIFY(!selector.complex_selectors().is_empty());
  154. return matches(selector, selector.complex_selectors().size() - 1, element);
  155. }
  156. }