| 
									
										
										
										
											2023-03-24 17:16:07 +00:00
										 |  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-24 17:16:07 +00:00
										 |  |  |  |  * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org> | 
					
						
							| 
									
										
										
										
											2025-07-10 12:17:27 +01:00
										 |  |  |  |  * Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-24 17:16:07 +00:00
										 |  |  |  |  * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech> | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-01 16:16:15 +01:00
										 |  |  |  | #include "NumberStyleValue.h"
 | 
					
						
							| 
									
										
										
										
											2025-08-18 15:29:22 +01:00
										 |  |  |  | #include <LibWeb/CSS/CSSUnitValue.h>
 | 
					
						
							| 
									
										
										
										
											2025-08-08 10:55:30 +01:00
										 |  |  |  | #include <LibWeb/CSS/Parser/ComponentValue.h>
 | 
					
						
							| 
									
										
										
										
											2025-08-18 16:07:12 +01:00
										 |  |  |  | #include <LibWeb/CSS/Serialize.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-24 17:16:07 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 00:59:49 +01:00
										 |  |  |  | String NumberStyleValue::to_string(SerializationMode) const | 
					
						
							| 
									
										
										
										
											2023-03-24 17:16:07 +00:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-08-18 16:07:12 +01:00
										 |  |  |  |     return serialize_a_number(m_value); | 
					
						
							| 
									
										
										
										
											2023-03-24 17:16:07 +00:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-10 12:17:27 +01:00
										 |  |  |  | Vector<Parser::ComponentValue> NumberStyleValue::tokenize() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     return { Parser::Token::create_number(Number { Number::Type::Number, m_value }) }; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-18 15:29:22 +01:00
										 |  |  |  | // https://drafts.css-houdini.org/css-typed-om-1/#reify-a-numeric-value
 | 
					
						
							|  |  |  |  | GC::Ref<CSSStyleValue> NumberStyleValue::reify(JS::Realm& realm, String const& associated_property) const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // NB: Step 1 doesn't apply here.
 | 
					
						
							|  |  |  |  |     // 2. If num is the unitless value 0 and num is a <dimension>, return a new CSSUnitValue with its value internal
 | 
					
						
							|  |  |  |  |     //    slot set to 0, and its unit internal slot set to "px".
 | 
					
						
							|  |  |  |  |     if (m_value == 0) { | 
					
						
							|  |  |  |  |         // NB: Determine whether the associated property expects 0 to be a <length>.
 | 
					
						
							|  |  |  |  |         // FIXME: Do this for registered custom properties.
 | 
					
						
							|  |  |  |  |         if (auto property_id = property_id_from_string(associated_property); property_id.has_value() | 
					
						
							|  |  |  |  |             && property_id != PropertyID::Custom | 
					
						
							|  |  |  |  |             && property_accepts_type(*property_id, ValueType::Length)) { | 
					
						
							|  |  |  |  |             return CSSUnitValue::create(realm, 0, "px"_fly_string); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Return a new CSSUnitValue with its value internal slot set to the numeric value of num, and its unit internal
 | 
					
						
							|  |  |  |  |     //    slot set to "number" if num is a <number>, "percent" if num is a <percentage>, and num’s unit if num is a
 | 
					
						
							|  |  |  |  |     //    <dimension>.
 | 
					
						
							|  |  |  |  |     //    If the value being reified is a computed value, the unit used must be the appropriate canonical unit for the
 | 
					
						
							|  |  |  |  |     //    value’s type, with the numeric value scaled accordingly.
 | 
					
						
							|  |  |  |  |     return CSSUnitValue::create(realm, m_value, "number"_fly_string); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-24 17:16:07 +00:00
										 |  |  |  | } |