Repository navigation
Should getBBox returns values when the shape can't be drawn #1018
Description
Using this WPT test as an example
https://wpt.live/svg/types/scripted/SVGGraphicsElement.getBBox-03.html
<rect id="rect1" x="1" y="2" width="-10" height="20"/>
The value for the rect width is a negative number, which is invalid.
- CSS 2.1 Negative values for 'width' are illegal.
and in https://svgwg.org/svg2-draft/shapes.html#RectElement
The width and height properties define the overall width and height of the rectangle :
- A negative value for either property is invalid and must be ignored.
- A computed value of zero for either dimension disables rendering of the element.
Then in 4.4.2. Interface SVGGraphicsElement for getBBox
The getBBox method is used to compute the bounding box of the current element. When the getBBox(options) method is called, the bounding box algorithm is invoked for the current element, with fill, stroke, markers and clipped members of the options dictionary argument used to control which parts of the element are included in the bounding box, using the element's user coordinate system as the coordinate system to return the bounding box in. A newly created DOMRect object that defines the computed bounding box is returned. If getBBox gets called on a non-rendered element, and the UA is not able to compute the geometry of the element, then throw an InvalidStateError.
Currently:
| Browser | x | y | width | height |
|---|---|---|---|---|
| Firefox | 0 | 0 | 0 | 0 |
| Safari | 0 | 0 | 0 | 0 |
| Chrome | 1 | 2 | 0 | 20 |
So I decided to try with a rect were width would be missing.
<rect id="rect1" x="1" y="2" height="20"/>
And still the results are the same. Chrome returns 0, Firefox/Safari return 0 for everything as the shape is not valid.
In 7.8. Sizing properties: the effect of the ‘width’ and ‘height’ properties
The value auto for width and height on other elements is treated as 0.
but that's not the case here. We do not have a value auto.
The rules for parsing attribute values are in 4.2. Attribute syntax.
The Initial value column gives the initial value for the attribute. When an attribute fails to parse according to the specified CSS Value Definition Syntax, ABNF or EBNF grammar, or if parsing according to the URL Standard or by the prose describing how to parse the attribute indicates failure, the attribute is assumed to have been specified as the given initial value.
There is no table for width in SVG2 as it refers to CSS.
The CSS 2.2 spec in 10.2 Content width: the 'width' property gives as initial value for width: auto
but I'm still confused…
It says ignore at the beginning, then getBBOX() can not be computed.
Opinions?
- Should the WPT tests be modified and it should return
InvalidStateError? - OR Should the spec be modified to clarify what invalid width/height default to?
Activity
Ahmad-S792 commentedon Sep 24, 2025
Relevant? https://bugs.webkit.org/show_bug.cgi?id=257615
karlcow commentedon Oct 9, 2025
https://www.w3.org/2025/10/09-svg-minutes.html#34be
goldenboy777 commentedon Oct 29, 2025
Resolution for this was added here
https://www.w3.org/2025/10/23-svg-minutes.html#34be
goldenboy777 commentedon Oct 29, 2025
I looked at the spec of Bounding box a little bit and saw
https://svgwg.org/svg2-draft/coords.html#BoundingBoxes calls out the case for zero width and/or height:
So a bounding box for the below case be 0x20 ??: (Because one of the dimensions resolves to zero the rendering of the element would be disabled, but that would only mean that it shouldn't contribute to the ancestor bboxes.)
A negative value of width/height is essentially ignored and for element like
rectit will be treated as zero.Doesn't this mean the resolution in https://www.w3.org/2025/10/23-svg-minutes.html#34be will change this? @karlcow @dirkschulze
karlcow commentedon Oct 30, 2025
The resolution that I was about to write down today as a PR is
So reading about what is written in the bounding box section.
that's an interesting case. so has dimensions but not visible. This is a valid bbox so it returns data.
Then there is the quote you found:
I don't understand this part is not part of the parsing algorithm below in this section :/ Should it be? Or should this paragraph be rewritten has an algorithm?
What are all the possible cases:
<rect id="rect1" x="10" y="20" width="30" height="40"/><rect id="rect1" x="10" y="20" width="-30" height="40"/><rect id="rect1" x="10" y="20" width="-30" height="-40"/><rect id="rect1" x="10" y="20" width="0" height="40"/><rect id="rect1" x="10" y="20" width="0" height="0"/><rect id="rect1" x="10" y="20" height="40"/><rect id="rect1" x="10" y="20"/>In the resolution, we are currently saying.
Your question is the case for
width="0" height="40"aka should it return(0, 40)or(0, 0)Let's put it back on the agenda. :D
Thanks @goldenboy777 for your sharp eye.
karlcow commentedon Oct 30, 2025
The current result in Safari, Firefox and Chrome
karlcow commentedon Oct 30, 2025
Note that the current resolution doesn't change anything in terms of rendering.
It's just what do we think is logical to return as values, and if it has any cascading impact on the rest.
It could mean we would need to modify the paragraph you found.
@nikolaszimmermann @shallawa @longsonr @nullhook
karlcow commentedon Oct 30, 2025
The codepen for the test above https://codepen.io/webcompat/full/jEWvBLm
karlcow commentedon Oct 30, 2025
https://www.w3.org/2025/10/30-svg-minutes.html#d410
@goldenboy777 will create WPT tests to cover all possible scenarios for getBBox and see how browsers align.
goldenboy777 commentedon Oct 30, 2025
Thanks for the reference, Karl, I see there are also existing WPTs like SVGGraphicsElement.getBBox*, I will see what's missing and either create or update existing ones
shepazu commentedon Oct 30, 2025
From a programmatic perspective, which is true of a lot of drawing and animation tools, it is better to have a predictable and meaningful output from
getBBox, even when the target is not currently rendered.Having done a lot of this, my personal preference would be for it to return all the information it can, meaning the value information for
x&y, and for whatever validwidthandheightvalues are available. If it's not valid (say,width="-10"), then return0, and my script can deal with that much more gracefully than returningnullor all0s.Keep in mind that a script may be coordinating multiple elements at once, so having all available information about the bbox of those elements as they mutate is valuable.
longsonr commentedon Oct 30, 2025
Firefox culls all invalid shapes early in the rendering process which is why it returns zeros. Changing that would have a performance cost for all those shapes that are not going to be rendered anyway.
karlcow commentedon Mar 18, 2026
In the process of adding more tests.
WebKit/WebKit#60860
karlcow commentedon Mar 19, 2026
The current resolution is
karlcow commentedon Mar 19, 2026
The spec says:
https://w3c.github.io/svgwg/svg2-draft/coords.html#BoundingBoxes
with
Then
And
Also
So there might more to change. There is also the algorithm to compute the bounding box.
https://w3c.github.io/svgwg/svg2-draft/coords.html#:~:text=algorithm%20to%20compute%20the%20bounding%20box
karlcow commentedon Mar 19, 2026
The algorithm to compute the bounding box is not really saying anything about the invalidity.
karlcow commentedon Mar 19, 2026
In https://w3c.github.io/svgwg/svg2-draft/coords.html#BoundingBoxes
This seems to contradict the SVG WG resolution.
getBBox returns (0, 0, 0, 0) when value is invalid or absent - Fix w3…
Modify getBBox behavior to return 0. Fix w3c#1018
karlcow commentedon Mar 19, 2026
I put a PR up for a review if we go with the resolution.
#1078
But for now it seems incomplete given my comments above.
Merge pull request #1078 from karlcow/1018-getbbox-zero-domrect