From 4e48afd9a7e32cc97fcec5086afc2d2967ec4360 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 15 Aug 2024 11:06:18 +0100 Subject: [PATCH] LibWeb: Store ShadowStyleValue's color as a StyleValue Colors can be specified in a way that `Gfx::Color` can't represent, such as named system colors, `currentColor`, or functions involving `calc()`. --- Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 16 ++++++++-------- .../LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp | 2 +- Userland/Libraries/LibWeb/CSS/StyleComputer.cpp | 4 ++-- .../Libraries/LibWeb/CSS/StyleProperties.cpp | 4 ++-- .../LibWeb/CSS/StyleValues/ShadowStyleValue.cpp | 3 +-- .../LibWeb/CSS/StyleValues/ShadowStyleValue.h | 12 ++++++------ 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index ef285748332..ec18a7c9f03 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -4280,7 +4280,7 @@ RefPtr Parser::parse_single_shadow_value(TokenStream color; + RefPtr color; RefPtr offset_x; RefPtr offset_y; RefPtr blur_radius; @@ -4296,10 +4296,10 @@ RefPtr Parser::parse_single_shadow_value(TokenStream Parser::parse_single_shadow_value(TokenStream Parser::parse_single_shadow_value(TokenStream Parser::parse_content_value(TokenStream& tokens) diff --git a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp index 5471ea8ddb7..05cca3dd8c5 100644 --- a/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp +++ b/Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp @@ -175,7 +175,7 @@ static RefPtr style_value_for_shadow(Vector con auto make_shadow_style_value = [](ShadowData const& shadow) { return ShadowStyleValue::create( - shadow.color, + CSSColorValue::create(shadow.color), style_value_for_length_percentage(shadow.offset_x), style_value_for_length_percentage(shadow.offset_y), style_value_for_length_percentage(shadow.blur_radius), diff --git a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp index 29bb545934c..5a1a4b9daa4 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleComputer.cpp @@ -1316,7 +1316,7 @@ static NonnullRefPtr interpolate_box_shadow(DOM::Element& e values.ensure_capacity(other.size()); for (size_t i = values.size(); i < other.size(); i++) { values.unchecked_append(ShadowStyleValue::create( - Color::Transparent, + CSSColorValue::create(Color::Transparent), LengthStyleValue::create(Length::make_px(0)), LengthStyleValue::create(Length::make_px(0)), LengthStyleValue::create(Length::make_px(0)), @@ -1339,7 +1339,7 @@ static NonnullRefPtr interpolate_box_shadow(DOM::Element& e auto const& from_shadow = from_shadows[i]->as_shadow(); auto const& to_shadow = to_shadows[i]->as_shadow(); auto result_shadow = ShadowStyleValue::create( - interpolate_color(from_shadow.color(), to_shadow.color(), delta), + CSSColorValue::create(interpolate_color(from_shadow.color()->to_color({}), to_shadow.color()->to_color({}), delta)), interpolate_value(element, from_shadow.offset_x(), to_shadow.offset_x(), delta), interpolate_value(element, from_shadow.offset_y(), to_shadow.offset_y(), delta), interpolate_value(element, from_shadow.blur_radius(), to_shadow.blur_radius(), delta), diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp index deb6639249c..0304b8d90a2 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp @@ -895,7 +895,7 @@ Vector StyleProperties::shadow(PropertyID property_id, Layout::Node return {}; }; - auto make_shadow_data = [resolve_to_length](ShadowStyleValue const& value) -> Optional { + auto make_shadow_data = [resolve_to_length, &layout_node](ShadowStyleValue const& value) -> Optional { auto maybe_offset_x = resolve_to_length(value.offset_x()); if (!maybe_offset_x.has_value()) return {}; @@ -909,7 +909,7 @@ Vector StyleProperties::shadow(PropertyID property_id, Layout::Node if (!maybe_spread_distance.has_value()) return {}; return ShadowData { - value.color(), + value.color()->to_color(verify_cast(layout_node)), maybe_offset_x.release_value(), maybe_offset_y.release_value(), maybe_blur_radius.release_value(), diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.cpp index 9b287444f5a..91d92a08107 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.cpp @@ -15,8 +15,7 @@ namespace Web::CSS { String ShadowStyleValue::to_string() const { StringBuilder builder; - serialize_a_srgb_value(builder, m_properties.color); - builder.appendff(" {} {} {} {}", m_properties.offset_x->to_string(), m_properties.offset_y->to_string(), m_properties.blur_radius->to_string(), m_properties.spread_distance->to_string()); + builder.appendff("{} {} {} {} {}", m_properties.color->to_string(), m_properties.offset_x->to_string(), m_properties.offset_y->to_string(), m_properties.blur_radius->to_string(), m_properties.spread_distance->to_string()); if (m_properties.placement == ShadowPlacement::Inner) builder.append(" inset"sv); return MUST(builder.to_string()); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.h index 0bcf4853b50..127fb6597c8 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/ShadowStyleValue.h @@ -23,18 +23,18 @@ enum class ShadowPlacement { class ShadowStyleValue final : public StyleValueWithDefaultOperators { public: static ValueComparingNonnullRefPtr create( - Color color, + ValueComparingNonnullRefPtr color, ValueComparingNonnullRefPtr offset_x, ValueComparingNonnullRefPtr offset_y, ValueComparingNonnullRefPtr blur_radius, ValueComparingNonnullRefPtr spread_distance, ShadowPlacement placement) { - return adopt_ref(*new (nothrow) ShadowStyleValue(color, move(offset_x), move(offset_y), move(blur_radius), move(spread_distance), placement)); + return adopt_ref(*new (nothrow) ShadowStyleValue(move(color), move(offset_x), move(offset_y), move(blur_radius), move(spread_distance), placement)); } virtual ~ShadowStyleValue() override = default; - Color color() const { return m_properties.color; } + ValueComparingNonnullRefPtr const& color() const { return m_properties.color; } ValueComparingNonnullRefPtr const& offset_x() const { return m_properties.offset_x; } ValueComparingNonnullRefPtr const& offset_y() const { return m_properties.offset_y; } ValueComparingNonnullRefPtr const& blur_radius() const { return m_properties.blur_radius; } @@ -47,7 +47,7 @@ public: private: ShadowStyleValue( - Color color, + ValueComparingNonnullRefPtr color, ValueComparingNonnullRefPtr offset_x, ValueComparingNonnullRefPtr offset_y, ValueComparingNonnullRefPtr blur_radius, @@ -55,7 +55,7 @@ private: ShadowPlacement placement) : StyleValueWithDefaultOperators(Type::Shadow) , m_properties { - .color = color, + .color = move(color), .offset_x = move(offset_x), .offset_y = move(offset_y), .blur_radius = move(blur_radius), @@ -68,7 +68,7 @@ private: virtual ValueComparingNonnullRefPtr absolutized(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const override; struct Properties { - Color color; + ValueComparingNonnullRefPtr color; ValueComparingNonnullRefPtr offset_x; ValueComparingNonnullRefPtr offset_y; ValueComparingNonnullRefPtr blur_radius;