SelectorEngine.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.type) {
  29. case Selector::Component::Type::Universal:
  30. return true;
  31. case Selector::Component::Type::Id:
  32. return component.value == element.attribute("id");
  33. case Selector::Component::Type::Class:
  34. return element.has_class(component.value);
  35. case Selector::Component::Type::TagName:
  36. return component.value == element.tag_name();
  37. default:
  38. ASSERT_NOT_REACHED();
  39. }
  40. }
  41. bool matches(const Selector& selector, int component_index, const Element& element)
  42. {
  43. auto& component = selector.components()[component_index];
  44. if (!matches(component, element))
  45. return false;
  46. switch (component.relation) {
  47. case Selector::Component::Relation::None:
  48. return true;
  49. case Selector::Component::Relation::Descendant:
  50. ASSERT(component_index != 0);
  51. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  52. if (!is<Element>(*ancestor))
  53. continue;
  54. if (matches(selector, component_index - 1, to<Element>(*ancestor)))
  55. return true;
  56. }
  57. return false;
  58. case Selector::Component::Relation::ImmediateChild:
  59. ASSERT(component_index != 0);
  60. if (!element.parent() || !is<Element>(*element.parent()))
  61. return false;
  62. return matches(selector, component_index - 1, to<Element>(*element.parent()));
  63. case Selector::Component::Relation::AdjacentSibling:
  64. ASSERT(component_index != 0);
  65. if (auto* sibling = element.previous_element_sibling())
  66. return matches(selector, component_index - 1, *sibling);
  67. return false;
  68. case Selector::Component::Relation::GeneralSibling:
  69. ASSERT(component_index != 0);
  70. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  71. if (matches(selector, component_index - 1, *sibling))
  72. return true;
  73. }
  74. return false;
  75. }
  76. ASSERT_NOT_REACHED();
  77. }
  78. bool matches(const Selector& selector, const Element& element)
  79. {
  80. ASSERT(!selector.components().is_empty());
  81. return matches(selector, selector.components().size() - 1, element);
  82. }
  83. }