StyleValue.h 2.5 KB

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