StyleValue.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <AK/Concepts.h>
  11. #include <AK/GenericShorthands.h>
  12. #include <AK/NonnullOwnPtr.h>
  13. #include <AK/RefCounted.h>
  14. #include <AK/RefPtr.h>
  15. #include <AK/String.h>
  16. #include <AK/StringView.h>
  17. #include <AK/Variant.h>
  18. #include <AK/Vector.h>
  19. #include <AK/WeakPtr.h>
  20. #include <LibGfx/Color.h>
  21. #include <LibURL/URL.h>
  22. #include <LibWeb/CSS/Enums.h>
  23. #include <LibWeb/CSS/Length.h>
  24. #include <LibWeb/CSS/ValueID.h>
  25. #include <LibWeb/Forward.h>
  26. namespace Web::CSS {
  27. template<typename T>
  28. struct ValueComparingNonnullRefPtr : public NonnullRefPtr<T> {
  29. using NonnullRefPtr<T>::NonnullRefPtr;
  30. ValueComparingNonnullRefPtr(NonnullRefPtr<T> const& other)
  31. : NonnullRefPtr<T>(other)
  32. {
  33. }
  34. ValueComparingNonnullRefPtr(NonnullRefPtr<T>&& other)
  35. : NonnullRefPtr<T>(move(other))
  36. {
  37. }
  38. bool operator==(ValueComparingNonnullRefPtr const& other) const
  39. {
  40. return this->ptr() == other.ptr() || this->ptr()->equals(*other);
  41. }
  42. private:
  43. using NonnullRefPtr<T>::operator==;
  44. };
  45. template<typename T>
  46. struct ValueComparingRefPtr : public RefPtr<T> {
  47. using RefPtr<T>::RefPtr;
  48. ValueComparingRefPtr(RefPtr<T> const& other)
  49. : RefPtr<T>(other)
  50. {
  51. }
  52. ValueComparingRefPtr(RefPtr<T>&& other)
  53. : RefPtr<T>(move(other))
  54. {
  55. }
  56. template<typename U>
  57. bool operator==(ValueComparingNonnullRefPtr<U> const& other) const
  58. {
  59. return this->ptr() == other.ptr() || (this->ptr() && this->ptr()->equals(*other));
  60. }
  61. bool operator==(ValueComparingRefPtr const& other) const
  62. {
  63. return this->ptr() == other.ptr() || (this->ptr() && other.ptr() && this->ptr()->equals(*other));
  64. }
  65. private:
  66. using RefPtr<T>::operator==;
  67. };
  68. using StyleValueVector = Vector<ValueComparingNonnullRefPtr<StyleValue const>>;
  69. #define ENUMERATE_STYLE_VALUE_TYPES \
  70. __ENUMERATE_STYLE_VALUE_TYPE(Angle, angle) \
  71. __ENUMERATE_STYLE_VALUE_TYPE(BackgroundRepeat, background_repeat) \
  72. __ENUMERATE_STYLE_VALUE_TYPE(BackgroundSize, background_size) \
  73. __ENUMERATE_STYLE_VALUE_TYPE(BasicShape, basic_shape) \
  74. __ENUMERATE_STYLE_VALUE_TYPE(BorderRadius, border_radius) \
  75. __ENUMERATE_STYLE_VALUE_TYPE(Calculated, calculated) \
  76. __ENUMERATE_STYLE_VALUE_TYPE(Color, color) \
  77. __ENUMERATE_STYLE_VALUE_TYPE(ConicGradient, conic_gradient) \
  78. __ENUMERATE_STYLE_VALUE_TYPE(Content, content) \
  79. __ENUMERATE_STYLE_VALUE_TYPE(CustomIdent, custom_ident) \
  80. __ENUMERATE_STYLE_VALUE_TYPE(Display, display) \
  81. __ENUMERATE_STYLE_VALUE_TYPE(Easing, easing) \
  82. __ENUMERATE_STYLE_VALUE_TYPE(Edge, edge) \
  83. __ENUMERATE_STYLE_VALUE_TYPE(FilterValueList, filter_value_list) \
  84. __ENUMERATE_STYLE_VALUE_TYPE(Flex, flex) \
  85. __ENUMERATE_STYLE_VALUE_TYPE(Frequency, frequency) \
  86. __ENUMERATE_STYLE_VALUE_TYPE(GridAutoFlow, grid_auto_flow) \
  87. __ENUMERATE_STYLE_VALUE_TYPE(GridTemplateArea, grid_template_area) \
  88. __ENUMERATE_STYLE_VALUE_TYPE(GridTrackPlacement, grid_track_placement) \
  89. __ENUMERATE_STYLE_VALUE_TYPE(GridTrackSizeList, grid_track_size_list) \
  90. __ENUMERATE_STYLE_VALUE_TYPE(Identifier, identifier) \
  91. __ENUMERATE_STYLE_VALUE_TYPE(Image, image) \
  92. __ENUMERATE_STYLE_VALUE_TYPE(Inherit, inherit) \
  93. __ENUMERATE_STYLE_VALUE_TYPE(Initial, initial) \
  94. __ENUMERATE_STYLE_VALUE_TYPE(Integer, integer) \
  95. __ENUMERATE_STYLE_VALUE_TYPE(Length, length) \
  96. __ENUMERATE_STYLE_VALUE_TYPE(LinearGradient, linear_gradient) \
  97. __ENUMERATE_STYLE_VALUE_TYPE(MathDepth, math_depth) \
  98. __ENUMERATE_STYLE_VALUE_TYPE(Number, number) \
  99. __ENUMERATE_STYLE_VALUE_TYPE(Percentage, percentage) \
  100. __ENUMERATE_STYLE_VALUE_TYPE(Position, position) \
  101. __ENUMERATE_STYLE_VALUE_TYPE(RadialGradient, radial_gradient) \
  102. __ENUMERATE_STYLE_VALUE_TYPE(Ratio, ratio) \
  103. __ENUMERATE_STYLE_VALUE_TYPE(Rect, rect) \
  104. __ENUMERATE_STYLE_VALUE_TYPE(Resolution, resolution) \
  105. __ENUMERATE_STYLE_VALUE_TYPE(Revert, revert) \
  106. __ENUMERATE_STYLE_VALUE_TYPE(Shadow, shadow) \
  107. __ENUMERATE_STYLE_VALUE_TYPE(Shorthand, shorthand) \
  108. __ENUMERATE_STYLE_VALUE_TYPE(String, string) \
  109. __ENUMERATE_STYLE_VALUE_TYPE(Time, time) \
  110. __ENUMERATE_STYLE_VALUE_TYPE(Transformation, transformation) \
  111. __ENUMERATE_STYLE_VALUE_TYPE(Transition, transition) \
  112. __ENUMERATE_STYLE_VALUE_TYPE(Unresolved, unresolved) \
  113. __ENUMERATE_STYLE_VALUE_TYPE(Unset, unset) \
  114. __ENUMERATE_STYLE_VALUE_TYPE(URL, url) \
  115. __ENUMERATE_STYLE_VALUE_TYPE(ValueList, value_list)
  116. // NOTE:
  117. using ValueListStyleValue = StyleValueList;
  118. class StyleValue : public RefCounted<StyleValue> {
  119. public:
  120. virtual ~StyleValue() = default;
  121. enum class Type {
  122. #define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \
  123. TitleCaseName,
  124. ENUMERATE_STYLE_VALUE_TYPES
  125. #undef __ENUMERATE_STYLE_VALUE_TYPE
  126. };
  127. Type type() const { return m_type; }
  128. #define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \
  129. bool is_##SnakeCaseName() const { return type() == Type::TitleCaseName; } \
  130. TitleCaseName##StyleValue const& as_##SnakeCaseName() const; \
  131. TitleCaseName##StyleValue& as_##SnakeCaseName() { return const_cast<TitleCaseName##StyleValue&>(const_cast<StyleValue const&>(*this).as_##SnakeCaseName()); }
  132. ENUMERATE_STYLE_VALUE_TYPES
  133. #undef __ENUMERATE_STYLE_VALUE_TYPE
  134. bool is_abstract_image() const
  135. {
  136. return AK::first_is_one_of(type(), Type::Image, Type::LinearGradient, Type::ConicGradient, Type::RadialGradient);
  137. }
  138. AbstractImageStyleValue const& as_abstract_image() const;
  139. AbstractImageStyleValue& as_abstract_image() { return const_cast<AbstractImageStyleValue&>(const_cast<StyleValue const&>(*this).as_abstract_image()); }
  140. // https://www.w3.org/TR/css-values-4/#common-keywords
  141. // https://drafts.csswg.org/css-cascade-4/#valdef-all-revert
  142. bool is_css_wide_keyword() const { return is_inherit() || is_initial() || is_revert() || is_unset(); }
  143. bool has_auto() const;
  144. virtual bool has_color() const { return false; }
  145. virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const;
  146. virtual Color to_color(Optional<Layout::NodeWithStyle const&>) const { return {}; }
  147. ValueID to_identifier() const;
  148. virtual String to_string() const = 0;
  149. [[nodiscard]] int to_font_weight() const;
  150. [[nodiscard]] int to_font_slope() const;
  151. [[nodiscard]] int to_font_stretch_width() const;
  152. virtual bool equals(StyleValue const& other) const = 0;
  153. bool operator==(StyleValue const& other) const
  154. {
  155. return this->equals(other);
  156. }
  157. protected:
  158. explicit StyleValue(Type);
  159. private:
  160. Type m_type;
  161. };
  162. template<typename T>
  163. struct StyleValueWithDefaultOperators : public StyleValue {
  164. using StyleValue::StyleValue;
  165. virtual bool equals(StyleValue const& other) const override
  166. {
  167. if (type() != other.type())
  168. return false;
  169. auto const& typed_other = static_cast<T const&>(other);
  170. return static_cast<T const&>(*this).properties_equal(typed_other);
  171. }
  172. };
  173. }
  174. template<>
  175. struct AK::Formatter<Web::CSS::StyleValue> : Formatter<StringView> {
  176. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::StyleValue const& style_value)
  177. {
  178. return Formatter<StringView>::format(builder, style_value.to_string());
  179. }
  180. };