From 0ec522ab54c82a96b4a053ad73076ab56104b60d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 20 Jun 2023 19:55:53 +0200 Subject: [PATCH] LibWeb: Don't infer SVG viewBox if width and/or height is zero The fix here has two parts: 1. Don't use the fallback viewBox at all if we're not in SVG-as-image. 2. Don't make a fallback viewBox with zero width and/or height. This fixes a crash on Bandcamp pages. Thanks Tim Flynn for reporting! --- ...ith-zero-intrinsic-size-and-no-viewbox.txt | 21 +++++++++++++++++++ ...th-zero-intrinsic-size-and-no-viewbox.html | 4 ++++ ...ith-zero-intrinsic-size-and-no-viewbox.svg | 3 +++ .../Libraries/LibWeb/SVG/SVGSVGElement.cpp | 5 +++-- 4 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 Tests/LibWeb/Layout/expected/svg/svg-with-zero-intrinsic-size-and-no-viewbox.txt create mode 100644 Tests/LibWeb/Layout/input/svg/svg-with-zero-intrinsic-size-and-no-viewbox.html create mode 100644 Tests/LibWeb/Layout/input/svg/svg-with-zero-intrinsic-size-and-no-viewbox.svg diff --git a/Tests/LibWeb/Layout/expected/svg/svg-with-zero-intrinsic-size-and-no-viewbox.txt b/Tests/LibWeb/Layout/expected/svg/svg-with-zero-intrinsic-size-and-no-viewbox.txt new file mode 100644 index 0000000000..a8a8ce415d --- /dev/null +++ b/Tests/LibWeb/Layout/expected/svg/svg-with-zero-intrinsic-size-and-no-viewbox.txt @@ -0,0 +1,21 @@ +Viewport <#document> at (0,0) content-size 800x600 children: not-inline + BlockContainer at (0,0) content-size 800x600 [BFC] children: not-inline + BlockContainer at (8,8) content-size 784x17.46875 children: inline + line 0 width: 8, height: 17.46875, bottom: 17.46875, baseline: 13.53125 + frag 0 from ImageBox start: 0, length: 0, rect: [8,21 0x0] + frag 1 from TextNode start: 0, length: 1, rect: [8,8 8x17.46875] + " " + frag 2 from SVGSVGBox start: 0, length: 0, rect: [16,21 0x0] + ImageBox at (8,21) content-size 0x0 children: not-inline + (SVG-as-image isolated context) + Viewport <#document> at (0,0) content-size 0x0 children: inline + SVGSVGBox at (0,0) content-size 0x0 [SVG] children: inline + TextNode <#text> + SVGGeometryBox at (0,0) content-size 1x1 children: not-inline + TextNode <#text> + TextNode <#text> + SVGSVGBox at (16,21) content-size 0x0 [SVG] children: inline + TextNode <#text> + SVGGeometryBox at (16,21) content-size 1x1 children: not-inline + TextNode <#text> + TextNode <#text> diff --git a/Tests/LibWeb/Layout/input/svg/svg-with-zero-intrinsic-size-and-no-viewbox.html b/Tests/LibWeb/Layout/input/svg/svg-with-zero-intrinsic-size-and-no-viewbox.html new file mode 100644 index 0000000000..f6ca2016cc --- /dev/null +++ b/Tests/LibWeb/Layout/input/svg/svg-with-zero-intrinsic-size-and-no-viewbox.html @@ -0,0 +1,4 @@ + + + + diff --git a/Tests/LibWeb/Layout/input/svg/svg-with-zero-intrinsic-size-and-no-viewbox.svg b/Tests/LibWeb/Layout/input/svg/svg-with-zero-intrinsic-size-and-no-viewbox.svg new file mode 100644 index 0000000000..a2406d6b4c --- /dev/null +++ b/Tests/LibWeb/Layout/input/svg/svg-with-zero-intrinsic-size-and-no-viewbox.svg @@ -0,0 +1,3 @@ + + + diff --git a/Userland/Libraries/LibWeb/SVG/SVGSVGElement.cpp b/Userland/Libraries/LibWeb/SVG/SVGSVGElement.cpp index 873439c917..76ff94e624 100644 --- a/Userland/Libraries/LibWeb/SVG/SVGSVGElement.cpp +++ b/Userland/Libraries/LibWeb/SVG/SVGSVGElement.cpp @@ -104,7 +104,7 @@ void SVGSVGElement::update_fallback_view_box_for_svg_as_image() height = height_value->as_length().length().absolute_length_to_px().to_double(); } - if (width.has_value() && height.has_value()) { + if (width.has_value() && width.value() > 0 && height.has_value() && height.value() > 0) { m_fallback_view_box_for_svg_as_image = ViewBox { 0, 0, width.value(), height.value() }; } else { m_fallback_view_box_for_svg_as_image = {}; @@ -121,7 +121,8 @@ Optional SVGSVGElement::view_box() const if (m_view_box.has_value()) return m_view_box; - if (m_fallback_view_box_for_svg_as_image.has_value()) + // NOTE: If the parent is a document, we're an element used as an image. + if (parent() && parent()->is_document() && m_fallback_view_box_for_svg_as_image.has_value()) return m_fallback_view_box_for_svg_as_image; return {};