SelectorEngine.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. return !(element.first_child_of_type<DOM::Element>() || element.first_child_of_type<DOM::Text>());
  90. case CSS::Selector::SimpleSelector::PseudoClass::Type::Root:
  91. return is<HTML::HTMLHtmlElement>(element);
  92. case CSS::Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  93. return !previous_sibling_with_same_tag_name(element);
  94. case CSS::Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  95. return !next_sibling_with_same_tag_name(element);
  96. case CSS::Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  97. return !previous_sibling_with_same_tag_name(element) && !next_sibling_with_same_tag_name(element);
  98. case CSS::Selector::SimpleSelector::PseudoClass::Type::Disabled:
  99. if (!element.tag_name().equals_ignoring_case(HTML::TagNames::input))
  100. return false;
  101. if (!element.has_attribute("disabled"))
  102. return false;
  103. return true;
  104. case CSS::Selector::SimpleSelector::PseudoClass::Type::Enabled:
  105. if (!element.tag_name().equals_ignoring_case(HTML::TagNames::input))
  106. return false;
  107. if (element.has_attribute("disabled"))
  108. return false;
  109. return true;
  110. case CSS::Selector::SimpleSelector::PseudoClass::Type::Checked:
  111. if (!element.tag_name().equals_ignoring_case(HTML::TagNames::input))
  112. return false;
  113. return static_cast<HTML::HTMLInputElement const&>(element).checked();
  114. case CSS::Selector::SimpleSelector::PseudoClass::Type::Not:
  115. for (auto& selector : pseudo_class.not_selector) {
  116. if (matches(selector, element))
  117. return false;
  118. }
  119. return true;
  120. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild:
  121. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  122. auto const step_size = pseudo_class.nth_child_pattern.step_size;
  123. auto const offset = pseudo_class.nth_child_pattern.offset;
  124. if (step_size == 0 && offset == 0)
  125. return false; // "If both a and b are equal to zero, the pseudo-class represents no element in the document tree."
  126. auto const* parent = element.parent_element();
  127. if (!parent)
  128. return false;
  129. int index = 1;
  130. if (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild) {
  131. for (auto* child = parent->first_child_of_type<DOM::Element>(); child && child != &element; child = child->next_element_sibling())
  132. ++index;
  133. } else {
  134. for (auto* child = parent->last_child_of_type<DOM::Element>(); child && child != &element; child = child->previous_element_sibling())
  135. ++index;
  136. }
  137. if (step_size < 0) {
  138. // When "step_size" is negative, selector represents first "offset" elements in document tree.
  139. return !(offset <= 0 || index > offset);
  140. } else if (step_size == 1) {
  141. // When "step_size == 1", selector represents last "offset" elements in document tree.
  142. return !(offset < 0 || index < offset);
  143. }
  144. // Like "a % b", but handles negative integers correctly.
  145. auto const canonical_modulo = [](int a, int b) -> int {
  146. int c = a % b;
  147. if ((c < 0 && b > 0) || (c > 0 && b < 0)) {
  148. c += b;
  149. }
  150. return c;
  151. };
  152. if (step_size == 0) {
  153. // Avoid divide by zero.
  154. if (index != offset) {
  155. return false;
  156. }
  157. } else if (canonical_modulo(index - offset, step_size) != 0) {
  158. return false;
  159. }
  160. return true;
  161. }
  162. return false;
  163. }
  164. static inline bool matches(CSS::Selector::SimpleSelector const& component, DOM::Element const& element)
  165. {
  166. switch (component.type) {
  167. case CSS::Selector::SimpleSelector::Type::Universal:
  168. return true;
  169. case CSS::Selector::SimpleSelector::Type::Id:
  170. return component.value == element.attribute(HTML::AttributeNames::id);
  171. case CSS::Selector::SimpleSelector::Type::Class:
  172. return element.has_class(component.value);
  173. case CSS::Selector::SimpleSelector::Type::TagName:
  174. return component.value == element.local_name();
  175. case CSS::Selector::SimpleSelector::Type::Attribute:
  176. return matches_attribute(component.attribute, element);
  177. case CSS::Selector::SimpleSelector::Type::PseudoClass:
  178. return matches_pseudo_class(component.pseudo_class, element);
  179. case CSS::Selector::SimpleSelector::Type::PseudoElement:
  180. // FIXME: Implement pseudo-elements.
  181. return false;
  182. default:
  183. VERIFY_NOT_REACHED();
  184. }
  185. }
  186. static inline bool matches(CSS::Selector const& selector, int component_list_index, DOM::Element const& element)
  187. {
  188. auto& relative_selector = selector.compound_selectors()[component_list_index];
  189. for (auto& simple_selector : relative_selector.simple_selectors) {
  190. if (!matches(simple_selector, element))
  191. return false;
  192. }
  193. switch (relative_selector.combinator) {
  194. case CSS::Selector::Combinator::None:
  195. return true;
  196. case CSS::Selector::Combinator::Descendant:
  197. VERIFY(component_list_index != 0);
  198. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  199. if (!is<DOM::Element>(*ancestor))
  200. continue;
  201. if (matches(selector, component_list_index - 1, static_cast<DOM::Element const&>(*ancestor)))
  202. return true;
  203. }
  204. return false;
  205. case CSS::Selector::Combinator::ImmediateChild:
  206. VERIFY(component_list_index != 0);
  207. if (!element.parent() || !is<DOM::Element>(*element.parent()))
  208. return false;
  209. return matches(selector, component_list_index - 1, static_cast<DOM::Element const&>(*element.parent()));
  210. case CSS::Selector::Combinator::NextSibling:
  211. VERIFY(component_list_index != 0);
  212. if (auto* sibling = element.previous_element_sibling())
  213. return matches(selector, component_list_index - 1, *sibling);
  214. return false;
  215. case CSS::Selector::Combinator::SubsequentSibling:
  216. VERIFY(component_list_index != 0);
  217. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  218. if (matches(selector, component_list_index - 1, *sibling))
  219. return true;
  220. }
  221. return false;
  222. case CSS::Selector::Combinator::Column:
  223. TODO();
  224. }
  225. VERIFY_NOT_REACHED();
  226. }
  227. bool matches(CSS::Selector const& selector, DOM::Element const& element)
  228. {
  229. VERIFY(!selector.compound_selectors().is_empty());
  230. return matches(selector, selector.compound_selectors().size() - 1, element);
  231. }
  232. }