SelectorEngine.cpp 6.8 KB

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