[SVG2]: Add 'auto' behavior to the 'width' and 'height' properties of the SVG <image> element
https://bugs.webkit.org/show_bug.cgi?id=202013

Patch by Said Abou-Hallawa <sabouhallawa@apple.com> on 2019-10-21
Reviewed by Simon Fraser.

LayoutTests/imported/w3c:

* web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-dynamic-image-change-expected.txt:
* web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-expected.txt:

Source/WebCore:

The spec page is: https://www.w3.org/TR/SVG/geometry.html#Sizing

Handle the case if the 'width' or the 'height' of an SVG <image> or both
are missing.

Tests: svg/custom/image-width-height-auto-dynamic.svg
       svg/custom/image-width-height-auto-initial.svg
       svg/custom/image-width-height-length-initial.svg

* rendering/svg/RenderSVGImage.cpp:
(WebCore::RenderSVGImage::calculateObjectBoundingBox const):
(WebCore::RenderSVGImage::updateImageViewport):
* rendering/svg/RenderSVGImage.h:

LayoutTests:

* svg/custom/image-width-height-auto-dynamic-expected.svg: Added.
* svg/custom/image-width-height-auto-dynamic.svg: Added.
* svg/custom/image-width-height-auto-initial-expected.svg: Added.
* svg/custom/image-width-height-auto-initial.svg: Added.
* svg/custom/image-width-height-length-initial-expected.svg: Added.
* svg/custom/image-width-height-length-initial.svg: Added.
* svg/custom/resources/100x200-green.png: Added.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@251378 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index 2027495..69b9da1 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,18 @@
+2019-10-21  Said Abou-Hallawa  <sabouhallawa@apple.com>
+
+        [SVG2]: Add 'auto' behavior to the 'width' and 'height' properties of the SVG <image> element
+        https://bugs.webkit.org/show_bug.cgi?id=202013
+
+        Reviewed by Simon Fraser.
+
+        * svg/custom/image-width-height-auto-dynamic-expected.svg: Added.
+        * svg/custom/image-width-height-auto-dynamic.svg: Added.
+        * svg/custom/image-width-height-auto-initial-expected.svg: Added.
+        * svg/custom/image-width-height-auto-initial.svg: Added.
+        * svg/custom/image-width-height-length-initial-expected.svg: Added.
+        * svg/custom/image-width-height-length-initial.svg: Added.
+        * svg/custom/resources/100x200-green.png: Added.
+
 2019-10-21  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         [Clipboard API] Implement ClipboardItem.getType() for platform clipboard items
diff --git a/LayoutTests/imported/w3c/ChangeLog b/LayoutTests/imported/w3c/ChangeLog
index b43dc4d..2022aab 100644
--- a/LayoutTests/imported/w3c/ChangeLog
+++ b/LayoutTests/imported/w3c/ChangeLog
@@ -1,3 +1,13 @@
+2019-10-21  Said Abou-Hallawa  <sabouhallawa@apple.com>
+
+        [SVG2]: Add 'auto' behavior to the 'width' and 'height' properties of the SVG <image> element
+        https://bugs.webkit.org/show_bug.cgi?id=202013
+
+        Reviewed by Simon Fraser.
+
+        * web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-dynamic-image-change-expected.txt:
+        * web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-expected.txt:
+
 2019-10-21  Chris Dumez  <cdumez@apple.com>
 
         XMLHttpRequest should not prevent entering the back/forward cache
diff --git a/LayoutTests/imported/w3c/web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-dynamic-image-change-expected.txt b/LayoutTests/imported/w3c/web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-dynamic-image-change-expected.txt
index c17a4ee..20efc0f 100644
--- a/LayoutTests/imported/w3c/web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-dynamic-image-change-expected.txt
+++ b/LayoutTests/imported/w3c/web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-dynamic-image-change-expected.txt
@@ -1,3 +1,3 @@
 
-FAIL Test <svg:image>'s sizing with css size as auto, with dynamic image change assert_equals: expected 256 but got 128
+PASS Test <svg:image>'s sizing with css size as auto, with dynamic image change 
 
