Selector.h 8.2 KB

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