Selector.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. Type type;
  128. Variant<Empty, Attribute, PseudoClass, PseudoElement, FlyString> value {};
  129. Attribute const& attribute() const { return value.get<Attribute>(); }
  130. Attribute& attribute() { return value.get<Attribute>(); }
  131. PseudoClass const& pseudo_class() const { return value.get<PseudoClass>(); }
  132. PseudoClass& pseudo_class() { return value.get<PseudoClass>(); }
  133. PseudoElement const& pseudo_element() const { return value.get<PseudoElement>(); }
  134. PseudoElement& pseudo_element() { return value.get<PseudoElement>(); }
  135. FlyString const& name() const { return value.get<FlyString>(); }
  136. FlyString& name() { return value.get<FlyString>(); }
  137. String serialize() const;
  138. };
  139. enum class Combinator {
  140. None,
  141. ImmediateChild, // >
  142. Descendant, // <whitespace>
  143. NextSibling, // +
  144. SubsequentSibling, // ~
  145. Column, // ||
  146. };
  147. struct CompoundSelector {
  148. // Spec-wise, the <combinator> is not part of a <compound-selector>,
  149. // but it is more understandable to put them together.
  150. Combinator combinator { Combinator::None };
  151. Vector<SimpleSelector> simple_selectors;
  152. };
  153. static NonnullRefPtr<Selector> create(Vector<CompoundSelector>&& compound_selectors)
  154. {
  155. return adopt_ref(*new Selector(move(compound_selectors)));
  156. }
  157. ~Selector() = default;
  158. Vector<CompoundSelector> const& compound_selectors() const { return m_compound_selectors; }
  159. Optional<PseudoElement> pseudo_element() const { return m_pseudo_element; }
  160. u32 specificity() const;
  161. String serialize() const;
  162. private:
  163. explicit Selector(Vector<CompoundSelector>&&);
  164. Vector<CompoundSelector> m_compound_selectors;
  165. mutable Optional<u32> m_specificity;
  166. Optional<Selector::PseudoElement> m_pseudo_element;
  167. };
  168. constexpr StringView pseudo_element_name(Selector::PseudoElement pseudo_element)
  169. {
  170. switch (pseudo_element) {
  171. case Selector::PseudoElement::Before:
  172. return "before"sv;
  173. case Selector::PseudoElement::After:
  174. return "after"sv;
  175. case Selector::PseudoElement::FirstLine:
  176. return "first-line"sv;
  177. case Selector::PseudoElement::FirstLetter:
  178. return "first-letter"sv;
  179. case Selector::PseudoElement::Marker:
  180. return "marker"sv;
  181. case Selector::PseudoElement::ProgressBar:
  182. return "-webkit-progress-bar"sv;
  183. case Selector::PseudoElement::ProgressValue:
  184. return "-webkit-progress-value"sv;
  185. }
  186. VERIFY_NOT_REACHED();
  187. }
  188. Optional<Selector::PseudoElement> pseudo_element_from_string(StringView);
  189. constexpr StringView pseudo_class_name(Selector::SimpleSelector::PseudoClass::Type pseudo_class)
  190. {
  191. switch (pseudo_class) {
  192. case Selector::SimpleSelector::PseudoClass::Type::Link:
  193. return "link"sv;
  194. case Selector::SimpleSelector::PseudoClass::Type::Visited:
  195. return "visited"sv;
  196. case Selector::SimpleSelector::PseudoClass::Type::Hover:
  197. return "hover"sv;
  198. case Selector::SimpleSelector::PseudoClass::Type::Focus:
  199. return "focus"sv;
  200. case Selector::SimpleSelector::PseudoClass::Type::FocusWithin:
  201. return "focus-within"sv;
  202. case Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  203. return "first-child"sv;
  204. case Selector::SimpleSelector::PseudoClass::Type::LastChild:
  205. return "last-child"sv;
  206. case Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  207. return "only-child"sv;
  208. case Selector::SimpleSelector::PseudoClass::Type::Empty:
  209. return "empty"sv;
  210. case Selector::SimpleSelector::PseudoClass::Type::Root:
  211. return "root"sv;
  212. case Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  213. return "first-of-type"sv;
  214. case Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  215. return "last-of-type"sv;
  216. case Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  217. return "only-of-type"sv;
  218. case Selector::SimpleSelector::PseudoClass::Type::NthOfType:
  219. return "nth-of-type"sv;
  220. case Selector::SimpleSelector::PseudoClass::Type::NthLastOfType:
  221. return "nth-last-of-type"sv;
  222. case Selector::SimpleSelector::PseudoClass::Type::Disabled:
  223. return "disabled"sv;
  224. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  225. return "enabled"sv;
  226. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  227. return "checked"sv;
  228. case Selector::SimpleSelector::PseudoClass::Type::Active:
  229. return "active"sv;
  230. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  231. return "nth-child"sv;
  232. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  233. return "nth-last-child"sv;
  234. case Selector::SimpleSelector::PseudoClass::Type::Is:
  235. return "is"sv;
  236. case Selector::SimpleSelector::PseudoClass::Type::Not:
  237. return "not"sv;
  238. case Selector::SimpleSelector::PseudoClass::Type::Where:
  239. return "where"sv;
  240. case Selector::SimpleSelector::PseudoClass::Type::Lang:
  241. return "lang"sv;
  242. }
  243. VERIFY_NOT_REACHED();
  244. }
  245. String serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors);
  246. }
  247. namespace AK {
  248. template<>
  249. struct Formatter<Web::CSS::Selector> : Formatter<StringView> {
  250. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Selector const& selector)
  251. {
  252. return Formatter<StringView>::format(builder, selector.serialize());
  253. }
  254. };
  255. }