Selector.h 2.9 KB

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