Selector.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/FlyString.h>
  8. #include <AK/String.h>
  9. #include <AK/Vector.h>
  10. namespace Web::CSS {
  11. class Selector {
  12. public:
  13. struct SimpleSelector {
  14. enum class Type {
  15. Invalid,
  16. Universal,
  17. TagName,
  18. Id,
  19. Class,
  20. };
  21. Type type { Type::Invalid };
  22. enum class PseudoClass {
  23. None,
  24. Link,
  25. Visited,
  26. Hover,
  27. Focus,
  28. FirstChild,
  29. LastChild,
  30. OnlyChild,
  31. Empty,
  32. Root,
  33. FirstOfType,
  34. LastOfType,
  35. NthChild,
  36. NthLastChild,
  37. Disabled,
  38. Enabled,
  39. Checked,
  40. Not,
  41. };
  42. PseudoClass pseudo_class { PseudoClass::None };
  43. enum class PseudoElement {
  44. None,
  45. Before,
  46. After,
  47. };
  48. PseudoElement pseudo_element { PseudoElement::None };
  49. FlyString value;
  50. enum class AttributeMatchType {
  51. None,
  52. HasAttribute,
  53. ExactValueMatch,
  54. Contains,
  55. StartsWith,
  56. };
  57. AttributeMatchType attribute_match_type { AttributeMatchType::None };
  58. FlyString attribute_name;
  59. String attribute_value;
  60. struct NthChildPattern {
  61. int step_size = 0;
  62. int offset = 0;
  63. static NthChildPattern parse(const StringView& args);
  64. };
  65. // FIXME: We don't need this field on every single SimpleSelector, but it's also annoying to malloc it somewhere.
  66. // Only used when "pseudo_class" is "NthChild" or "NthLastChild".
  67. NthChildPattern nth_child_pattern;
  68. String not_selector {};
  69. };
  70. struct ComplexSelector {
  71. enum class Relation {
  72. None,
  73. ImmediateChild,
  74. Descendant,
  75. AdjacentSibling,
  76. GeneralSibling,
  77. Column,
  78. };
  79. Relation relation { Relation::None };
  80. using CompoundSelector = Vector<SimpleSelector>;
  81. CompoundSelector compound_selector;
  82. };
  83. explicit Selector(Vector<ComplexSelector>&&);
  84. ~Selector();
  85. const Vector<ComplexSelector>& complex_selectors() const { return m_complex_selectors; }
  86. u32 specificity() const;
  87. private:
  88. Vector<ComplexSelector> m_complex_selectors;
  89. };
  90. }