StyleValue.h 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * Copyright (c) 2018-2020, 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/DeprecatedString.h>
  12. #include <AK/Function.h>
  13. #include <AK/GenericShorthands.h>
  14. #include <AK/NonnullOwnPtr.h>
  15. #include <AK/RefCounted.h>
  16. #include <AK/RefPtr.h>
  17. #include <AK/String.h>
  18. #include <AK/StringView.h>
  19. #include <AK/URL.h>
  20. #include <AK/Variant.h>
  21. #include <AK/Vector.h>
  22. #include <AK/WeakPtr.h>
  23. #include <LibGfx/Bitmap.h>
  24. #include <LibGfx/Color.h>
  25. #include <LibGfx/Painter.h>
  26. #include <LibWeb/CSS/Angle.h>
  27. #include <LibWeb/CSS/Display.h>
  28. #include <LibWeb/CSS/Enums.h>
  29. #include <LibWeb/CSS/Frequency.h>
  30. #include <LibWeb/CSS/GridTrackPlacement.h>
  31. #include <LibWeb/CSS/GridTrackSize.h>
  32. #include <LibWeb/CSS/Length.h>
  33. #include <LibWeb/CSS/Number.h>
  34. #include <LibWeb/CSS/Parser/ComponentValue.h>
  35. #include <LibWeb/CSS/Percentage.h>
  36. #include <LibWeb/CSS/PropertyID.h>
  37. #include <LibWeb/CSS/Resolution.h>
  38. #include <LibWeb/CSS/Time.h>
  39. #include <LibWeb/CSS/TransformFunctions.h>
  40. #include <LibWeb/CSS/ValueID.h>
  41. #include <LibWeb/Forward.h>
  42. #include <LibWeb/Loader/ImageResource.h>
  43. #include <LibWeb/Painting/GradientPainting.h>
  44. namespace Web::CSS {
  45. enum class BackgroundSize {
  46. Contain,
  47. Cover,
  48. LengthPercentage,
  49. };
  50. enum class ShadowPlacement {
  51. Outer,
  52. Inner,
  53. };
  54. enum class FlexBasis {
  55. Content,
  56. LengthPercentage,
  57. Auto,
  58. };
  59. // FIXME: Named PositionValue to avoid conflicts with enums, but this represents a <position>
  60. struct PositionValue {
  61. enum class HorizontalPreset {
  62. Left,
  63. Center,
  64. Right
  65. };
  66. enum class VerticalPreset {
  67. Top,
  68. Center,
  69. Bottom
  70. };
  71. enum class HorizontalEdge {
  72. Left,
  73. Right
  74. };
  75. enum class VerticalEdge {
  76. Top,
  77. Bottom
  78. };
  79. static PositionValue center()
  80. {
  81. return PositionValue { HorizontalPreset::Center, VerticalPreset::Center };
  82. }
  83. Variant<HorizontalPreset, LengthPercentage> horizontal_position { HorizontalPreset::Left };
  84. Variant<VerticalPreset, LengthPercentage> vertical_position { VerticalPreset::Top };
  85. HorizontalEdge x_relative_to { HorizontalEdge::Left };
  86. VerticalEdge y_relative_to { VerticalEdge::Top };
  87. CSSPixelPoint resolved(Layout::Node const& node, CSSPixelRect const& rect) const;
  88. ErrorOr<void> serialize(StringBuilder&) const;
  89. bool operator==(PositionValue const&) const = default;
  90. };
  91. struct EdgeRect {
  92. Length top_edge;
  93. Length right_edge;
  94. Length bottom_edge;
  95. Length left_edge;
  96. Gfx::FloatRect resolved(Layout::Node const&, Gfx::FloatRect) const;
  97. bool operator==(EdgeRect const&) const = default;
  98. };
  99. // FIXME: Find a better place for this helper.
  100. inline Gfx::Painter::ScalingMode to_gfx_scaling_mode(CSS::ImageRendering css_value)
  101. {
  102. switch (css_value) {
  103. case CSS::ImageRendering::Auto:
  104. case CSS::ImageRendering::HighQuality:
  105. case CSS::ImageRendering::Smooth:
  106. return Gfx::Painter::ScalingMode::BilinearBlend;
  107. case CSS::ImageRendering::CrispEdges:
  108. return Gfx::Painter::ScalingMode::NearestNeighbor;
  109. case CSS::ImageRendering::Pixelated:
  110. return Gfx::Painter::ScalingMode::SmoothPixels;
  111. }
  112. VERIFY_NOT_REACHED();
  113. }
  114. template<typename T>
  115. struct ValueComparingNonnullRefPtr : public NonnullRefPtr<T> {
  116. using NonnullRefPtr<T>::NonnullRefPtr;
  117. ValueComparingNonnullRefPtr(NonnullRefPtr<T> const& other)
  118. : NonnullRefPtr<T>(other)
  119. {
  120. }
  121. ValueComparingNonnullRefPtr(NonnullRefPtr<T>&& other)
  122. : NonnullRefPtr<T>(move(other))
  123. {
  124. }
  125. bool operator==(ValueComparingNonnullRefPtr const& other) const
  126. {
  127. return this->ptr() == other.ptr() || this->ptr()->equals(*other);
  128. }
  129. private:
  130. using NonnullRefPtr<T>::operator==;
  131. };
  132. template<typename T>
  133. struct ValueComparingRefPtr : public RefPtr<T> {
  134. using RefPtr<T>::RefPtr;
  135. ValueComparingRefPtr(RefPtr<T> const& other)
  136. : RefPtr<T>(other)
  137. {
  138. }
  139. ValueComparingRefPtr(RefPtr<T>&& other)
  140. : RefPtr<T>(move(other))
  141. {
  142. }
  143. template<typename U>
  144. bool operator==(ValueComparingNonnullRefPtr<U> const& other) const
  145. {
  146. return this->ptr() == other.ptr() || (this->ptr() && this->ptr()->equals(*other));
  147. }
  148. bool operator==(ValueComparingRefPtr const& other) const
  149. {
  150. return this->ptr() == other.ptr() || (this->ptr() && other.ptr() && this->ptr()->equals(*other));
  151. }
  152. private:
  153. using RefPtr<T>::operator==;
  154. };
  155. using StyleValueVector = Vector<ValueComparingNonnullRefPtr<StyleValue const>>;
  156. Optional<CSS::Length> absolutized_length(CSS::Length const&, CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const&, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height);
  157. class StyleValue : public RefCounted<StyleValue> {
  158. public:
  159. virtual ~StyleValue() = default;
  160. enum class Type {
  161. Angle,
  162. Background,
  163. BackgroundRepeat,
  164. BackgroundSize,
  165. Border,
  166. BorderRadius,
  167. BorderRadiusShorthand,
  168. Calculated,
  169. Color,
  170. ConicGradient,
  171. Content,
  172. FilterValueList,
  173. Flex,
  174. FlexFlow,
  175. Font,
  176. Frequency,
  177. GridAreaShorthand,
  178. GridTemplateArea,
  179. GridTrackPlacement,
  180. GridTrackPlacementShorthand,
  181. GridTrackSizeList,
  182. Identifier,
  183. Image,
  184. Inherit,
  185. Initial,
  186. Invalid,
  187. Length,
  188. LinearGradient,
  189. ListStyle,
  190. Numeric,
  191. Overflow,
  192. Percentage,
  193. Position,
  194. RadialGradient,
  195. Rect,
  196. Resolution,
  197. Shadow,
  198. String,
  199. TextDecoration,
  200. Time,
  201. Transformation,
  202. Unresolved,
  203. Unset,
  204. ValueList
  205. };
  206. Type type() const { return m_type; }
  207. bool is_abstract_image() const { return AK::first_is_one_of(type(), Type::Image, Type::LinearGradient, Type::ConicGradient, Type::RadialGradient); }
  208. bool is_angle() const { return type() == Type::Angle; }
  209. bool is_background() const { return type() == Type::Background; }
  210. bool is_background_repeat() const { return type() == Type::BackgroundRepeat; }
  211. bool is_background_size() const { return type() == Type::BackgroundSize; }
  212. bool is_border() const { return type() == Type::Border; }
  213. bool is_border_radius() const { return type() == Type::BorderRadius; }
  214. bool is_border_radius_shorthand() const { return type() == Type::BorderRadiusShorthand; }
  215. bool is_calculated() const { return type() == Type::Calculated; }
  216. bool is_color() const { return type() == Type::Color; }
  217. bool is_conic_gradient() const { return type() == Type::ConicGradient; }
  218. bool is_content() const { return type() == Type::Content; }
  219. bool is_filter_value_list() const { return type() == Type::FilterValueList; }
  220. bool is_flex() const { return type() == Type::Flex; }
  221. bool is_flex_flow() const { return type() == Type::FlexFlow; }
  222. bool is_font() const { return type() == Type::Font; }
  223. bool is_frequency() const { return type() == Type::Frequency; }
  224. bool is_grid_area_shorthand() const { return type() == Type::GridAreaShorthand; }
  225. bool is_grid_template_area() const { return type() == Type::GridTemplateArea; }
  226. bool is_grid_track_placement() const { return type() == Type::GridTrackPlacement; }
  227. bool is_grid_track_placement_shorthand() const { return type() == Type::GridTrackPlacementShorthand; }
  228. bool is_grid_track_size_list() const { return type() == Type::GridTrackSizeList; }
  229. bool is_identifier() const { return type() == Type::Identifier; }
  230. bool is_image() const { return type() == Type::Image; }
  231. bool is_inherit() const { return type() == Type::Inherit; }
  232. bool is_initial() const { return type() == Type::Initial; }
  233. bool is_length() const { return type() == Type::Length; }
  234. bool is_linear_gradient() const { return type() == Type::LinearGradient; }
  235. bool is_list_style() const { return type() == Type::ListStyle; }
  236. bool is_numeric() const { return type() == Type::Numeric; }
  237. bool is_overflow() const { return type() == Type::Overflow; }
  238. bool is_percentage() const { return type() == Type::Percentage; }
  239. bool is_position() const { return type() == Type::Position; }
  240. bool is_radial_gradient() const { return type() == Type::RadialGradient; }
  241. bool is_rect() const { return type() == Type::Rect; }
  242. bool is_resolution() const { return type() == Type::Resolution; }
  243. bool is_shadow() const { return type() == Type::Shadow; }
  244. bool is_string() const { return type() == Type::String; }
  245. bool is_text_decoration() const { return type() == Type::TextDecoration; }
  246. bool is_time() const { return type() == Type::Time; }
  247. bool is_transformation() const { return type() == Type::Transformation; }
  248. bool is_unresolved() const { return type() == Type::Unresolved; }
  249. bool is_unset() const { return type() == Type::Unset; }
  250. bool is_value_list() const { return type() == Type::ValueList; }
  251. bool is_builtin() const { return is_inherit() || is_initial() || is_unset(); }
  252. AbstractImageStyleValue const& as_abstract_image() const;
  253. AngleStyleValue const& as_angle() const;
  254. BackgroundStyleValue const& as_background() const;
  255. BackgroundRepeatStyleValue const& as_background_repeat() const;
  256. BackgroundSizeStyleValue const& as_background_size() const;
  257. BorderRadiusStyleValue const& as_border_radius() const;
  258. BorderRadiusShorthandStyleValue const& as_border_radius_shorthand() const;
  259. BorderStyleValue const& as_border() const;
  260. CalculatedStyleValue const& as_calculated() const;
  261. ColorStyleValue const& as_color() const;
  262. ConicGradientStyleValue const& as_conic_gradient() const;
  263. ContentStyleValue const& as_content() const;
  264. FilterValueListStyleValue const& as_filter_value_list() const;
  265. FlexFlowStyleValue const& as_flex_flow() const;
  266. FlexStyleValue const& as_flex() const;
  267. FontStyleValue const& as_font() const;
  268. FrequencyStyleValue const& as_frequency() const;
  269. GridAreaShorthandStyleValue const& as_grid_area_shorthand() const;
  270. GridTemplateAreaStyleValue const& as_grid_template_area() const;
  271. GridTrackPlacementShorthandStyleValue const& as_grid_track_placement_shorthand() const;
  272. GridTrackPlacementStyleValue const& as_grid_track_placement() const;
  273. GridTrackSizeStyleValue const& as_grid_track_size_list() const;
  274. IdentifierStyleValue const& as_identifier() const;
  275. ImageStyleValue const& as_image() const;
  276. InheritStyleValue const& as_inherit() const;
  277. InitialStyleValue const& as_initial() const;
  278. LengthStyleValue const& as_length() const;
  279. LinearGradientStyleValue const& as_linear_gradient() const;
  280. ListStyleStyleValue const& as_list_style() const;
  281. NumericStyleValue const& as_numeric() const;
  282. OverflowStyleValue const& as_overflow() const;
  283. PercentageStyleValue const& as_percentage() const;
  284. PositionStyleValue const& as_position() const;
  285. RadialGradientStyleValue const& as_radial_gradient() const;
  286. RectStyleValue const& as_rect() const;
  287. ResolutionStyleValue const& as_resolution() const;
  288. ShadowStyleValue const& as_shadow() const;
  289. StringStyleValue const& as_string() const;
  290. TextDecorationStyleValue const& as_text_decoration() const;
  291. TimeStyleValue const& as_time() const;
  292. TransformationStyleValue const& as_transformation() const;
  293. UnresolvedStyleValue const& as_unresolved() const;
  294. UnsetStyleValue const& as_unset() const;
  295. StyleValueList const& as_value_list() const;
  296. AbstractImageStyleValue& as_abstract_image() { return const_cast<AbstractImageStyleValue&>(const_cast<StyleValue const&>(*this).as_abstract_image()); }
  297. AngleStyleValue& as_angle() { return const_cast<AngleStyleValue&>(const_cast<StyleValue const&>(*this).as_angle()); }
  298. BackgroundStyleValue& as_background() { return const_cast<BackgroundStyleValue&>(const_cast<StyleValue const&>(*this).as_background()); }
  299. BackgroundRepeatStyleValue& as_background_repeat() { return const_cast<BackgroundRepeatStyleValue&>(const_cast<StyleValue const&>(*this).as_background_repeat()); }
  300. BackgroundSizeStyleValue& as_background_size() { return const_cast<BackgroundSizeStyleValue&>(const_cast<StyleValue const&>(*this).as_background_size()); }
  301. BorderRadiusStyleValue& as_border_radius() { return const_cast<BorderRadiusStyleValue&>(const_cast<StyleValue const&>(*this).as_border_radius()); }
  302. BorderRadiusShorthandStyleValue& as_border_radius_shorthand() { return const_cast<BorderRadiusShorthandStyleValue&>(const_cast<StyleValue const&>(*this).as_border_radius_shorthand()); }
  303. BorderStyleValue& as_border() { return const_cast<BorderStyleValue&>(const_cast<StyleValue const&>(*this).as_border()); }
  304. CalculatedStyleValue& as_calculated() { return const_cast<CalculatedStyleValue&>(const_cast<StyleValue const&>(*this).as_calculated()); }
  305. ColorStyleValue& as_color() { return const_cast<ColorStyleValue&>(const_cast<StyleValue const&>(*this).as_color()); }
  306. ConicGradientStyleValue& as_conic_gradient() { return const_cast<ConicGradientStyleValue&>(const_cast<StyleValue const&>(*this).as_conic_gradient()); }
  307. ContentStyleValue& as_content() { return const_cast<ContentStyleValue&>(const_cast<StyleValue const&>(*this).as_content()); }
  308. FilterValueListStyleValue& as_filter_value_list() { return const_cast<FilterValueListStyleValue&>(const_cast<StyleValue const&>(*this).as_filter_value_list()); }
  309. FlexFlowStyleValue& as_flex_flow() { return const_cast<FlexFlowStyleValue&>(const_cast<StyleValue const&>(*this).as_flex_flow()); }
  310. FlexStyleValue& as_flex() { return const_cast<FlexStyleValue&>(const_cast<StyleValue const&>(*this).as_flex()); }
  311. FontStyleValue& as_font() { return const_cast<FontStyleValue&>(const_cast<StyleValue const&>(*this).as_font()); }
  312. FrequencyStyleValue& as_frequency() { return const_cast<FrequencyStyleValue&>(const_cast<StyleValue const&>(*this).as_frequency()); }
  313. GridAreaShorthandStyleValue& as_grid_area_shorthand() { return const_cast<GridAreaShorthandStyleValue&>(const_cast<StyleValue const&>(*this).as_grid_area_shorthand()); }
  314. GridTemplateAreaStyleValue& as_grid_template_area() { return const_cast<GridTemplateAreaStyleValue&>(const_cast<StyleValue const&>(*this).as_grid_template_area()); }
  315. GridTrackPlacementShorthandStyleValue& as_grid_track_placement_shorthand() { return const_cast<GridTrackPlacementShorthandStyleValue&>(const_cast<StyleValue const&>(*this).as_grid_track_placement_shorthand()); }
  316. GridTrackPlacementStyleValue& as_grid_track_placement() { return const_cast<GridTrackPlacementStyleValue&>(const_cast<StyleValue const&>(*this).as_grid_track_placement()); }
  317. GridTrackSizeStyleValue& as_grid_track_size_list() { return const_cast<GridTrackSizeStyleValue&>(const_cast<StyleValue const&>(*this).as_grid_track_size_list()); }
  318. IdentifierStyleValue& as_identifier() { return const_cast<IdentifierStyleValue&>(const_cast<StyleValue const&>(*this).as_identifier()); }
  319. ImageStyleValue& as_image() { return const_cast<ImageStyleValue&>(const_cast<StyleValue const&>(*this).as_image()); }
  320. InheritStyleValue& as_inherit() { return const_cast<InheritStyleValue&>(const_cast<StyleValue const&>(*this).as_inherit()); }
  321. InitialStyleValue& as_initial() { return const_cast<InitialStyleValue&>(const_cast<StyleValue const&>(*this).as_initial()); }
  322. LengthStyleValue& as_length() { return const_cast<LengthStyleValue&>(const_cast<StyleValue const&>(*this).as_length()); }
  323. LinearGradientStyleValue& as_linear_gradient() { return const_cast<LinearGradientStyleValue&>(const_cast<StyleValue const&>(*this).as_linear_gradient()); }
  324. ListStyleStyleValue& as_list_style() { return const_cast<ListStyleStyleValue&>(const_cast<StyleValue const&>(*this).as_list_style()); }
  325. NumericStyleValue& as_numeric() { return const_cast<NumericStyleValue&>(const_cast<StyleValue const&>(*this).as_numeric()); }
  326. OverflowStyleValue& as_overflow() { return const_cast<OverflowStyleValue&>(const_cast<StyleValue const&>(*this).as_overflow()); }
  327. PercentageStyleValue& as_percentage() { return const_cast<PercentageStyleValue&>(const_cast<StyleValue const&>(*this).as_percentage()); }
  328. PositionStyleValue& as_position() { return const_cast<PositionStyleValue&>(const_cast<StyleValue const&>(*this).as_position()); }
  329. RadialGradientStyleValue& as_radial_gradient() { return const_cast<RadialGradientStyleValue&>(const_cast<StyleValue const&>(*this).as_radial_gradient()); }
  330. RectStyleValue& as_rect() { return const_cast<RectStyleValue&>(const_cast<StyleValue const&>(*this).as_rect()); }
  331. ResolutionStyleValue& as_resolution() { return const_cast<ResolutionStyleValue&>(const_cast<StyleValue const&>(*this).as_resolution()); }
  332. ShadowStyleValue& as_shadow() { return const_cast<ShadowStyleValue&>(const_cast<StyleValue const&>(*this).as_shadow()); }
  333. StringStyleValue& as_string() { return const_cast<StringStyleValue&>(const_cast<StyleValue const&>(*this).as_string()); }
  334. TextDecorationStyleValue& as_text_decoration() { return const_cast<TextDecorationStyleValue&>(const_cast<StyleValue const&>(*this).as_text_decoration()); }
  335. TimeStyleValue& as_time() { return const_cast<TimeStyleValue&>(const_cast<StyleValue const&>(*this).as_time()); }
  336. TransformationStyleValue& as_transformation() { return const_cast<TransformationStyleValue&>(const_cast<StyleValue const&>(*this).as_transformation()); }
  337. UnresolvedStyleValue& as_unresolved() { return const_cast<UnresolvedStyleValue&>(const_cast<StyleValue const&>(*this).as_unresolved()); }
  338. UnsetStyleValue& as_unset() { return const_cast<UnsetStyleValue&>(const_cast<StyleValue const&>(*this).as_unset()); }
  339. StyleValueList& as_value_list() { return const_cast<StyleValueList&>(const_cast<StyleValue const&>(*this).as_value_list()); }
  340. virtual bool has_auto() const { return false; }
  341. virtual bool has_color() const { return false; }
  342. virtual bool has_identifier() const { return false; }
  343. virtual bool has_length() const { return false; }
  344. virtual bool has_rect() const { return false; }
  345. virtual bool has_number() const { return false; }
  346. virtual bool has_integer() const { return false; }
  347. virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const;
  348. virtual Color to_color(Layout::NodeWithStyle const&) const { return {}; }
  349. virtual EdgeRect to_rect() const { VERIFY_NOT_REACHED(); }
  350. virtual CSS::ValueID to_identifier() const { return ValueID::Invalid; }
  351. virtual Length to_length() const { VERIFY_NOT_REACHED(); }
  352. virtual float to_number() const { return 0; }
  353. virtual float to_integer() const { return 0; }
  354. virtual ErrorOr<String> to_string() const = 0;
  355. virtual bool equals(StyleValue const& other) const = 0;
  356. bool operator==(StyleValue const& other) const
  357. {
  358. return this->equals(other);
  359. }
  360. protected:
  361. explicit StyleValue(Type);
  362. private:
  363. Type m_type { Type::Invalid };
  364. };
  365. template<typename T>
  366. struct StyleValueWithDefaultOperators : public StyleValue {
  367. using StyleValue::StyleValue;
  368. virtual bool equals(StyleValue const& other) const override
  369. {
  370. if (type() != other.type())
  371. return false;
  372. auto const& typed_other = static_cast<T const&>(other);
  373. return static_cast<T const&>(*this).properties_equal(typed_other);
  374. }
  375. };
  376. class CalculatedStyleValue : public StyleValue {
  377. public:
  378. enum class ResolvedType {
  379. Angle,
  380. Frequency,
  381. Integer,
  382. Length,
  383. Number,
  384. Percentage,
  385. Time,
  386. };
  387. enum class SumOperation {
  388. Add,
  389. Subtract,
  390. };
  391. enum class ProductOperation {
  392. Multiply,
  393. Divide,
  394. };
  395. using PercentageBasis = Variant<Empty, Angle, Frequency, Length, Time>;
  396. class CalculationResult {
  397. public:
  398. using Value = Variant<Number, Angle, Frequency, Length, Percentage, Time>;
  399. CalculationResult(Value value)
  400. : m_value(move(value))
  401. {
  402. }
  403. void add(CalculationResult const& other, Layout::Node const*, PercentageBasis const& percentage_basis);
  404. void subtract(CalculationResult const& other, Layout::Node const*, PercentageBasis const& percentage_basis);
  405. void multiply_by(CalculationResult const& other, Layout::Node const*);
  406. void divide_by(CalculationResult const& other, Layout::Node const*);
  407. Value const& value() const { return m_value; }
  408. private:
  409. void add_or_subtract_internal(SumOperation op, CalculationResult const& other, Layout::Node const*, PercentageBasis const& percentage_basis);
  410. Value m_value;
  411. };
  412. struct CalcSum;
  413. struct CalcSumPartWithOperator;
  414. struct CalcProduct;
  415. struct CalcProductPartWithOperator;
  416. struct CalcNumberSum;
  417. struct CalcNumberSumPartWithOperator;
  418. struct CalcNumberProduct;
  419. struct CalcNumberProductPartWithOperator;
  420. struct CalcNumberValue {
  421. Variant<Number, NonnullOwnPtr<CalcNumberSum>> value;
  422. ErrorOr<String> to_string() const;
  423. Optional<ResolvedType> resolved_type() const;
  424. CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const;
  425. };
  426. struct CalcValue {
  427. Variant<Number, Angle, Frequency, Length, Percentage, Time, NonnullOwnPtr<CalcSum>> value;
  428. ErrorOr<String> to_string() const;
  429. Optional<ResolvedType> resolved_type() const;
  430. CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const;
  431. bool contains_percentage() const;
  432. };
  433. // This represents that: https://www.w3.org/TR/css-values-3/#calc-syntax
  434. struct CalcSum {
  435. CalcSum(NonnullOwnPtr<CalcProduct> first_calc_product, Vector<NonnullOwnPtr<CalcSumPartWithOperator>> additional)
  436. : first_calc_product(move(first_calc_product))
  437. , zero_or_more_additional_calc_products(move(additional)) {};
  438. NonnullOwnPtr<CalcProduct> first_calc_product;
  439. Vector<NonnullOwnPtr<CalcSumPartWithOperator>> zero_or_more_additional_calc_products;
  440. ErrorOr<String> to_string() const;
  441. Optional<ResolvedType> resolved_type() const;
  442. CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const;
  443. bool contains_percentage() const;
  444. };
  445. struct CalcNumberSum {
  446. CalcNumberSum(NonnullOwnPtr<CalcNumberProduct> first_calc_number_product, Vector<NonnullOwnPtr<CalcNumberSumPartWithOperator>> additional)
  447. : first_calc_number_product(move(first_calc_number_product))
  448. , zero_or_more_additional_calc_number_products(move(additional)) {};
  449. NonnullOwnPtr<CalcNumberProduct> first_calc_number_product;
  450. Vector<NonnullOwnPtr<CalcNumberSumPartWithOperator>> zero_or_more_additional_calc_number_products;
  451. ErrorOr<String> to_string() const;
  452. Optional<ResolvedType> resolved_type() const;
  453. CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const;
  454. };
  455. struct CalcProduct {
  456. CalcValue first_calc_value;
  457. Vector<NonnullOwnPtr<CalcProductPartWithOperator>> zero_or_more_additional_calc_values;
  458. ErrorOr<String> to_string() const;
  459. Optional<ResolvedType> resolved_type() const;
  460. CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const;
  461. bool contains_percentage() const;
  462. };
  463. struct CalcSumPartWithOperator {
  464. CalcSumPartWithOperator(SumOperation op, NonnullOwnPtr<CalcProduct> calc_product)
  465. : op(op)
  466. , value(move(calc_product)) {};
  467. SumOperation op;
  468. NonnullOwnPtr<CalcProduct> value;
  469. ErrorOr<String> to_string() const;
  470. Optional<ResolvedType> resolved_type() const;
  471. CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const;
  472. bool contains_percentage() const;
  473. };
  474. struct CalcProductPartWithOperator {
  475. ProductOperation op;
  476. Variant<CalcValue, CalcNumberValue> value;
  477. ErrorOr<String> to_string() const;
  478. Optional<ResolvedType> resolved_type() const;
  479. CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const;
  480. bool contains_percentage() const;
  481. };
  482. struct CalcNumberProduct {
  483. CalcNumberValue first_calc_number_value;
  484. Vector<NonnullOwnPtr<CalcNumberProductPartWithOperator>> zero_or_more_additional_calc_number_values;
  485. ErrorOr<String> to_string() const;
  486. Optional<ResolvedType> resolved_type() const;
  487. CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const;
  488. };
  489. struct CalcNumberProductPartWithOperator {
  490. ProductOperation op;
  491. CalcNumberValue value;
  492. ErrorOr<String> to_string() const;
  493. Optional<ResolvedType> resolved_type() const;
  494. CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const;
  495. };
  496. struct CalcNumberSumPartWithOperator {
  497. CalcNumberSumPartWithOperator(SumOperation op, NonnullOwnPtr<CalcNumberProduct> calc_number_product)
  498. : op(op)
  499. , value(move(calc_number_product)) {};
  500. SumOperation op;
  501. NonnullOwnPtr<CalcNumberProduct> value;
  502. ErrorOr<String> to_string() const;
  503. Optional<ResolvedType> resolved_type() const;
  504. CalculationResult resolve(Layout::Node const*, PercentageBasis const& percentage_basis) const;
  505. };
  506. static ValueComparingNonnullRefPtr<CalculatedStyleValue> create(NonnullOwnPtr<CalcSum> calc_sum, ResolvedType resolved_type)
  507. {
  508. return adopt_ref(*new CalculatedStyleValue(move(calc_sum), resolved_type));
  509. }
  510. ErrorOr<String> to_string() const override;
  511. virtual bool equals(StyleValue const& other) const override;
  512. ResolvedType resolved_type() const { return m_resolved_type; }
  513. NonnullOwnPtr<CalcSum> const& expression() const { return m_expression; }
  514. bool resolves_to_angle() const { return m_resolved_type == ResolvedType::Angle; }
  515. Optional<Angle> resolve_angle() const;
  516. Optional<Angle> resolve_angle_percentage(Angle const& percentage_basis) const;
  517. bool resolves_to_frequency() const { return m_resolved_type == ResolvedType::Frequency; }
  518. Optional<Frequency> resolve_frequency() const;
  519. Optional<Frequency> resolve_frequency_percentage(Frequency const& percentage_basis) const;
  520. bool resolves_to_length() const { return m_resolved_type == ResolvedType::Length; }
  521. Optional<Length> resolve_length(Layout::Node const& layout_node) const;
  522. Optional<Length> resolve_length_percentage(Layout::Node const&, Length const& percentage_basis) const;
  523. bool resolves_to_percentage() const { return m_resolved_type == ResolvedType::Percentage; }
  524. Optional<Percentage> resolve_percentage() const;
  525. bool resolves_to_time() const { return m_resolved_type == ResolvedType::Time; }
  526. Optional<Time> resolve_time() const;
  527. Optional<Time> resolve_time_percentage(Time const& percentage_basis) const;
  528. bool resolves_to_integer() const { return m_resolved_type == ResolvedType::Integer; }
  529. bool resolves_to_number() const { return resolves_to_integer() || m_resolved_type == ResolvedType::Number; }
  530. Optional<float> resolve_number();
  531. Optional<i64> resolve_integer();
  532. bool contains_percentage() const;
  533. private:
  534. explicit CalculatedStyleValue(NonnullOwnPtr<CalcSum> calc_sum, ResolvedType resolved_type)
  535. : StyleValue(Type::Calculated)
  536. , m_resolved_type(resolved_type)
  537. , m_expression(move(calc_sum))
  538. {
  539. }
  540. ResolvedType m_resolved_type;
  541. NonnullOwnPtr<CalcSum> m_expression;
  542. };
  543. class NumericStyleValue : public StyleValueWithDefaultOperators<NumericStyleValue> {
  544. public:
  545. static ValueComparingNonnullRefPtr<NumericStyleValue> create_float(float value)
  546. {
  547. return adopt_ref(*new NumericStyleValue(value));
  548. }
  549. static ValueComparingNonnullRefPtr<NumericStyleValue> create_integer(i64 value)
  550. {
  551. return adopt_ref(*new NumericStyleValue(value));
  552. }
  553. virtual bool has_length() const override { return to_number() == 0; }
  554. virtual Length to_length() const override { return Length::make_px(0); }
  555. virtual bool has_number() const override { return true; }
  556. virtual float to_number() const override
  557. {
  558. return m_value.visit(
  559. [](float value) { return value; },
  560. [](i64 value) { return (float)value; });
  561. }
  562. virtual bool has_integer() const override { return m_value.has<i64>(); }
  563. virtual float to_integer() const override { return m_value.get<i64>(); }
  564. virtual ErrorOr<String> to_string() const override;
  565. bool properties_equal(NumericStyleValue const& other) const { return m_value == other.m_value; }
  566. private:
  567. explicit NumericStyleValue(Variant<float, i64> value)
  568. : StyleValueWithDefaultOperators(Type::Numeric)
  569. , m_value(move(value))
  570. {
  571. }
  572. Variant<float, i64> m_value { (i64)0 };
  573. };
  574. class OverflowStyleValue final : public StyleValueWithDefaultOperators<OverflowStyleValue> {
  575. public:
  576. static ValueComparingNonnullRefPtr<OverflowStyleValue> create(ValueComparingNonnullRefPtr<StyleValue> overflow_x, ValueComparingNonnullRefPtr<StyleValue> overflow_y)
  577. {
  578. return adopt_ref(*new OverflowStyleValue(move(overflow_x), move(overflow_y)));
  579. }
  580. virtual ~OverflowStyleValue() override = default;
  581. ValueComparingNonnullRefPtr<StyleValue> overflow_x() const { return m_properties.overflow_x; }
  582. ValueComparingNonnullRefPtr<StyleValue> overflow_y() const { return m_properties.overflow_y; }
  583. virtual ErrorOr<String> to_string() const override;
  584. bool properties_equal(OverflowStyleValue const& other) const { return m_properties == other.m_properties; }
  585. private:
  586. OverflowStyleValue(ValueComparingNonnullRefPtr<StyleValue> overflow_x, ValueComparingNonnullRefPtr<StyleValue> overflow_y)
  587. : StyleValueWithDefaultOperators(Type::Overflow)
  588. , m_properties { .overflow_x = move(overflow_x), .overflow_y = move(overflow_y) }
  589. {
  590. }
  591. struct Properties {
  592. ValueComparingNonnullRefPtr<StyleValue> overflow_x;
  593. ValueComparingNonnullRefPtr<StyleValue> overflow_y;
  594. bool operator==(Properties const&) const = default;
  595. } m_properties;
  596. };
  597. class PercentageStyleValue final : public StyleValueWithDefaultOperators<PercentageStyleValue> {
  598. public:
  599. static ValueComparingNonnullRefPtr<PercentageStyleValue> create(Percentage percentage)
  600. {
  601. return adopt_ref(*new PercentageStyleValue(move(percentage)));
  602. }
  603. virtual ~PercentageStyleValue() override = default;
  604. Percentage const& percentage() const { return m_percentage; }
  605. Percentage& percentage() { return m_percentage; }
  606. virtual ErrorOr<String> to_string() const override;
  607. bool properties_equal(PercentageStyleValue const& other) const { return m_percentage == other.m_percentage; }
  608. private:
  609. PercentageStyleValue(Percentage&& percentage)
  610. : StyleValueWithDefaultOperators(Type::Percentage)
  611. , m_percentage(percentage)
  612. {
  613. }
  614. Percentage m_percentage;
  615. };
  616. class PositionStyleValue final : public StyleValueWithDefaultOperators<PositionStyleValue> {
  617. public:
  618. static ValueComparingNonnullRefPtr<PositionStyleValue> create(PositionEdge edge_x, LengthPercentage const& offset_x, PositionEdge edge_y, LengthPercentage const& offset_y)
  619. {
  620. return adopt_ref(*new PositionStyleValue(edge_x, offset_x, edge_y, offset_y));
  621. }
  622. virtual ~PositionStyleValue() override = default;
  623. PositionEdge edge_x() const { return m_properties.edge_x; }
  624. LengthPercentage const& offset_x() const { return m_properties.offset_x; }
  625. PositionEdge edge_y() const { return m_properties.edge_y; }
  626. LengthPercentage const& offset_y() const { return m_properties.offset_y; }
  627. virtual ErrorOr<String> to_string() const override;
  628. bool properties_equal(PositionStyleValue const& other) const { return m_properties == other.m_properties; }
  629. private:
  630. PositionStyleValue(PositionEdge edge_x, LengthPercentage const& offset_x, PositionEdge edge_y, LengthPercentage const& offset_y)
  631. : StyleValueWithDefaultOperators(Type::Position)
  632. , m_properties { .edge_x = edge_x, .offset_x = offset_x, .edge_y = edge_y, .offset_y = offset_y }
  633. {
  634. }
  635. struct Properties {
  636. PositionEdge edge_x;
  637. LengthPercentage offset_x;
  638. PositionEdge edge_y;
  639. LengthPercentage offset_y;
  640. bool operator==(Properties const&) const = default;
  641. } m_properties;
  642. };
  643. class ResolutionStyleValue : public StyleValueWithDefaultOperators<ResolutionStyleValue> {
  644. public:
  645. static ValueComparingNonnullRefPtr<ResolutionStyleValue> create(Resolution resolution)
  646. {
  647. return adopt_ref(*new ResolutionStyleValue(move(resolution)));
  648. }
  649. virtual ~ResolutionStyleValue() override { }
  650. Resolution const& resolution() const { return m_resolution; }
  651. virtual ErrorOr<String> to_string() const override { return m_resolution.to_string(); }
  652. bool properties_equal(ResolutionStyleValue const& other) const { return m_resolution == other.m_resolution; }
  653. private:
  654. explicit ResolutionStyleValue(Resolution resolution)
  655. : StyleValueWithDefaultOperators(Type::Resolution)
  656. , m_resolution(move(resolution))
  657. {
  658. }
  659. Resolution m_resolution;
  660. };
  661. class ShadowStyleValue final : public StyleValueWithDefaultOperators<ShadowStyleValue> {
  662. public:
  663. static ValueComparingNonnullRefPtr<ShadowStyleValue>
  664. create(Color color, Length const& offset_x, Length const& offset_y, Length const& blur_radius, Length const& spread_distance, ShadowPlacement placement)
  665. {
  666. return adopt_ref(*new ShadowStyleValue(color, offset_x, offset_y, blur_radius, spread_distance, placement));
  667. }
  668. virtual ~ShadowStyleValue() override = default;
  669. Color color() const { return m_properties.color; }
  670. Length const& offset_x() const { return m_properties.offset_x; }
  671. Length const& offset_y() const { return m_properties.offset_y; }
  672. Length const& blur_radius() const { return m_properties.blur_radius; }
  673. Length const& spread_distance() const { return m_properties.spread_distance; }
  674. ShadowPlacement placement() const { return m_properties.placement; }
  675. virtual ErrorOr<String> to_string() const override;
  676. bool properties_equal(ShadowStyleValue const& other) const { return m_properties == other.m_properties; }
  677. private:
  678. explicit ShadowStyleValue(Color color, Length const& offset_x, Length const& offset_y, Length const& blur_radius, Length const& spread_distance, ShadowPlacement placement)
  679. : StyleValueWithDefaultOperators(Type::Shadow)
  680. , m_properties { .color = color, .offset_x = offset_x, .offset_y = offset_y, .blur_radius = blur_radius, .spread_distance = spread_distance, .placement = placement }
  681. {
  682. }
  683. virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const override;
  684. struct Properties {
  685. Color color;
  686. Length offset_x;
  687. Length offset_y;
  688. Length blur_radius;
  689. Length spread_distance;
  690. ShadowPlacement placement;
  691. bool operator==(Properties const&) const = default;
  692. } m_properties;
  693. };
  694. class StringStyleValue : public StyleValueWithDefaultOperators<StringStyleValue> {
  695. public:
  696. static ValueComparingNonnullRefPtr<StringStyleValue> create(String const& string)
  697. {
  698. return adopt_ref(*new StringStyleValue(string));
  699. }
  700. virtual ~StringStyleValue() override = default;
  701. ErrorOr<String> to_string() const override { return m_string; }
  702. bool properties_equal(StringStyleValue const& other) const { return m_string == other.m_string; }
  703. private:
  704. explicit StringStyleValue(String const& string)
  705. : StyleValueWithDefaultOperators(Type::String)
  706. , m_string(string)
  707. {
  708. }
  709. String m_string;
  710. };
  711. class TextDecorationStyleValue final : public StyleValueWithDefaultOperators<TextDecorationStyleValue> {
  712. public:
  713. static ValueComparingNonnullRefPtr<TextDecorationStyleValue> create(
  714. ValueComparingNonnullRefPtr<StyleValue> line,
  715. ValueComparingNonnullRefPtr<StyleValue> thickness,
  716. ValueComparingNonnullRefPtr<StyleValue> style,
  717. ValueComparingNonnullRefPtr<StyleValue> color)
  718. {
  719. return adopt_ref(*new TextDecorationStyleValue(move(line), move(thickness), move(style), move(color)));
  720. }
  721. virtual ~TextDecorationStyleValue() override = default;
  722. ValueComparingNonnullRefPtr<StyleValue> line() const { return m_properties.line; }
  723. ValueComparingNonnullRefPtr<StyleValue> thickness() const { return m_properties.thickness; }
  724. ValueComparingNonnullRefPtr<StyleValue> style() const { return m_properties.style; }
  725. ValueComparingNonnullRefPtr<StyleValue> color() const { return m_properties.color; }
  726. virtual ErrorOr<String> to_string() const override;
  727. bool properties_equal(TextDecorationStyleValue const& other) const { return m_properties == other.m_properties; }
  728. private:
  729. TextDecorationStyleValue(
  730. ValueComparingNonnullRefPtr<StyleValue> line,
  731. ValueComparingNonnullRefPtr<StyleValue> thickness,
  732. ValueComparingNonnullRefPtr<StyleValue> style,
  733. ValueComparingNonnullRefPtr<StyleValue> color)
  734. : StyleValueWithDefaultOperators(Type::TextDecoration)
  735. , m_properties { .line = move(line), .thickness = move(thickness), .style = move(style), .color = move(color) }
  736. {
  737. }
  738. struct Properties {
  739. ValueComparingNonnullRefPtr<StyleValue> line;
  740. ValueComparingNonnullRefPtr<StyleValue> thickness;
  741. ValueComparingNonnullRefPtr<StyleValue> style;
  742. ValueComparingNonnullRefPtr<StyleValue> color;
  743. bool operator==(Properties const&) const = default;
  744. } m_properties;
  745. };
  746. class TimeStyleValue : public StyleValueWithDefaultOperators<TimeStyleValue> {
  747. public:
  748. static ValueComparingNonnullRefPtr<TimeStyleValue> create(Time time)
  749. {
  750. return adopt_ref(*new TimeStyleValue(move(time)));
  751. }
  752. virtual ~TimeStyleValue() override { }
  753. Time const& time() const { return m_time; }
  754. virtual ErrorOr<String> to_string() const override { return m_time.to_string(); }
  755. bool properties_equal(TimeStyleValue const& other) const { return m_time == other.m_time; }
  756. private:
  757. explicit TimeStyleValue(Time time)
  758. : StyleValueWithDefaultOperators(Type::Time)
  759. , m_time(move(time))
  760. {
  761. }
  762. Time m_time;
  763. };
  764. class TransformationStyleValue final : public StyleValueWithDefaultOperators<TransformationStyleValue> {
  765. public:
  766. static ValueComparingNonnullRefPtr<TransformationStyleValue> create(CSS::TransformFunction transform_function, StyleValueVector&& values)
  767. {
  768. return adopt_ref(*new TransformationStyleValue(transform_function, move(values)));
  769. }
  770. virtual ~TransformationStyleValue() override = default;
  771. CSS::TransformFunction transform_function() const { return m_properties.transform_function; }
  772. StyleValueVector values() const { return m_properties.values; }
  773. virtual ErrorOr<String> to_string() const override;
  774. bool properties_equal(TransformationStyleValue const& other) const { return m_properties == other.m_properties; }
  775. private:
  776. TransformationStyleValue(CSS::TransformFunction transform_function, StyleValueVector&& values)
  777. : StyleValueWithDefaultOperators(Type::Transformation)
  778. , m_properties { .transform_function = transform_function, .values = move(values) }
  779. {
  780. }
  781. struct Properties {
  782. CSS::TransformFunction transform_function;
  783. StyleValueVector values;
  784. bool operator==(Properties const& other) const;
  785. } m_properties;
  786. };
  787. class UnresolvedStyleValue final : public StyleValue {
  788. public:
  789. static ValueComparingNonnullRefPtr<UnresolvedStyleValue> create(Vector<Parser::ComponentValue>&& values, bool contains_var_or_attr)
  790. {
  791. return adopt_ref(*new UnresolvedStyleValue(move(values), contains_var_or_attr));
  792. }
  793. virtual ~UnresolvedStyleValue() override = default;
  794. virtual ErrorOr<String> to_string() const override;
  795. Vector<Parser::ComponentValue> const& values() const { return m_values; }
  796. bool contains_var_or_attr() const { return m_contains_var_or_attr; }
  797. virtual bool equals(StyleValue const& other) const override;
  798. private:
  799. UnresolvedStyleValue(Vector<Parser::ComponentValue>&& values, bool contains_var_or_attr)
  800. : StyleValue(Type::Unresolved)
  801. , m_values(move(values))
  802. , m_contains_var_or_attr(contains_var_or_attr)
  803. {
  804. }
  805. Vector<Parser::ComponentValue> m_values;
  806. bool m_contains_var_or_attr { false };
  807. };
  808. class UnsetStyleValue final : public StyleValueWithDefaultOperators<UnsetStyleValue> {
  809. public:
  810. static ValueComparingNonnullRefPtr<UnsetStyleValue> the()
  811. {
  812. static ValueComparingNonnullRefPtr<UnsetStyleValue> instance = adopt_ref(*new UnsetStyleValue);
  813. return instance;
  814. }
  815. virtual ~UnsetStyleValue() override = default;
  816. ErrorOr<String> to_string() const override { return "unset"_string; }
  817. bool properties_equal(UnsetStyleValue const&) const { return true; }
  818. private:
  819. UnsetStyleValue()
  820. : StyleValueWithDefaultOperators(Type::Unset)
  821. {
  822. }
  823. };
  824. class StyleValueList final : public StyleValueWithDefaultOperators<StyleValueList> {
  825. public:
  826. enum class Separator {
  827. Space,
  828. Comma,
  829. };
  830. static ValueComparingNonnullRefPtr<StyleValueList> create(StyleValueVector&& values, Separator separator) { return adopt_ref(*new StyleValueList(move(values), separator)); }
  831. size_t size() const { return m_properties.values.size(); }
  832. StyleValueVector const& values() const { return m_properties.values; }
  833. ValueComparingNonnullRefPtr<StyleValue const> value_at(size_t i, bool allow_loop) const
  834. {
  835. if (allow_loop)
  836. return m_properties.values[i % size()];
  837. return m_properties.values[i];
  838. }
  839. virtual ErrorOr<String> to_string() const override;
  840. bool properties_equal(StyleValueList const& other) const { return m_properties == other.m_properties; }
  841. private:
  842. StyleValueList(StyleValueVector&& values, Separator separator)
  843. : StyleValueWithDefaultOperators(Type::ValueList)
  844. , m_properties { .separator = separator, .values = move(values) }
  845. {
  846. }
  847. struct Properties {
  848. Separator separator;
  849. StyleValueVector values;
  850. bool operator==(Properties const&) const;
  851. } m_properties;
  852. };
  853. class RectStyleValue : public StyleValueWithDefaultOperators<RectStyleValue> {
  854. public:
  855. static ValueComparingNonnullRefPtr<RectStyleValue> create(EdgeRect rect);
  856. virtual ~RectStyleValue() override = default;
  857. EdgeRect rect() const { return m_rect; }
  858. virtual ErrorOr<String> to_string() const override;
  859. virtual bool has_rect() const override { return true; }
  860. virtual EdgeRect to_rect() const override { return m_rect; }
  861. bool properties_equal(RectStyleValue const& other) const { return m_rect == other.m_rect; }
  862. private:
  863. explicit RectStyleValue(EdgeRect rect)
  864. : StyleValueWithDefaultOperators(Type::Rect)
  865. , m_rect(rect)
  866. {
  867. }
  868. EdgeRect m_rect;
  869. };
  870. }
  871. template<>
  872. struct AK::Formatter<Web::CSS::StyleValue> : Formatter<StringView> {
  873. ErrorOr<void> format(FormatBuilder& builder, Web::CSS::StyleValue const& style_value)
  874. {
  875. return Formatter<StringView>::format(builder, TRY(style_value.to_string()));
  876. }
  877. };