Selector.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. Before,
  20. After,
  21. FirstLine,
  22. FirstLetter,
  23. Marker,
  24. };
  25. static auto constexpr PseudoElementCount = to_underlying(PseudoElement::Marker) + 1;
  26. struct SimpleSelector {
  27. enum class Type {
  28. Universal,
  29. TagName,
  30. Id,
  31. Class,
  32. Attribute,
  33. PseudoClass,
  34. PseudoElement,
  35. };
  36. struct ANPlusBPattern {
  37. int step_size { 0 }; // "A"
  38. int offset = { 0 }; // "B"
  39. String serialize() const
  40. {
  41. return String::formatted("{}n{:+}", step_size, offset);
  42. }
  43. };
  44. struct PseudoClass {
  45. enum class Type {
  46. Link,
  47. Visited,
  48. Hover,
  49. Focus,
  50. FocusWithin,
  51. FirstChild,
  52. LastChild,
  53. OnlyChild,
  54. NthChild,
  55. NthLastChild,
  56. Empty,
  57. Root,
  58. FirstOfType,
  59. LastOfType,
  60. OnlyOfType,
  61. NthOfType,
  62. NthLastOfType,
  63. Disabled,
  64. Enabled,
  65. Checked,
  66. Is,
  67. Not,
  68. Where,
  69. Active,
  70. Lang,
  71. };
  72. Type type;
  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 argument_selector_list {};
  77. // Used for :lang(en-gb,dk)
  78. Vector<FlyString> languages {};
  79. };
  80. struct Attribute {
  81. enum class MatchType {
  82. HasAttribute,
  83. ExactValueMatch,
  84. ContainsWord, // [att~=val]
  85. ContainsString, // [att*=val]
  86. StartsWithSegment, // [att|=val]
  87. StartsWithString, // [att^=val]
  88. EndsWithString, // [att$=val]
  89. };
  90. MatchType match_type;
  91. FlyString name {};
  92. String value {};
  93. };
  94. Type type;
  95. Variant<Empty, Attribute, PseudoClass, PseudoElement, FlyString> value {};
  96. Attribute const& attribute() const { return value.get<Attribute>(); }
  97. Attribute& attribute() { return value.get<Attribute>(); }
  98. PseudoClass const& pseudo_class() const { return value.get<PseudoClass>(); }
  99. PseudoClass& pseudo_class() { return value.get<PseudoClass>(); }
  100. PseudoElement const& pseudo_element() const { return value.get<PseudoElement>(); }
  101. PseudoElement& pseudo_element() { return value.get<PseudoElement>(); }
  102. FlyString const& name() const { return value.get<FlyString>(); }
  103. FlyString& name() { return value.get<FlyString>(); }
  104. String serialize() const;
  105. };
  106. enum class Combinator {
  107. None,
  108. ImmediateChild, // >
  109. Descendant, // <whitespace>
  110. NextSibling, // +
  111. SubsequentSibling, // ~
  112. Column, // ||
  113. };
  114. struct CompoundSelector {
  115. // Spec-wise, the <combinator> is not part of a <compound-selector>,
  116. // but it is more understandable to put them together.
  117. Combinator combinator { Combinator::None };
  118. Vector<SimpleSelector> simple_selectors;
  119. };
  120. static NonnullRefPtr<Selector> create(Vector<CompoundSelector>&& compound_selectors)
  121. {
  122. return adopt_ref(*new Selector(move(compound_selectors)));
  123. }
  124. ~Selector() = default;
  125. Vector<CompoundSelector> const& compound_selectors() const { return m_compound_selectors; }
  126. Optional<PseudoElement> pseudo_element() const { return m_pseudo_element; }
  127. u32 specificity() const;
  128. String serialize() const;
  129. private:
  130. explicit Selector(Vector<CompoundSelector>&&);
  131. Vector<CompoundSelector> m_compound_selectors;
  132. mutable Optional<u32> m_specificity;
  133. Optional<Selector::PseudoElement> m_pseudo_element;
  134. };
  135. constexpr StringView pseudo_element_name(Selector::PseudoElement pseudo_element)
  136. {
  137. switch (pseudo_element) {
  138. case Selector::PseudoElement::Before:
  139. return "before"sv;
  140. case Selector::PseudoElement::After:
  141. return "after"sv;
  142. case Selector::PseudoElement::FirstLine:
  143. return "first-line"sv;
  144. case Selector::PseudoElement::FirstLetter:
  145. return "first-letter"sv;
  146. case Selector::PseudoElement::Marker:
  147. return "marker"sv;
  148. }
  149. VERIFY_NOT_REACHED();
  150. }
  151. Optional<Selector::PseudoElement> pseudo_element_from_string(StringView);
  152. constexpr StringView pseudo_class_name(Selector::SimpleSelector::PseudoClass::Type pseudo_class)
  153. {
  154. switch (pseudo_class) {
  155. case Selector::SimpleSelector::PseudoClass::Type::Link:
  156. return "link"sv;
  157. case Selector::SimpleSelector::PseudoClass::Type::Visited:
  158. return "visited"sv;
  159. case Selector::SimpleSelector::PseudoClass::Type::Hover:
  160. return "hover"sv;
  161. case Selector::SimpleSelector::PseudoClass::Type::Focus:
  162. return "focus"sv;
  163. case Selector::SimpleSelector::PseudoClass::Type::FocusWithin:
  164. return "focus-within"sv;
  165. case Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  166. return "first-child"sv;
  167. case Selector::SimpleSelector::PseudoClass::Type::LastChild:
  168. return "last-child"sv;
  169. case Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  170. return "only-child"sv;
  171. case Selector::SimpleSelector::PseudoClass::Type::Empty:
  172. return "empty"sv;
  173. case Selector::SimpleSelector::PseudoClass::Type::Root:
  174. return "root"sv;
  175. case Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  176. return "first-of-type"sv;
  177. case Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  178. return "last-of-type"sv;
  179. case Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  180. return "only-of-type"sv;
  181. case Selector::SimpleSelector::PseudoClass::Type::NthOfType:
  182. return "nth-of-type"sv;
  183. case Selector::SimpleSelector::PseudoClass::Type::NthLastOfType:
  184. return "nth-last-of-type"sv;
  185. case Selector::SimpleSelector::PseudoClass::Type::Disabled:
  186. return "disabled"sv;
  187. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  188. return "enabled"sv;
  189. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  190. return "checked"sv;
  191. case Selector::SimpleSelector::PseudoClass::Type::Active:
  192. return "active"sv;
  193. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  194. return "nth-child"sv;
  195. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  196. return "nth-last-child"sv;
  197. case Selector::SimpleSelector::PseudoClass::Type::Is:
  198. return "is"sv;
  199. case Selector::SimpleSelector::PseudoClass::Type::Not:
  200. return "not"sv;
  201. case Selector::SimpleSelector::PseudoClass::Type::Where:
  202. return "where"sv;
  203. case Selector::SimpleSelector::PseudoClass::Type::Lang:
  204. return "lang"sv;
  205. }
  206. VERIFY_NOT_REACHED();
  207. }
  208. String serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors);
  209. }
  210. namespace AK {
  211. template<>
  212. struct Formatter<Web::CSS::Selector> : Formatter<StringView> {
  213. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Selector const& selector)
  214. {
  215. return Formatter<StringView>::format(builder, selector.serialize());
  216. }
  217. };
  218. }