Selector.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/FlyString.h>
  9. #include <AK/NonnullRefPtrVector.h>
  10. #include <AK/RefCounted.h>
  11. #include <AK/String.h>
  12. #include <AK/Vector.h>
  13. namespace Web::CSS {
  14. using SelectorList = NonnullRefPtrVector<class Selector>;
  15. // This is a <complex-selector> in the spec. https://www.w3.org/TR/selectors-4/#complex
  16. class Selector : public RefCounted<Selector> {
  17. public:
  18. enum class PseudoElement {
  19. None,
  20. Before,
  21. After,
  22. FirstLine,
  23. FirstLetter,
  24. Marker,
  25. };
  26. static auto constexpr PseudoElementCount = to_underlying(PseudoElement::Marker) + 1;
  27. struct SimpleSelector {
  28. enum class Type {
  29. Invalid,
  30. Universal,
  31. TagName,
  32. Id,
  33. Class,
  34. Attribute,
  35. PseudoClass,
  36. PseudoElement,
  37. };
  38. Type type { Type::Invalid };
  39. struct ANPlusBPattern {
  40. int step_size { 0 }; // "A"
  41. int offset = { 0 }; // "B"
  42. String serialize() const
  43. {
  44. return String::formatted("{}n{:+}", step_size, offset);
  45. }
  46. };
  47. struct PseudoClass {
  48. enum class Type {
  49. None,
  50. Link,
  51. Visited,
  52. Hover,
  53. Focus,
  54. FirstChild,
  55. LastChild,
  56. OnlyChild,
  57. NthChild,
  58. NthLastChild,
  59. Empty,
  60. Root,
  61. FirstOfType,
  62. LastOfType,
  63. OnlyOfType,
  64. NthOfType,
  65. NthLastOfType,
  66. Disabled,
  67. Enabled,
  68. Checked,
  69. Not,
  70. Active,
  71. };
  72. Type type { Type::None };
  73. // FIXME: We don't need this field on every single SimpleSelector, but it's also annoying to malloc it somewhere.
  74. // Only used when "pseudo_class" is "NthChild" or "NthLastChild".
  75. ANPlusBPattern nth_child_pattern;
  76. SelectorList not_selector {};
  77. };
  78. PseudoClass pseudo_class {};
  79. PseudoElement pseudo_element { PseudoElement::None };
  80. FlyString value {};
  81. struct Attribute {
  82. enum class MatchType {
  83. None,
  84. HasAttribute,
  85. ExactValueMatch,
  86. ContainsWord, // [att~=val]
  87. ContainsString, // [att*=val]
  88. StartsWithSegment, // [att|=val]
  89. StartsWithString, // [att^=val]
  90. EndsWithString, // [att$=val]
  91. };
  92. MatchType match_type { MatchType::None };
  93. FlyString name {};
  94. String value {};
  95. };
  96. Attribute attribute {};
  97. String serialize() const;
  98. };
  99. enum class Combinator {
  100. None,
  101. ImmediateChild, // >
  102. Descendant, // <whitespace>
  103. NextSibling, // +
  104. SubsequentSibling, // ~
  105. Column, // ||
  106. };
  107. struct CompoundSelector {
  108. // Spec-wise, the <combinator> is not part of a <compound-selector>,
  109. // but it is more understandable to put them together.
  110. Combinator combinator { Combinator::None };
  111. Vector<SimpleSelector> simple_selectors;
  112. };
  113. static NonnullRefPtr<Selector> create(Vector<CompoundSelector>&& compound_selectors)
  114. {
  115. return adopt_ref(*new Selector(move(compound_selectors)));
  116. }
  117. ~Selector();
  118. Vector<CompoundSelector> const& compound_selectors() const { return m_compound_selectors; }
  119. Optional<PseudoElement> pseudo_element() const { return m_pseudo_element; }
  120. u32 specificity() const;
  121. String serialize() const;
  122. private:
  123. explicit Selector(Vector<CompoundSelector>&&);
  124. Vector<CompoundSelector> m_compound_selectors;
  125. mutable Optional<u32> m_specificity;
  126. Optional<Selector::PseudoElement> m_pseudo_element;
  127. };
  128. constexpr StringView pseudo_element_name(Selector::PseudoElement pseudo_element)
  129. {
  130. switch (pseudo_element) {
  131. case Selector::PseudoElement::Before:
  132. return "before"sv;
  133. case Selector::PseudoElement::After:
  134. return "after"sv;
  135. case Selector::PseudoElement::FirstLine:
  136. return "first-line"sv;
  137. case Selector::PseudoElement::FirstLetter:
  138. return "first-letter"sv;
  139. case Selector::PseudoElement::Marker:
  140. return "marker"sv;
  141. case Selector::PseudoElement::None:
  142. break;
  143. }
  144. VERIFY_NOT_REACHED();
  145. }
  146. Optional<Selector::PseudoElement> pseudo_element_from_string(StringView);
  147. constexpr StringView pseudo_class_name(Selector::SimpleSelector::PseudoClass::Type pseudo_class)
  148. {
  149. switch (pseudo_class) {
  150. case Selector::SimpleSelector::PseudoClass::Type::Link:
  151. return "link"sv;
  152. case Selector::SimpleSelector::PseudoClass::Type::Visited:
  153. return "visited"sv;
  154. case Selector::SimpleSelector::PseudoClass::Type::Hover:
  155. return "hover"sv;
  156. case Selector::SimpleSelector::PseudoClass::Type::Focus:
  157. return "focus"sv;
  158. case Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  159. return "first-child"sv;
  160. case Selector::SimpleSelector::PseudoClass::Type::LastChild:
  161. return "last-child"sv;
  162. case Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  163. return "only-child"sv;
  164. case Selector::SimpleSelector::PseudoClass::Type::Empty:
  165. return "empty"sv;
  166. case Selector::SimpleSelector::PseudoClass::Type::Root:
  167. return "root"sv;
  168. case Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  169. return "first-of-type"sv;
  170. case Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  171. return "last-of-type"sv;
  172. case Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  173. return "only-of-type"sv;
  174. case Selector::SimpleSelector::PseudoClass::Type::NthOfType:
  175. return "nth-of-type"sv;
  176. case Selector::SimpleSelector::PseudoClass::Type::NthLastOfType:
  177. return "nth-last-of-type"sv;
  178. case Selector::SimpleSelector::PseudoClass::Type::Disabled:
  179. return "disabled"sv;
  180. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  181. return "enabled"sv;
  182. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  183. return "checked"sv;
  184. case Selector::SimpleSelector::PseudoClass::Type::Active:
  185. return "active"sv;
  186. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  187. return "nth-child"sv;
  188. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  189. return "nth-last-child"sv;
  190. case Selector::SimpleSelector::PseudoClass::Type::Not:
  191. return "not"sv;
  192. case Selector::SimpleSelector::PseudoClass::Type::None:
  193. break;
  194. }
  195. VERIFY_NOT_REACHED();
  196. }
  197. String serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors);
  198. }
  199. namespace AK {
  200. template<>
  201. struct Formatter<Web::CSS::Selector> : Formatter<StringView> {
  202. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Selector const& selector)
  203. {
  204. return Formatter<StringView>::format(builder, selector.serialize());
  205. }
  206. };
  207. }