SelectorEngine.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. case CSS::Selector::SimpleSelector::PseudoClass::NthChild:
  81. const auto step_size = component.nth_child_pattern.step_size;
  82. const auto offset = component.nth_child_pattern.offset;
  83. if (step_size == 0 && offset == 0)
  84. return false; // "If both a and b are equal to zero, the pseudo-class represents no element in the document tree."
  85. const auto* parent = element.parent_element();
  86. if (!parent)
  87. return false;
  88. int index = 1;
  89. for (auto* child = parent->first_child_of_type<DOM::Element>(); child && child != &element; child = child->next_element_sibling()) {
  90. ++index;
  91. }
  92. if (step_size < 0) {
  93. // When "step_size" is negative, selector represents first "offset" elements in document tree.
  94. if (offset <= 0 || index > offset)
  95. return false;
  96. else
  97. break;
  98. } else if (step_size == 1) {
  99. // When "step_size == 1", selector represents last "offset" elements in document tree.
  100. if (offset < 0 || index < offset)
  101. return false;
  102. else
  103. break;
  104. }
  105. // Like "a % b", but handles negative integers correctly.
  106. const auto canonical_modulo = [](int a, int b) -> int {
  107. int c = a % b;
  108. if ((c < 0 && b > 0) || (c > 0 && b < 0)) {
  109. c += b;
  110. }
  111. return c;
  112. };
  113. if (step_size == 0) {
  114. // Avoid divide by zero.
  115. if (index != offset) {
  116. return false;
  117. }
  118. } else if (canonical_modulo(index - offset, step_size) != 0) {
  119. return false;
  120. }
  121. break;
  122. }
  123. switch (component.attribute_match_type) {
  124. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  125. if (!element.has_attribute(component.attribute_name))
  126. return false;
  127. break;
  128. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  129. if (element.attribute(component.attribute_name) != component.attribute_value)
  130. return false;
  131. break;
  132. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  133. if (!element.attribute(component.attribute_name).split(' ').contains_slow(component.attribute_value))
  134. return false;
  135. break;
  136. default:
  137. break;
  138. }
  139. switch (component.type) {
  140. case CSS::Selector::SimpleSelector::Type::Universal:
  141. return true;
  142. case CSS::Selector::SimpleSelector::Type::Id:
  143. return component.value == element.attribute(HTML::AttributeNames::id);
  144. case CSS::Selector::SimpleSelector::Type::Class:
  145. return element.has_class(component.value);
  146. case CSS::Selector::SimpleSelector::Type::TagName:
  147. return component.value == element.local_name();
  148. default:
  149. VERIFY_NOT_REACHED();
  150. }
  151. }
  152. static bool matches(const CSS::Selector& selector, int component_list_index, const DOM::Element& element)
  153. {
  154. auto& component_list = selector.complex_selectors()[component_list_index];
  155. for (auto& component : component_list.compound_selector) {
  156. if (!matches(component, element))
  157. return false;
  158. }
  159. switch (component_list.relation) {
  160. case CSS::Selector::ComplexSelector::Relation::None:
  161. return true;
  162. case CSS::Selector::ComplexSelector::Relation::Descendant:
  163. VERIFY(component_list_index != 0);
  164. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  165. if (!is<DOM::Element>(*ancestor))
  166. continue;
  167. if (matches(selector, component_list_index - 1, downcast<DOM::Element>(*ancestor)))
  168. return true;
  169. }
  170. return false;
  171. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  172. VERIFY(component_list_index != 0);
  173. if (!element.parent() || !is<DOM::Element>(*element.parent()))
  174. return false;
  175. return matches(selector, component_list_index - 1, downcast<DOM::Element>(*element.parent()));
  176. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  177. VERIFY(component_list_index != 0);
  178. if (auto* sibling = element.previous_element_sibling())
  179. return matches(selector, component_list_index - 1, *sibling);
  180. return false;
  181. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  182. VERIFY(component_list_index != 0);
  183. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  184. if (matches(selector, component_list_index - 1, *sibling))
  185. return true;
  186. }
  187. return false;
  188. case CSS::Selector::ComplexSelector::Relation::Column:
  189. TODO();
  190. }
  191. VERIFY_NOT_REACHED();
  192. }
  193. bool matches(const CSS::Selector& selector, const DOM::Element& element)
  194. {
  195. VERIFY(!selector.complex_selectors().is_empty());
  196. return matches(selector, selector.complex_selectors().size() - 1, element);
  197. }
  198. }