Selector.h 8.3 KB

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