StyleValue.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 Document;
  9. namespace CSS {
  10. enum class ValueID {
  11. Invalid,
  12. VendorSpecificLink,
  13. };
  14. }
  15. class StyleValue : public RefCounted<StyleValue> {
  16. public:
  17. virtual ~StyleValue();
  18. enum class Type {
  19. Invalid,
  20. Inherit,
  21. Initial,
  22. String,
  23. Length,
  24. Color,
  25. Identifier,
  26. };
  27. Type type() const { return m_type; }
  28. bool is_inherit() const { return type() == Type::Inherit; }
  29. bool is_initial() const { return type() == Type::Initial; }
  30. bool is_color() const { return type() == Type::Color; }
  31. bool is_identifier() const { return type() == Type::Identifier; }
  32. virtual String to_string() const = 0;
  33. virtual Length to_length() const { return {}; }
  34. virtual Color to_color(const Document&) const { return {}; }
  35. virtual bool is_auto() const { return false; }
  36. protected:
  37. explicit StyleValue(Type);
  38. private:
  39. Type m_type { Type::Invalid };
  40. };
  41. class StringStyleValue : public StyleValue {
  42. public:
  43. static NonnullRefPtr<StringStyleValue> create(const String& string)
  44. {
  45. return adopt(*new StringStyleValue(string));
  46. }
  47. virtual ~StringStyleValue() override {}
  48. String to_string() const override { return m_string; }
  49. private:
  50. explicit StringStyleValue(const String& string)
  51. : StyleValue(Type::String)
  52. , m_string(string)
  53. {
  54. }
  55. String m_string;
  56. };
  57. class LengthStyleValue : public StyleValue {
  58. public:
  59. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  60. {
  61. return adopt(*new LengthStyleValue(length));
  62. }
  63. virtual ~LengthStyleValue() override {}
  64. virtual String to_string() const override { return m_length.to_string(); }
  65. virtual Length to_length() const override { return m_length; }
  66. const Length& length() const { return m_length; }
  67. virtual bool is_auto() const override { return m_length.is_auto(); }
  68. private:
  69. explicit LengthStyleValue(const Length& length)
  70. : StyleValue(Type::Length)
  71. , m_length(length)
  72. {
  73. }
  74. Length m_length;
  75. };
  76. class InitialStyleValue final : public StyleValue {
  77. public:
  78. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  79. virtual ~InitialStyleValue() override {}
  80. String to_string() const override { return "initial"; }
  81. private:
  82. InitialStyleValue()
  83. : StyleValue(Type::Initial)
  84. {
  85. }
  86. };
  87. class InheritStyleValue final : public StyleValue {
  88. public:
  89. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  90. virtual ~InheritStyleValue() override {}
  91. String to_string() const override { return "inherit"; }
  92. private:
  93. InheritStyleValue()
  94. : StyleValue(Type::Inherit)
  95. {
  96. }
  97. };
  98. class ColorStyleValue : public StyleValue {
  99. public:
  100. static NonnullRefPtr<ColorStyleValue> create(Color color)
  101. {
  102. return adopt(*new ColorStyleValue(color));
  103. }
  104. virtual ~ColorStyleValue() override {}
  105. Color color() const { return m_color; }
  106. String to_string() const override { return m_color.to_string(); }
  107. Color to_color(const Document&) const override { return m_color; }
  108. private:
  109. explicit ColorStyleValue(Color color)
  110. : StyleValue(Type::Color)
  111. , m_color(color)
  112. {
  113. }
  114. Color m_color;
  115. };
  116. class IdentifierStyleValue final : public StyleValue {
  117. public:
  118. static NonnullRefPtr<IdentifierStyleValue> create(CSS::ValueID id)
  119. {
  120. return adopt(*new IdentifierStyleValue(id));
  121. }
  122. virtual ~IdentifierStyleValue() override {}
  123. CSS::ValueID id() const { return m_id; }
  124. virtual String to_string() const override;
  125. virtual Color to_color(const Document&) const override;
  126. private:
  127. explicit IdentifierStyleValue(CSS::ValueID id)
  128. : StyleValue(Type::Identifier)
  129. , m_id(id)
  130. {
  131. }
  132. CSS::ValueID m_id { CSS::ValueID::Invalid };
  133. };