SelectorEngine.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2023, 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/HTMLButtonElement.h>
  16. #include <LibWeb/HTML/HTMLFieldSetElement.h>
  17. #include <LibWeb/HTML/HTMLHtmlElement.h>
  18. #include <LibWeb/HTML/HTMLInputElement.h>
  19. #include <LibWeb/HTML/HTMLMediaElement.h>
  20. #include <LibWeb/HTML/HTMLOptGroupElement.h>
  21. #include <LibWeb/HTML/HTMLOptionElement.h>
  22. #include <LibWeb/HTML/HTMLProgressElement.h>
  23. #include <LibWeb/HTML/HTMLSelectElement.h>
  24. #include <LibWeb/HTML/HTMLTextAreaElement.h>
  25. #include <LibWeb/Infra/Strings.h>
  26. namespace Web::SelectorEngine {
  27. // https://drafts.csswg.org/selectors-4/#the-lang-pseudo
  28. static inline bool matches_lang_pseudo_class(DOM::Element const& element, Vector<FlyString> const& languages)
  29. {
  30. FlyString element_language;
  31. for (auto const* e = &element; e; e = e->parent_element()) {
  32. auto lang = e->attribute(HTML::AttributeNames::lang);
  33. if (!lang.is_null()) {
  34. element_language = FlyString::from_deprecated_fly_string(lang).release_value_but_fixme_should_propagate_errors();
  35. break;
  36. }
  37. }
  38. if (element_language.is_empty())
  39. return false;
  40. // FIXME: This is ad-hoc. Implement a proper language range matching algorithm as recommended by BCP47.
  41. for (auto const& language : languages) {
  42. if (language.is_empty())
  43. continue;
  44. if (language == "*"sv)
  45. return true;
  46. if (!element_language.to_string().contains('-') && Infra::is_ascii_case_insensitive_match(element_language, language))
  47. return true;
  48. auto parts = element_language.to_string().split_limit('-', 2).release_value_but_fixme_should_propagate_errors();
  49. if (Infra::is_ascii_case_insensitive_match(parts[0], language))
  50. return true;
  51. }
  52. return false;
  53. }
  54. // https://html.spec.whatwg.org/multipage/semantics-other.html#selector-link
  55. static inline bool matches_link_pseudo_class(DOM::Element const& element)
  56. {
  57. // All a elements that have an href attribute, and all area elements that have an href attribute, must match one of :link and :visited.
  58. if (!is<HTML::HTMLAnchorElement>(element) && !is<HTML::HTMLAreaElement>(element))
  59. return false;
  60. return element.has_attribute(HTML::AttributeNames::href);
  61. }
  62. static inline bool matches_hover_pseudo_class(DOM::Element const& element)
  63. {
  64. auto* hovered_node = element.document().hovered_node();
  65. if (!hovered_node)
  66. return false;
  67. if (&element == hovered_node)
  68. return true;
  69. return element.is_ancestor_of(*hovered_node);
  70. }
  71. // https://html.spec.whatwg.org/multipage/semantics-other.html#selector-checked
  72. static inline bool matches_checked_pseudo_class(DOM::Element const& element)
  73. {
  74. // The :checked pseudo-class must match any element falling into one of the following categories:
  75. // - input elements whose type attribute is in the Checkbox state and whose checkedness state is true
  76. // - input elements whose type attribute is in the Radio Button state and whose checkedness state is true
  77. if (is<HTML::HTMLInputElement>(element)) {
  78. auto const& input_element = static_cast<HTML::HTMLInputElement const&>(element);
  79. switch (input_element.type_state()) {
  80. case HTML::HTMLInputElement::TypeAttributeState::Checkbox:
  81. case HTML::HTMLInputElement::TypeAttributeState::RadioButton:
  82. return static_cast<HTML::HTMLInputElement const&>(element).checked();
  83. default:
  84. return false;
  85. }
  86. }
  87. // - option elements whose selectedness is true
  88. if (is<HTML::HTMLOptionElement>(element)) {
  89. return static_cast<HTML::HTMLOptionElement const&>(element).selected();
  90. }
  91. return false;
  92. }
  93. // https://html.spec.whatwg.org/multipage/semantics-other.html#selector-indeterminate
  94. static inline bool matches_indeterminate_pseudo_class(DOM::Element const& element)
  95. {
  96. // The :indeterminate pseudo-class must match any element falling into one of the following categories:
  97. // - input elements whose type attribute is in the Checkbox state and whose indeterminate IDL attribute is set to true
  98. // FIXME: - input elements whose type attribute is in the Radio Button state and whose radio button group contains no input elements whose checkedness state is true.
  99. if (is<HTML::HTMLInputElement>(element)) {
  100. auto const& input_element = static_cast<HTML::HTMLInputElement const&>(element);
  101. switch (input_element.type_state()) {
  102. case HTML::HTMLInputElement::TypeAttributeState::Checkbox:
  103. return input_element.indeterminate();
  104. default:
  105. return false;
  106. }
  107. }
  108. // - progress elements with no value content attribute
  109. if (is<HTML::HTMLProgressElement>(element)) {
  110. return !element.has_attribute(HTML::AttributeNames::value);
  111. }
  112. return false;
  113. }
  114. static inline bool matches_attribute(CSS::Selector::SimpleSelector::Attribute const& attribute, [[maybe_unused]] Optional<CSS::CSSStyleSheet const&> style_sheet_for_rule, DOM::Element const& element)
  115. {
  116. // FIXME: Check the attribute's namespace, once we support that in DOM::Element!
  117. auto attribute_name = attribute.qualified_name.name.name.to_deprecated_fly_string();
  118. if (attribute.match_type == CSS::Selector::SimpleSelector::Attribute::MatchType::HasAttribute) {
  119. // Early way out in case of an attribute existence selector.
  120. return element.has_attribute(attribute_name);
  121. }
  122. auto const case_insensitive_match = (attribute.case_type == CSS::Selector::SimpleSelector::Attribute::CaseType::CaseInsensitiveMatch);
  123. auto const case_sensitivity = case_insensitive_match
  124. ? CaseSensitivity::CaseInsensitive
  125. : CaseSensitivity::CaseSensitive;
  126. switch (attribute.match_type) {
  127. case CSS::Selector::SimpleSelector::Attribute::MatchType::ExactValueMatch:
  128. return case_insensitive_match
  129. ? Infra::is_ascii_case_insensitive_match(element.attribute(attribute_name), attribute.value)
  130. : element.attribute(attribute_name) == attribute.value.to_deprecated_string();
  131. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsWord: {
  132. if (attribute.value.is_empty()) {
  133. // This selector is always false is match value is empty.
  134. return false;
  135. }
  136. auto const view = element.attribute(attribute_name).split_view(' ');
  137. auto const size = view.size();
  138. for (size_t i = 0; i < size; ++i) {
  139. auto const value = view.at(i);
  140. if (case_insensitive_match
  141. ? Infra::is_ascii_case_insensitive_match(value, attribute.value)
  142. : value == attribute.value) {
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. case CSS::Selector::SimpleSelector::Attribute::MatchType::ContainsString:
  149. return !attribute.value.is_empty()
  150. && element.attribute(attribute_name).contains(attribute.value, case_sensitivity);
  151. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithSegment: {
  152. auto const element_attr_value = element.attribute(attribute_name);
  153. if (element_attr_value.is_empty()) {
  154. // If the attribute value on element is empty, the selector is true
  155. // if the match value is also empty and false otherwise.
  156. return attribute.value.is_empty();
  157. }
  158. if (attribute.value.is_empty()) {
  159. return false;
  160. }
  161. auto segments = element_attr_value.split_view('-');
  162. return case_insensitive_match
  163. ? Infra::is_ascii_case_insensitive_match(segments.first(), attribute.value)
  164. : segments.first() == attribute.value;
  165. }
  166. case CSS::Selector::SimpleSelector::Attribute::MatchType::StartsWithString:
  167. return !attribute.value.is_empty()
  168. && element.attribute(attribute_name).starts_with(attribute.value, case_sensitivity);
  169. case CSS::Selector::SimpleSelector::Attribute::MatchType::EndsWithString:
  170. return !attribute.value.is_empty()
  171. && element.attribute(attribute_name).ends_with(attribute.value, case_sensitivity);
  172. default:
  173. break;
  174. }
  175. return false;
  176. }
  177. static inline DOM::Element const* previous_sibling_with_same_tag_name(DOM::Element const& element)
  178. {
  179. for (auto const* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  180. if (sibling->tag_name() == element.tag_name())
  181. return sibling;
  182. }
  183. return nullptr;
  184. }
  185. static inline DOM::Element const* next_sibling_with_same_tag_name(DOM::Element const& element)
  186. {
  187. for (auto const* sibling = element.next_element_sibling(); sibling; sibling = sibling->next_element_sibling()) {
  188. if (sibling->tag_name() == element.tag_name())
  189. return sibling;
  190. }
  191. return nullptr;
  192. }
  193. static inline bool matches_pseudo_class(CSS::Selector::SimpleSelector::PseudoClassSelector const& pseudo_class, Optional<CSS::CSSStyleSheet const&> style_sheet_for_rule, DOM::Element const& element, JS::GCPtr<DOM::ParentNode const> scope)
  194. {
  195. switch (pseudo_class.type) {
  196. case CSS::PseudoClass::Link:
  197. return matches_link_pseudo_class(element);
  198. case CSS::PseudoClass::Visited:
  199. // FIXME: Maybe match this selector sometimes?
  200. return false;
  201. case CSS::PseudoClass::Active:
  202. return element.is_active();
  203. case CSS::PseudoClass::Hover:
  204. return matches_hover_pseudo_class(element);
  205. case CSS::PseudoClass::Focus:
  206. return element.is_focused();
  207. case CSS::PseudoClass::FocusVisible:
  208. // FIXME: We should only apply this when a visible focus is useful. Decide when that is!
  209. return element.is_focused();
  210. case CSS::PseudoClass::FocusWithin: {
  211. auto* focused_element = element.document().focused_element();
  212. return focused_element && element.is_inclusive_ancestor_of(*focused_element);
  213. }
  214. case CSS::PseudoClass::FirstChild:
  215. return !element.previous_element_sibling();
  216. case CSS::PseudoClass::LastChild:
  217. return !element.next_element_sibling();
  218. case CSS::PseudoClass::OnlyChild:
  219. return !(element.previous_element_sibling() || element.next_element_sibling());
  220. case CSS::PseudoClass::Empty: {
  221. if (!element.has_children())
  222. return true;
  223. if (element.first_child_of_type<DOM::Element>())
  224. return false;
  225. // NOTE: CSS Selectors level 4 changed ":empty" to also match whitespace-only text nodes.
  226. // However, none of the major browser supports this yet, so let's just hang back until they do.
  227. bool has_nonempty_text_child = false;
  228. element.for_each_child_of_type<DOM::Text>([&](auto const& text_child) {
  229. if (!text_child.data().is_empty()) {
  230. has_nonempty_text_child = true;
  231. return IterationDecision::Break;
  232. }
  233. return IterationDecision::Continue;
  234. });
  235. return !has_nonempty_text_child;
  236. }
  237. case CSS::PseudoClass::Root:
  238. return is<HTML::HTMLHtmlElement>(element);
  239. case CSS::PseudoClass::Host:
  240. // FIXME: Implement :host selector.
  241. return false;
  242. case CSS::PseudoClass::Scope:
  243. return scope ? &element == scope : is<HTML::HTMLHtmlElement>(element);
  244. case CSS::PseudoClass::FirstOfType:
  245. return !previous_sibling_with_same_tag_name(element);
  246. case CSS::PseudoClass::LastOfType:
  247. return !next_sibling_with_same_tag_name(element);
  248. case CSS::PseudoClass::OnlyOfType:
  249. return !previous_sibling_with_same_tag_name(element) && !next_sibling_with_same_tag_name(element);
  250. case CSS::PseudoClass::Lang:
  251. return matches_lang_pseudo_class(element, pseudo_class.languages);
  252. case CSS::PseudoClass::Disabled:
  253. // https://html.spec.whatwg.org/multipage/semantics-other.html#selector-disabled
  254. // The :disabled pseudo-class must match any element that is actually disabled.
  255. return element.is_actually_disabled();
  256. case CSS::PseudoClass::Enabled:
  257. // https://html.spec.whatwg.org/multipage/semantics-other.html#selector-enabled
  258. // 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.
  259. return (is<HTML::HTMLButtonElement>(element) || is<HTML::HTMLInputElement>(element) || is<HTML::HTMLSelectElement>(element) || is<HTML::HTMLTextAreaElement>(element) || is<HTML::HTMLOptGroupElement>(element) || is<HTML::HTMLOptionElement>(element) || is<HTML::HTMLFieldSetElement>(element))
  260. && !element.is_actually_disabled();
  261. case CSS::PseudoClass::Checked:
  262. return matches_checked_pseudo_class(element);
  263. case CSS::PseudoClass::Indeterminate:
  264. return matches_indeterminate_pseudo_class(element);
  265. case CSS::PseudoClass::Defined:
  266. return element.is_defined();
  267. case CSS::PseudoClass::Is:
  268. case CSS::PseudoClass::Where:
  269. for (auto& selector : pseudo_class.argument_selector_list) {
  270. if (matches(selector, style_sheet_for_rule, element))
  271. return true;
  272. }
  273. return false;
  274. case CSS::PseudoClass::Not:
  275. for (auto& selector : pseudo_class.argument_selector_list) {
  276. if (matches(selector, style_sheet_for_rule, element))
  277. return false;
  278. }
  279. return true;
  280. case CSS::PseudoClass::NthChild:
  281. case CSS::PseudoClass::NthLastChild:
  282. case CSS::PseudoClass::NthOfType:
  283. case CSS::PseudoClass::NthLastOfType: {
  284. auto const step_size = pseudo_class.nth_child_pattern.step_size;
  285. auto const offset = pseudo_class.nth_child_pattern.offset;
  286. if (step_size == 0 && offset == 0)
  287. return false; // "If both a and b are equal to zero, the pseudo-class represents no element in the document tree."
  288. auto const* parent = element.parent_element();
  289. if (!parent)
  290. return false;
  291. auto matches_selector_list = [&style_sheet_for_rule](CSS::SelectorList const& list, DOM::Element const& element) {
  292. if (list.is_empty())
  293. return true;
  294. for (auto const& child_selector : list) {
  295. if (matches(child_selector, style_sheet_for_rule, element)) {
  296. return true;
  297. }
  298. }
  299. return false;
  300. };
  301. int index = 1;
  302. switch (pseudo_class.type) {
  303. case CSS::PseudoClass::NthChild: {
  304. if (!matches_selector_list(pseudo_class.argument_selector_list, element))
  305. return false;
  306. for (auto* child = parent->first_child_of_type<DOM::Element>(); child && child != &element; child = child->next_element_sibling()) {
  307. if (matches_selector_list(pseudo_class.argument_selector_list, *child))
  308. ++index;
  309. }
  310. break;
  311. }
  312. case CSS::PseudoClass::NthLastChild: {
  313. if (!matches_selector_list(pseudo_class.argument_selector_list, element))
  314. return false;
  315. for (auto* child = parent->last_child_of_type<DOM::Element>(); child && child != &element; child = child->previous_element_sibling()) {
  316. if (matches_selector_list(pseudo_class.argument_selector_list, *child))
  317. ++index;
  318. }
  319. break;
  320. }
  321. case CSS::PseudoClass::NthOfType: {
  322. for (auto* child = previous_sibling_with_same_tag_name(element); child; child = previous_sibling_with_same_tag_name(*child))
  323. ++index;
  324. break;
  325. }
  326. case CSS::PseudoClass::NthLastOfType: {
  327. for (auto* child = next_sibling_with_same_tag_name(element); child; child = next_sibling_with_same_tag_name(*child))
  328. ++index;
  329. break;
  330. }
  331. default:
  332. VERIFY_NOT_REACHED();
  333. }
  334. // When "step_size == -1", selector represents first "offset" elements in document tree.
  335. if (step_size == -1)
  336. return !(offset <= 0 || index > offset);
  337. // When "step_size == 1", selector represents last "offset" elements in document tree.
  338. if (step_size == 1)
  339. return !(offset < 0 || index < offset);
  340. // When "step_size == 0", selector picks only the "offset" element.
  341. if (step_size == 0)
  342. return index == offset;
  343. // If both are negative, nothing can match.
  344. if (step_size < 0 && offset < 0)
  345. return false;
  346. // Like "a % b", but handles negative integers correctly.
  347. auto const canonical_modulo = [](int a, int b) -> int {
  348. int c = a % b;
  349. if ((c < 0 && b > 0) || (c > 0 && b < 0)) {
  350. c += b;
  351. }
  352. return c;
  353. };
  354. // When "step_size < 0", we start at "offset" and count backwards.
  355. if (step_size < 0)
  356. return index <= offset && canonical_modulo(index - offset, -step_size) == 0;
  357. // Otherwise, we start at "offset" and count forwards.
  358. return index >= offset && canonical_modulo(index - offset, step_size) == 0;
  359. }
  360. case CSS::PseudoClass::Playing: {
  361. if (!is<HTML::HTMLMediaElement>(element))
  362. return false;
  363. auto const& media_element = static_cast<HTML::HTMLMediaElement const&>(element);
  364. return !media_element.paused();
  365. }
  366. case CSS::PseudoClass::Paused: {
  367. if (!is<HTML::HTMLMediaElement>(element))
  368. return false;
  369. auto const& media_element = static_cast<HTML::HTMLMediaElement const&>(element);
  370. return media_element.paused();
  371. }
  372. case CSS::PseudoClass::Seeking: {
  373. if (!is<HTML::HTMLMediaElement>(element))
  374. return false;
  375. auto const& media_element = static_cast<HTML::HTMLMediaElement const&>(element);
  376. return media_element.seeking();
  377. }
  378. case CSS::PseudoClass::Muted: {
  379. if (!is<HTML::HTMLMediaElement>(element))
  380. return false;
  381. auto const& media_element = static_cast<HTML::HTMLMediaElement const&>(element);
  382. return media_element.muted();
  383. }
  384. case CSS::PseudoClass::VolumeLocked: {
  385. // FIXME: Currently we don't allow the user to specify an override volume, so this is always false.
  386. // Once we do, implement this!
  387. return false;
  388. }
  389. case CSS::PseudoClass::Buffering: {
  390. if (!is<HTML::HTMLMediaElement>(element))
  391. return false;
  392. auto const& media_element = static_cast<HTML::HTMLMediaElement const&>(element);
  393. return media_element.blocked();
  394. }
  395. case CSS::PseudoClass::Stalled: {
  396. if (!is<HTML::HTMLMediaElement>(element))
  397. return false;
  398. auto const& media_element = static_cast<HTML::HTMLMediaElement const&>(element);
  399. return media_element.stalled();
  400. }
  401. case CSS::PseudoClass::Target:
  402. return element.is_target();
  403. }
  404. return false;
  405. }
  406. static inline bool matches(CSS::Selector::SimpleSelector const& component, Optional<CSS::CSSStyleSheet const&> style_sheet_for_rule, DOM::Element const& element, JS::GCPtr<DOM::ParentNode const> scope)
  407. {
  408. switch (component.type) {
  409. case CSS::Selector::SimpleSelector::Type::Universal:
  410. case CSS::Selector::SimpleSelector::Type::TagName: {
  411. auto qualified_name = component.qualified_name();
  412. // Reject if the tag name doesn't match
  413. if (component.type == CSS::Selector::SimpleSelector::Type::TagName) {
  414. // See https://html.spec.whatwg.org/multipage/semantics-other.html#case-sensitivity-of-selectors
  415. if (element.document().document_type() == DOM::Document::Type::HTML) {
  416. if (qualified_name.name.lowercase_name != element.local_name().view())
  417. return false;
  418. } else if (!Infra::is_ascii_case_insensitive_match(qualified_name.name.name, element.local_name())) {
  419. return false;
  420. }
  421. }
  422. // Match the namespace
  423. switch (qualified_name.namespace_type) {
  424. case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Default:
  425. // "if no default namespace has been declared for selectors, this is equivalent to *|E."
  426. if (!style_sheet_for_rule.has_value() || !style_sheet_for_rule->default_namespace().has_value())
  427. return true;
  428. // "Otherwise it is equivalent to ns|E where ns is the default namespace."
  429. return element.namespace_() == style_sheet_for_rule->default_namespace();
  430. case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::None:
  431. // "elements with name E without a namespace"
  432. return element.namespace_().is_empty();
  433. case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Any:
  434. // "elements with name E in any namespace, including those without a namespace"
  435. return true;
  436. case CSS::Selector::SimpleSelector::QualifiedName::NamespaceType::Named:
  437. // "elements with name E in namespace ns"
  438. // Unrecognized namespace prefixes are invalid, so don't match.
  439. // (We can't detect this at parse time, since a namespace rule may be inserted later.)
  440. // So, if we don't have a context to look up namespaces from, we fail to match.
  441. if (!style_sheet_for_rule.has_value())
  442. return false;
  443. auto selector_namespace = style_sheet_for_rule->namespace_uri(qualified_name.namespace_);
  444. return selector_namespace.has_value() && selector_namespace.value() == element.namespace_();
  445. }
  446. VERIFY_NOT_REACHED();
  447. }
  448. case CSS::Selector::SimpleSelector::Type::Id:
  449. return component.name() == element.attribute(HTML::AttributeNames::id).view();
  450. case CSS::Selector::SimpleSelector::Type::Class:
  451. return element.has_class(component.name());
  452. case CSS::Selector::SimpleSelector::Type::Attribute:
  453. return matches_attribute(component.attribute(), style_sheet_for_rule, element);
  454. case CSS::Selector::SimpleSelector::Type::PseudoClass:
  455. return matches_pseudo_class(component.pseudo_class(), style_sheet_for_rule, element, scope);
  456. case CSS::Selector::SimpleSelector::Type::PseudoElement:
  457. // Pseudo-element matching/not-matching is handled in the top level matches().
  458. return true;
  459. default:
  460. VERIFY_NOT_REACHED();
  461. }
  462. }
  463. static inline bool matches(CSS::Selector const& selector, Optional<CSS::CSSStyleSheet const&> style_sheet_for_rule, int component_list_index, DOM::Element const& element, JS::GCPtr<DOM::ParentNode const> scope)
  464. {
  465. auto& relative_selector = selector.compound_selectors()[component_list_index];
  466. for (auto& simple_selector : relative_selector.simple_selectors) {
  467. if (!matches(simple_selector, style_sheet_for_rule, element, scope))
  468. return false;
  469. }
  470. switch (relative_selector.combinator) {
  471. case CSS::Selector::Combinator::None:
  472. return true;
  473. case CSS::Selector::Combinator::Descendant:
  474. VERIFY(component_list_index != 0);
  475. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  476. if (!is<DOM::Element>(*ancestor))
  477. continue;
  478. if (matches(selector, style_sheet_for_rule, component_list_index - 1, static_cast<DOM::Element const&>(*ancestor), scope))
  479. return true;
  480. }
  481. return false;
  482. case CSS::Selector::Combinator::ImmediateChild:
  483. VERIFY(component_list_index != 0);
  484. if (!element.parent() || !is<DOM::Element>(*element.parent()))
  485. return false;
  486. return matches(selector, style_sheet_for_rule, component_list_index - 1, static_cast<DOM::Element const&>(*element.parent()), scope);
  487. case CSS::Selector::Combinator::NextSibling:
  488. VERIFY(component_list_index != 0);
  489. if (auto* sibling = element.previous_element_sibling())
  490. return matches(selector, style_sheet_for_rule, component_list_index - 1, *sibling, scope);
  491. return false;
  492. case CSS::Selector::Combinator::SubsequentSibling:
  493. VERIFY(component_list_index != 0);
  494. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  495. if (matches(selector, style_sheet_for_rule, component_list_index - 1, *sibling, scope))
  496. return true;
  497. }
  498. return false;
  499. case CSS::Selector::Combinator::Column:
  500. TODO();
  501. }
  502. VERIFY_NOT_REACHED();
  503. }
  504. bool matches(CSS::Selector const& selector, Optional<CSS::CSSStyleSheet const&> style_sheet_for_rule, DOM::Element const& element, Optional<CSS::Selector::PseudoElement> pseudo_element, JS::GCPtr<DOM::ParentNode const> scope)
  505. {
  506. VERIFY(!selector.compound_selectors().is_empty());
  507. if (pseudo_element.has_value() && selector.pseudo_element() != pseudo_element)
  508. return false;
  509. if (!pseudo_element.has_value() && selector.pseudo_element().has_value())
  510. return false;
  511. return matches(selector, style_sheet_for_rule, selector.compound_selectors().size() - 1, element, scope);
  512. }
  513. }