SelectorEngine.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. for (auto& selector : pseudo_class.not_selector) {
  112. if (matches(selector, element))
  113. return false;
  114. }
  115. return true;
  116. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild:
  117. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  118. auto const step_size = pseudo_class.nth_child_pattern.step_size;
  119. auto const offset = pseudo_class.nth_child_pattern.offset;
  120. if (step_size == 0 && offset == 0)
  121. return false; // "If both a and b are equal to zero, the pseudo-class represents no element in the document tree."
  122. auto const* parent = element.parent_element();
  123. if (!parent)
  124. return false;
  125. int index = 1;
  126. if (pseudo_class.type == CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild) {
  127. for (auto* child = parent->first_child_of_type<DOM::Element>(); child && child != &element; child = child->next_element_sibling())
  128. ++index;
  129. } else {
  130. for (auto* child = parent->last_child_of_type<DOM::Element>(); child && child != &element; child = child->previous_element_sibling())
  131. ++index;
  132. }
  133. if (step_size < 0) {
  134. // When "step_size" is negative, selector represents first "offset" elements in document tree.
  135. return !(offset <= 0 || index > offset);
  136. } else if (step_size == 1) {
  137. // When "step_size == 1", selector represents last "offset" elements in document tree.
  138. return !(offset < 0 || index < offset);
  139. }
  140. // Like "a % b", but handles negative integers correctly.
  141. auto const canonical_modulo = [](int a, int b) -> int {
  142. int c = a % b;
  143. if ((c < 0 && b > 0) || (c > 0 && b < 0)) {
  144. c += b;
  145. }
  146. return c;
  147. };
  148. if (step_size == 0) {
  149. // Avoid divide by zero.
  150. if (index != offset) {
  151. return false;
  152. }
  153. } else if (canonical_modulo(index - offset, step_size) != 0) {
  154. return false;
  155. }
  156. return true;
  157. }
  158. return false;
  159. }
  160. static bool matches(CSS::Selector::SimpleSelector const& component, DOM::Element const& element)
  161. {
  162. switch (component.type) {
  163. case CSS::Selector::SimpleSelector::Type::Universal:
  164. return true;
  165. case CSS::Selector::SimpleSelector::Type::Id:
  166. return component.value == element.attribute(HTML::AttributeNames::id);
  167. case CSS::Selector::SimpleSelector::Type::Class:
  168. return element.has_class(component.value);
  169. case CSS::Selector::SimpleSelector::Type::TagName:
  170. return component.value == element.local_name();
  171. case CSS::Selector::SimpleSelector::Type::Attribute:
  172. return matches_attribute(component.attribute, element);
  173. case CSS::Selector::SimpleSelector::Type::PseudoClass:
  174. return matches_pseudo_class(component.pseudo_class, element);
  175. case CSS::Selector::SimpleSelector::Type::PseudoElement:
  176. // FIXME: Implement pseudo-elements.
  177. return false;
  178. default:
  179. VERIFY_NOT_REACHED();
  180. }
  181. }
  182. static bool matches(CSS::Selector const& selector, int component_list_index, DOM::Element const& element)
  183. {
  184. auto& component_list = selector.complex_selectors()[component_list_index];
  185. for (auto& component : component_list.compound_selector) {
  186. if (!matches(component, element))
  187. return false;
  188. }
  189. switch (component_list.relation) {
  190. case CSS::Selector::ComplexSelector::Relation::None:
  191. return true;
  192. case CSS::Selector::ComplexSelector::Relation::Descendant:
  193. VERIFY(component_list_index != 0);
  194. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  195. if (!is<DOM::Element>(*ancestor))
  196. continue;
  197. if (matches(selector, component_list_index - 1, verify_cast<DOM::Element>(*ancestor)))
  198. return true;
  199. }
  200. return false;
  201. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  202. VERIFY(component_list_index != 0);
  203. if (!element.parent() || !is<DOM::Element>(*element.parent()))
  204. return false;
  205. return matches(selector, component_list_index - 1, verify_cast<DOM::Element>(*element.parent()));
  206. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  207. VERIFY(component_list_index != 0);
  208. if (auto* sibling = element.previous_element_sibling())
  209. return matches(selector, component_list_index - 1, *sibling);
  210. return false;
  211. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  212. VERIFY(component_list_index != 0);
  213. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  214. if (matches(selector, component_list_index - 1, *sibling))
  215. return true;
  216. }
  217. return false;
  218. case CSS::Selector::ComplexSelector::Relation::Column:
  219. TODO();
  220. }
  221. VERIFY_NOT_REACHED();
  222. }
  223. bool matches(CSS::Selector const& selector, DOM::Element const& element)
  224. {
  225. VERIFY(!selector.complex_selectors().is_empty());
  226. return matches(selector, selector.complex_selectors().size() - 1, element);
  227. }
  228. }