Selector.h 8.3 KB

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