diff --git a/LayoutTests/imported/w3c/web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-expected.txt b/LayoutTests/imported/w3c/web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-expected.txt
index a45ffbe..9669bda 100644
--- a/LayoutTests/imported/w3c/web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-expected.txt
+++ b/LayoutTests/imported/w3c/web-platform-tests/svg/geometry/svg-image-intrinsic-size-with-cssstyle-auto-expected.txt
@@ -1,32 +1,32 @@
 
 PASS <svg:image> 'auto' sizing with 256x256 png image, attributes width='64' height='64' 
-FAIL <svg:image> 'auto' sizing with 256x256 png image, attributes width='128' height='64' and CSS 'width:auto' assert_equals: expected 64 but got 128
-FAIL <svg:image> 'auto' sizing with 256x256 png image, attributes width='64' height='128' and CSS 'height:auto' assert_equals: expected 64 but got 128
-FAIL <svg:image> 'auto' sizing with 256x256 png image, attributes width='64' height='64' and CSS 'width:auto; height:auto' assert_equals: expected 256 but got 128
+PASS <svg:image> 'auto' sizing with 256x256 png image, attributes width='128' height='64' and CSS 'width:auto' 
+PASS <svg:image> 'auto' sizing with 256x256 png image, attributes width='64' height='128' and CSS 'height:auto' 
+PASS <svg:image> 'auto' sizing with 256x256 png image, attributes width='64' height='64' and CSS 'width:auto; height:auto' 
 PASS <svg:image> 'auto' sizing with default sized svg image, attributes width='50' height='50' 
-FAIL <svg:image> 'auto' sizing with 200x100 svg image, attributes width='50' height='50' and CSS 'width:auto; height:auto' assert_equals: expected 200 but got 50
-FAIL <svg:image> 'auto' sizing with 200x100 svg image, attributes width='50' height='50' and CSS 'width:auto' assert_equals: expected 100 but got 50
-FAIL <svg:image> 'auto' sizing with 200x100 svg image, without attributes width and height and CSS 'width:auto' assert_equals: expected 200 but got 0
-FAIL <svg:image> 'auto' sizing with 200x100 svg image, attributes height='50' and 'width:auto' assert_equals: expected 100 but got 0
-FAIL <svg:image> 'auto' sizing with 200x100 svg image, attributes width='50' and CSS height:auto assert_equals: expected 25 but got 0
-FAIL <svg:image> 'auto' sizing with default sized svg image, attributes width='200' height='200' and CSS 'width:auto; height:auto' assert_equals: expected 300 but got 200
-FAIL <svg:image> 'auto' sizing with default sized svg image, attributes height='200' and CSS 'width:auto; height:auto' assert_equals: expected 300 but got 0
-FAIL <svg:image> 'auto' sizing with default sized svg image, attributes width='200' and CSS 'width:auto; height:auto' assert_equals: expected 300 but got 200
-FAIL <svg:image> 'auto' sizing with default sized svg image, without attributes width and height, no css width/height specified assert_equals: expected 300 but got 0
+PASS <svg:image> 'auto' sizing with 200x100 svg image, attributes width='50' height='50' and CSS 'width:auto; height:auto' 
+PASS <svg:image> 'auto' sizing with 200x100 svg image, attributes width='50' height='50' and CSS 'width:auto' 
+PASS <svg:image> 'auto' sizing with 200x100 svg image, without attributes width and height and CSS 'width:auto' 
+PASS <svg:image> 'auto' sizing with 200x100 svg image, attributes height='50' and 'width:auto' 
+PASS <svg:image> 'auto' sizing with 200x100 svg image, attributes width='50' and CSS height:auto 
+PASS <svg:image> 'auto' sizing with default sized svg image, attributes width='200' height='200' and CSS 'width:auto; height:auto' 
+PASS <svg:image> 'auto' sizing with default sized svg image, attributes height='200' and CSS 'width:auto; height:auto' 
+PASS <svg:image> 'auto' sizing with default sized svg image, attributes width='200' and CSS 'width:auto; height:auto' 
+PASS <svg:image> 'auto' sizing with default sized svg image, without attributes width and height, no css width/height specified 
 PASS <svg:image> 'auto' sizing with default sized svg image viewBox='0 0 400 100', attributes width='60' height='60' and no css width/height specified 
