SelectorEngine.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/HTMLAnchorElement.h>
  14. #include <LibWeb/HTML/HTMLAreaElement.h>
  15. #include <LibWeb/HTML/HTMLHtmlElement.h>
  16. #include <LibWeb/HTML/HTMLInputElement.h>
  17. namespace Web::SelectorEngine {
  18. // https://drafts.csswg.org/selectors-4/#the-lang-pseudo
  19. static inline bool matches_lang_pseudo_class(DOM::Element const& element, Vector<FlyString> const& languages)
  20. {
  21. FlyString element_language;
  22. for (auto const* e = &element; e; e = e->parent_element()) {
  23. auto lang = e->attribute(HTML::AttributeNames::lang);
  24. if (!lang.is_null()) {
  25. element_language = lang;
  26. break;
  27. }
  28. }
  29. if (element_language.is_null())
  30. return false;
  31. // FIXME: This is ad-hoc. Implement a proper language range matching algorithm as recommended by BCP47.
  32. for (auto const& language : languages) {
  33. if (language.is_empty())
  34. return false;
  35. if (language == "*"sv)
  36. return true;
  37. if (!element_language.view().contains('-'))
  38. return element_language.equals_ignoring_case(language);
  39. auto parts = element_language.view().split_view('-');
  40. return parts[0].equals_ignoring_case(language);
  41. }
  42. return false;
  43. }
  44. // https://html.spec.whatwg.org/multipage/semantics-other.html#selector-link
  45. static inline bool matches_link_pseudo_class(DOM::Element const& element)
  46. {
  47. // All a elements that have an href attribute, and all area elements that have an href attribute, must match one of :link and :visited.
  48. if (!is<HTML::HTMLAnchorElement>(element) && !is<HTML::HTMLAreaElement>(element))
  49. return false;
  50. return element.has_attribute(HTML::AttributeNames::href);
  51. }
  52. static inline bool matches_hover_pseudo_class(DOM::Element const& element)
  53. {
  54. auto* hovered_node = element.document().hovered_node();
  55. if (!hovered_node)
  56. return false;
  57. if (&element == hovered_node)
  58. return true;
  59. return element.is_ancestor_of(*hovered_node);
  60. }
  61. // https://html.spec.whatwg.org/multipage/semantics-other.html#selector-checked
  62. static inline bool matches_checked_pseudo_class(DOM::Element const& element)
  63. {
  64. // The :checked pseudo-class must match any element falling into one of the following categories:
  65. // - input elements whose type attribute is in the Checkbox state and whose checkedness state is true
  66. // - input elements whose type attribute is in the Radio Button state and whose checkedness state is true
  67. if (is<HTML::HTMLInputElement>(element)) {
  68. auto const& input_element = static_cast<HTML::HTMLInputElement const&>(element);
  69. switch (input_element.type_state()) {
  70. case HTML::HTMLInputElement::TypeAttributeState::Checkbox:
  71. case HTML::HTMLInputElement::TypeAttributeState::RadioButton:
  72. return static_cast<HTML::HTMLInputElement const&>(element).checked();
  73. default:
  74. return false;
  75. }
  76. }
  77. // FIXME: - option elements whose selectedness is true
  78. return false;
  79. }
  80. static inline bool matches_attribute(CSS::Selector::SimpleSelector::Attribute const& attribute, DOM::Element const& element)
  81. {
  82. if (attribute.match_type == CSS::Selector::SimpleSelector::Attribute::MatchType::HasAttribute) {
  83. // Early way out in case of an attribute existence selector.
  84. return element.has_attribute(attribute.name);
  85. }
  86. auto const case_insensitive_match = (attribute.case_type == CSS::Selector::SimpleSelector::Attribute::CaseType::CaseInsensitiveMatch);
  87. auto const case_sensitivity = case_insensitive_match
  88. ? CaseSensitivity::CaseInsensitive
  89. : CaseSensitivity::CaseSensitive;
  90. switch (attribute.match_type) {
  91. case CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
  92. return case_insensitive_match
  93. ? element.attribute(attribute.name).equals_ignoring_case(attribute.value)
  94. : element.attribute(attribute.name) == attribute.value;
  95. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord: {
  96. if (attribute.value.is_empty()) {
  97. // This selector is always false is match value is empty.
  98. return false;
  99. }
  100. auto const view = element.attribute(attribute.name).split_view(' ');
  101. auto const size = view.size();
  102. for (size_t i = 0; i < size; ++i) {
  103. auto const value = view.at(i);
  104. if (case_insensitive_match
  105. ? value.equals_ignoring_case(attribute.value)
  106. : value == attribute.value) {
  107. return true;
  108. }
  109. }
  110. return false;
  111. }
  112. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString:
  113. return !attribute.value.is_empty()
  114. && element.attribute(attribute.name).contains(attribute.value, case_sensitivity);
  115. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment: {
  116. auto const element_attr_value = element.attribute(attribute.name);
  117. if (element_attr_value.is_empty()) {
  118. // If the attribute value on element is empty, the selector is true
  119. // if the match value is also empty and false otherwise.
  120. return attribute.value.is_empty();
  121. }
  122. if (attribute.value.is_empty()) {
  123. return false;
  124. }
  125. auto segments = element_attr_value.split_view('-');
  126. return case_insensitive_match
  127. ? segments.first().equals_ignoring_case(attribute.value)
  128. : segments.first() == attribute.value;
  129. }
  130. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
  131. return !attribute.value.is_empty()
  132. && element.attribute(attribute.name).starts_with(attribute.value, case_sensitivity);
  133. case CSS::Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
  134. return !attribute.value.is_empty()
  135. && element.attribute(attribute.name).ends_with(attribute.value, case_sensitivity);
  136. default:
  137. break;
  138. }
  139. return false;
  140. }
  141. static inline DOM::Element const* previous_sibling_with_same_tag_name(DOM::Element const& element)
  142. {
  143. for (auto const* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  144. if (sibling->tag_name() == element.tag_name())
  145. return sibling;
  146. }
  147. return nullptr;
  148. }
  149. static inline DOM::Element const* next_sibling_with_same_tag_name(DOM::Element const& element)
  150. {
  151. for (auto const* sibling = element.next_element_sibling(); sibling; sibling = sibling->next_element_sibling()) {
  152. if (sibling->tag_name() == element.tag_name())
  153. return sibling;
  154. }
  155. return nullptr;
  156. }
  157. static inline bool matches_pseudo_class(CSS::Selector::SimpleSelector::PseudoClass const& pseudo_class, DOM::Element const& element)
  158. {
  159. switch (pseudo_class.type) {
  160. case CSS::Selector::SimpleSelector::PseudoClass::Type::Link:
  161. return matches_link_pseudo_class(element);
  162. case CSS::Selector::SimpleSelector::PseudoClass::Type::Visited:
  163. // FIXME: Maybe match this selector sometimes?
  164. return false;
  165. case CSS::Selector::SimpleSelector::PseudoClass::Type::Active:
  166. return element.is_active();
  167. case CSS::Selector::SimpleSelector::PseudoClass::Type::Hover:
  168. return matches_hover_pseudo_class(element);
  169. case CSS::Selector::SimpleSelector::PseudoClass::Type::Focus:
  170. return element.is_focused();
  171. case CSS::Selector::SimpleSelector::PseudoClass::Type::FocusWithin: {
  172. auto* focused_element = element.document().focused_element();
  173. return focused_element && element.is_inclusive_ancestor_of(*focused_element);
  174. }
  175. case CSS::Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  176. return !element.previous_element_sibling();
  177. case CSS::Selector::SimpleSelector::PseudoClass::Type::LastChild:
  178. return !element.next_element_sibling();
  179. case CSS::Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  180. return !(element.previous_element_sibling() || element.next_element_sibling());
  181. case CSS::Selector::SimpleSelector::PseudoClass::Type::Empty: {
  182. if (!element.has_children())
  183. return true;
  184. if (element.first_child_of_type<DOM::Element>())
  185. return false;
  186. // NOTE: CSS Selectors level 4 changed ":empty" to also match whitespace-only text nodes.
  187. // However, none of the major browser supports this yet, so let's just hang back until they do.
  188. bool has_nonempty_text_child = false;
  189. element.for_each_child_of_type<DOM::Text>([&](auto const& text_child) {
  190. if (!text_child.data().is_empty()) {
  191. has_nonempty_text_child = true;
  192. return IterationDecision::Break;
  193. }
  194. return IterationDecision::Continue;
  195. });
  196. return !has_nonempty_text_child;
  197. }
  198. case CSS::Selector::SimpleSelector::PseudoClass::Type::Root:
  199. return is<HTML::HTMLHtmlElement>(element);
  200. case CSS::Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  201. return !previous_sibling_with_same_tag_name(element);
  202. case CSS::Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  203. return !next_sibling_with_same_tag_name(element);
  204. case CSS::Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  205. return !previous_sibling_with_same_tag_name(element) && !next_sibling_with_same_tag_name(element);
  206. case CSS::Selector::SimpleSelector::PseudoClass::Type::Lang:
  207. return matches_lang_pseudo_class(element, pseudo_class.languages);
  208. case CSS::Selector::SimpleSelector::PseudoClass::Type::Disabled:
  209. // https://html.spec.whatwg.org/multipage/semantics-other.html#selector-disabled
  210. // The :disabled pseudo-class must match any element that is actually disabled.
  211. return element.is_actually_disabled();
  212. case CSS::Selector::SimpleSelector::PseudoClass::Type::Enabled:
  213. // https://html.spec.whatwg.org/multipage/semantics-other.html#selector-enabled
  214. // The :enabled pseudo-class must match any button, input, select, textarea, optgroup, option, fieldset element, or form-associated custom element that is not actually disabled.
  215. return !element.is_actually_disabled();
  216. case CSS::Selector::SimpleSelector::PseudoClass::Type::Checked:
  217. return matches_checked_pseudo_class(element);
  218. case CSS::Selector::SimpleSelector::PseudoClass::Type::Is:
  219. case CSS::Selector::SimpleSelector::PseudoClass::Type::Where:
  220. for (auto& selector : pseudo_class.argument_selector_list) {
  221. if (matches(selector, element))
  222. return true;
  223. }
  224. return false;
  225. case CSS::Selector::SimpleSelector::PseudoClass::Type::Not:
  226. for (auto& selector : pseudo_class.argument_selector_list) {
  227. if (matches(selector, element))
  228. return false;
  229. }
  230. return true;
  231. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild:
  232. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  233. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthOfType:
  234. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastOfType:
  235. auto const step_size = pseudo_class.nth_child_pattern.step_size;
  236. auto const offset = pseudo_class.nth_child_pattern.offset;
  237. if (step_size == 0 && offset == 0)
  238. return false; // "If both a and b are equal to zero, the pseudo-class represents no element in the document tree."
  239. auto const* parent = element.parent_element();
  240. if (!parent)
  241. return false;
  242. auto matches_selector_list = [](CSS::SelectorList const& list, DOM::Element const& element) {
  243. if (list.is_empty())
  244. return true;
  245. for (auto const& child_selector : list) {
  246. if (matches(child_selector, element)) {
  247. return true;
  248. }
  249. }
  250. return false;
  251. };
  252. int index = 1;
  253. switch (pseudo_class.type) {
  254. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthChild: {
  255. if (!matches_selector_list(pseudo_class.argument_selector_list, element))
  256. return false;
  257. for (auto* child = parent->first_child_of_type<DOM::Element>(); child && child != &element; child = child->next_element_sibling()) {
  258. if (matches_selector_list(pseudo_class.argument_selector_list, *child))
  259. ++index;
  260. }
  261. break;
  262. }
  263. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastChild: {
  264. if (!matches_selector_list(pseudo_class.argument_selector_list, element))
  265. return false;
  266. for (auto* child = parent->last_child_of_type<DOM::Element>(); child && child != &element; child = child->previous_element_sibling()) {
  267. if (matches_selector_list(pseudo_class.argument_selector_list, *child))
  268. ++index;
  269. }
  270. break;
  271. }
  272. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthOfType: {
  273. for (auto* child = previous_sibling_with_same_tag_name(element); child; child = previous_sibling_with_same_tag_name(*child))
  274. ++index;
  275. break;
  276. }
  277. case CSS::Selector::SimpleSelector::PseudoClass::Type::NthLastOfType: {
  278. for (auto* child = next_sibling_with_same_tag_name(element); child; child = next_sibling_with_same_tag_name(*child))
  279. ++index;
  280. break;
  281. }
  282. default:
  283. VERIFY_NOT_REACHED();
  284. }
  285. // When "step_size == -1", selector represents first "offset" elements in document tree.
  286. if (step_size == -1)
  287. return !(offset <= 0 || index > offset);
  288. // When "step_size == 1", selector represents last "offset" elements in document tree.
  289. if (step_size == 1)
  290. return !(offset < 0 || index < offset);
  291. // When "step_size == 0", selector picks only the "offset" element.
  292. if (step_size == 0)
  293. return index == offset;
  294. // If both are negative, nothing can match.
  295. if (step_size < 0 && offset < 0)
  296. return false;
  297. // Like "a % b", but handles negative integers correctly.
  298. auto const canonical_modulo = [](int a, int b) -> int {
  299. int c = a % b;
  300. if ((c < 0 && b > 0) || (c > 0 && b < 0)) {
  301. c += b;
  302. }
  303. return c;
  304. };
  305. // When "step_size < 0", we start at "offset" and count backwards.
  306. if (step_size < 0)
  307. return index <= offset && canonical_modulo(index - offset, -step_size) == 0;
  308. // Otherwise, we start at "offset" and count forwards.
  309. return index >= offset && canonical_modulo(index - offset, step_size) == 0;
  310. }
  311. return false;
  312. }
  313. static inline bool matches(CSS::Selector::SimpleSelector const& component, DOM::Element const& element)
  314. {
  315. switch (component.type) {
  316. case CSS::Selector::SimpleSelector::Type::Universal:
  317. return true;
  318. case CSS::Selector::SimpleSelector::Type::Id:
  319. return component.name() == element.attribute(HTML::AttributeNames::id);
  320. case CSS::Selector::SimpleSelector::Type::Class:
  321. return element.has_class(component.name());
  322. case CSS::Selector::SimpleSelector::Type::TagName:
  323. // See https://html.spec.whatwg.org/multipage/semantics-other.html#case-sensitivity-of-selectors
  324. if (element.document().document_type() == DOM::Document::Type::HTML)
  325. return component.lowercase_name() == element.local_name();
  326. return component.name().equals_ignoring_case(element.local_name());
  327. case CSS::Selector::SimpleSelector::Type::Attribute:
  328. return matches_attribute(component.attribute(), element);
  329. case CSS::Selector::SimpleSelector::Type::PseudoClass:
  330. return matches_pseudo_class(component.pseudo_class(), element);
  331. case CSS::Selector::SimpleSelector::Type::PseudoElement:
  332. // Pseudo-element matching/not-matching is handled in the top level matches().
  333. return true;
  334. default:
  335. VERIFY_NOT_REACHED();
  336. }
  337. }
  338. static inline bool matches(CSS::Selector const& selector, int component_list_index, DOM::Element const& element)
  339. {
  340. auto& relative_selector = selector.compound_selectors()[component_list_index];
  341. for (auto& simple_selector : relative_selector.simple_selectors) {
  342. if (!matches(simple_selector, element))
  343. return false;
  344. }
  345. switch (relative_selector.combinator) {
  346. case CSS::Selector::Combinator::None:
  347. return true;
  348. case CSS::Selector::Combinator::Descendant:
  349. VERIFY(component_list_index != 0);
  350. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  351. if (!is<DOM::Element>(*ancestor))
  352. continue;
  353. if (matches(selector, component_list_index - 1, static_cast<DOM::Element const&>(*ancestor)))
  354. return true;
  355. }
  356. return false;
  357. case CSS::Selector::Combinator::ImmediateChild:
  358. VERIFY(component_list_index != 0);
  359. if (!element.parent() || !is<DOM::Element>(*element.parent()))
  360. return false;
  361. return matches(selector, component_list_index - 1, static_cast<DOM::Element const&>(*element.parent()));
  362. case CSS::Selector::Combinator::NextSibling:
  363. VERIFY(component_list_index != 0);
  364. if (auto* sibling = element.previous_element_sibling())
  365. return matches(selector, component_list_index - 1, *sibling);
  366. return false;
  367. case CSS::Selector::Combinator::SubsequentSibling:
  368. VERIFY(component_list_index != 0);
  369. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  370. if (matches(selector, component_list_index - 1, *sibling))
  371. return true;
  372. }
  373. return false;
  374. case CSS::Selector::Combinator::Column:
  375. TODO();
  376. }
  377. VERIFY_NOT_REACHED();
  378. }
  379. bool matches(CSS::Selector const& selector, DOM::Element const& element, Optional<CSS::Selector::PseudoElement> pseudo_element)
  380. {
  381. VERIFY(!selector.compound_selectors().is_empty());
  382. if (pseudo_element.has_value() && selector.pseudo_element() != pseudo_element)
  383. return false;
  384. if (!pseudo_element.has_value() && selector.pseudo_element().has_value())
  385. return false;
  386. return matches(selector, selector.compound_selectors().size() - 1, element);
  387. }
  388. }