LibWeb: Don't crash when calling getBBox() on the outermost SVG element

This commit is contained in:
Tim Ledbetter 2024-09-06 23:38:13 +01:00 committed by Andreas Kling
parent 7a26de7464
commit b140206a91
Notes: github-actions[bot] 2024-09-07 12:35:52 +00:00
3 changed files with 17 additions and 1 deletions

View file

@ -0,0 +1 @@
Bounding box of empty SVG element - x: 0, y: 0, width: 0, height: 0

View file

@ -0,0 +1,11 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<svg></svg>
<script>
test(() => {
const svgElement = document.querySelector("svg");
const boundingBox = svgElement.getBBox();
println(`Bounding box of empty SVG element - x: ${boundingBox.x}, y: ${boundingBox.y}, width: ${boundingBox.width}, height: ${boundingBox.height}`);
svgElement.remove();
});
</script>

View file

@ -262,6 +262,7 @@ Optional<float> SVGGraphicsElement::stroke_width() const
return width.to_px(*layout_node(), scaled_viewport_size).to_double();
}
// https://svgwg.org/svg2-draft/types.html#__svg__SVGGraphicsElement__getBBox
JS::NonnullGCPtr<Geometry::DOMRect> SVGGraphicsElement::get_b_box(Optional<SVGBoundingBoxOptions>)
{
// FIXME: It should be possible to compute this without layout updates. The bounding box is within the
@ -272,7 +273,10 @@ JS::NonnullGCPtr<Geometry::DOMRect> SVGGraphicsElement::get_b_box(Optional<SVGBo
if (!layout_node())
return Geometry::DOMRect::create(realm());
// Invert the SVG -> screen space transform.
auto svg_element_rect = shadow_including_first_ancestor_of_type<SVG::SVGSVGElement>()->paintable_box()->absolute_rect();
auto owner_svg_element = this->owner_svg_element();
if (!owner_svg_element)
return Geometry::DOMRect::create(realm());
auto svg_element_rect = owner_svg_element->paintable_box()->absolute_rect();
auto inverse_transform = static_cast<Painting::SVGGraphicsPaintable&>(*paintable_box()).computed_transforms().svg_to_css_pixels_transform().inverse();
auto translated_rect = paintable_box()->absolute_rect().to_type<float>().translated(-svg_element_rect.location().to_type<float>());
if (inverse_transform.has_value())