Bug 196910 - If the source of an <img> element is SVG image, the aspect ratio of the SVG image is preserved
Summary: If the source of an <img> element is SVG image, the aspect ratio of the SVG i...
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: SVG (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: cathiechen
URL:
Keywords: BrowserCompat, InRadar
Depends on:
Blocks:
 
Reported: 2019-04-14 19:55 PDT by cathiechen
Modified: 2024-02-08 15:15 PST (History)
8 users (show)

See Also:


Attachments
test case (766 bytes, text/html)
2019-04-17 10:05 PDT, Said Abou-Hallawa
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description cathiechen 2019-04-14 19:55:10 PDT
When the ratio is different, Chrome and Firefox will stretch the svgImage to fit the ratio of <img>,
WebKit will position the svgImage in the middle.

E.g. 
```
<img style="width: 400px; height: 500px;" src="./green.svg"/>
```

```
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="50">
  <rect fill="lime" width="100" height="50"/>
  <text x="0" y="15" fill="red">text</text>
</svg>

```
Comment 1 Said Abou-Hallawa 2019-04-17 10:05:41 PDT
Created attachment 367639 [details]
test case
Comment 2 Said Abou-Hallawa 2019-04-17 10:11:22 PDT
I am not sure which one is correct: preserving the aspect ratio or stretching the SVG image inside the <img> element. I personally like the WebKit behavior. I am not sure if there is specs addressing this issue. Does this cause any known bug?
Comment 3 cathiechen 2019-04-18 02:22:50 PDT
No known bug, I just met this by accident. :)

I think the benefit of stretching the SVG image is that for the ratio insensitive SVG image, it could be reused by more scenario.

For instance, we need green images with different sizes and different ratio.
By stretching the SVG image, we could reuse one SVG image, otherwise we need to create different SVG images for every ratio.


PS: the ratio insensitive SVG image:
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="50">
  <rect fill="lime" width="100" height="50"/>
</svg>
Comment 4 Frédéric Wang (:fredw) 2019-04-18 23:55:18 PDT
(In reply to Said Abou-Hallawa from comment #2)
> I am not sure if there is specs addressing this issue.

I'm curious about this too. Cathie, can you please investigate about this? (and raise the issue to the proper standardization group if necessary).
Comment 5 cathiechen 2019-04-24 01:43:35 PDT
(In reply to Frédéric Wang (:fredw) from comment #4)
> (In reply to Said Abou-Hallawa from comment #2)
> > I am not sure if there is specs addressing this issue.
> 
> I'm curious about this too. Cathie, can you please investigate about this?
> (and raise the issue to the proper standardization group if necessary).


Not found in the Spec, but seems it has been disscussed in https://github.com/w3c/svgwg/issues/249#issuecomment-253092701.
Comment 6 Nikolas Zimmermann 2019-10-21 08:51:21 PDT
We synthesize a viewBox if width or height properties are supplied on the outermost <svg> element and if we're embedded through SVGImage.

See SVGSVGElement::currentViewBoxRect():

    if (!downcast<RenderSVGRoot>(*renderer()).isEmbeddedThroughSVGImage())
        return { };

    Length intrinsicWidth = this->intrinsicWidth();
    Length intrinsicHeight = this->intrinsicHeight();
    if (!intrinsicWidth.isFixed() || !intrinsicHeight.isFixed())
        return { };

    // If no viewBox is specified but non-relative width/height values, then we
    // should always synthesize a viewBox if we're embedded through a SVGImage.    
    return { 0, 0, floatValueForLength(intrinsicWidth, 0), floatValueForLength(intrinsicHeight, 0) };
}

If you add viewBox="0 0 100 50" manually to Saids testcase the other browser will behave as the current WebKit rendering without the viewBox.

I added the code in 1adc559cd690b1bff7b5886ddee9e7112d238a92, 7 years ago.
Back then this matched Opera behavior and my understanding of the specs -- that could be different now. If that is the case (and there are WPT tests proving this) let's change it :-)
Comment 7 Karl Dubost 2022-12-26 17:07:17 PST
This test is failing 
http://wpt.live/css/css-ui/box-sizing-016.html

```
img {
    box-sizing: border-box;
    width: auto;
    height: auto;
    background: white;
    padding-bottom: 30px;
    max-height: 100px;
}
```

and

```
<svg 
  style="background: green" 
  xmlns="http://www.w3.org/2000/svg" 
  viewBox="0 0 100 100">
  <g id='testmeta' 
    xmlns:html='http://www.w3.org/1999/xhtml'>
  </g>
</svg>

```

and 

```
  <img src="support/r1-1.svg">
```

As soon as we set height to 100px. It is working. So I guess the current code doesn't take into account the max-height for the aspect ratio.
Comment 8 Radar WebKit Bug Importer 2024-02-08 15:15:08 PST
<rdar://problem/122586412>