Skip to content

Truly independent web browser

LibWeb: Resolve currentcolor in SVG paint resolved values

Andreas Kling9cc45acb3e61
diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
index 58e6f73379e..232dbf2cdb6 100644
--- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
+++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
@@ -663,6 +663,7 @@ static void ensure_pseudo_element_style_for_cssom(Layout::BegunRead const& read,
}
static RefPtr<StyleValue const> resolve_color_style_value(StyleValue const&, Color, Optional<ColorResolutionStyle> = {});
+static NonnullRefPtr<StyleValue const> resolve_svg_paint_style_value(StyleValue const&, ColorResolutionStyle);
static bool is_min_or_max_size_property(PropertyID property_id)
{
@@ -904,6 +905,9 @@ Optional<StyleProperty> CSSStyleProperties::get_direct_property(PropertyNameAndI
switch (computed_property_id) {
case PropertyID::Color:
return resolve_color_style_value(*computed_value, computed_values->color(), ColorResolutionStyle::for_computed_values(*computed_values)).release_nonnull();
+ case PropertyID::Fill:
+ case PropertyID::Stroke:
+ return resolve_svg_paint_style_value(*computed_value, ColorResolutionStyle::for_computed_values(*computed_values));
case PropertyID::CaretColor:
return resolve_color_style_value(*computed_value, computed_values->caret_color()).release_nonnull();
case PropertyID::BackgroundColor:
@@ -1113,6 +1117,26 @@ static RefPtr<StyleValue const> resolve_color_style_value(StyleValue const& styl
return ColorStyleValue::create_from_color(computed_color, ColorSyntax::Modern);
}
+// https://drafts.csswg.org/css-color-4/#resolving-color-values
+// The resolved value of a <color> is its used value.
+static NonnullRefPtr<StyleValue const> resolve_svg_paint_style_value(StyleValue const& style_value, ColorResolutionStyle color_resolution_style)
+{
+ if (!style_value.depends_on_current_color())
+ return style_value;
+
+ if (style_value.is_value_list()) {
+ auto const& list = style_value.as_value_list();
+ auto values = list.values();
+ for (auto& value : values)
+ value = resolve_svg_paint_style_value(*value, color_resolution_style);
+ return StyleValueList::create(move(values), list.separator());
+ }
+
+ if (auto color = style_value.to_color(color_resolution_style); color.has_value())
+ return resolve_color_style_value(style_value, *color, color_resolution_style).release_nonnull();
+ return style_value;
+}
+
// https://svgwg.org/svg2-draft/geometry.html#Sizing
// The CSS width and height properties are used for sizing some SVG elements. Specifically, they are used to size
// ‘rect’, ‘svg’, ‘image’ and ‘foreignObject’.
@@ -1273,6 +1297,9 @@ RefPtr<StyleValue const> CSSStyleProperties::style_value_for_computed_property(L
VERIFY(style);
return resolve_color_style_value(*get_computed_value(property_id), style->color(), ColorResolutionStyle::for_element(*owner_node()));
}
+ case PropertyID::Fill:
+ case PropertyID::Stroke:
+ return resolve_svg_paint_style_value(*get_computed_value(property_id), color_resolution_style);
case PropertyID::ColumnRuleColor:
return resolve_color_style_value(*get_computed_value(property_id), layout_node.column_rule_color(), color_resolution_style);
case PropertyID::OutlineColor:
diff --git a/Tests/LibWeb/Text/expected/svg-paint-computed-currentcolor.txt b/Tests/LibWeb/Text/expected/svg-paint-computed-currentcolor.txt
index 7ac572a0ec7..28f70d4a0d3 100644
--- a/Tests/LibWeb/Text/expected/svg-paint-computed-currentcolor.txt
+++ b/Tests/LibWeb/Text/expected/svg-paint-computed-currentcolor.txt
@@ -1,28 +1,28 @@
-block fill: currentcolor
-block inherited fill: currentcolor
-block changed color fill: currentcolor
-block fallback fill: url("#gradient") currentcolor
-block fill rgb(from currentcolor r g b): false
-block fill color-mix(in srgb, currentcolor, white): false
+block fill: rgb(255, 0, 0)
+block inherited fill: rgb(0, 0, 255)
+block changed color fill: rgb(0, 128, 0)
+block fallback fill: url("#gradient") rgb(0, 128, 0)
+block fill rgb(from currentcolor r g b): true
+block fill color-mix(in srgb, currentcolor, white): true
block none fill: none
-block stroke: currentcolor
-block inherited stroke: currentcolor
-block changed color stroke: currentcolor
-block fallback stroke: url("#gradient") currentcolor
-block stroke rgb(from currentcolor r g b): false
-block stroke color-mix(in srgb, currentcolor, white): false
+block stroke: rgb(255, 0, 0)
+block inherited stroke: rgb(0, 0, 255)
+block changed color stroke: rgb(0, 128, 0)
+block fallback stroke: url("#gradient") rgb(0, 128, 0)
+block stroke rgb(from currentcolor r g b): true
+block stroke color-mix(in srgb, currentcolor, white): true
block none stroke: none
-none fill: currentcolor
-none inherited fill: currentcolor
-none changed color fill: currentcolor
-none fallback fill: url("#gradient") currentcolor
-none fill rgb(from currentcolor r g b): false
-none fill color-mix(in srgb, currentcolor, white): false
+none fill: rgb(255, 0, 0)
+none inherited fill: rgb(0, 0, 255)
+none changed color fill: rgb(0, 128, 0)
+none fallback fill: url("#gradient") rgb(0, 128, 0)
+none fill rgb(from currentcolor r g b): true
+none fill color-mix(in srgb, currentcolor, white): true
none none fill: none
-none stroke: currentcolor
-none inherited stroke: currentcolor
-none changed color stroke: currentcolor
-none fallback stroke: url("#gradient") currentcolor
-none stroke rgb(from currentcolor r g b): false
-none stroke color-mix(in srgb, currentcolor, white): false
+none stroke: rgb(255, 0, 0)
+none inherited stroke: rgb(0, 0, 255)
+none changed color stroke: rgb(0, 128, 0)
+none fallback stroke: url("#gradient") rgb(0, 128, 0)
+none stroke rgb(from currentcolor r g b): true
+none stroke color-mix(in srgb, currentcolor, white): true
none none stroke: none
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/pservers/scripted/svg-fill-currentcolor-visited-getcomputedstyle.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/pservers/scripted/svg-fill-currentcolor-visited-getcomputedstyle.txt
index 70d205139ba..aac4bbd069a 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/pservers/scripted/svg-fill-currentcolor-visited-getcomputedstyle.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/pservers/scripted/svg-fill-currentcolor-visited-getcomputedstyle.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail Property fill value 'currentColor' should not leak :visited for SVG fill with currentColor
\ No newline at end of file
+1 Pass
+Pass Property fill value 'currentColor' should not leak :visited for SVG fill with currentColor
\ No newline at end of file