Bug 1965560 part 4: Adjust img.width/img.height internals to return the naturalWidth and naturalHeight when the img is not being rendered. a=RyanVM

This is to implement the spec text here, where the spec defines the IDL
attributes width and height in terms of the natural width and height when the
image is not being rendered:
https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-width

This patch diverges from that^ spec text in two key ways, for interoperability,
as described in this github comment:
https://github.com/whatwg/html/issues/11287#issuecomment-2923467541

I've called these out in the code itself, and I intend to update the spec to
address this divergence in that whatwg/html github-issue.

Original Revision: https://phabricator.services.mozilla.com/D251988

Differential Revision: https://phabricator.services.mozilla.com/D267128
This commit is contained in:
Daniel Holbert
2025-10-02 00:59:27 +00:00
committed by rvandermeulen@mozilla.com
parent 0a2d3789fb
commit 3d163cc822
2 changed files with 19 additions and 209 deletions

View File

@@ -1347,10 +1347,28 @@ CSSIntSize nsImageLoadingContent::GetWidthHeightForImage() {
CSSIntSize size;
nsCOMPtr<imgIContainer> image;
if (mCurrentRequest) {
if (StaticPrefs::image_natural_size_fallback_enabled()) {
// Our image is not rendered (we don't have any frame); so we should should
// return the natural size, per:
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-width
//
// Note that the spec says to use the "density-corrected natural width and
// height of the image", but we don't do that -- we specifically request
// the NaturalSize *without* density-correction here. This handles a case
// where browsers deviate from the spec in an interoperable way, which
// hopefully we'll address in the spec soon. See case (2) in this comment
// for more:
// https://github.com/whatwg/html/issues/11287#issuecomment-2923467541
size = NaturalSize(DoDensityCorrection::No);
} else if (mCurrentRequest) {
mCurrentRequest->GetImage(getter_AddRefs(image));
}
// If we have width or height attrs, we'll let those stomp on whatever
// NaturalSize we may have gotten above. This handles a case where browsers
// deviate from the spec in an interoperable way, which hopefully we'll
// address in the spec soon. See case (1) in this comment for more:
// https://github.com/whatwg/html/issues/11287#issuecomment-2923467541
const nsAttrValue* value;
if ((value = element->GetParsedAttr(nsGkAtoms::width)) &&
value->Type() == nsAttrValue::eInteger) {

View File

@@ -2,211 +2,3 @@
# TODO(dholbert): Look into this crash in bug 1969597
expected:
if (processor == "x86") and (os == "linux"): CRASH
# These "when not rendered" expected-failures are all bug 1965560:
[SVG image, no natural dimensions (when not rendered)]
expected: FAIL
[SVG image with width attr, no natural dimensions (when not rendered)]
expected: FAIL
[SVG image with height attr, no natural dimensions (when not rendered)]
expected: FAIL
[SVG image, percengage natural dimensions (when not rendered)]
expected: FAIL
[SVG image, negative percengage natural dimensions (when not rendered)]
expected: FAIL
[SVG image, with natural width (when not rendered)]
expected: FAIL
[SVG image, with natural height (when not rendered)]
expected: FAIL
[SVG image, with natural width of 0 (when not rendered)]
expected: FAIL
[SVG image, with natural height of 0 (when not rendered)]
expected: FAIL
[SVG image, with natural width being negative (when not rendered)]
expected: FAIL
[SVG image, with natural height being negative (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, and aspect ratio from viewBox (when not rendered)]
expected: FAIL
[SVG image, percengage natural dimensions, and aspect ratio from viewBox (when not rendered)]
expected: FAIL
[SVG image, negative percengage natural dimensions, and aspect ratio from viewBox (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, viewBox with 0 width/height (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, viewBox with 0 width (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, viewBox with 0 height (when not rendered)]
expected: FAIL
[SVG image, with natural width, viewBox with 0 width/height (when not rendered)]
expected: FAIL
[SVG image, with natural width, viewBox with 0 width (when not rendered)]
expected: FAIL
[SVG image, with natural width, viewBox with 0 height (when not rendered)]
expected: FAIL
[SVG image, with natural height, viewBox with 0 width/height (when not rendered)]
expected: FAIL
[SVG image, with natural height, viewBox with 0 width (when not rendered)]
expected: FAIL
[SVG image, with natural height, viewBox with 0 height (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image with width attr, no natural dimensions (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image with height attr, no natural dimensions (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, percengage natural dimensions (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, negative percengage natural dimensions (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural width (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural height (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural width of 0 (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural height of 0 (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural width being negative (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural height being negative (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, and aspect ratio from viewBox (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, percengage natural dimensions, and aspect ratio from viewBox (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, negative percengage natural dimensions, and aspect ratio from viewBox (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, viewBox with 0 width/height (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, viewBox with 0 width (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, viewBox with 0 height (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural width, viewBox with 0 width/height (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural width, viewBox with 0 width (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural width, viewBox with 0 height (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural height, viewBox with 0 width/height (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural height, viewBox with 0 width (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, with natural height, viewBox with 0 height (with srcset/1x) (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image with width attr, no natural dimensions (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image with height attr, no natural dimensions (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, percengage natural dimensions (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, negative percengage natural dimensions (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural width (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural height (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural width of 0 (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural height of 0 (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural width being negative (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural height being negative (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, and aspect ratio from viewBox (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, percengage natural dimensions, and aspect ratio from viewBox (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, negative percengage natural dimensions, and aspect ratio from viewBox (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, viewBox with 0 width/height (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, viewBox with 0 width (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, no natural dimensions, viewBox with 0 height (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural width, viewBox with 0 width/height (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural width, viewBox with 0 width (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural width, viewBox with 0 height (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural height, viewBox with 0 width/height (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural height, viewBox with 0 width (with srcset/2x) (when not rendered)]
expected: FAIL
[SVG image, with natural height, viewBox with 0 height (with srcset/2x) (when not rendered)]
expected: FAIL