StyleValue.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. protected:
  23. explicit StyleValue(Type);
  24. private:
  25. Type m_type { Type::Invalid };
  26. };
  27. class StringStyleValue : public StyleValue {
  28. public:
  29. static NonnullRefPtr<StringStyleValue> create(const String& string)
  30. {
  31. return adopt(*new StringStyleValue(string));
  32. }
  33. virtual ~StringStyleValue() override {}
  34. String to_string() const override { return m_string; }
  35. private:
  36. explicit StringStyleValue(const String& string)
  37. : StyleValue(Type::String)
  38. , m_string(string)
  39. {
  40. }
  41. String m_string;
  42. };
  43. class LengthStyleValue : public StyleValue {
  44. public:
  45. static NonnullRefPtr<LengthStyleValue> create(const Length& length)
  46. {
  47. return adopt(*new LengthStyleValue(length));
  48. }
  49. virtual ~LengthStyleValue() override {}
  50. virtual String to_string() const override { return m_length.to_string(); }
  51. virtual Length to_length() const override { return m_length; }
  52. const Length& length() const { return m_length; }
  53. private:
  54. explicit LengthStyleValue(const Length& length)
  55. : StyleValue(Type::Length)
  56. , m_length(length)
  57. {
  58. }
  59. Length m_length;
  60. };
  61. class InitialStyleValue final : public StyleValue {
  62. public:
  63. static NonnullRefPtr<InitialStyleValue> create() { return adopt(*new InitialStyleValue); }
  64. virtual ~InitialStyleValue() override {}
  65. String to_string() const override { return "initial"; }
  66. private:
  67. InitialStyleValue()
  68. : StyleValue(Type::Initial)
  69. {
  70. }
  71. };
  72. class InheritStyleValue final : public StyleValue {
  73. public:
  74. static NonnullRefPtr<InheritStyleValue> create() { return adopt(*new InheritStyleValue); }
  75. virtual ~InheritStyleValue() override {}
  76. String to_string() const override { return "inherit"; }
  77. private:
  78. InheritStyleValue()
  79. : StyleValue(Type::Inherit)
  80. {
  81. }
  82. };