mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 17:52:45 +00:00 
			
		
		
		
	LibWeb: Add parsing for NumericStyleValue
This StyleValue can hold an arbitrary float value.
This commit is contained in:
		
							parent
							
								
									ae3e6510d6
								
							
						
					
					
						commit
						af4d80af4d
					
				
					 2 changed files with 27 additions and 1 deletions
				
			
		|  | @ -240,8 +240,12 @@ RefPtr<CSS::StyleValue> parse_css_value(const CSS::ParsingContext& context, cons | |||
|     } | ||||
| 
 | ||||
|     auto length = parse_length(context, string, is_bad_length); | ||||
|     if (is_bad_length) | ||||
|     if (is_bad_length) { | ||||
|         auto float_number = try_parse_float(string); | ||||
|         if (float_number.has_value()) | ||||
|             return CSS::NumericStyleValue::create(float_number.value()); | ||||
|         return nullptr; | ||||
|     } | ||||
|     if (!length.is_undefined()) | ||||
|         return CSS::LengthStyleValue::create(length); | ||||
| 
 | ||||
|  |  | |||
|  | @ -207,6 +207,7 @@ public: | |||
|         Image, | ||||
|         Position, | ||||
|         CustomProperty, | ||||
|         Numeric, | ||||
|     }; | ||||
| 
 | ||||
|     Type type() const { return m_type; } | ||||
|  | @ -220,6 +221,7 @@ public: | |||
|     bool is_length() const { return type() == Type::Length; } | ||||
|     bool is_position() const { return type() == Type::Position; } | ||||
|     bool is_custom_property() const { return type() == Type::CustomProperty; } | ||||
|     bool is_numeric() const { return type() == Type::Numeric; } | ||||
| 
 | ||||
|     virtual String to_string() const = 0; | ||||
|     virtual Length to_length() const { return Length::make_auto(); } | ||||
|  | @ -266,6 +268,26 @@ private: | |||
|     String m_custom_property_name {}; | ||||
| }; | ||||
| 
 | ||||
| class NumericStyleValue : public StyleValue { | ||||
| public: | ||||
|     static NonnullRefPtr<NumericStyleValue> create(float value) | ||||
|     { | ||||
|         return adopt_ref(*new NumericStyleValue(value)); | ||||
|     } | ||||
| 
 | ||||
|     float value() const { return m_value; } | ||||
|     String to_string() const override { return String::formatted("{}", m_value); } | ||||
| 
 | ||||
| private: | ||||
|     explicit NumericStyleValue(float value) | ||||
|         : StyleValue(Type::Numeric) | ||||
|         , m_value(value) | ||||
|     { | ||||
|     } | ||||
| 
 | ||||
|     float m_value { 0 }; | ||||
| }; | ||||
| 
 | ||||
| class StringStyleValue : public StyleValue { | ||||
| public: | ||||
|     static NonnullRefPtr<StringStyleValue> create(const String& string) | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Tobias Christiansen
						Tobias Christiansen