SelectorEngine.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibWeb/CSS/SelectorEngine.h>
  27. #include <LibWeb/DOM/Document.h>
  28. #include <LibWeb/DOM/Element.h>
  29. #include <LibWeb/DOM/Text.h>
  30. namespace Web {
  31. namespace SelectorEngine {
  32. static bool matches_hover_pseudo_class(const Element& element)
  33. {
  34. auto* hovered_node = element.document().hovered_node();
  35. if (!hovered_node)
  36. return false;
  37. if (&element == hovered_node)
  38. return true;
  39. return element.is_ancestor_of(*hovered_node);
  40. }
  41. bool matches(const Selector::SimpleSelector& component, const Element& element)
  42. {
  43. switch (component.pseudo_class) {
  44. case Selector::SimpleSelector::PseudoClass::None:
  45. break;
  46. case Selector::SimpleSelector::PseudoClass::Link:
  47. if (!element.is_link())
  48. return false;
  49. break;
  50. case Selector::SimpleSelector::PseudoClass::Hover:
  51. if (!matches_hover_pseudo_class(element))
  52. return false;
  53. break;
  54. case Selector::SimpleSelector::PseudoClass::Focus:
  55. // FIXME: Implement matches_focus_pseudo_class(element)
  56. return false;
  57. case Selector::SimpleSelector::PseudoClass::FirstChild:
  58. if (element.previous_element_sibling())
  59. return false;
  60. break;
  61. case Selector::SimpleSelector::PseudoClass::LastChild:
  62. if (element.next_element_sibling())
  63. return false;
  64. break;
  65. case Selector::SimpleSelector::PseudoClass::OnlyChild:
  66. if (element.previous_element_sibling() || element.next_element_sibling())
  67. return false;
  68. break;
  69. case Selector::SimpleSelector::PseudoClass::Empty:
  70. if (element.first_child_of_type<Element>() || element.first_child_of_type<Text>())
  71. return false;
  72. break;
  73. case Selector::SimpleSelector::PseudoClass::Root:
  74. if (!element.is_html_element())
  75. return false;
  76. break;
  77. }
  78. switch (component.attribute_match_type) {
  79. case Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  80. if (!element.has_attribute(component.attribute_name))
  81. return false;
  82. break;
  83. case Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  84. if (element.attribute(component.attribute_name) != component.attribute_value)
  85. return false;
  86. break;
  87. default:
  88. break;
  89. }
  90. switch (component.type) {
  91. case Selector::SimpleSelector::Type::Universal:
  92. return true;
  93. case Selector::SimpleSelector::Type::Id:
  94. return component.value == element.attribute("id");
  95. case Selector::SimpleSelector::Type::Class:
  96. return element.has_class(component.value);
  97. case Selector::SimpleSelector::Type::TagName:
  98. return component.value == element.tag_name();
  99. default:
  100. ASSERT_NOT_REACHED();
  101. }
  102. }
  103. bool matches(const Selector& selector, int component_list_index, const Element& element)
  104. {
  105. auto& component_list = selector.complex_selectors()[component_list_index];
  106. for (auto& component : component_list.compound_selector) {
  107. if (!matches(component, element))
  108. return false;
  109. }
  110. switch (component_list.relation) {
  111. case Selector::ComplexSelector::Relation::None:
  112. return true;
  113. case Selector::ComplexSelector::Relation::Descendant:
  114. ASSERT(component_list_index != 0);
  115. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  116. if (!is<Element>(*ancestor))
  117. continue;
  118. if (matches(selector, component_list_index - 1, to<Element>(*ancestor)))
  119. return true;
  120. }
  121. return false;
  122. case Selector::ComplexSelector::Relation::ImmediateChild:
  123. ASSERT(component_list_index != 0);
  124. if (!element.parent() || !is<Element>(*element.parent()))
  125. return false;
  126. return matches(selector, component_list_index - 1, to<Element>(*element.parent()));
  127. case Selector::ComplexSelector::Relation::AdjacentSibling:
  128. ASSERT(component_list_index != 0);
  129. if (auto* sibling = element.previous_element_sibling())
  130. return matches(selector, component_list_index - 1, *sibling);
  131. return false;
  132. case Selector::ComplexSelector::Relation::GeneralSibling:
  133. ASSERT(component_list_index != 0);
  134. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  135. if (matches(selector, component_list_index - 1, *sibling))
  136. return true;
  137. }
  138. return false;
  139. }
  140. ASSERT_NOT_REACHED();
  141. }
  142. bool matches(const Selector& selector, const Element& element)
  143. {
  144. ASSERT(!selector.complex_selectors().is_empty());
  145. return matches(selector, selector.complex_selectors().size() - 1, element);
  146. }
  147. }
  148. }