Selector.h 10 KB

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