SelectorEngine.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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::ParsingContext(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::Contains:
  173. if (!element.attribute(component.attribute_name).split(' ').contains_slow(component.attribute_value))
  174. return false;
  175. break;
  176. default:
  177. break;
  178. }
  179. switch (component.type) {
  180. case CSS::Selector::SimpleSelector::Type::Universal:
  181. return true;
  182. case CSS::Selector::SimpleSelector::Type::Id:
  183. return component.value == element.attribute(HTML::AttributeNames::id);
  184. case CSS::Selector::SimpleSelector::Type::Class:
  185. return element.has_class(component.value);
  186. case CSS::Selector::SimpleSelector::Type::TagName:
  187. return component.value == element.local_name();
  188. default:
  189. VERIFY_NOT_REACHED();
  190. }
  191. }
  192. static bool matches(const CSS::Selector& selector, int component_list_index, const DOM::Element& element)
  193. {
  194. auto& component_list = selector.complex_selectors()[component_list_index];
  195. for (auto& component : component_list.compound_selector) {
  196. if (!matches(component, element))
  197. return false;
  198. }
  199. switch (component_list.relation) {
  200. case CSS::Selector::ComplexSelector::Relation::None:
  201. return true;
  202. case CSS::Selector::ComplexSelector::Relation::Descendant:
  203. VERIFY(component_list_index != 0);
  204. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  205. if (!is<DOM::Element>(*ancestor))
  206. continue;
  207. if (matches(selector, component_list_index - 1, verify_cast<DOM::Element>(*ancestor)))
  208. return true;
  209. }
  210. return false;
  211. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  212. VERIFY(component_list_index != 0);
  213. if (!element.parent() || !is<DOM::Element>(*element.parent()))
  214. return false;
  215. return matches(selector, component_list_index - 1, verify_cast<DOM::Element>(*element.parent()));
  216. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  217. VERIFY(component_list_index != 0);
  218. if (auto* sibling = element.previous_element_sibling())
  219. return matches(selector, component_list_index - 1, *sibling);
  220. return false;
  221. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  222. VERIFY(component_list_index != 0);
  223. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  224. if (matches(selector, component_list_index - 1, *sibling))
  225. return true;
  226. }
  227. return false;
  228. case CSS::Selector::ComplexSelector::Relation::Column:
  229. TODO();
  230. }
  231. VERIFY_NOT_REACHED();
  232. }
  233. bool matches(const CSS::Selector& selector, const DOM::Element& element)
  234. {
  235. VERIFY(!selector.complex_selectors().is_empty());
  236. return matches(selector, selector.complex_selectors().size() - 1, element);
  237. }
  238. }