Selector.h 4.3 KB

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