Selector.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 NthChildPattern {
  31. int step_size { 0 };
  32. int offset = { 0 };
  33. static NthChildPattern parse(StringView const& args);
  34. };
  35. struct PseudoClass {
  36. enum class Type {
  37. None,
  38. Link,
  39. Visited,
  40. Hover,
  41. Focus,
  42. FirstChild,
  43. LastChild,
  44. OnlyChild,
  45. Empty,
  46. Root,
  47. FirstOfType,
  48. LastOfType,
  49. NthChild,
  50. NthLastChild,
  51. Disabled,
  52. Enabled,
  53. Checked,
  54. Not,
  55. Active,
  56. };
  57. Type type { Type::None };
  58. // FIXME: We don't need this field on every single SimpleSelector, but it's also annoying to malloc it somewhere.
  59. // Only used when "pseudo_class" is "NthChild" or "NthLastChild".
  60. NthChildPattern nth_child_pattern;
  61. SelectorList not_selector {};
  62. };
  63. PseudoClass pseudo_class {};
  64. enum class PseudoElement {
  65. None,
  66. Before,
  67. After,
  68. FirstLine,
  69. FirstLetter,
  70. };
  71. PseudoElement pseudo_element { PseudoElement::None };
  72. FlyString value {};
  73. struct Attribute {
  74. enum class MatchType {
  75. None,
  76. HasAttribute,
  77. ExactValueMatch,
  78. ContainsWord, // [att~=val]
  79. ContainsString, // [att*=val]
  80. StartsWithSegment, // [att|=val]
  81. StartsWithString, // [att^=val]
  82. EndsWithString, // [att$=val]
  83. };
  84. MatchType match_type { MatchType::None };
  85. FlyString name {};
  86. String value {};
  87. };
  88. Attribute attribute {};
  89. };
  90. enum class Combinator {
  91. None,
  92. ImmediateChild, // >
  93. Descendant, // <whitespace>
  94. NextSibling, // +
  95. SubsequentSibling, // ~
  96. Column, // ||
  97. };
  98. struct CompoundSelector {
  99. // Spec-wise, the <combinator> is not part of a <compound-selector>,
  100. // but it is more understandable to put them together.
  101. Combinator combinator { Combinator::None };
  102. Vector<SimpleSelector> simple_selectors;
  103. };
  104. static NonnullRefPtr<Selector> create(Vector<CompoundSelector>&& compound_selectors)
  105. {
  106. return adopt_ref(*new Selector(move(compound_selectors)));
  107. }
  108. ~Selector();
  109. Vector<CompoundSelector> const& compound_selectors() const { return m_compound_selectors; }
  110. u32 specificity() const;
  111. private:
  112. explicit Selector(Vector<CompoundSelector>&&);
  113. Vector<CompoundSelector> m_compound_selectors;
  114. };
  115. }