Skip to content

Truly independent web browser

LibWeb: Resolve width and height of most SVG elements to computed values

Andreas Kling96dcb7e92576
diff --git a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
index 8116c9bd6b6..22bd70affad 100644
--- a/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
+++ b/Libraries/LibWeb/CSS/CSSStyleProperties.cpp
@@ -43,6 +43,7 @@
#include <LibWeb/Page/Page.h>
#include <LibWeb/Painting/BoxModelMetrics.h>
#include <LibWeb/Painting/BoxViews.h>
+#include <LibWeb/SVG/TagNames.h>
#include <LibWebCommon/Infra/Strings.h>
namespace Web::CSS {
@@ -1070,6 +1071,16 @@ static RefPtr<StyleValue const> resolve_color_style_value(StyleValue const& styl
return ColorStyleValue::create_from_color(computed_color, ColorSyntax::Modern);
}
+// 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’.
+static bool svg_sizing_properties_apply(DOM::Element const& element)
+{
+ if (!element.is_svg_element())
+ return true;
+ return element.local_name().is_one_of(SVG::TagNames::rect, SVG::TagNames::svg, SVG::TagNames::image, SVG::TagNames::foreignObject);
+}
+
RefPtr<StyleValue const> CSSStyleProperties::style_value_for_computed_property(Layout::NodeWithStyle const& layout_node, PropertyID property_id) const
{
if (!owner_node().has_value()) {
@@ -1269,6 +1280,8 @@ RefPtr<StyleValue const> CSSStyleProperties::style_value_for_computed_property(L
// display property is not none or contents, then the resolved value is the used value.
// Otherwise the resolved value is the computed value.
case PropertyID::Height: {
+ if (!svg_sizing_properties_apply(element))
+ return get_computed_value(property_id);
auto maybe_used_height = used_size_for_property(
[](auto const& box_layout_node) { return Painting::content_height(box_layout_node); },
[](auto const& box_layout_node) { return Painting::unbroken_border_box_size(box_layout_node).height(); });
@@ -1309,6 +1322,8 @@ RefPtr<StyleValue const> CSSStyleProperties::style_value_for_computed_property(L
return LengthStyleValue::create(Length::make_px(maybe_used_value.release_value()));
return style_value_for_length_percentage_or_auto(layout_node.padding().top());
case PropertyID::Width: {
+ if (!svg_sizing_properties_apply(element))
+ return get_computed_value(property_id);
auto maybe_used_width = used_size_for_property(
[](auto const& box_layout_node) { return Painting::content_width(box_layout_node); },
[](auto const& box_layout_node) { return Painting::unbroken_border_box_size(box_layout_node).width(); });
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/geometry/parsing/height-computed.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/geometry/parsing/height-computed.txt
index 397d86253ed..4185fe90e92 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/geometry/parsing/height-computed.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/geometry/parsing/height-computed.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 34 tests
-20 Pass
-14 Fail
+30 Pass
+4 Fail
Pass SVG Geometry Properties: getComputedStyle().height, <rect> initial
Pass SVG Geometry Properties: getComputedStyle().height, <rect> presentation attribute
Pass SVG Geometry Properties: getComputedStyle().height, <rect> inline style (auto)
@@ -28,13 +28,13 @@ Fail SVG Geometry Properties: getComputedStyle().height, <svg> inline style (aut
Pass SVG Geometry Properties: getComputedStyle().height, <svg> inline style (pixels)
Fail SVG Geometry Properties: getComputedStyle().height, <svg> inline style (percentage)
Pass SVG Geometry Properties: getComputedStyle().height, <svg> inline style but "display: none"
-Fail SVG Geometry Properties: getComputedStyle().height, <g> initial
-Fail SVG Geometry Properties: getComputedStyle().height, <g> initial (with dummy attribute)
-Fail SVG Geometry Properties: getComputedStyle().height, <g> inline style (auto)
-Fail SVG Geometry Properties: getComputedStyle().height, <g> inline style (pixels)
-Fail SVG Geometry Properties: getComputedStyle().height, <g> inline style (percentage)
-Fail SVG Geometry Properties: getComputedStyle().height, <text> initial
-Fail SVG Geometry Properties: getComputedStyle().height, <text> initial (with dummy attribute)
-Fail SVG Geometry Properties: getComputedStyle().height, <text> inline style (auto)
-Fail SVG Geometry Properties: getComputedStyle().height, <text> inline style (pixels)
-Fail SVG Geometry Properties: getComputedStyle().height, <text> inline style (percentage)
\ No newline at end of file
+Pass SVG Geometry Properties: getComputedStyle().height, <g> initial
+Pass SVG Geometry Properties: getComputedStyle().height, <g> initial (with dummy attribute)
+Pass SVG Geometry Properties: getComputedStyle().height, <g> inline style (auto)
+Pass SVG Geometry Properties: getComputedStyle().height, <g> inline style (pixels)
+Pass SVG Geometry Properties: getComputedStyle().height, <g> inline style (percentage)
+Pass SVG Geometry Properties: getComputedStyle().height, <text> initial
+Pass SVG Geometry Properties: getComputedStyle().height, <text> initial (with dummy attribute)
+Pass SVG Geometry Properties: getComputedStyle().height, <text> inline style (auto)
+Pass SVG Geometry Properties: getComputedStyle().height, <text> inline style (pixels)
+Pass SVG Geometry Properties: getComputedStyle().height, <text> inline style (percentage)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/geometry/parsing/width-computed.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/geometry/parsing/width-computed.txt
index b9a05cd3ba7..29b6517ce23 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/svg/geometry/parsing/width-computed.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/geometry/parsing/width-computed.txt
@@ -2,8 +2,8 @@ Harness status: OK
Found 34 tests
-20 Pass
-14 Fail
+30 Pass
+4 Fail
Pass SVG Geometry Properties: getComputedStyle().width, <rect> initial
Pass SVG Geometry Properties: getComputedStyle().width, <rect> presentation attribute
Pass SVG Geometry Properties: getComputedStyle().width, <rect> inline style (percentage)
@@ -28,13 +28,13 @@ Fail SVG Geometry Properties: getComputedStyle().width, <svg> inline style (auto
Pass SVG Geometry Properties: getComputedStyle().width, <svg> inline style (pixels)
Fail SVG Geometry Properties: getComputedStyle().width, <svg> inline style (percentage)
Pass SVG Geometry Properties: getComputedStyle().width, <svg> inline style but "display: none"
-Fail SVG Geometry Properties: getComputedStyle().width, <g> initial
-Fail SVG Geometry Properties: getComputedStyle().width, <g> initial (with dummy attribute)
-Fail SVG Geometry Properties: getComputedStyle().width, <g> inline style (percentage)
-Fail SVG Geometry Properties: getComputedStyle().width, <g> inline style (auto)
-Fail SVG Geometry Properties: getComputedStyle().width, <g> inline style (pixels)
-Fail SVG Geometry Properties: getComputedStyle().width, <text> initial
-Fail SVG Geometry Properties: getComputedStyle().width, <text> initial (with dummy attribute)
-Fail SVG Geometry Properties: getComputedStyle().width, <text> inline style (percentage)
-Fail SVG Geometry Properties: getComputedStyle().width, <text> inline style (auto)
-Fail SVG Geometry Properties: getComputedStyle().width, <text> inline style (pixels)
\ No newline at end of file
+Pass SVG Geometry Properties: getComputedStyle().width, <g> initial
+Pass SVG Geometry Properties: getComputedStyle().width, <g> initial (with dummy attribute)
+Pass SVG Geometry Properties: getComputedStyle().width, <g> inline style (percentage)
+Pass SVG Geometry Properties: getComputedStyle().width, <g> inline style (auto)
+Pass SVG Geometry Properties: getComputedStyle().width, <g> inline style (pixels)
+Pass SVG Geometry Properties: getComputedStyle().width, <text> initial
+Pass SVG Geometry Properties: getComputedStyle().width, <text> initial (with dummy attribute)
+Pass SVG Geometry Properties: getComputedStyle().width, <text> inline style (percentage)
+Pass SVG Geometry Properties: getComputedStyle().width, <text> inline style (auto)
+Pass SVG Geometry Properties: getComputedStyle().width, <text> inline style (pixels)
\ No newline at end of file