Selector.h 4.3 KB

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