StyleValue.h 4.0 KB

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