Selector.h 8.5 KB

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