| 
									
										
										
										
											2022-03-06 13:48:32 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-08-18 14:12:24 +01:00
										 |  |  |  * Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org> | 
					
						
							| 
									
										
										
										
											2022-03-06 13:48:32 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Ratio.h"
 | 
					
						
							| 
									
										
										
										
											2025-08-18 14:12:24 +01:00
										 |  |  | #include <LibWeb/CSS/Serialize.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-06 13:48:32 +00:00
										 |  |  | #include <math.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-20 13:02:41 +01:00
										 |  |  | Ratio::Ratio(double first, double second) | 
					
						
							| 
									
										
										
										
											2022-03-06 13:48:32 +00:00
										 |  |  |     : m_first_value(first) | 
					
						
							|  |  |  |     , m_second_value(second) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://www.w3.org/TR/css-values-4/#degenerate-ratio
 | 
					
						
							|  |  |  | bool Ratio::is_degenerate() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return !isfinite(m_first_value) || m_first_value == 0 | 
					
						
							|  |  |  |         || !isfinite(m_second_value) || m_second_value == 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-22 12:25:30 +01:00
										 |  |  | String Ratio::to_string() const | 
					
						
							| 
									
										
										
										
											2022-03-06 13:48:32 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-08-18 14:12:24 +01:00
										 |  |  |     // https://drafts.csswg.org/cssom/#serialize-a-css-value
 | 
					
						
							|  |  |  |     // -> <ratio>
 | 
					
						
							|  |  |  |     // The numerator serialized as per <number> followed by the literal string " / ", followed by the denominator
 | 
					
						
							|  |  |  |     // serialized as per <number>.
 | 
					
						
							|  |  |  |     StringBuilder builder; | 
					
						
							|  |  |  |     serialize_a_number(builder, m_first_value); | 
					
						
							|  |  |  |     builder.append(" / "sv); | 
					
						
							|  |  |  |     serialize_a_number(builder, m_second_value); | 
					
						
							|  |  |  |     return builder.to_string_without_validation(); | 
					
						
							| 
									
										
										
										
											2022-03-06 13:48:32 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |