Selector.h 9.4 KB

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