SelectorEngine.cpp 4.4 KB

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