StyleValue.h 4.0 KB

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