-FAIL <svg:image> 'auto' sizing with default sized svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto' assert_equals: expected 240 but got 60
-FAIL <svg:image> 'auto' sizing with default sized svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'height:auto' assert_equals: expected 15 but got 60
-FAIL <svg:image> 'auto' sizing with default sized svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto; height:auto' assert_equals: expected 300 but got 60
+PASS <svg:image> 'auto' sizing with default sized svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto' 
+PASS <svg:image> 'auto' sizing with default sized svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'height:auto' 
+FAIL <svg:image> 'auto' sizing with default sized svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto; height:auto' assert_equals: expected 300 but got 400
 PASS <svg:image> 'auto' sizing with svg image width='200' viewBox='0 0 400 100', attributes width='60' height='60' and no css width/height specified 
-FAIL <svg:image> 'auto' sizing with svg image width='200' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto' assert_equals: expected 240 but got 60
-FAIL <svg:image> 'auto' sizing with svg image width='200' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'height:auto' assert_equals: expected 15 but got 60
-FAIL <svg:image> 'auto' sizing with svg image width='200' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto; height:auto' assert_equals: expected 200 but got 60
+PASS <svg:image> 'auto' sizing with svg image width='200' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto' 
+PASS <svg:image> 'auto' sizing with svg image width='200' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'height:auto' 
+FAIL <svg:image> 'auto' sizing with svg image width='200' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto; height:auto' assert_equals: expected 200 but got 400
 PASS <svg:image> 'auto' sizing with svg image height='100' viewBox='0 0 400 100', attributes width='60' height='60' and no css width/height specified 
-FAIL <svg:image> 'auto' sizing with svg image height='100' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto' assert_equals: expected 240 but got 60
-FAIL <svg:image> 'auto' sizing with svg image height='100' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'height:auto' assert_equals: expected 15 but got 60
-FAIL <svg:image> 'auto' sizing with svg image height='100' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto; height:auto' assert_equals: expected 400 but got 60
+PASS <svg:image> 'auto' sizing with svg image height='100' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto' 
+PASS <svg:image> 'auto' sizing with svg image height='100' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'height:auto' 
+PASS <svg:image> 'auto' sizing with svg image height='100' viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto; height:auto' 
 PASS <svg:image> 'auto' sizing with 200x100 svg image viewBox='0 0 400 100', attributes width='60' height='60' and no css width/height specified 
-FAIL <svg:image> 'auto' sizing with 200x100 svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto' assert_equals: expected 120 but got 60
-FAIL <svg:image> 'auto' sizing with 200x100 svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'height:auto' assert_equals: expected 30 but got 60
-FAIL <svg:image> 'auto' sizing with 200x100 svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto; height:auto' assert_equals: expected 200 but got 60
+PASS <svg:image> 'auto' sizing with 200x100 svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto' 
+PASS <svg:image> 'auto' sizing with 200x100 svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'height:auto' 
+PASS <svg:image> 'auto' sizing with 200x100 svg image viewBox='0 0 400 100', attributes width='60' height='60' and CSS 'width:auto; height:auto' 
 
