StyleValue.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. #include <AK/RefCounted.h>
  3. #include <AK/RefPtr.h>
  4. #include <AK/String.h>
  5. #include <AK/StringView.h>
  6. #include <LibDraw/Color.h>
  7. #include <LibHTML/CSS/Length.h>
  8. class StyleValue : public RefCounted<StyleValue> {
  9. public:
  10. virtual ~StyleValue();
  11. enum class Type {
  12. Invalid,
  13. Inherit,
  14. Initial,
  15. String,
  16. Length,
  17. Color,
  18. };
  19. Type type() const { return m_type; }
  20. bool is_inherit() const { return type() == Type::Inherit; }
  21. bool is_initial() const { return type() == Type::Initial; }
  22. bool is_color() const { return type() == Type::Color; }
  23. virtual String to_string() const = 0;
  24. virtual Length to_length() const { return {}; }
  25. virtual Color to_color() const { return {}; }
  26. virtual bool is_auto() const { return false; }
  27. protected:
  28. explicit StyleValue(Type);
  29. private:
  30. Type m_type { Type::Invalid };
  31. };
  32. class StringStyleValue : public StyleValue {
  33. public:
  34. static NonnullRefPtr<StringStyleValue> create(const String& string)
  35. {
  36. return adopt(*new StringStyleValue(string));
  37. }
  38. virtual ~StringStyleValue() override {}
  39. String to_string() const override { return m_string; }
  40. private:
  41. explicit StringStyleValue(const String& string)
  42. : StyleValue(Type::String)
  43. , m_string(string)
  44. {
  45. }
  46. String m_string;
  47. };
  48. class LengthStyleValue : public StyleValue {
  49. public:
  50. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  51. {
  52. return adopt(*new LengthStyleValue(length));
  53. }
  54. virtual ~LengthStyleValue() override {}
  55. virtual String to_string() const override { return m_length.to_string(); }
  56. virtual Length to_length() const override { return m_length; }
  57. const Length& length() const { return m_length; }
  58. virtual bool is_auto() const override { return m_length.is_auto(); }
  59. private:
  60. explicit LengthStyleValue(const Length& length)
  61. : StyleValue(Type::Length)
  62. , m_length(length)
  63. {
  64. }
  65. Length m_length;
  66. };
  67. class InitialStyleValue final : public StyleValue {
  68. public:
  69. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  70. virtual ~InitialStyleValue() override {}
  71. String to_string() const override { return "initial"; }
  72. private:
  73. InitialStyleValue()
  74. : StyleValue(Type::Initial)
  75. {
  76. }
  77. };
  78. class InheritStyleValue final : public StyleValue {
  79. public:
  80. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  81. virtual ~InheritStyleValue() override {}
  82. String to_string() const override { return "inherit"; }
  83. private:
  84. InheritStyleValue()
  85. : StyleValue(Type::Inherit)
  86. {
  87. }
  88. };
  89. class ColorStyleValue : public StyleValue {
  90. public:
  91. static NonnullRefPtr<ColorStyleValue> create(Color color)
  92. {
  93. return adopt(*new ColorStyleValue(color));
  94. }
  95. virtual ~ColorStyleValue() override {}
  96. Color color() const { return m_color; }
  97. String to_string() const override { return m_color.to_string(); }
  98. Color to_color() const override { return m_color; }
  99. private:
  100. explicit ColorStyleValue(Color color)
  101. : StyleValue(Type::Color)
  102. , m_color(color)
  103. {
  104. }
  105. Color m_color;
  106. };