Selector.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/Vector.h>
  9. namespace Web::CSS {
  10. class Selector {
  11. public:
  12. struct SimpleSelector {
  13. enum class Type {
  14. Invalid,
  15. Universal,
  16. TagName,
  17. Id,
  18. Class,
  19. };
  20. Type type { Type::Invalid };
  21. enum class PseudoClass {
  22. None,
  23. Link,
  24. Visited,
  25. Hover,
  26. Focus,
  27. FirstChild,
  28. LastChild,
  29. OnlyChild,
  30. Empty,
  31. Root,
  32. FirstOfType,
  33. LastOfType,
  34. NthChild,
  35. };
  36. PseudoClass pseudo_class { PseudoClass::None };
  37. enum class PseudoElement {
  38. None,
  39. Before,
  40. After,
  41. };
  42. PseudoElement pseudo_element { PseudoElement::None };
  43. FlyString value;
  44. enum class AttributeMatchType {
  45. None,
  46. HasAttribute,
  47. ExactValueMatch,
  48. Contains,
  49. StartsWith,
  50. };
  51. AttributeMatchType attribute_match_type { AttributeMatchType::None };
  52. FlyString attribute_name;
  53. String attribute_value;
  54. struct NthChildPattern {
  55. int step_size = 0;
  56. int offset = 0;
  57. static NthChildPattern parse(const StringView& args);
  58. };
  59. // FIXME: We don't need this field on every single SimpleSelector, but it's also annoying to malloc it somewhere.
  60. // Only used when "pseudo_class" == PseudoClass::NthChild.
  61. NthChildPattern nth_child_pattern;
  62. };
  63. struct ComplexSelector {
  64. enum class Relation {
  65. None,
  66. ImmediateChild,
  67. Descendant,
  68. AdjacentSibling,
  69. GeneralSibling,
  70. Column,
  71. };
  72. Relation relation { Relation::None };
  73. using CompoundSelector = Vector<SimpleSelector>;
  74. CompoundSelector compound_selector;
  75. };
  76. explicit Selector(Vector<ComplexSelector>&&);
  77. ~Selector();
  78. const Vector<ComplexSelector>& complex_selectors() const { return m_complex_selectors; }
  79. u32 specificity() const;
  80. private:
  81. Vector<ComplexSelector> m_complex_selectors;
  82. };
  83. }