Selector.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. Active,
  42. };
  43. PseudoClass pseudo_class { PseudoClass::None };
  44. enum class PseudoElement {
  45. None,
  46. Before,
  47. After,
  48. };
  49. PseudoElement pseudo_element { PseudoElement::None };
  50. FlyString value;
  51. enum class AttributeMatchType {
  52. None,
  53. HasAttribute,
  54. ExactValueMatch,
  55. Contains,
  56. StartsWith,
  57. };
  58. AttributeMatchType attribute_match_type { AttributeMatchType::None };
  59. FlyString attribute_name;
  60. String attribute_value;
  61. struct NthChildPattern {
  62. int step_size = 0;
  63. int offset = 0;
  64. static NthChildPattern parse(const StringView& args);
  65. };
  66. // FIXME: We don't need this field on every single SimpleSelector, but it's also annoying to malloc it somewhere.
  67. // Only used when "pseudo_class" is "NthChild" or "NthLastChild".
  68. NthChildPattern nth_child_pattern;
  69. // FIXME: This wants to be a Selector, rather than parsing it each time it is used.
  70. String not_selector {};
  71. };
  72. struct ComplexSelector {
  73. enum class Relation {
  74. None,
  75. ImmediateChild,
  76. Descendant,
  77. AdjacentSibling,
  78. GeneralSibling,
  79. Column,
  80. };
  81. Relation relation { Relation::None };
  82. using CompoundSelector = Vector<SimpleSelector>;
  83. CompoundSelector compound_selector;
  84. };
  85. explicit Selector(Vector<ComplexSelector>&&);
  86. ~Selector();
  87. const Vector<ComplexSelector>& complex_selectors() const { return m_complex_selectors; }
  88. u32 specificity() const;
  89. private:
  90. Vector<ComplexSelector> m_complex_selectors;
  91. };
  92. }