Skip to content

Commit 4ff60b0

Browse files
authored
Make naturalWidth & naturalHeight return non-0 when the image lacks a dimension
Unfortunately the more correct behavior was never implemented widely enough and web content started to depend on these properties being non-0. We take special care that dimensions that are missing do not get divided by density so we return the same values for them as CSS (300 by 150). Tests: web-platform-tests/wpt#59304. Fixes #11287.
1 parent 5ef89b8 commit 4ff60b0

1 file changed

Lines changed: 33 additions & 16 deletions

File tree

source

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3800,6 +3800,7 @@ a.setAttribute('href', 'https://example.com/'); // change the content attribute
38003800

38013801
<ul class="brief">
38023802
<li><dfn data-x-href="https://drafts.csswg.org/css-images/#default-object-size">default object size</dfn></li>
3803+
<li><dfn data-x-href="https://drafts.csswg.org/css-images/#default-sizing">default sizing algorithm</dfn></li>
38033804
<li><dfn data-x-href="https://drafts.csswg.org/css-images/#concrete-object-size">concrete object size</dfn></li>
38043805
<li><dfn data-x-href="https://drafts.csswg.org/css-images/#natural-dimensions">natural dimensions</dfn></li>
38053806
<li><dfn data-x-href="https://drafts.csswg.org/css-images/#natural-height">natural height</dfn></li>
@@ -31280,8 +31281,8 @@ interface <dfn interface>HTMLImageElement</dfn> : <span>HTMLElement</span> {
3128031281
<dt><code data-x=""><var>image</var>.<span subdfn data-x="dom-img-naturalHeight">naturalHeight</span></code></dt>
3128131282

3128231283
<dd>
31283-
<p>These attributes return the natural dimensions of the image, or 0 if the dimensions are not
31284-
known.</p>
31284+
<p>These attributes return the <span>density-corrected natural width and height</span> of the
31285+
image, or 0 if the image is not <i data-x="img-available">available</i>.</p>
3128531286
</dd>
3128631287

3128731288
<dt><code data-x=""><var>image</var>.<span subdfn data-x="dom-img-complete">complete</span></code></dt>
@@ -31329,12 +31330,17 @@ interface <dfn interface>HTMLImageElement</dfn> : <span>HTMLElement</span> {
3132931330
</div>
3133031331

3133131332
<div algorithm>
31332-
<p>The IDL attributes <dfn attribute for="HTMLImageElement"
31333+
<p>The <dfn attribute for="HTMLImageElement"
3133331334
data-x="dom-img-naturalWidth"><code>naturalWidth</code></dfn> and <dfn attribute
31334-
for="HTMLImageElement" data-x="dom-img-naturalHeight"><code>naturalHeight</code></dfn> must return
31335-
the <span>density-corrected natural width and height</span> of the image, in <span
31336-
data-x="'px'">CSS pixels</span>, if the image has <span>density-corrected natural width and
31337-
height</span> and is <i data-x="img-available">available</i>, or else 0. <ref>CSS</ref></p>
31335+
for="HTMLImageElement" data-x="dom-img-naturalHeight"><code>naturalHeight</code></dfn> getter
31336+
steps are:</p>
31337+
31338+
<ol>
31339+
<li><p>If the image is not <i data-x="img-available">available</i>, then return 0.</p></li>
31340+
31341+
<li><p>Return the respective component of the image's <span>density-corrected natural width and
31342+
height</span>, in <span data-x="'px'">CSS pixels</span>. <ref>CSS</ref></p></li>
31343+
</ol>
3133831344
</div>
3133931345

3134031346
<p class="note">Since the <span>density-corrected natural width and height</span> of an image
@@ -32482,25 +32488,36 @@ was an English &lt;a href="/wiki/Music_hall">music hall&lt;/a> singer, ...</code
3248232488
natural width and height</dfn> of an <code>img</code> element <var>img</var>:</p>
3248332489

3248432490
<ol>
32491+
<li><p>Let <var>density</var> be <var>img</var>'s <span>current request</span>'s <span>current
32492+
pixel density</span>.</p></li>
32493+
3248532494
<li>
32486-
<p>Let <var>dim</var> be <var>img</var>'s <span>current request</span>'s <span>preferred
32487-
density-corrected dimensions</span>.</p>
32495+
<p>Let <var>dimensions</var> be <var>img</var>'s <span>current request</span>'s
32496+
<span>preferred density-corrected dimensions</span>.</p>
3248832497

3248932498
<p class="note">The <span>preferred density-corrected dimensions</span> are set in the
3249032499
<span>prepare an image for presentation</span> algorithm based on meta information in the
3249132500
image.</p>
3249232501
</li>
3249332502

32494-
<li><p>If <var>dim</var> is null, set <var>dim</var> to <var>img</var>'s <span>natural
32495-
dimensions</span>.</p></li>
32503+
<li><p>If <var>dimensions</var> is not null, then set
32504+
<var>dimensions</var>'s width to <var>dimensions</var>'s width divided by
32505+
<var>density</var>, set <var>dimensions</var>'s height to <var>dimensions</var>'s height
32506+
divided by <var>density</var>, and return <var>dimensions</var>.</p></li>
32507+
32508+
<li><p>Let <var>intrinsicWidth</var>, <var>intrinsicHeight</var>, and
32509+
<var>intrinsicRatio</var> be <var>img</var>'s <span data-x="natural dimensions">intrinsic
32510+
width, intrinsic height, and intrinsic aspect ratio</span>, if any, respectively.</p></li>
3249632511

32497-
<li><p>Set <var>dim</var>'s width to <var>dim</var>'s width divided by <var>img</var>'s
32498-
<span>current request</span>'s <span>current pixel density</span>.</p></li>
32512+
<li><p>If <var>intrinsicWidth</var> is not absent, then set <var>intrinsicWidth</var> to
32513+
<var>intrinsicWidth</var> divided by <var>density</var>.</p></li>
3249932514

32500-
<li><p>Set <var>dim</var>'s height to <var>dim</var>'s height divided by <var>img</var>'s
32501-
<span>current request</span>'s <span>current pixel density</span>.</p></li>
32515+
<li><p>If <var>intrinsicHeight</var> is not absent, then set <var>intrinsicHeight</var> to
32516+
<var>intrinsicHeight</var> divided by <var>density</var>.</p></li>
3250232517

32503-
<li><p>Return <var>dim</var>.</p></li>
32518+
<li><p>Return the result of applying the <span>default sizing algorithm</span> with
32519+
<var>intrinsicWidth</var>, <var>intrinsicHeight</var>, and <var>intrinsicRatio</var>, using a
32520+
<span>default object size</span> of 300 by 150.</p></li>
3250432521
</ol>
3250532522
</div>
3250632523

0 commit comments

Comments
 (0)