SelectorEngine.cpp 24 KB

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