SelectorEngine.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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::Active:
  43. if (!element.is_active())
  44. return false;
  45. break;
  46. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  47. if (!matches_hover_pseudo_class(element))
  48. return false;
  49. break;
  50. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  51. if (!element.is_focused())
  52. return false;
  53. break;
  54. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  55. if (element.previous_element_sibling())
  56. return false;
  57. break;
  58. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  59. if (element.next_element_sibling())
  60. return false;
  61. break;
  62. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  63. if (element.previous_element_sibling() || element.next_element_sibling())
  64. return false;
  65. break;
  66. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  67. if (element.first_child_of_type<DOM::Element>() || element.first_child_of_type<DOM::Text>())
  68. return false;
  69. break;
  70. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  71. if (!is<HTML::HTMLElement>(element))
  72. return false;
  73. break;
  74. case CSS::Selector::SimpleSelector::PseudoClass::FirstOfType:
  75. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  76. if (sibling->tag_name() == element.tag_name())
  77. return false;
  78. }
  79. break;
  80. case CSS::Selector::SimpleSelector::PseudoClass::LastOfType:
  81. for (auto* sibling = element.next_element_sibling(); sibling; sibling = sibling->next_element_sibling()) {
  82. if (sibling->tag_name() == element.tag_name())
  83. return false;
  84. }
  85. break;
  86. case CSS::Selector::SimpleSelector::PseudoClass::Disabled:
  87. if (!element.tag_name().equals_ignoring_case(HTML::TagNames::input))
  88. return false;
  89. if (!element.has_attribute("disabled"))
  90. return false;
  91. break;
  92. case CSS::Selector::SimpleSelector::PseudoClass::Enabled:
  93. if (!element.tag_name().equals_ignoring_case(HTML::TagNames::input))
  94. return false;
  95. if (element.has_attribute("disabled"))
  96. return false;
  97. break;
  98. case CSS::Selector::SimpleSelector::PseudoClass::Checked:
  99. if (!element.tag_name().equals_ignoring_case(HTML::TagNames::input))
  100. return false;
  101. if (!element.has_attribute("checked"))
  102. return false;
  103. break;
  104. case CSS::Selector::SimpleSelector::PseudoClass::Not: {
  105. if (component.not_selector.is_empty())
  106. return false;
  107. auto not_selector = Web::parse_selector(CSS::DeprecatedParsingContext(element), component.not_selector);
  108. if (!not_selector.has_value())
  109. return false;
  110. auto not_matches = matches(not_selector.value(), element);
  111. if (not_matches)
  112. return false;
  113. break;
  114. }
  115. case CSS::Selector::SimpleSelector::PseudoClass::NthChild:
  116. case CSS::Selector::SimpleSelector::PseudoClass::NthLastChild:
  117. const auto step_size = component.nth_child_pattern.step_size;
  118. const auto offset = component.nth_child_pattern.offset;
  119. if (step_size == 0 && offset == 0)
  120. return false; // "If both a and b are equal to zero, the pseudo-class represents no element in the document tree."
  121. const auto* parent = element.parent_element();
  122. if (!parent)
  123. return false;
  124. int index = 1;
  125. if (component.pseudo_class == CSS::Selector::SimpleSelector::PseudoClass::NthChild) {
  126. for (auto* child = parent->first_child_of_type<DOM::Element>(); child && child != &element; child = child->next_element_sibling())
  127. ++index;
  128. } else {
  129. for (auto* child = parent->last_child_of_type<DOM::Element>(); child && child != &element; child = child->previous_element_sibling())
  130. ++index;
  131. }
  132. if (step_size < 0) {
  133. // When "step_size" is negative, selector represents first "offset" elements in document tree.
  134. if (offset <= 0 || index > offset)
  135. return false;
  136. else
  137. break;
  138. } else if (step_size == 1) {
  139. // When "step_size == 1", selector represents last "offset" elements in document tree.
  140. if (offset < 0 || index < offset)
  141. return false;
  142. else
  143. break;
  144. }
  145. // Like "a % b", but handles negative integers correctly.
  146. const auto canonical_modulo = [](int a, int b) -> int {
  147. int c = a % b;
  148. if ((c < 0 && b > 0) || (c > 0 && b < 0)) {
  149. c += b;
  150. }
  151. return c;
  152. };
  153. if (step_size == 0) {
  154. // Avoid divide by zero.
  155. if (index != offset) {
  156. return false;
  157. }
  158. } else if (canonical_modulo(index - offset, step_size) != 0) {
  159. return false;
  160. }
  161. break;
  162. }
  163. switch (component.attribute_match_type) {
  164. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  165. if (!element.has_attribute(component.attribute_name))
  166. return false;
  167. break;
  168. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  169. if (element.attribute(component.attribute_name) != component.attribute_value)
  170. return false;
  171. break;
  172. case CSS::Selector::SimpleSelector::AttributeMatchType::ContainsWord:
  173. if (!element.attribute(component.attribute_name).split(' ').contains_slow(component.attribute_value))
  174. return false;
  175. break;
  176. case CSS::Selector::SimpleSelector::AttributeMatchType::ContainsString:
  177. if (!element.attribute(component.attribute_name).contains(component.attribute_value))
  178. return false;
  179. break;
  180. case CSS::Selector::SimpleSelector::AttributeMatchType::StartsWithSegment:
  181. if (element.attribute(component.attribute_name).split('-').first() != component.attribute_value)
  182. return false;
  183. break;
  184. case CSS::Selector::SimpleSelector::AttributeMatchType::StartsWithString:
  185. if (!element.attribute(component.attribute_name).starts_with(component.attribute_value))
  186. return false;
  187. break;
  188. case CSS::Selector::SimpleSelector::AttributeMatchType::EndsWithString:
  189. if (!element.attribute(component.attribute_name).ends_with(component.attribute_value))
  190. return false;
  191. break;
  192. default:
  193. break;
  194. }
  195. switch (component.type) {
  196. case CSS::Selector::SimpleSelector::Type::Universal:
  197. return true;
  198. case CSS::Selector::SimpleSelector::Type::Id:
  199. return component.value == element.attribute(HTML::AttributeNames::id);
  200. case CSS::Selector::SimpleSelector::Type::Class:
  201. return element.has_class(component.value);
  202. case CSS::Selector::SimpleSelector::Type::TagName:
  203. return component.value == element.local_name();
  204. default:
  205. VERIFY_NOT_REACHED();
  206. }
  207. }
  208. static bool matches(const CSS::Selector& selector, int component_list_index, const DOM::Element& element)
  209. {
  210. auto& component_list = selector.complex_selectors()[component_list_index];
  211. for (auto& component : component_list.compound_selector) {
  212. if (!matches(component, element))
  213. return false;
  214. }
  215. switch (component_list.relation) {
  216. case CSS::Selector::ComplexSelector::Relation::None:
  217. return true;
  218. case CSS::Selector::ComplexSelector::Relation::Descendant:
  219. VERIFY(component_list_index != 0);
  220. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  221. if (!is<DOM::Element>(*ancestor))
  222. continue;
  223. if (matches(selector, component_list_index - 1, verify_cast<DOM::Element>(*ancestor)))
  224. return true;
  225. }
  226. return false;
  227. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  228. VERIFY(component_list_index != 0);
  229. if (!element.parent() || !is<DOM::Element>(*element.parent()))
  230. return false;
  231. return matches(selector, component_list_index - 1, verify_cast<DOM::Element>(*element.parent()));
  232. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  233. VERIFY(component_list_index != 0);
  234. if (auto* sibling = element.previous_element_sibling())
  235. return matches(selector, component_list_index - 1, *sibling);
  236. return false;
  237. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  238. VERIFY(component_list_index != 0);
  239. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  240. if (matches(selector, component_list_index - 1, *sibling))
  241. return true;
  242. }
  243. return false;
  244. case CSS::Selector::ComplexSelector::Relation::Column:
  245. TODO();
  246. }
  247. VERIFY_NOT_REACHED();
  248. }
  249. bool matches(const CSS::Selector& selector, const DOM::Element& element)
  250. {
  251. VERIFY(!selector.complex_selectors().is_empty());
  252. return matches(selector, selector.complex_selectors().size() - 1, element);
  253. }
  254. }