SelectorEngine.cpp 10 KB

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