diff --git a/LayoutTests/svg/custom/image-width-height-auto-dynamic-expected.svg b/LayoutTests/svg/custom/image-width-height-auto-dynamic-expected.svg
new file mode 100644
index 0000000..f6ecf50
--- /dev/null
+++ b/LayoutTests/svg/custom/image-width-height-auto-dynamic-expected.svg
@@ -0,0 +1,10 @@
+<svg xmlns="http://www.w3.org/2000/svg">
+    <rect id="rect-1" x="10" y="10" width="50" height="100" fill="green"/>
+    <rect id="rect-2" x="110" y="10" width="50" height="100" fill="green"/>
+    <rect id="rect-3" x="210" y="10" width="50" height="100" fill="green"/>
+    <rect id="rect-4" x="310" y="10" width="50" height="100" fill="green"/>
+    <use y="110" href="#rect-1"/>
+    <use y="110" href="#rect-2"/>
+    <use y="110" href="#rect-3"/>
+    <use y="110" href="#rect-4"/>
+</svg>
diff --git a/LayoutTests/svg/custom/image-width-height-auto-dynamic.svg b/LayoutTests/svg/custom/image-width-height-auto-dynamic.svg
new file mode 100644
index 0000000..6acd553
--- /dev/null
+++ b/LayoutTests/svg/custom/image-width-height-auto-dynamic.svg
@@ -0,0 +1,46 @@
+<svg xmlns="http://www.w3.org/2000/svg">
+    <g id="g-1" transform="translate(10, 10)">
+        <image id="image-1" href="resources/100x200-green.png" width="50" height="100"/>
+    </g>
+    <g id="g-2" transform="translate(110, 10)">
+        <image id="image-2" href="resources/100x200-green.png" width="50" height="100"/>
+    </g>
+
+    <g id="g-3" transform="translate(210, 10)">
+        <image id="image-3" href="resources/100x200-green.png" width="50" height="100">
+            <animate attributeType="XML" attributeName="width" from="0" to="50" dur="1s" fill="freeze"/>
+        </image>
+    </g>
+
+    <g id="g-4" transform="translate(310, 10)">
+        <image id="image-4" href="resources/100x200-green.png" width="50" height="100">
+            <animate attributeType="XML" attributeName="height" from="0" to="100" dur="1s" fill="freeze"/>
+        </image>
+    </g>
+
+    <use y="110" href="#g-1"/>
+    <use y="110" href="#g-2"/>
+    <use y="110" href="#g-3"/>
+    <use y="110" href="#g-4"/>
+
+    <script>
+        if (window.testRunner)
+            testRunner.waitUntilDone();
+
+        window.addEventListener('load', (event) => {
+            let image1 = document.getElementById('image-1');
+            let image2 = document.getElementById('image-2');
+            let image3 = document.getElementById('image-3');
+            let image4 = document.getElementById('image-4');
+
+            image1.style['height'] = 'auto';
+            image2.style['width'] = 'auto';
+            image3.style['height'] = 'auto';
+            image4.style['width'] = 'auto';
+
+            document.documentElement.setCurrentTime(5);
+            if (window.testRunner)
+                testRunner.notifyDone();
+        });
+    </script>
+</svg>
diff --git a/LayoutTests/svg/custom/image-width-height-auto-initial-expected.svg b/LayoutTests/svg/custom/image-width-height-auto-initial-expected.svg
new file mode 100644
index 0000000..f6ecf50
--- /dev/null
+++ b/LayoutTests/svg/custom/image-width-height-auto-initial-expected.svg
@@ -0,0 +1,10 @@
+<svg xmlns="http://www.w3.org/2000/svg">
+    <rect id="rect-1" x="10" y="10" width="50" height="100" fill="green"/>
+    <rect id="rect-2" x="110" y="10" width="50" height="100" fill="green"/>
+    <rect id="rect-3" x="210" y="10" width="50" height="100" fill="green"/>
+    <rect id="rect-4" x="310" y="10" width="50" height="100" fill="green"/>
+    <use y="110" href="#rect-1"/>
+    <use y="110" href="#rect-2"/>
+    <use y="110" href="#rect-3"/>
+    <use y="110" href="#rect-4"/>
+</svg>
diff --git a/LayoutTests/svg/custom/image-width-height-auto-initial.svg b/LayoutTests/svg/custom/image-width-height-auto-initial.svg
new file mode 100644
index 0000000..fdb06ce
--- /dev/null
+++ b/LayoutTests/svg/custom/image-width-height-auto-initial.svg
@@ -0,0 +1,42 @@
+<svg xmlns="http://www.w3.org/2000/svg">
+    <style>
+        #image-1 { height: auto; }
+        #image-2 { width: auto; }
+        #image-3 { height: auto; }
+        #image-4 { width: auto; }
+    </style>
+    <g id="g-1" transform="translate(10, 10)">
+        <image href="resources/100x200-green.png" width="50"/>
+    </g>
+    <g id="g-2" transform="translate(110, 10)">
+        <image href="resources/100x200-green.png" height="100"/>
+    </g>
+
+    <g id="g-3" transform="translate(210, 10)">
+        <image href="resources/100x200-green.png">
+            <animate attributeType="XML" attributeName="width" from="0" to="50" dur="1s" fill="freeze"/>
+        </image>
+    </g>
+
+    <g id="g-4" transform="translate(310, 10)">
+        <image href="resources/100x200-green.png">
+            <animate attributeType="XML" attributeName="height" from="0" to="100" dur="1s" fill="freeze"/>
+        </image>
+    </g>
+
+    <use y="110" href="#g-1"/>
+    <use y="110" href="#g-2"/>
+    <use y="110" href="#g-3"/>
+    <use y="110" href="#g-4"/>
+
+    <script>
+        if (window.testRunner)
+            testRunner.waitUntilDone();
+
+        window.addEventListener('load', (event) => {
+            document.documentElement.setCurrentTime(5);
+            if (window.testRunner)
+                testRunner.notifyDone();
+        });
+    </script>
+</svg>
diff --git a/LayoutTests/svg/custom/image-width-height-length-initial-expected.svg b/LayoutTests/svg/custom/image-width-height-length-initial-expected.svg
new file mode 100644
index 0000000..f6ecf50
--- /dev/null
+++ b/LayoutTests/svg/custom/image-width-height-length-initial-expected.svg
@@ -0,0 +1,10 @@
+<svg xmlns="http://www.w3.org/2000/svg">
+    <rect id="rect-1" x="10" y="10" width="50" height="100" fill="green"/>
+    <rect id="rect-2" x="110" y="10" width="50" height="100" fill="green"/>
+    <rect id="rect-3" x="210" y="10" width="50" height="100" fill="green"/>
+    <rect id="rect-4" x="310" y="10" width="50" height="100" fill="green"/>
+    <use y="110" href="#rect-1"/>
+    <use y="110" href="#rect-2"/>
+    <use y="110" href="#rect-3"/>
+    <use y="110" href="#rect-4"/>
+</svg>
diff --git a/LayoutTests/svg/custom/image-width-height-length-initial.svg b/LayoutTests/svg/custom/image-width-height-length-initial.svg
new file mode 100644
index 0000000..9bcdaf9
--- /dev/null
+++ b/LayoutTests/svg/custom/image-width-height-length-initial.svg
@@ -0,0 +1,42 @@
+<svg xmlns="http://www.w3.org/2000/svg">
+    <style>
+        #image-1 { height: 100px; }
+        #image-2 { width: 50px; }
+        #image-3 { height: 100px; }
+        #image-4 { width: 50px; }
+    </style>
+    <g id="g-1" transform="translate(10, 10)">
+        <image id="image-1" href="resources/100x200-green.png" width="50"/>
+    </g>
+    <g id="g-2" transform="translate(110, 10)">
+        <image id="image-2" href="resources/100x200-green.png" height="100"/>
+    </g>
+
+    <g id="g-3" transform="translate(210, 10)">
+        <image id="image-3" href="resources/100x200-green.png">
+            <animate attributeType="XML" attributeName="width" from="0" to="50" dur="1s" fill="freeze"/>
+        </image>
+    </g>
+
+    <g id="g-4" transform="translate(310, 10)">
+        <image id="image-4" href="resources/100x200-green.png">
+            <animate attributeType="XML" attributeName="height" from="0" to="100" dur="1s" fill="freeze"/>
+        </image>
+    </g>
+
+    <use y="110" href="#g-1"/>
+    <use y="110" href="#g-2"/>
+    <use y="110" href="#g-3"/>
+    <use y="110" href="#g-4"/>
+
+    <script>
+        if (window.testRunner)
+            testRunner.waitUntilDone();
+
+        window.addEventListener('load', (event) => {
+            document.documentElement.setCurrentTime(5);
+            if (window.testRunner)
+                testRunner.notifyDone();
+        });
+    </script>
+</svg>
diff --git a/LayoutTests/svg/custom/resources/100x200-green.png b/LayoutTests/svg/custom/resources/100x200-green.png
new file mode 100644
index 0000000..8eb6945
--- /dev/null
+++ b/LayoutTests/svg/custom/resources/100x200-green.png
Binary files differ
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 7fbdb5b..4bea45e 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,24 @@
+2019-10-21  Said Abou-Hallawa  <sabouhallawa@apple.com>
+
+        [SVG2]: Add 'auto' behavior to the 'width' and 'height' properties of the SVG <image> element
+        https://bugs.webkit.org/show_bug.cgi?id=202013
+
+        Reviewed by Simon Fraser.
+
+        The spec page is: https://www.w3.org/TR/SVG/geometry.html#Sizing
+
+        Handle the case if the 'width' or the 'height' of an SVG <image> or both
+        are missing.
+
+        Tests: svg/custom/image-width-height-auto-dynamic.svg
+               svg/custom/image-width-height-auto-initial.svg
+               svg/custom/image-width-height-length-initial.svg
+
+        * rendering/svg/RenderSVGImage.cpp:
+        (WebCore::RenderSVGImage::calculateObjectBoundingBox const):
+        (WebCore::RenderSVGImage::updateImageViewport):
+        * rendering/svg/RenderSVGImage.h:
+
 2019-10-21  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         [Clipboard API] Implement ClipboardItem.getType() for platform clipboard items
