| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2024-10-04 13:19:50 +02:00
										 |  |  |  * Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  |  * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org> | 
					
						
							| 
									
										
										
										
											2025-04-10 16:35:59 +01:00
										 |  |  |  * Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org> | 
					
						
							| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  |  * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "StyleValueList.h"
 | 
					
						
							| 
									
										
										
										
											2025-07-10 12:17:27 +01:00
										 |  |  | #include <LibWeb/CSS/Parser/ComponentValue.h>
 | 
					
						
							| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::CSS { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool StyleValueList::Properties::operator==(Properties const& other) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return separator == other.separator && values.span() == other.values.span(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-08 10:11:51 +01:00
										 |  |  | ValueComparingNonnullRefPtr<StyleValue const> StyleValueList::absolutized(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const | 
					
						
							| 
									
										
										
										
											2025-08-04 20:13:18 +12:00
										 |  |  | { | 
					
						
							|  |  |  |     StyleValueVector absolutized_style_values; | 
					
						
							|  |  |  |     absolutized_style_values.ensure_capacity(m_properties.values.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (auto const& value : m_properties.values) | 
					
						
							|  |  |  |         absolutized_style_values.append(value->absolutized(viewport_rect, font_metrics, root_font_metrics)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return StyleValueList::create(move(absolutized_style_values), m_properties.separator); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-07 00:59:49 +01:00
										 |  |  | String StyleValueList::to_string(SerializationMode mode) const | 
					
						
							| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-07-31 14:59:19 +01:00
										 |  |  |     if (m_properties.values.is_empty()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  |     auto separator = ""sv; | 
					
						
							|  |  |  |     switch (m_properties.separator) { | 
					
						
							|  |  |  |     case Separator::Space: | 
					
						
							|  |  |  |         separator = " "sv; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case Separator::Comma: | 
					
						
							|  |  |  |         separator = ", "sv; | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-31 14:59:19 +01:00
										 |  |  |     auto first_value = m_properties.values.first(); | 
					
						
							|  |  |  |     if (all_of(m_properties.values, [&](auto const& property) { return property == first_value; })) | 
					
						
							|  |  |  |         return first_value->to_string(mode); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  |     StringBuilder builder; | 
					
						
							|  |  |  |     for (size_t i = 0; i < m_properties.values.size(); ++i) { | 
					
						
							| 
									
										
										
										
											2024-12-07 00:59:49 +01:00
										 |  |  |         builder.append(m_properties.values[i]->to_string(mode)); | 
					
						
							| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  |         if (i != m_properties.values.size() - 1) | 
					
						
							| 
									
										
										
										
											2023-08-22 14:08:15 +01:00
										 |  |  |             builder.append(separator); | 
					
						
							| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-22 14:08:15 +01:00
										 |  |  |     return MUST(builder.to_string()); | 
					
						
							| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-10 16:35:59 +01:00
										 |  |  | void StyleValueList::set_style_sheet(GC::Ptr<CSSStyleSheet> style_sheet) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::set_style_sheet(style_sheet); | 
					
						
							|  |  |  |     for (auto& value : m_properties.values) | 
					
						
							| 
									
										
										
										
											2025-08-08 10:11:51 +01:00
										 |  |  |         const_cast<StyleValue&>(*value).set_style_sheet(style_sheet); | 
					
						
							| 
									
										
										
										
											2025-04-10 16:35:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-10 12:17:27 +01:00
										 |  |  | Vector<Parser::ComponentValue> StyleValueList::tokenize() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Vector<Parser::ComponentValue> component_values; | 
					
						
							|  |  |  |     bool first = true; | 
					
						
							|  |  |  |     for (auto const& value : m_properties.values) { | 
					
						
							|  |  |  |         if (first) { | 
					
						
							|  |  |  |             first = false; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             if (m_properties.separator == Separator::Comma) | 
					
						
							|  |  |  |                 component_values.empend(Parser::Token::create(Parser::Token::Type::Comma)); | 
					
						
							|  |  |  |             component_values.empend(Parser::Token::create_whitespace(" "_string)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         component_values.extend(value->tokenize()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return component_values; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-25 00:12:21 +00:00
										 |  |  | } |