SelectorEngine.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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::Component& component, const Element& element)
  15. {
  16. switch (component.pseudo_class) {
  17. case Selector::Component::PseudoClass::None:
  18. break;
  19. case Selector::Component::PseudoClass::Link:
  20. if (!element.is_link())
  21. return false;
  22. break;
  23. case Selector::Component::PseudoClass::Hover:
  24. if (!matches_hover_pseudo_class(element))
  25. return false;
  26. break;
  27. }
  28. switch (component.attribute_match_type) {
  29. case Selector::Component::AttributeMatchType::HasAttribute:
  30. if (!element.has_attribute(component.attribute_name))
  31. return false;
  32. break;
  33. case Selector::Component::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::Component::Type::Universal:
  42. return true;
  43. case Selector::Component::Type::Id:
  44. return component.value == element.attribute("id");
  45. case Selector::Component::Type::Class:
  46. return element.has_class(component.value);
  47. case Selector::Component::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_index, const Element& element)
  54. {
  55. auto& component = selector.components()[component_index];
  56. if (!matches(component, element))
  57. return false;
  58. switch (component.relation) {
  59. case Selector::Component::Relation::None:
  60. return true;
  61. case Selector::Component::Relation::Descendant:
  62. ASSERT(component_index != 0);
  63. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  64. if (!is<Element>(*ancestor))
  65. continue;
  66. if (matches(selector, component_index - 1, to<Element>(*ancestor)))
  67. return true;
  68. }
  69. return false;
  70. case Selector::Component::Relation::ImmediateChild:
  71. ASSERT(component_index != 0);
  72. if (!element.parent() || !is<Element>(*element.parent()))
  73. return false;
  74. return matches(selector, component_index - 1, to<Element>(*element.parent()));
  75. case Selector::Component::Relation::AdjacentSibling:
  76. ASSERT(component_index != 0);
  77. if (auto* sibling = element.previous_element_sibling())
  78. return matches(selector, component_index - 1, *sibling);
  79. return false;
  80. case Selector::Component::Relation::GeneralSibling:
  81. ASSERT(component_index != 0);
  82. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  83. if (matches(selector, component_index - 1, *sibling))
  84. return true;
  85. }
  86. return false;
  87. }
  88. ASSERT_NOT_REACHED();
  89. }
  90. bool matches(const Selector& selector, const Element& element)
  91. {
  92. ASSERT(!selector.components().is_empty());
  93. return matches(selector, selector.components().size() - 1, element);
  94. }
  95. }