diff --git a/Source/WebCore/rendering/svg/RenderSVGImage.cpp b/Source/WebCore/rendering/svg/RenderSVGImage.cpp
index 24cde4b..c448485 100644
--- a/Source/WebCore/rendering/svg/RenderSVGImage.cpp
+++ b/Source/WebCore/rendering/svg/RenderSVGImage.cpp
@@ -68,14 +68,42 @@
     return downcast<SVGImageElement>(RenderSVGModelObject::element());
 }
 
+FloatRect RenderSVGImage::calculateObjectBoundingBox() const
+{
+    LayoutSize intrinsicSize;
+    if (CachedImage* cachedImage = imageResource().cachedImage())
+        intrinsicSize = cachedImage->imageSizeForRenderer(nullptr, style().effectiveZoom());
+
+    SVGLengthContext lengthContext(&imageElement());
+
+    Length width = style().width();
+    Length height = style().height();
+
+    float concreteWidth;
+    if (!width.isAuto())
+        concreteWidth = lengthContext.valueForLength(width, SVGLengthMode::Width);
+    else if (!height.isAuto() && !intrinsicSize.isEmpty())
+        concreteWidth = lengthContext.valueForLength(height, SVGLengthMode::Height) * intrinsicSize.width() / intrinsicSize.height();
+    else
+        concreteWidth = intrinsicSize.width();
+
+    float concreteHeight;
+    if (!height.isAuto())
+        concreteHeight = lengthContext.valueForLength(height, SVGLengthMode::Height);
+    else if (!width.isAuto() && !intrinsicSize.isEmpty())
+        concreteHeight = lengthContext.valueForLength(width, SVGLengthMode::Width) * intrinsicSize.height() / intrinsicSize.width();
+    else
+        concreteHeight = intrinsicSize.height();
+
+    return { imageElement().x().value(lengthContext), imageElement().y().value(lengthContext), concreteWidth, concreteHeight };
+}
+
 bool RenderSVGImage::updateImageViewport()
 {
     FloatRect oldBoundaries = m_objectBoundingBox;
+    m_objectBoundingBox = calculateObjectBoundingBox();
+
     bool updatedViewport = false;
-
-    SVGLengthContext lengthContext(&imageElement());
-    m_objectBoundingBox = FloatRect(imageElement().x().value(lengthContext), imageElement().y().value(lengthContext), imageElement().width().value(lengthContext), imageElement().height().value(lengthContext));
-
     URL imageSourceURL = document().completeURL(imageElement().imageSourceURL());
 
     // Images with preserveAspectRatio=none should force non-uniform scaling. This can be achieved
@@ -223,7 +251,8 @@
     // Update the SVGImageCache sizeAndScales entry in case image loading finished after layout.
     // (https://bugs.webkit.org/show_bug.cgi?id=99489)
     m_objectBoundingBox = FloatRect();
-    updateImageViewport();
+    if (updateImageViewport())
+        setNeedsLayout();
 
     invalidateBufferedForeground();
 
diff --git a/Source/WebCore/rendering/svg/RenderSVGImage.h b/Source/WebCore/rendering/svg/RenderSVGImage.h
index 2a1c8a1..74148c59 100644
--- a/Source/WebCore/rendering/svg/RenderSVGImage.h
+++ b/Source/WebCore/rendering/svg/RenderSVGImage.h
@@ -62,6 +62,7 @@
 
     const AffineTransform& localToParentTransform() const override { return m_localTransform; }
 
+    FloatRect calculateObjectBoundingBox() const;
     FloatRect objectBoundingBox() const override { return m_objectBoundingBox; }
     FloatRect strokeBoundingBox() const override { return m_objectBoundingBox; }
     FloatRect repaintRectInLocalCoordinates() const override { return m_repaintBoundingBox; }