SelectorEngine.cpp 3.0 KB

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