SelectorEngine.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <LibHTML/CSS/SelectorEngine.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/Element.h>
  4. namespace SelectorEngine {
  5. static bool matches_hover_pseudo_class(const Element& element)
  6. {
  7. auto* hovered_node = element.document().hovered_node();
  8. if (!hovered_node)
  9. return false;
  10. if (&element == hovered_node)
  11. return true;
  12. return element.is_ancestor_of(*hovered_node);
  13. }
  14. bool matches(const Selector::SimpleSelector& component, const Element& element)
  15. {
  16. switch (component.pseudo_class) {
  17. case Selector::SimpleSelector::PseudoClass::None:
  18. break;
  19. case Selector::SimpleSelector::PseudoClass::Link:
  20. if (!element.is_link())
  21. return false;
  22. break;
  23. case Selector::SimpleSelector::PseudoClass::Hover:
  24. if (!matches_hover_pseudo_class(element))
  25. return false;
  26. break;
  27. }
  28. switch (component.attribute_match_type) {
  29. case Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  30. if (!element.has_attribute(component.attribute_name))
  31. return false;
  32. break;
  33. case Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  34. if (element.attribute(component.attribute_name) != component.attribute_value)
  35. return false;
  36. break;
  37. default:
  38. break;
  39. }
  40. switch (component.type) {
  41. case Selector::SimpleSelector::Type::Universal:
  42. return true;
  43. case Selector::SimpleSelector::Type::Id:
  44. return component.value == element.attribute("id");
  45. case Selector::SimpleSelector::Type::Class:
  46. return element.has_class(component.value);
  47. case Selector::SimpleSelector::Type::TagName:
  48. return component.value == element.tag_name();
  49. default:
  50. ASSERT_NOT_REACHED();
  51. }
  52. }
  53. bool matches(const Selector& selector, int component_list_index, const Element& element)
  54. {
  55. auto& component_list = selector.complex_selectors()[component_list_index];
  56. for (auto& component : component_list.compound_selector) {
  57. if (!matches(component, element))
  58. return false;
  59. }
  60. switch (component_list.relation) {
  61. case Selector::ComplexSelector::Relation::None:
  62. return true;
  63. case Selector::ComplexSelector::Relation::Descendant:
  64. ASSERT(component_list_index != 0);
  65. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  66. if (!is<Element>(*ancestor))
  67. continue;
  68. if (matches(selector, component_list_index - 1, to<Element>(*ancestor)))
  69. return true;
  70. }
  71. return false;
  72. case Selector::ComplexSelector::Relation::ImmediateChild:
  73. ASSERT(component_list_index != 0);
  74. if (!element.parent() || !is<Element>(*element.parent()))
  75. return false;
  76. return matches(selector, component_list_index - 1, to<Element>(*element.parent()));
  77. case Selector::ComplexSelector::Relation::AdjacentSibling:
  78. ASSERT(component_list_index != 0);
  79. if (auto* sibling = element.previous_element_sibling())
  80. return matches(selector, component_list_index - 1, *sibling);
  81. return false;
  82. case Selector::ComplexSelector::Relation::GeneralSibling:
  83. ASSERT(component_list_index != 0);
  84. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  85. if (matches(selector, component_list_index - 1, *sibling))
  86. return true;
  87. }
  88. return false;
  89. }
  90. ASSERT_NOT_REACHED();
  91. }
  92. bool matches(const Selector& selector, const Element& element)
  93. {
  94. ASSERT(!selector.complex_selectors().is_empty());
  95. return matches(selector, selector.complex_selectors().size() - 1, element);
  96. }
  97. }