Selector.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. FirstChild,
  55. LastChild,
  56. OnlyChild,
  57. NthChild,
  58. NthLastChild,
  59. Empty,
  60. Root,
  61. FirstOfType,
  62. LastOfType,
  63. OnlyOfType,
  64. NthOfType,
  65. NthLastOfType,
  66. Disabled,
  67. Enabled,
  68. Checked,
  69. Is,
  70. Not,
  71. Active,
  72. };
  73. Type type { Type::None };
  74. // FIXME: We don't need this field on every single SimpleSelector, but it's also annoying to malloc it somewhere.
  75. // Only used when "pseudo_class" is "NthChild" or "NthLastChild".
  76. ANPlusBPattern nth_child_pattern;
  77. SelectorList argument_selector_list {};
  78. };
  79. PseudoClass pseudo_class {};
  80. PseudoElement pseudo_element { PseudoElement::None };
  81. FlyString value {};
  82. struct Attribute {
  83. enum class MatchType {
  84. None,
  85. HasAttribute,
  86. ExactValueMatch,
  87. ContainsWord, // [att~=val]
  88. ContainsString, // [att*=val]
  89. StartsWithSegment, // [att|=val]
  90. StartsWithString, // [att^=val]
  91. EndsWithString, // [att$=val]
  92. };
  93. MatchType match_type { MatchType::None };
  94. FlyString name {};
  95. String value {};
  96. };
  97. Attribute attribute {};
  98. String serialize() const;
  99. };
  100. enum class Combinator {
  101. None,
  102. ImmediateChild, // >
  103. Descendant, // <whitespace>
  104. NextSibling, // +
  105. SubsequentSibling, // ~
  106. Column, // ||
  107. };
  108. struct CompoundSelector {
  109. // Spec-wise, the <combinator> is not part of a <compound-selector>,
  110. // but it is more understandable to put them together.
  111. Combinator combinator { Combinator::None };
  112. Vector<SimpleSelector> simple_selectors;
  113. };
  114. static NonnullRefPtr<Selector> create(Vector<CompoundSelector>&& compound_selectors)
  115. {
  116. return adopt_ref(*new Selector(move(compound_selectors)));
  117. }
  118. ~Selector() = default;
  119. Vector<CompoundSelector> const& compound_selectors() const { return m_compound_selectors; }
  120. Optional<PseudoElement> pseudo_element() const { return m_pseudo_element; }
  121. u32 specificity() const;
  122. String serialize() const;
  123. private:
  124. explicit Selector(Vector<CompoundSelector>&&);
  125. Vector<CompoundSelector> m_compound_selectors;
  126. mutable Optional<u32> m_specificity;
  127. Optional<Selector::PseudoElement> m_pseudo_element;
  128. };
  129. constexpr StringView pseudo_element_name(Selector::PseudoElement pseudo_element)
  130. {
  131. switch (pseudo_element) {
  132. case Selector::PseudoElement::Before:
  133. return "before"sv;
  134. case Selector::PseudoElement::After:
  135. return "after"sv;
  136. case Selector::PseudoElement::FirstLine:
  137. return "first-line"sv;
  138. case Selector::PseudoElement::FirstLetter:
  139. return "first-letter"sv;
  140. case Selector::PseudoElement::Marker:
  141. return "marker"sv;
  142. case Selector::PseudoElement::None:
  143. break;
  144. }
  145. VERIFY_NOT_REACHED();
  146. }
  147. Optional<Selector::PseudoElement> pseudo_element_from_string(StringView);
  148. constexpr StringView pseudo_class_name(Selector::SimpleSelector::PseudoClass::Type pseudo_class)
  149. {
  150. switch (pseudo_class) {
  151. case Selector::SimpleSelector::PseudoClass::Type::Link:
  152. return "link"sv;
  153. case Selector::SimpleSelector::PseudoClass::Type::Visited:
  154. return "visited"sv;
  155. case Selector::SimpleSelector::PseudoClass::Type::Hover:
  156. return "hover"sv;
  157. case Selector::SimpleSelector::PseudoClass::Type::Focus:
  158. return "focus"sv;
  159. case Selector::SimpleSelector::PseudoClass::Type::FirstChild:
  160. return "first-child"sv;
  161. case Selector::SimpleSelector::PseudoClass::Type::LastChild:
  162. return "last-child"sv;
  163. case Selector::SimpleSelector::PseudoClass::Type::OnlyChild:
  164. return "only-child"sv;
  165. case Selector::SimpleSelector::PseudoClass::Type::Empty:
  166. return "empty"sv;
  167. case Selector::SimpleSelector::PseudoClass::Type::Root:
  168. return "root"sv;
  169. case Selector::SimpleSelector::PseudoClass::Type::FirstOfType:
  170. return "first-of-type"sv;
  171. case Selector::SimpleSelector::PseudoClass::Type::LastOfType:
  172. return "last-of-type"sv;
  173. case Selector::SimpleSelector::PseudoClass::Type::OnlyOfType:
  174. return "only-of-type"sv;
  175. case Selector::SimpleSelector::PseudoClass::Type::NthOfType:
  176. return "nth-of-type"sv;
  177. case Selector::SimpleSelector::PseudoClass::Type::NthLastOfType:
  178. return "nth-last-of-type"sv;
  179. case Selector::SimpleSelector::PseudoClass::Type::Disabled:
  180. return "disabled"sv;
  181. case Selector::SimpleSelector::PseudoClass::Type::Enabled:
  182. return "enabled"sv;
  183. case Selector::SimpleSelector::PseudoClass::Type::Checked:
  184. return "checked"sv;
  185. case Selector::SimpleSelector::PseudoClass::Type::Active:
  186. return "active"sv;
  187. case Selector::SimpleSelector::PseudoClass::Type::NthChild:
  188. return "nth-child"sv;
  189. case Selector::SimpleSelector::PseudoClass::Type::NthLastChild:
  190. return "nth-last-child"sv;
  191. case Selector::SimpleSelector::PseudoClass::Type::Is:
  192. return "is"sv;
  193. case Selector::SimpleSelector::PseudoClass::Type::Not:
  194. return "not"sv;
  195. case Selector::SimpleSelector::PseudoClass::Type::None:
  196. break;
  197. }
  198. VERIFY_NOT_REACHED();
  199. }
  200. String serialize_a_group_of_selectors(NonnullRefPtrVector<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. }