SelectorEngine.cpp 7.4 KB

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