Selector.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. class Selector : public RefCounted<Selector> {
  15. public:
  16. struct SimpleSelector {
  17. enum class Type {
  18. Invalid,
  19. Universal,
  20. TagName,
  21. Id,
  22. Class,
  23. Attribute,
  24. PseudoClass,
  25. PseudoElement,
  26. };
  27. Type type { Type::Invalid };
  28. struct NthChildPattern {
  29. int step_size = 0;
  30. int offset = 0;
  31. static NthChildPattern parse(StringView const& args);
  32. };
  33. struct PseudoClass {
  34. enum class Type {
  35. None,
  36. Link,
  37. Visited,
  38. Hover,
  39. Focus,
  40. FirstChild,
  41. LastChild,
  42. OnlyChild,
  43. Empty,
  44. Root,
  45. FirstOfType,
  46. LastOfType,
  47. NthChild,
  48. NthLastChild,
  49. Disabled,
  50. Enabled,
  51. Checked,
  52. Not,
  53. Active,
  54. };
  55. Type type { Type::None };
  56. // FIXME: We don't need this field on every single SimpleSelector, but it's also annoying to malloc it somewhere.
  57. // Only used when "pseudo_class" is "NthChild" or "NthLastChild".
  58. NthChildPattern nth_child_pattern;
  59. NonnullRefPtrVector<Selector> not_selector {};
  60. };
  61. PseudoClass pseudo_class;
  62. enum class PseudoElement {
  63. None,
  64. Before,
  65. After,
  66. FirstLine,
  67. FirstLetter,
  68. };
  69. PseudoElement pseudo_element { PseudoElement::None };
  70. FlyString value;
  71. struct Attribute {
  72. enum class MatchType {
  73. None,
  74. HasAttribute,
  75. ExactValueMatch,
  76. ContainsWord, // [att~=val]
  77. ContainsString, // [att*=val]
  78. StartsWithSegment, // [att|=val]
  79. StartsWithString, // [att^=val]
  80. EndsWithString, // [att$=val]
  81. };
  82. MatchType match_type { MatchType::None };
  83. FlyString name;
  84. String value;
  85. };
  86. Attribute attribute;
  87. };
  88. struct ComplexSelector {
  89. enum class Relation {
  90. None,
  91. ImmediateChild,
  92. Descendant,
  93. AdjacentSibling,
  94. GeneralSibling,
  95. Column,
  96. };
  97. Relation relation { Relation::None };
  98. using CompoundSelector = Vector<SimpleSelector>;
  99. CompoundSelector compound_selector;
  100. };
  101. static NonnullRefPtr<Selector> create(Vector<ComplexSelector>&& complex_selectors)
  102. {
  103. return adopt_ref(*new Selector(move(complex_selectors)));
  104. }
  105. ~Selector();
  106. Vector<ComplexSelector> const& complex_selectors() const { return m_complex_selectors; }
  107. u32 specificity() const;
  108. private:
  109. explicit Selector(Vector<ComplexSelector>&&);
  110. Vector<ComplexSelector> m_complex_selectors;
  111. };
  112. }