Selector.h 8.0 KB

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