SelectorEngine.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/AttributeNames.h>
  28. #include <LibWeb/DOM/Document.h>
  29. #include <LibWeb/DOM/Element.h>
  30. #include <LibWeb/DOM/Text.h>
  31. namespace Web::SelectorEngine {
  32. static bool matches_hover_pseudo_class(const DOM::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 CSS::Selector::SimpleSelector& component, const DOM::Element& element)
  42. {
  43. switch (component.pseudo_class) {
  44. case CSS::Selector::SimpleSelector::PseudoClass::None:
  45. break;
  46. case CSS::Selector::SimpleSelector::PseudoClass::Link:
  47. if (!element.is_link())
  48. return false;
  49. break;
  50. case CSS::Selector::SimpleSelector::PseudoClass::Visited:
  51. // FIXME: Maybe match this selector sometimes?
  52. return false;
  53. case CSS::Selector::SimpleSelector::PseudoClass::Hover:
  54. if (!matches_hover_pseudo_class(element))
  55. return false;
  56. break;
  57. case CSS::Selector::SimpleSelector::PseudoClass::Focus:
  58. // FIXME: Implement matches_focus_pseudo_class(element)
  59. return false;
  60. case CSS::Selector::SimpleSelector::PseudoClass::FirstChild:
  61. if (element.previous_element_sibling())
  62. return false;
  63. break;
  64. case CSS::Selector::SimpleSelector::PseudoClass::LastChild:
  65. if (element.next_element_sibling())
  66. return false;
  67. break;
  68. case CSS::Selector::SimpleSelector::PseudoClass::OnlyChild:
  69. if (element.previous_element_sibling() || element.next_element_sibling())
  70. return false;
  71. break;
  72. case CSS::Selector::SimpleSelector::PseudoClass::Empty:
  73. if (element.first_child_of_type<DOM::Element>() || element.first_child_of_type<DOM::Text>())
  74. return false;
  75. break;
  76. case CSS::Selector::SimpleSelector::PseudoClass::Root:
  77. if (!element.is_html_element())
  78. return false;
  79. break;
  80. }
  81. switch (component.attribute_match_type) {
  82. case CSS::Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  83. if (!element.has_attribute(component.attribute_name))
  84. return false;
  85. break;
  86. case CSS::Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  87. if (element.attribute(component.attribute_name) != component.attribute_value)
  88. return false;
  89. break;
  90. case CSS::Selector::SimpleSelector::AttributeMatchType::Contains:
  91. if (!element.attribute(component.attribute_name).split(' ').contains_slow(component.attribute_value))
  92. return false;
  93. break;
  94. default:
  95. break;
  96. }
  97. switch (component.type) {
  98. case CSS::Selector::SimpleSelector::Type::Universal:
  99. return true;
  100. case CSS::Selector::SimpleSelector::Type::Id:
  101. return component.value == element.attribute(HTML::AttributeNames::id);
  102. case CSS::Selector::SimpleSelector::Type::Class:
  103. return element.has_class(component.value);
  104. case CSS::Selector::SimpleSelector::Type::TagName:
  105. return component.value == element.local_name();
  106. default:
  107. ASSERT_NOT_REACHED();
  108. }
  109. }
  110. bool matches(const CSS::Selector& selector, int component_list_index, const DOM::Element& element)
  111. {
  112. auto& component_list = selector.complex_selectors()[component_list_index];
  113. for (auto& component : component_list.compound_selector) {
  114. if (!matches(component, element))
  115. return false;
  116. }
  117. switch (component_list.relation) {
  118. case CSS::Selector::ComplexSelector::Relation::None:
  119. return true;
  120. case CSS::Selector::ComplexSelector::Relation::Descendant:
  121. ASSERT(component_list_index != 0);
  122. for (auto* ancestor = element.parent(); ancestor; ancestor = ancestor->parent()) {
  123. if (!is<DOM::Element>(*ancestor))
  124. continue;
  125. if (matches(selector, component_list_index - 1, downcast<DOM::Element>(*ancestor)))
  126. return true;
  127. }
  128. return false;
  129. case CSS::Selector::ComplexSelector::Relation::ImmediateChild:
  130. ASSERT(component_list_index != 0);
  131. if (!element.parent() || !is<DOM::Element>(*element.parent()))
  132. return false;
  133. return matches(selector, component_list_index - 1, downcast<DOM::Element>(*element.parent()));
  134. case CSS::Selector::ComplexSelector::Relation::AdjacentSibling:
  135. ASSERT(component_list_index != 0);
  136. if (auto* sibling = element.previous_element_sibling())
  137. return matches(selector, component_list_index - 1, *sibling);
  138. return false;
  139. case CSS::Selector::ComplexSelector::Relation::GeneralSibling:
  140. ASSERT(component_list_index != 0);
  141. for (auto* sibling = element.previous_element_sibling(); sibling; sibling = sibling->previous_element_sibling()) {
  142. if (matches(selector, component_list_index - 1, *sibling))
  143. return true;
  144. }
  145. return false;
  146. }
  147. ASSERT_NOT_REACHED();
  148. }
  149. bool matches(const CSS::Selector& selector, const DOM::Element& element)
  150. {
  151. ASSERT(!selector.complex_selectors().is_empty());
  152. return matches(selector, selector.complex_selectors().size() - 1, element);
  153. }
  154. }