Selector.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, 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. struct SimpleSelector {
  19. enum class Type {
  20. Invalid,
  21. Universal,
  22. TagName,
  23. Id,
  24. Class,
  25. Attribute,
  26. PseudoClass,
  27. PseudoElement,
  28. };
  29. Type type { Type::Invalid };
  30. struct ANPlusBPattern {
  31. int step_size { 0 }; // "A"
  32. int offset = { 0 }; // "B"
  33. String serialize() const
  34. {
  35. return String::formatted("{}n{:+}", step_size, offset);
  36. }
  37. };
  38. struct PseudoClass {
  39. enum class Type {
  40. None,
  41. Link,
  42. Visited,
  43. Hover,
  44. Focus,
  45. FirstChild,
  46. LastChild,
  47. OnlyChild,
  48. Empty,
  49. Root,
  50. FirstOfType,
  51. LastOfType,
  52. OnlyOfType,
  53. NthChild,
  54. NthLastChild,
  55. Disabled,
  56. Enabled,
  57. Checked,
  58. Not,
  59. Active,
  60. };
  61. Type type { Type::None };
  62. // FIXME: We don't need this field on every single SimpleSelector, but it's also annoying to malloc it somewhere.
  63. // Only used when "pseudo_class" is "NthChild" or "NthLastChild".
  64. ANPlusBPattern nth_child_pattern;
  65. SelectorList not_selector {};
  66. };
  67. PseudoClass pseudo_class {};
  68. enum class PseudoElement {
  69. None,
  70. Before,
  71. After,
  72. FirstLine,
  73. FirstLetter,
  74. };
  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. u32 specificity() const;
  116. String serialize() const;
  117. private:
  118. explicit Selector(Vector<CompoundSelector>&&);
  119. Vector<CompoundSelector> m_compound_selectors;
  120. mutable Optional<u32> m_specificity;
  121. };
  122. constexpr StringView pseudo_element_name(Selector::SimpleSelector::PseudoElement);
  123. constexpr StringView pseudo_class_name(Selector::SimpleSelector::PseudoClass::Type);
  124. String serialize_a_group_of_selectors(NonnullRefPtrVector<Selector> const& selectors);
  125. }
  126. namespace AK {
  127. template<>
  128. struct Formatter<Web::CSS::Selector> : Formatter<StringView> {
  129. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Selector const& selector)
  130. {
  131. return Formatter<StringView>::format(builder, selector.serialize());
  132. }
  133. };
  134. }