Selector.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@gmail.com>
  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. static ANPlusBPattern parse(StringView const& args);
  34. String to_string() const
  35. {
  36. return String::formatted("{}n{:+}", step_size, offset);
  37. }
  38. };
  39. struct PseudoClass {
  40. enum class Type {
  41. None,
  42. Link,
  43. Visited,
  44. Hover,
  45. Focus,
  46. FirstChild,
  47. LastChild,
  48. OnlyChild,
  49. Empty,
  50. Root,
  51. FirstOfType,
  52. LastOfType,
  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. };
  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. private:
  116. explicit Selector(Vector<CompoundSelector>&&);
  117. Vector<CompoundSelector> m_compound_selectors;
  118. };
  119. }