SelectorEngine.cpp 26 KB

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