SelectorEngine.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/CSS/Parser/DeprecatedCSSParser.h>
  7. #include <LibWeb/CSS/SelectorEngine.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Element.h>
  10. #include <LibWeb/DOM/Text.h>
  11. #include <LibWeb/HTML/AttributeNames.h>
  12. #include <LibWeb/HTML/HTMLElement.h>
  13. namespace Web::SelectorEngine {
  14. static bool matches_hover_pseudo_class(const DOM::Element& element)
  15. {
  16. auto* hovered_node = element.document().hovered_node();
  17. if (!hovered_node)
  18. return false;
  19. if (&element == hovered_node)
  20. return true;
  21. return element.is_ancestor_of(*hovered_node);
  22. }
  23. static bool matches(const CSS::Selector::SimpleSelector& component, const DOM::Element& element)
  24. {
  25. switch (component.pseudo_element) {
  26. case CSS::Selector::SimpleSelector::PseudoElement::None:
  27. break;
  28. default:
  29. // FIXME: Implement pseudo-elements.
  30. return false;
  31. }
  32. switch (component.pseudo_class) {
  33. case CSS::Selector::SimpleSelector::PseudoClass::None:
  34. break;
  35. case CSS::Selector::SimpleSelector::PseudoClass::Link:
  36. if (!element.is_link())
  37. return false;
  38. break;
  39. case CSS::Selector::SimpleSelector::PseudoClass::Visited:
  40. // FIXME: Maybe match this selector sometimes?
  41. return false;
  42. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  43. if (!matches_hover_pseudo_class(element))
  44. return false;
  45. break;
  46. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  47. // FIXME: Implement matches_focus_pseudo_class(element)
  48. return false;
  49. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  50. if (element.previous_element_sibling())
  51. return false;
  52. break;
  53. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  54. if (element.next_element_sibling())
  55. return false;
  56. break;
  57. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  58. if (element.previous_element_sibling() || element.next_element_sibling())
  59. return false;
  60. break;
  61. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  62. if (element.first_child_of_type<DOM::Element>() || element.first_child_of_type<DOM::Text>())
  63. return false;
  64. break;
  65. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  66. if (!is<HTML::HTMLElement>(element))
  67. return false;
  68. break;
  69. case CSS::Selector::SimpleSelector::PseudoClass::FirstOfType:
  70. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  71. if (sibling->tag_name() == element.tag_name())
  72. return false;
  73. }
  74. break;
  75. case CSS::Selector::SimpleSelector::PseudoClass::LastOfType:
  76. for (auto* sibling = element.next_element_sibling(); sibling; sibling = sibling->next_element_sibling()) {
  77. if (sibling->tag_name() == element.tag_name())
  78. return false;
  79. }
  80. break;
  81. case CSS::Selector::SimpleSelector::PseudoClass::Disabled:
  82. if (!element.tag_name().equals_ignoring_case(HTML::TagNames::input))
  83. return false;
  84. if (!element.has_attribute("disabled"))
  85. return false;
  86. break;
  87. case CSS::Selector::SimpleSelector::PseudoClass::Enabled:
  88. if (!element.tag_name().equals_ignoring_case(HTML::TagNames::input))
  89. return false;
  90. if (element.has_attribute("disabled"))
  91. return false;
  92. break;
  93. case CSS::Selector::SimpleSelector::PseudoClass::Checked:
  94. if (!element.tag_name().equals_ignoring_case(HTML::TagNames::input))
  95. return false;
  96. if (!element.has_attribute("checked"))
  97. return false;
  98. break;
  99. case CSS::Selector::SimpleSelector::PseudoClass::Not: {
  100. if (component.not_selector.is_empty())
  101. return false;
  102. auto not_selector = Web::parse_selector(CSS::ParsingContext(element), component.not_selector);
  103. if (!not_selector.has_value())
  104. return false;
  105. auto not_matches = matches(not_selector.value(), element);
  106. if (not_matches)
  107. return false;
  108. break;
  109. }
  110. case CSS::Selector::SimpleSelector::PseudoClass::NthChild:
  111. case CSS::Selector::SimpleSelector::PseudoClass::NthLastChild:
  112. const auto step_size = component.nth_child_pattern.step_size;
  113. const auto offset = component.nth_child_pattern.offset;
  114. if (step_size == 0 && offset == 0)
  115. return false; // "If both a and b are equal to zero, the pseudo-class represents no element in the document tree."
  116. const auto* parent = element.parent_element();
  117. if (!parent)
  118. return false;
  119. int index = 1;
  120. if (component.pseudo_class == CSS::Selector::SimpleSelector::PseudoClass::NthChild) {
  121. for (auto* child = parent->first_child_of_type<DOM::Element>(); child && child != &element; child = child->next_element_sibling())
  122. ++index;
  123. } else {
  124. for (auto* child = parent->last_child_of_type<DOM::Element>(); child && child != &element; child = child->previous_element_sibling())
  125. ++index;
  126. }
  127. if (step_size < 0) {
  128. // When "step_size" is negative, selector represents first "offset" elements in document tree.
  129. if (offset <= 0 || index > offset)
  130. return false;
  131. else
  132. break;
  133. } else if (step_size == 1) {
  134. // When "step_size == 1", selector represents last "offset" elements in document tree.
  135. if (offset < 0 || index < offset)
  136. return false;
  137. else
  138. break;
  139. }
  140. // Like "a % b", but handles negative integers correctly.
  141. const auto canonical_modulo = [](int a, int b) -> int {
  142. int c = a % b;
  143. if ((c < 0 && b > 0) || (c > 0 && b < 0)) {
  144. c += b;
  145. }
  146. return c;
  147. };
  148. if (step_size == 0) {
  149. // Avoid divide by zero.
  150. if (index != offset) {
  151. return false;
  152. }
  153. } else if (canonical_modulo(index - offset, step_size) != 0) {
  154. return false;
  155. }
  156. break;
  157. }
  158. switch (component.attribute_match_type) {
  159. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  160. if (!element.has_attribute(component.attribute_name))
  161. return false;
  162. break;
  163. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  164. if (element.attribute(component.attribute_name) != component.attribute_value)
  165. return false;
  166. break;
  167. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  168. if (!element.attribute(component.attribute_name).split(' ').contains_slow(component.attribute_value))
  169. return false;
  170. break;
  171. default:
  172. break;
  173. }
  174. switch (component.type) {
  175. case CSS::Selector::SimpleSelector::Type::Universal:
  176. return true;
  177. case CSS::Selector::SimpleSelector::Type::Id:
  178. return component.value == element.attribute(HTML::AttributeNames::id);
  179. case CSS::Selector::SimpleSelector::Type::Class:
  180. return element.has_class(component.value);
  181. case CSS::Selector::SimpleSelector::Type::TagName:
  182. return component.value == element.local_name();
  183. default:
  184. VERIFY_NOT_REACHED();
  185. }
  186. }
  187. static bool matches(const CSS::Selector& selector, int component_list_index, const DOM::Element& element)
  188. {
  189. auto& component_list = selector.complex_selectors()[component_list_index];
  190. for (auto& component : component_list.compound_selector) {
  191. if (!matches(component, element))
  192. return false;
  193. }
  194. switch (component_list.relation) {
  195. case CSS::Selector::ComplexSelector::Relation::None:
  196. return true;
  197. case CSS::Selector::ComplexSelector::Relation::Descendant:
  198. VERIFY(component_list_index != 0);
  199. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  200. if (!is<DOM::Element>(*ancestor))
  201. continue;
  202. if (matches(selector, component_list_index - 1, downcast<DOM::Element>(*ancestor)))
  203. return true;
  204. }
  205. return false;
  206. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  207. VERIFY(component_list_index != 0);
  208. if (!element.parent() || !is<DOM::Element>(*element.parent()))
  209. return false;
  210. return matches(selector, component_list_index - 1, downcast<DOM::Element>(*element.parent()));
  211. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  212. VERIFY(component_list_index != 0);
  213. if (auto* sibling = element.previous_element_sibling())
  214. return matches(selector, component_list_index - 1, *sibling);
  215. return false;
  216. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  217. VERIFY(component_list_index != 0);
  218. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  219. if (matches(selector, component_list_index - 1, *sibling))
  220. return true;
  221. }
  222. return false;
  223. case CSS::Selector::ComplexSelector::Relation::Column:
  224. TODO();
  225. }
  226. VERIFY_NOT_REACHED();
  227. }
  228. bool matches(const CSS::Selector& selector, const DOM::Element& element)
  229. {
  230. VERIFY(!selector.complex_selectors().is_empty());
  231. return matches(selector, selector.complex_selectors().size() - 1, element);
  232. }
  233. }