From 2480b94ae70679e54abd8274c5f2bcf076f7d6f0 Mon Sep 17 00:00:00 2001 From: Michiel Date: Sun, 19 Mar 2023 15:44:12 +0100 Subject: [PATCH] LibWeb: Support more length types in SVG width/height attributes Previously we were using the HTML parse_dimension_value method for the height and width attributes of an SVG element. These attributes should however be treated as css properties instead and thus also support calc() and absolute units so we use the css parser for this instead. --- Userland/Libraries/LibWeb/Layout/SVGSVGBox.cpp | 18 +++++++++--------- .../LibWeb/SVG/SVGForeignObjectElement.cpp | 8 ++++---- .../Libraries/LibWeb/SVG/SVGSVGElement.cpp | 6 +++--- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Userland/Libraries/LibWeb/Layout/SVGSVGBox.cpp b/Userland/Libraries/LibWeb/Layout/SVGSVGBox.cpp index 53d1b4967ab..fedb8dd7231 100644 --- a/Userland/Libraries/LibWeb/Layout/SVGSVGBox.cpp +++ b/Userland/Libraries/LibWeb/Layout/SVGSVGBox.cpp @@ -5,7 +5,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include +#include #include #include #include @@ -27,14 +27,14 @@ void SVGSVGBox::prepare_for_replaced_layout() if (dom_node().has_attribute(HTML::AttributeNames::width) && dom_node().has_attribute(HTML::AttributeNames::height)) { Optional w; Optional h; - if (auto width = HTML::parse_dimension_value(dom_node().attribute(HTML::AttributeNames::width))) { - if (width->has_length()) - w = width->to_length().to_px(*this); - } - if (auto height = HTML::parse_dimension_value(dom_node().attribute(HTML::AttributeNames::height))) { - if (height->has_length()) - h = height->to_length().to_px(*this); - } + auto parsing_context = CSS::Parser::ParsingContext { document() }; + auto width = parse_css_value(parsing_context, dom_node().attribute(Web::HTML::AttributeNames::width), CSS::PropertyID::Width); + if (!width.is_null() && width->has_length()) + w = width->to_length().to_px(*this); + + auto height = parse_css_value(parsing_context, dom_node().attribute((HTML::AttributeNames::height)), CSS::PropertyID::Height); + if (!height.is_null() && height->has_length()) + h = height->to_length().to_px(*this); if (w.has_value() && h.has_value()) { set_intrinsic_width(*w); set_intrinsic_height(*h); diff --git a/Userland/Libraries/LibWeb/SVG/SVGForeignObjectElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGForeignObjectElement.cpp index 6ad9d19c36e..ccaa0a79c5e 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGForeignObjectElement.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGForeignObjectElement.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include #include @@ -63,11 +63,11 @@ JS::GCPtr SVGForeignObjectElement::create_layout_node(NonnullRefPt void SVGForeignObjectElement::apply_presentational_hints(CSS::StyleProperties& style) const { Base::apply_presentational_hints(style); - - if (auto width_value = HTML::parse_dimension_value(attribute(SVG::AttributeNames::width))) + auto parsing_context = CSS::Parser::ParsingContext { document() }; + if (auto width_value = parse_css_value(parsing_context, attribute(Web::HTML::AttributeNames::width), CSS::PropertyID::Width)) style.set_property(CSS::PropertyID::Width, width_value.release_nonnull()); - if (auto height_value = HTML::parse_dimension_value(attribute(SVG::AttributeNames::height))) + if (auto height_value = parse_css_value(parsing_context, attribute(Web::HTML::AttributeNames::height), CSS::PropertyID::Height)) style.set_property(CSS::PropertyID::Height, height_value.release_nonnull()); } diff --git a/Userland/Libraries/LibWeb/SVG/SVGSVGElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGSVGElement.cpp index 897aa64d099..302a98c70ef 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGSVGElement.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGSVGElement.cpp @@ -5,7 +5,6 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include #include #include #include @@ -38,7 +37,8 @@ JS::GCPtr SVGSVGElement::create_layout_node(NonnullRefPtr