SelectorEngine.cpp 10 KB

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