Selector.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, 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/NonnullRefPtrVector.h>
  10. #include <AK/RefCounted.h>
  11. #include <AK/String.h>
  12. #include <AK/Vector.h>
  13. namespace Web::CSS {
  14. using SelectorList = NonnullRefPtrVector<class Selector>;
  15. // This is a <complex-selector> in the spec. https://www.w3.org/TR/selectors-4/#complex
  16. class Selector : public RefCounted<Selector> {
  17. public:
  18. enum class PseudoElement {
  19. None,
  20. Before,
  21. After,
  22. FirstLine,
  23. FirstLetter,
  24. Marker,
  25. };
  26. static auto constexpr PseudoElementCount = to_underlying(PseudoElement::Marker) + 1;
  27. struct SimpleSelector {
  28. enum class Type {
  29. Invalid,
  30. Universal,
  31. TagName,
  32. Id,
  33. Class,
  34. Attribute,
  35. PseudoClass,
  36. PseudoElement,
  37. };
  38. Type type { Type::Invalid };
  39. struct ANPlusBPattern {
  40. int step_size { 0 }; // "A"
  41. int offset = { 0 }; // "B"
  42. String serialize() const
  43. {
  44. return String::formatted("{}n{:+}", step_size, offset);
  45. }
  46. };
  47. struct PseudoClass {
  48. enum class Type {
  49. None,
  50. Link,
  51. Visited,
  52. Hover,
  53. Focus,
  54. FocusWithin,
  55. FirstChild,
  56. LastChild,
  57. OnlyChild,
  58. NthChild,
  59. NthLastChild,
  60. Empty,
  61. Root,
  62. FirstOfType,
  63. LastOfType,
  64. OnlyOfType,
  65. NthOfType,
  66. NthLastOfType,
  67. Disabled,
  68. Enabled,
  69. Checked,
  70. Is,
  71. Not,
  72. Where,
  73. Active,
  74. Lang,
  75. };
  76. Type type { Type::None };
  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. };
  84. PseudoClass pseudo_class {};
  85. PseudoElement pseudo_element { PseudoElement::None };
  86. FlyString value {};
  87. struct Attribute {
  88. enum class MatchType {
  89. None,
  90. HasAttribute,
  91. ExactValueMatch,
  92. ContainsWord, // [att~=val]
  93. ContainsString, // [att*=val]
  94. StartsWithSegment, // [att|=val]
  95. StartsWithString, // [att^=val]
  96. EndsWithString, // [att$=val]
  97. };
  98. MatchType match_type { MatchType::None };
  99. FlyString name {};
  100. String value {};
  101. };
  102. Attribute attribute {};
  103. String serialize() const;
  104. };
  105. enum class Combinator {
  106. None,
  107. ImmediateChild, // >
  108. Descendant, // <whitespace>
  109. NextSibling, // +
  110. SubsequentSibling, // ~
  111. Column, // ||
  112. };
  113. struct CompoundSelector {
  114. // Spec-wise, the <combinator> is not part of a <compound-selector>,
  115. // but it is more understandable to put them together.
  116. Combinator combinator { Combinator::None };
  117. Vector<SimpleSelector> simple_selectors;
  118. };
  119. static NonnullRefPtr<Selector> create(Vector<CompoundSelector>&& compound_selectors)
  120. {
  121. return adopt_ref(*new Selector(move(compound_selectors)));
  122. }
  123. ~Selector() = default;
  124. Vector<CompoundSelector> const& compound_selectors() const { return m_compound_selectors; }
  125. Optional<PseudoElement> pseudo_element() const { return m_pseudo_element; }
  126. u32 specificity() const;
  127. String serialize() const;
  128. private:
  129. explicit Selector(Vector<CompoundSelector>&&);
  130. Vector<CompoundSelector> m_compound_selectors;
  131. mutable Optional<u32> m_specificity;
  132. Optional<Selector::PseudoElement> m_pseudo_element;
  133. };
  134. constexpr StringView pseudo_element_name(Selector::PseudoElement pseudo_element)
  135. {
  136. switch (pseudo_element) {
  137. case Selector::PseudoElement::Before:
  138. return "before"sv;
  139. case Selector::PseudoElement::After:
  140. return "after"sv;
  141. case Selector::PseudoElement::FirstLine:
  142. return "first-line"sv;
  143. case Selector::PseudoElement::FirstLetter:
  144. return "first-letter"sv;
  145. case Selector::PseudoElement::Marker:
  146. return "marker"sv;
  147. case Selector::PseudoElement::None:
  148. break;
  149. }
  150. VERIFY_NOT_REACHED();
  151. }
  152. Optional<Selector::PseudoElement> pseudo_element_from_string(StringView);
  153. constexpr StringView pseudo_class_name(Selector::SimpleSelector::PseudoClass::Type pseudo_class)
  154. {
  155. switch (pseudo_class) {
  156. case Selector::SimpleSelector::PseudoClass::Type::Link:
  157. return "link"sv;
  158. case Selector::SimpleSelector::PseudoClass::Type::Visited:
  159. return "visited"sv;
  160. case Selector::SimpleSelector::PseudoClass::Type::Hover:
  161. return "hover"sv;
  162. case Selector::SimpleSelector::PseudoClass::Type::Focus:
  163. return "focus"sv;
  164. case Selector::SimpleSelector::PseudoClass::Type::FocusWithin:
  165. return "focus-within"sv;
  166. case Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  167. return "first-child"sv;
  168. case Selector::SimpleSelector::PseudoClass::Type::LastChild:
  169. return "last-child"sv;
  170. case Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  171. return "only-child"sv;
  172. case Selector::SimpleSelector::PseudoClass::Type::Empty:
  173. return "empty"sv;
  174. case Selector::SimpleSelector::PseudoClass::Type::Root:
  175. return "root"sv;
  176. case Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  177. return "first-of-type"sv;
  178. case Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  179. return "last-of-type"sv;
  180. case Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  181. return "only-of-type"sv;
  182. case Selector::SimpleSelector::PseudoClass::Type::NthOfType:
  183. return "nth-of-type"sv;
  184. case Selector::SimpleSelector::PseudoClass::Type::NthLastOfType:
  185. return "nth-last-of-type"sv;
  186. case Selector::SimpleSelector::PseudoClass::Type::Disabled:
  187. return "disabled"sv;
  188. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  189. return "enabled"sv;
  190. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  191. return "checked"sv;
  192. case Selector::SimpleSelector::PseudoClass::Type::Active:
  193. return "active"sv;
  194. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  195. return "nth-child"sv;
  196. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  197. return "nth-last-child"sv;
  198. case Selector::SimpleSelector::PseudoClass::Type::Is:
  199. return "is"sv;
  200. case Selector::SimpleSelector::PseudoClass::Type::Not:
  201. return "not"sv;
  202. case Selector::SimpleSelector::PseudoClass::Type::Where:
  203. return "where"sv;
  204. case Selector::SimpleSelector::PseudoClass::Type::Lang:
  205. return "lang"sv;
  206. case Selector::SimpleSelector::PseudoClass::Type::None:
  207. break;
  208. }
  209. VERIFY_NOT_REACHED();
  210. }
  211. String serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors);
  212. }
  213. namespace AK {
  214. template<>
  215. struct Formatter<Web::CSS::Selector> : Formatter<StringView> {
  216. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Selector const& selector)
  217. {
  218. return Formatter<StringView>::format(builder, selector.serialize());
  219. }
  220. };
  221. }