SelectorEngine.cpp 10 KB

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