From 5826fe094f8a37f2dc788cf3ca4685d7a7fbffcc Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 2 Feb 2022 20:53:55 +0000 Subject: [PATCH] LibWeb: Allow comma- or space-separated StyleValueLists This lets us produce valid CSS in its to_string() method, instead of always adding commas as before. :^) Also, finally added a Formatter for StyleValues. --- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 24 +++++++++---------- .../CSS/ResolvedCSSStyleDeclaration.cpp | 6 ++--- Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 20 +++++++++------- Userland/Libraries/LibWeb/CSS/StyleValue.h | 18 ++++++++++++-- 4 files changed, 43 insertions(+), 25 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 5e015934680..ac82b489740 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -2498,7 +2498,7 @@ RefPtr Parser::parse_comma_separated_value_list(Vector Parser::parse_simple_comma_separated_value_list(Vector const& component_values) @@ -2670,13 +2670,13 @@ RefPtr Parser::parse_background_value(Vector Parser::parse_border_radius_shorthand_value(Vector Parser::parse_box_shadow_value(Vector const& component_values) @@ -3457,7 +3457,7 @@ RefPtr Parser::parse_font_family_value(Vector Parser::parse_list_style_value(Vector const& component_values) @@ -3648,7 +3648,7 @@ RefPtr Parser::parse_transform_value(Vector transformations.append(TransformationStyleValue::create(maybe_function.value(), move(values))); } - return StyleValueList::create(move(transformations)); + return StyleValueList::create(move(transformations), StyleValueList::Separator::Space); } RefPtr Parser::parse_as_css_value(PropertyID property_id) @@ -3816,7 +3816,7 @@ Result, Parser::ParsingResult> Parser::parse_css_value parsed_values.append(parsed_value.release_nonnull()); } if (!parsed_values.is_empty() && parsed_values.size() <= property_maximum_value_count(property_id)) - return { StyleValueList::create(move(parsed_values)) }; + return { StyleValueList::create(move(parsed_values), StyleValueList::Separator::Space) }; } return ParsingResult::SyntaxError; diff --git a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp index e25f1511f99..0562f0e44ed 100644 --- a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp @@ -83,7 +83,7 @@ static RefPtr style_value_for_display(CSS::Display display) break; } - return StyleValueList::create(move(values)); + return StyleValueList::create(move(values), StyleValueList::Separator::Space); } if (display.is_internal()) { @@ -550,7 +550,7 @@ RefPtr ResolvedCSSStyleDeclaration::style_value_for_property(Layout: values.append(style_value_for_length_percentage(margin.right)); values.append(style_value_for_length_percentage(margin.bottom)); values.append(style_value_for_length_percentage(margin.left)); - return StyleValueList::create(move(values)); + return StyleValueList::create(move(values), StyleValueList::Separator::Space); } case CSS::PropertyID::MarginTop: return style_value_for_length_percentage(layout_node.computed_values().margin().top); @@ -567,7 +567,7 @@ RefPtr ResolvedCSSStyleDeclaration::style_value_for_property(Layout: values.append(style_value_for_length_percentage(padding.right)); values.append(style_value_for_length_percentage(padding.bottom)); values.append(style_value_for_length_percentage(padding.left)); - return StyleValueList::create(move(values)); + return StyleValueList::create(move(values), StyleValueList::Separator::Space); } case CSS::PropertyID::PaddingTop: return style_value_for_length_percentage(layout_node.computed_values().padding().top); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index 5aab974d67e..d1f4604e2e9 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -595,15 +595,19 @@ String UnresolvedStyleValue::to_string() const String StyleValueList::to_string() const { - StringBuilder builder; - builder.appendff("List[{}](", m_values.size()); - for (size_t i = 0; i < m_values.size(); ++i) { - if (i) - builder.append(','); - builder.append(m_values[i].to_string()); + String separator = ""; + switch (m_separator) { + case Separator::Space: + separator = " "; + break; + case Separator::Comma: + separator = ", "; + break; + default: + VERIFY_NOT_REACHED(); } - builder.append(')'); - return builder.to_string(); + + return String::join(separator, m_values); } } diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index fa733b22fde..2d70907bf70 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -1329,7 +1329,11 @@ private: class StyleValueList final : public StyleValue { public: - static NonnullRefPtr create(NonnullRefPtrVector&& values) { return adopt_ref(*new StyleValueList(move(values))); } + enum class Separator { + Space, + Comma, + }; + static NonnullRefPtr create(NonnullRefPtrVector&& values, Separator separator) { return adopt_ref(*new StyleValueList(move(values), separator)); } size_t size() const { return m_values.size(); } NonnullRefPtrVector const& values() const { return m_values; } @@ -1343,13 +1347,23 @@ public: virtual String to_string() const override; private: - StyleValueList(NonnullRefPtrVector&& values) + StyleValueList(NonnullRefPtrVector&& values, Separator separator) : StyleValue(Type::ValueList) + , m_separator(separator) , m_values(move(values)) { } + Separator m_separator; NonnullRefPtrVector m_values; }; } + +template<> +struct AK::Formatter : Formatter { + ErrorOr format(FormatBuilder& builder, Web::CSS::StyleValue const& style_value) + { + return Formatter::format(builder, style_value.to_string()); + } +};