Selector.h 4.4 KB

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