SelectorEngine.cpp 11 KB

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