Selector.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
  3. * Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.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. #include <LibWeb/CSS/Keyword.h>
  13. #include <LibWeb/CSS/Parser/ComponentValue.h>
  14. #include <LibWeb/CSS/PseudoClass.h>
  15. namespace Web::CSS {
  16. using SelectorList = Vector<NonnullRefPtr<class Selector>>;
  17. // This is a <complex-selector> in the spec. https://www.w3.org/TR/selectors-4/#complex
  18. class Selector : public RefCounted<Selector> {
  19. public:
  20. class PseudoElement {
  21. public:
  22. enum class Type : u8 {
  23. Before,
  24. After,
  25. FirstLine,
  26. FirstLetter,
  27. Marker,
  28. MeterBar,
  29. MeterEvenLessGoodValue,
  30. MeterOptimumValue,
  31. MeterSuboptimumValue,
  32. ProgressValue,
  33. ProgressBar,
  34. Placeholder,
  35. Selection,
  36. SliderRunnableTrack,
  37. SliderThumb,
  38. Backdrop,
  39. FileSelectorButton,
  40. DetailsContent,
  41. // Keep this last.
  42. KnownPseudoElementCount,
  43. // https://www.w3.org/TR/selectors-4/#compat
  44. // NOTE: This is not last as the 'unknown -webkit- pseudo-elements' are not stored as part of any Element.
  45. UnknownWebKit,
  46. };
  47. explicit PseudoElement(Type type)
  48. : m_type(type)
  49. {
  50. VERIFY(is_known_pseudo_element_type(type));
  51. }
  52. PseudoElement(Type type, String name)
  53. : m_type(type)
  54. , m_name(move(name))
  55. {
  56. }
  57. bool operator==(PseudoElement const&) const = default;
  58. static Optional<PseudoElement> from_string(FlyString const&);
  59. [[nodiscard]] static bool is_known_pseudo_element_type(Type type)
  60. {
  61. return to_underlying(type) < to_underlying(CSS::Selector::PseudoElement::Type::KnownPseudoElementCount);
  62. }
  63. static StringView name(Selector::PseudoElement::Type pseudo_element);
  64. StringView name() const
  65. {
  66. if (!m_name.is_empty())
  67. return m_name;
  68. return name(m_type);
  69. }
  70. Type type() const { return m_type; }
  71. private:
  72. Type m_type;
  73. String m_name;
  74. };
  75. struct SimpleSelector {
  76. enum class Type : u8 {
  77. Universal,
  78. TagName,
  79. Id,
  80. Class,
  81. Attribute,
  82. PseudoClass,
  83. PseudoElement,
  84. Nesting,
  85. Invalid,
  86. };
  87. struct ANPlusBPattern {
  88. int step_size { 0 }; // "A"
  89. int offset = { 0 }; // "B"
  90. // https://www.w3.org/TR/css-syntax-3/#serializing-anb
  91. String serialize() const
  92. {
  93. // 1. If A is zero, return the serialization of B.
  94. if (step_size == 0) {
  95. return String::number(offset);
  96. }
  97. // 2. Otherwise, let result initially be an empty string.
  98. StringBuilder result;
  99. // 3.
  100. // - A is 1: Append "n" to result.
  101. if (step_size == 1)
  102. result.append('n');
  103. // - A is -1: Append "-n" to result.
  104. else if (step_size == -1)
  105. result.append("-n"sv);
  106. // - A is non-zero: Serialize A and append it to result, then append "n" to result.
  107. else if (step_size != 0)
  108. result.appendff("{}n", step_size);
  109. // 4.
  110. // - B is greater than zero: Append "+" to result, then append the serialization of B to result.
  111. if (offset > 0)
  112. result.appendff("+{}", offset);
  113. // - B is less than zero: Append the serialization of B to result.
  114. if (offset < 0)
  115. result.appendff("{}", offset);
  116. // 5. Return result.
  117. return MUST(result.to_string());
  118. }
  119. };
  120. struct PseudoClassSelector {
  121. PseudoClass type;
  122. // FIXME: We don't need this field on every single SimpleSelector, but it's also annoying to malloc it somewhere.
  123. // Only used when "pseudo_class" is "NthChild" or "NthLastChild".
  124. ANPlusBPattern nth_child_pattern {};
  125. // FIXME: This would make more sense as part of SelectorList but that's currently a `using`
  126. bool is_forgiving { false };
  127. SelectorList argument_selector_list {};
  128. // Used for :lang(en-gb,dk)
  129. Vector<FlyString> languages {};
  130. // Used by :dir()
  131. Optional<Keyword> keyword {};
  132. };
  133. struct Name {
  134. Name(FlyString n)
  135. : name(move(n))
  136. , lowercase_name(name.to_string().to_lowercase().release_value_but_fixme_should_propagate_errors())
  137. {
  138. }
  139. FlyString name;
  140. FlyString lowercase_name;
  141. };
  142. // Equivalent to `<wq-name>`
  143. // https://www.w3.org/TR/selectors-4/#typedef-wq-name
  144. struct QualifiedName {
  145. enum class NamespaceType {
  146. Default, // `E`
  147. None, // `|E`
  148. Any, // `*|E`
  149. Named, // `ns|E`
  150. };
  151. NamespaceType namespace_type { NamespaceType::Default };
  152. FlyString namespace_ {};
  153. Name name;
  154. };
  155. struct Attribute {
  156. enum class MatchType {
  157. HasAttribute,
  158. ExactValueMatch,
  159. ContainsWord, // [att~=val]
  160. ContainsString, // [att*=val]
  161. StartsWithSegment, // [att|=val]
  162. StartsWithString, // [att^=val]
  163. EndsWithString, // [att$=val]
  164. };
  165. enum class CaseType {
  166. DefaultMatch,
  167. CaseSensitiveMatch,
  168. CaseInsensitiveMatch,
  169. };
  170. MatchType match_type;
  171. QualifiedName qualified_name;
  172. String value {};
  173. CaseType case_type;
  174. };
  175. struct Invalid {
  176. Vector<Parser::ComponentValue> component_values;
  177. };
  178. Type type;
  179. Variant<Empty, Attribute, PseudoClassSelector, PseudoElement, Name, QualifiedName, Invalid> value {};
  180. Attribute const& attribute() const { return value.get<Attribute>(); }
  181. Attribute& attribute() { return value.get<Attribute>(); }
  182. PseudoClassSelector const& pseudo_class() const { return value.get<PseudoClassSelector>(); }
  183. PseudoClassSelector& pseudo_class() { return value.get<PseudoClassSelector>(); }
  184. PseudoElement const& pseudo_element() const { return value.get<PseudoElement>(); }
  185. PseudoElement& pseudo_element() { return value.get<PseudoElement>(); }
  186. FlyString const& name() const { return value.get<Name>().name; }
  187. FlyString& name() { return value.get<Name>().name; }
  188. FlyString const& lowercase_name() const { return value.get<Name>().lowercase_name; }
  189. FlyString& lowercase_name() { return value.get<Name>().lowercase_name; }
  190. QualifiedName const& qualified_name() const { return value.get<QualifiedName>(); }
  191. QualifiedName& qualified_name() { return value.get<QualifiedName>(); }
  192. String serialize() const;
  193. Optional<SimpleSelector> absolutized(SimpleSelector const& selector_for_nesting) const;
  194. };
  195. enum class Combinator {
  196. None,
  197. ImmediateChild, // >
  198. Descendant, // <whitespace>
  199. NextSibling, // +
  200. SubsequentSibling, // ~
  201. Column, // ||
  202. };
  203. struct CompoundSelector {
  204. // Spec-wise, the <combinator> is not part of a <compound-selector>,
  205. // but it is more understandable to put them together.
  206. Combinator combinator { Combinator::None };
  207. Vector<SimpleSelector> simple_selectors;
  208. Optional<CompoundSelector> absolutized(SimpleSelector const& selector_for_nesting) const;
  209. };
  210. static NonnullRefPtr<Selector> create(Vector<CompoundSelector>&& compound_selectors)
  211. {
  212. return adopt_ref(*new Selector(move(compound_selectors)));
  213. }
  214. ~Selector() = default;
  215. Vector<CompoundSelector> const& compound_selectors() const { return m_compound_selectors; }
  216. Optional<PseudoElement> const& pseudo_element() const { return m_pseudo_element; }
  217. NonnullRefPtr<Selector> relative_to(SimpleSelector const&) const;
  218. bool contains_the_nesting_selector() const { return m_contains_the_nesting_selector; }
  219. RefPtr<Selector> absolutized(SimpleSelector const& selector_for_nesting) const;
  220. u32 specificity() const;
  221. String serialize() const;
  222. auto const& ancestor_hashes() const { return m_ancestor_hashes; }
  223. private:
  224. explicit Selector(Vector<CompoundSelector>&&);
  225. Vector<CompoundSelector> m_compound_selectors;
  226. mutable Optional<u32> m_specificity;
  227. Optional<Selector::PseudoElement> m_pseudo_element;
  228. bool m_contains_the_nesting_selector { false };
  229. void collect_ancestor_hashes();
  230. Array<u32, 8> m_ancestor_hashes;
  231. };
  232. String serialize_a_group_of_selectors(SelectorList const& selectors);
  233. SelectorList adapt_nested_relative_selector_list(SelectorList const&);
  234. bool is_has_allowed_pseudo_element(Selector::PseudoElement::Type);
  235. }
  236. namespace AK {
  237. template<>
  238. struct Formatter<Web::CSS::Selector> : Formatter<StringView> {
  239. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Selector const& selector)
  240. {
  241. return Formatter<StringView>::format(builder, selector.serialize());
  242. }
  243. };
  244. }