From f099ee3d4767e1f15fe44516c99e9c9079600871 Mon Sep 17 00:00:00 2001 From: MacDue Date: Wed, 19 Apr 2023 18:31:00 +0100 Subject: [PATCH] LibWeb: Allow doing .to_color() on a StyleValue without a layout node This will be needed to access the color of a stop from a SVG gradient element (which does not participate in layout, so does not have a layout node). --- Userland/Libraries/LibWeb/CSS/StyleValue.h | 2 +- .../LibWeb/CSS/StyleValues/ColorStyleValue.h | 2 +- .../LibWeb/CSS/StyleValues/IdentifierStyleValue.cpp | 13 +++++++++---- .../LibWeb/CSS/StyleValues/IdentifierStyleValue.h | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index 76661d942b..f4feaca106 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -285,7 +285,7 @@ public: virtual ValueComparingNonnullRefPtr absolutized(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels line_height, CSSPixels root_line_height) const; - virtual Color to_color(Layout::NodeWithStyle const&) const { return {}; } + virtual Color to_color(Optional) const { return {}; } ValueID to_identifier() const; virtual Length to_length() const { VERIFY_NOT_REACHED(); } virtual float to_number() const { return 0; } diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/ColorStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/ColorStyleValue.h index acb8fca621..3e1c83c8b0 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/ColorStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/ColorStyleValue.h @@ -22,7 +22,7 @@ public: Color color() const { return m_color; } virtual ErrorOr to_string() const override; virtual bool has_color() const override { return true; } - virtual Color to_color(Layout::NodeWithStyle const&) const override { return m_color; } + virtual Color to_color(Optional) const override { return m_color; } bool properties_equal(ColorStyleValue const& other) const { return m_color == other.m_color; }; diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/IdentifierStyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValues/IdentifierStyleValue.cpp index ca5d97a66a..996ca997f2 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/IdentifierStyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/IdentifierStyleValue.cpp @@ -84,15 +84,20 @@ bool IdentifierStyleValue::has_color() const } } -Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const +Color IdentifierStyleValue::to_color(Optional node) const { if (id() == CSS::ValueID::Currentcolor) { - if (!node.has_style()) + if (!node.has_value() || !node->has_style()) return Color::Black; - return node.computed_values().color(); + return node->computed_values().color(); } - auto& document = node.document(); + if (!node.has_value()) { + // FIXME: Can't resolve palette colors without layout node. + return Color::Black; + } + + auto& document = node->document(); if (id() == CSS::ValueID::LibwebLink) return document.link_color(); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/IdentifierStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/IdentifierStyleValue.h index 225506f047..9d85aa5aec 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/IdentifierStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/IdentifierStyleValue.h @@ -25,7 +25,7 @@ public: ValueID id() const { return m_id; } virtual bool has_color() const override; - virtual Color to_color(Layout::NodeWithStyle const& node) const override; + virtual Color to_color(Optional node) const override; virtual ErrorOr to_string() const override; bool properties_equal(IdentifierStyleValue const& other) const { return m_id == other.m_id; }