SelectorEngine.cpp 12 KB

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