SelectorEngine.cpp 10 KB

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