| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-05-16 20:02:16 +01:00
										 |  |  |  * Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org> | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-30 15:55:02 +01:00
										 |  |  | #include <LibWeb/CSS/Percentage.h>
 | 
					
						
							| 
									
										
										
										
											2024-12-11 15:05:56 +00:00
										 |  |  | #include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
 | 
					
						
							| 
									
										
										
										
											2025-05-16 20:02:16 +01:00
										 |  |  | #include <LibWeb/CSS/Time.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-09-02 13:48:49 +01:00
										 |  |  | Time::Time(double value, TimeUnit unit) | 
					
						
							|  |  |  |     : m_unit(unit) | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  |     , m_value(value) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  | Time Time::make_seconds(double value) | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-02 13:48:49 +01:00
										 |  |  |     return { value, TimeUnit::S }; | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Time Time::percentage_of(Percentage const& percentage) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-02 13:48:49 +01:00
										 |  |  |     return Time { percentage.as_fraction() * m_value, m_unit }; | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-16 20:02:16 +01:00
										 |  |  | String Time::to_string(SerializationMode serialization_mode) const | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-08-18 14:10:11 +01:00
										 |  |  |     // https://drafts.csswg.org/cssom/#serialize-a-css-value
 | 
					
						
							|  |  |  |     // -> <time>
 | 
					
						
							|  |  |  |     // The time in seconds serialized as per <number> followed by the literal string "s".
 | 
					
						
							|  |  |  |     // AD-HOC: WPT expects us to serialize using the actual unit, like for other dimensions.
 | 
					
						
							|  |  |  |     //         https://github.com/w3c/csswg-drafts/issues/12616
 | 
					
						
							|  |  |  |     if (serialization_mode == SerializationMode::ResolvedValue) { | 
					
						
							|  |  |  |         StringBuilder builder; | 
					
						
							|  |  |  |         serialize_a_number(builder, to_seconds()); | 
					
						
							|  |  |  |         builder.append("s"sv); | 
					
						
							|  |  |  |         return builder.to_string_without_validation(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     StringBuilder builder; | 
					
						
							|  |  |  |     serialize_a_number(builder, raw_value()); | 
					
						
							|  |  |  |     builder.append(unit_name()); | 
					
						
							|  |  |  |     return builder.to_string_without_validation(); | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-27 21:10:21 +02:00
										 |  |  | double Time::to_seconds() const | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-04 12:27:20 +01:00
										 |  |  |     return ratio_between_units(m_unit, TimeUnit::S) * m_value; | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-26 23:30:11 +03:30
										 |  |  | double Time::to_milliseconds() const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-04 12:27:20 +01:00
										 |  |  |     return ratio_between_units(m_unit, TimeUnit::Ms) * m_value; | 
					
						
							| 
									
										
										
										
											2023-05-26 23:30:11 +03:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-15 15:18:27 -06:00
										 |  |  | Time Time::resolve_calculated(NonnullRefPtr<CalculatedStyleValue const> const& calculated, Layout::Node const& layout_node, Time const& reference_value) | 
					
						
							| 
									
										
										
										
											2024-08-02 14:28:24 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-09-05 16:22:17 +02:00
										 |  |  |     CalculationResolutionContext context { | 
					
						
							|  |  |  |         .percentage_basis = reference_value, | 
					
						
							|  |  |  |         .length_resolution_context = Length::ResolutionContext::for_layout_node(layout_node), | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     return calculated->resolve_time(context).value(); | 
					
						
							| 
									
										
										
										
											2024-08-02 14:28:24 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-21 19:29:43 +00:00
										 |  |  | } |