From ae99cbe534c67f58829499501547c87dd63338b6 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 24 Jan 2022 15:43:44 +0000 Subject: [PATCH] LibWeb: Treat SVG fill/stroke/stroke-width attributes as CSS properties Rather than having separate systems for the attributes and their CSS equivalents, we can treat the attributes as presentational hints and convert them to CSS properties. This means they can be inherited, as they should. :^) As noted, the `fill` and `stroke` attributes do not fully match the `fill` and `stroke` properties. The CSS spec is still an early draft and not entirely helpful, so we can just pretend they are the same for now. --- .../LibWeb/SVG/SVGGraphicsElement.cpp | 37 +++++++++---------- .../Libraries/LibWeb/SVG/SVGGraphicsElement.h | 9 +---- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp index ffe00c2bc7..73db727cf7 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.cpp @@ -1,10 +1,11 @@ /* * Copyright (c) 2020, Matthew Olsson - * Copyright (c) 2021, Sam Atkins + * Copyright (c) 2021-2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ +#include #include namespace Web::SVG { @@ -14,25 +15,27 @@ SVGGraphicsElement::SVGGraphicsElement(DOM::Document& document, QualifiedName qu { } -void SVGGraphicsElement::parse_attribute(FlyString const& name, String const& value) +void SVGGraphicsElement::apply_presentational_hints(CSS::StyleProperties& style) const { - SVGElement::parse_attribute(name, value); - - if (name == "fill") { - m_fill_color = Gfx::Color::from_string(value).value_or(Color::Transparent); - } else if (name == "stroke") { - m_stroke_color = Gfx::Color::from_string(value).value_or(Color::Transparent); - } else if (name == "stroke-width") { - auto result = value.to_int(); - if (result.has_value()) - m_stroke_width = result.value(); - } + CSS::ParsingContext parsing_context { document() }; + for_each_attribute([&](auto& name, auto& value) { + if (name.equals_ignoring_case("fill")) { + // FIXME: The `fill` attribute and CSS `fill` property are not the same! But our support is limited enough that they are equivalent for now. + if (auto fill_value = parse_css_value(parsing_context, value, CSS::PropertyID::Fill)) + style.set_property(CSS::PropertyID::Fill, fill_value.release_nonnull()); + } else if (name.equals_ignoring_case("stroke")) { + // FIXME: The `stroke` attribute and CSS `stroke` property are not the same! But our support is limited enough that they are equivalent for now. + if (auto stroke_value = parse_css_value(parsing_context, value, CSS::PropertyID::Stroke)) + style.set_property(CSS::PropertyID::Stroke, stroke_value.release_nonnull()); + } else if (name.equals_ignoring_case("stroke-width")) { + if (auto stroke_width_value = parse_css_value(parsing_context, value, CSS::PropertyID::StrokeWidth)) + style.set_property(CSS::PropertyID::StrokeWidth, stroke_width_value.release_nonnull()); + } + }); } Optional SVGGraphicsElement::fill_color() const { - if (m_fill_color.has_value()) - return m_fill_color; if (!layout_node()) return {}; // FIXME: In the working-draft spec, `fill` is intended to be a shorthand, with `fill-color` @@ -42,8 +45,6 @@ Optional SVGGraphicsElement::fill_color() const Optional SVGGraphicsElement::stroke_color() const { - if (m_stroke_color.has_value()) - return m_stroke_color; if (!layout_node()) return {}; // FIXME: In the working-draft spec, `stroke` is intended to be a shorthand, with `stroke-color` @@ -53,8 +54,6 @@ Optional SVGGraphicsElement::stroke_color() const Optional SVGGraphicsElement::stroke_width() const { - if (m_stroke_width.has_value()) - return m_stroke_width; if (!layout_node()) return {}; // FIXME: Converting to pixels isn't really correct - values should be in "user units" diff --git a/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.h b/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.h index 58019e67a9..06c76dd5f2 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.h +++ b/Userland/Libraries/LibWeb/SVG/SVGGraphicsElement.h @@ -1,6 +1,6 @@ /* * Copyright (c) 2020, Matthew Olsson - * Copyright (c) 2021, Sam Atkins + * Copyright (c) 2021-2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -20,16 +20,11 @@ public: SVGGraphicsElement(DOM::Document&, QualifiedName); - virtual void parse_attribute(FlyString const& name, String const& value) override; + virtual void apply_presentational_hints(CSS::StyleProperties&) const override; Optional fill_color() const; Optional stroke_color() const; Optional stroke_width() const; - -protected: - Optional m_fill_color; - Optional m_stroke_color; - Optional m_stroke_width; }; }