| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2022, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-30 10:50:40 +01:00
										 |  |  |  * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/CSS/Length.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-30 17:13:37 +01:00
										 |  |  | #include <LibWeb/CSS/PercentageOr.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Size { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     enum class Type { | 
					
						
							|  |  |  |         Auto, | 
					
						
							| 
									
										
										
										
											2023-03-30 10:50:40 +01:00
										 |  |  |         Calculated, | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |         Length, | 
					
						
							|  |  |  |         Percentage, | 
					
						
							|  |  |  |         MinContent, | 
					
						
							|  |  |  |         MaxContent, | 
					
						
							|  |  |  |         FitContent, | 
					
						
							|  |  |  |         None, // NOTE: This is only valid for max-width and max-height.
 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static Size make_auto(); | 
					
						
							| 
									
										
										
										
											2022-11-03 14:43:24 +00:00
										 |  |  |     static Size make_px(CSSPixels); | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     static Size make_length(Length); | 
					
						
							|  |  |  |     static Size make_percentage(Percentage); | 
					
						
							| 
									
										
										
										
											2025-04-15 15:18:27 -06:00
										 |  |  |     static Size make_calculated(NonnullRefPtr<CalculatedStyleValue const>); | 
					
						
							| 
									
										
										
										
											2025-08-27 11:43:44 +01:00
										 |  |  |     static Size make_length_percentage(LengthPercentage const&); | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     static Size make_min_content(); | 
					
						
							|  |  |  |     static Size make_max_content(); | 
					
						
							| 
									
										
										
										
											2025-02-26 18:16:36 +01:00
										 |  |  |     static Size make_fit_content(LengthPercentage available_space); | 
					
						
							| 
									
										
										
										
											2023-05-26 18:58:06 +02:00
										 |  |  |     static Size make_fit_content(); | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     static Size make_none(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool is_auto() const { return m_type == Type::Auto; } | 
					
						
							| 
									
										
										
										
											2023-03-30 10:50:40 +01:00
										 |  |  |     bool is_calculated() const { return m_type == Type::Calculated; } | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     bool is_length() const { return m_type == Type::Length; } | 
					
						
							|  |  |  |     bool is_percentage() const { return m_type == Type::Percentage; } | 
					
						
							|  |  |  |     bool is_min_content() const { return m_type == Type::MinContent; } | 
					
						
							|  |  |  |     bool is_max_content() const { return m_type == Type::MaxContent; } | 
					
						
							|  |  |  |     bool is_fit_content() const { return m_type == Type::FitContent; } | 
					
						
							|  |  |  |     bool is_none() const { return m_type == Type::None; } | 
					
						
							| 
									
										
										
										
											2025-09-23 14:59:55 +01:00
										 |  |  |     Type type() const { return m_type; } | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-27 11:43:44 +01:00
										 |  |  |     bool is_length_percentage() const { return is_length() || is_percentage() || is_calculated(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-06 16:33:06 +02:00
										 |  |  |     [[nodiscard]] CSSPixels to_px(Layout::Node const&, CSSPixels reference_value) const; | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     bool contains_percentage() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-11 15:05:56 +00:00
										 |  |  |     CalculatedStyleValue const& calculated() const | 
					
						
							| 
									
										
										
										
											2023-03-30 10:50:40 +01:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(is_calculated()); | 
					
						
							| 
									
										
										
										
											2025-08-28 12:29:58 +01:00
										 |  |  |         return m_length_percentage->calculated(); | 
					
						
							| 
									
										
										
										
											2023-03-30 10:50:40 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 12:29:58 +01:00
										 |  |  |     Length const& length() const | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(is_length()); | 
					
						
							| 
									
										
										
										
											2025-08-28 12:29:58 +01:00
										 |  |  |         return m_length_percentage->length(); | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 12:29:58 +01:00
										 |  |  |     Percentage const& percentage() const | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(is_percentage()); | 
					
						
							| 
									
										
										
										
											2025-08-28 12:29:58 +01:00
										 |  |  |         return m_length_percentage->percentage(); | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-23 15:00:28 +01:00
										 |  |  |     LengthPercentage const& length_percentage() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         VERIFY(is_length_percentage()); | 
					
						
							|  |  |  |         return *m_length_percentage; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-28 12:29:58 +01:00
										 |  |  |     Optional<LengthPercentage> const& fit_content_available_space() const | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         VERIFY(is_fit_content()); | 
					
						
							| 
									
										
										
										
											2025-02-26 18:16:36 +01:00
										 |  |  |         return m_length_percentage; | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-03 15:37:42 +12:00
										 |  |  |     String to_string(SerializationMode) const; | 
					
						
							| 
									
										
										
										
											2025-08-27 11:43:44 +01:00
										 |  |  |     bool operator==(Size const&) const = default; | 
					
						
							| 
									
										
										
										
											2022-09-26 11:14:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2025-08-28 12:29:58 +01:00
										 |  |  |     explicit Size(Type type, Optional<LengthPercentage> = {}); | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Type m_type {}; | 
					
						
							| 
									
										
										
										
											2025-08-28 12:29:58 +01:00
										 |  |  |     Optional<LengthPercentage> m_length_percentage; | 
					
						
							| 
									
										
										
										
											2022-09-25 15:13:09 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-09-26 11:14:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | struct AK::Formatter<Web::CSS::Size> : Formatter<StringView> { | 
					
						
							|  |  |  |     ErrorOr<void> format(FormatBuilder& builder, Web::CSS::Size const& size) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-08-03 15:37:42 +12:00
										 |  |  |         return Formatter<StringView>::format(builder, size.to_string(Web::CSS::SerializationMode::Normal)); | 
					
						
							| 
									
										
										
										
											2022-09-26 11:14:42 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | }; |