Selector.h 11 KB

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