Selector.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. struct SimpleSelector {
  27. enum class Type {
  28. Invalid,
  29. Universal,
  30. TagName,
  31. Id,
  32. Class,
  33. Attribute,
  34. PseudoClass,
  35. PseudoElement,
  36. };
  37. Type type { Type::Invalid };
  38. struct ANPlusBPattern {
  39. int step_size { 0 }; // "A"
  40. int offset = { 0 }; // "B"
  41. String serialize() const
  42. {
  43. return String::formatted("{}n{:+}", step_size, offset);
  44. }
  45. };
  46. struct PseudoClass {
  47. enum class Type {
  48. None,
  49. Link,
  50. Visited,
  51. Hover,
  52. Focus,
  53. FirstChild,
  54. LastChild,
  55. OnlyChild,
  56. NthChild,
  57. NthLastChild,
  58. Empty,
  59. Root,
  60. FirstOfType,
  61. LastOfType,
  62. OnlyOfType,
  63. NthOfType,
  64. NthLastOfType,
  65. Disabled,
  66. Enabled,
  67. Checked,
  68. Not,
  69. Active,
  70. };
  71. Type type { Type::None };
  72. // FIXME: We don't need this field on every single SimpleSelector, but it's also annoying to malloc it somewhere.
  73. // Only used when "pseudo_class" is "NthChild" or "NthLastChild".
  74. ANPlusBPattern nth_child_pattern;
  75. SelectorList not_selector {};
  76. };
  77. PseudoClass pseudo_class {};
  78. PseudoElement pseudo_element { PseudoElement::None };
  79. FlyString value {};
  80. struct Attribute {
  81. enum class MatchType {
  82. None,
  83. HasAttribute,
  84. ExactValueMatch,
  85. ContainsWord, // [att~=val]
  86. ContainsString, // [att*=val]
  87. StartsWithSegment, // [att|=val]
  88. StartsWithString, // [att^=val]
  89. EndsWithString, // [att$=val]
  90. };
  91. MatchType match_type { MatchType::None };
  92. FlyString name {};
  93. String value {};
  94. };
  95. Attribute attribute {};
  96. String serialize() const;
  97. };
  98. enum class Combinator {
  99. None,
  100. ImmediateChild, // >
  101. Descendant, // <whitespace>
  102. NextSibling, // +
  103. SubsequentSibling, // ~
  104. Column, // ||
  105. };
  106. struct CompoundSelector {
  107. // Spec-wise, the <combinator> is not part of a <compound-selector>,
  108. // but it is more understandable to put them together.
  109. Combinator combinator { Combinator::None };
  110. Vector<SimpleSelector> simple_selectors;
  111. };
  112. static NonnullRefPtr<Selector> create(Vector<CompoundSelector>&& compound_selectors)
  113. {
  114. return adopt_ref(*new Selector(move(compound_selectors)));
  115. }
  116. ~Selector();
  117. Vector<CompoundSelector> const& compound_selectors() const { return m_compound_selectors; }
  118. Optional<PseudoElement> pseudo_element() const;
  119. u32 specificity() const;
  120. String serialize() const;
  121. private:
  122. explicit Selector(Vector<CompoundSelector>&&);
  123. Vector<CompoundSelector> m_compound_selectors;
  124. mutable Optional<u32> m_specificity;
  125. };
  126. constexpr StringView pseudo_element_name(Selector::PseudoElement);
  127. constexpr StringView pseudo_class_name(Selector::SimpleSelector::PseudoClass::Type);
  128. String serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors);
  129. }
  130. namespace AK {
  131. template<>
  132. struct Formatter<Web::CSS::Selector> : Formatter<StringView> {
  133. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Selector const& selector)
  134. {
  135. return Formatter<StringView>::format(builder, selector.serialize());
  136. }
  137. };
  138. }