Selector.h 12 KB

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