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
<stop> element (which does not participate in layout, so does not have
a layout node).
This commit is contained in:
MacDue 2023-04-19 18:31:00 +01:00 committed by Andreas Kling
parent 2013761feb
commit f099ee3d47
Notes: sideshowbarker 2024-07-16 23:52:10 +09:00
4 changed files with 12 additions and 7 deletions

View file

@ -285,7 +285,7 @@ public:
virtual ValueComparingNonnullRefPtr<StyleValue const> 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<Layout::NodeWithStyle const&>) const { return {}; }
ValueID to_identifier() const;
virtual Length to_length() const { VERIFY_NOT_REACHED(); }
virtual float to_number() const { return 0; }

View file

@ -22,7 +22,7 @@ public:
Color color() const { return m_color; }
virtual ErrorOr<String> 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<Layout::NodeWithStyle const&>) const override { return m_color; }
bool properties_equal(ColorStyleValue const& other) const { return m_color == other.m_color; };

View file

@ -84,15 +84,20 @@ bool IdentifierStyleValue::has_color() const
}
}
Color IdentifierStyleValue::to_color(Layout::NodeWithStyle const& node) const
Color IdentifierStyleValue::to_color(Optional<Layout::NodeWithStyle const&> 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();

View file

@ -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<Layout::NodeWithStyle const&> node) const override;
virtual ErrorOr<String> to_string() const override;
bool properties_equal(IdentifierStyleValue const& other) const { return m_id == other.m_id; }