From baae27864d8a49e77146a063e9269269d6637be9 Mon Sep 17 00:00:00 2001 From: Noam Rosenthal Date: Fri, 23 Oct 2026 00:03:32 -0700 Subject: [PATCH] Use the dimension attribute aspect ratio for srcset source selection Third part of https://github.com/whatwg/html/pull/13053, behind the `SrcsetTwoDimensionalSizes` runtime flag. For srcset candidates without an `h` descriptor, use the dimension attribute aspect ratio (from the `width`/`height` attributes of the ``, or of the `` when it has one of them) as the source aspect ratio when computing the source concrete width for a two-dimensional size. This is also applied in the preload scanner. Since these attributes now affect source selection, changing them on an `` or a previous sibling `` is a relevant mutation, so re-run source selection. To keep that re-selection side-effect free when the selected URL did not change, `SelectSourceURL()` now only resets the layout disposition when it actually updates the image loader. Bug: 572724710 Change-Id: I2317a0b452ef8f0ca2438c41d8574312d19b9135 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8563321 Reviewed-by: Stefan Zager Commit-Queue: Noam Rosenthal Cr-Commit-Position: refs/heads/main@{#1724591} --- .../sizes/sizes-2d.tentative.html | 38 ++++++ .../sizes/sizes-auto-2d.tentative.html | 114 +++++++++++++++++- 2 files changed, 151 insertions(+), 1 deletion(-) diff --git a/html/semantics/embedded-content/the-img-element/sizes/sizes-2d.tentative.html b/html/semantics/embedded-content/the-img-element/sizes/sizes-2d.tentative.html index 17e5bef0e9efd6..a00c724263479a 100644 --- a/html/semantics/embedded-content/the-img-element/sizes/sizes-2d.tentative.html +++ b/html/semantics/embedded-content/the-img-element/sizes/sizes-2d.tentative.html @@ -92,8 +92,14 @@ {sizes: '(min-width: 0px) cover 10px 1000px 5px, 1px', h: square, ref: 'ref1'}, {sizes: '(min-width: 0px) contain 1000px, cover 10px 1000px', h: square, ref: 'ref2'}, + // Without 'h' descriptors, the dimension attribute aspect ratio is used: + // max(10px, 1000px * 1) = 1000px. + {sizes: 'cover 10px 1000px', width: '1', height: '1', h: ['', ''], ref: 'ref2'}, + {sizes: '(min-width: 0px) cover 10px 1000px, 1px', width: '1', height: '1', h: ['', ''], ref: 'ref2'}, // With no aspect ratio at all, the width is used. {sizes: 'cover 10px 1000px', h: ['', ''], ref: 'ref1'}, + // 'h' descriptors take precedence over the dimension attribute aspect ratio. + {sizes: 'cover 10px 10px', width: '1', height: '1', h: [' 50h', ' 51h'], ref: 'ref1'}, // An explicit fit in sizes is not affected by the computed 'object-fit'. {sizes: 'contain 10px 1000px', style: 'object-fit: cover', h: square, ref: 'ref1'}, {sizes: 'cover 10px 1000px', style: 'object-fit: contain', h: square, ref: 'ref2'}, @@ -177,6 +183,38 @@ test_picture(obj, i); test_link_preload(obj, i); } + + // A source without width/height attributes uses the img's dimension + // attribute aspect ratio. + { + const picture = document.createElement('picture'); + const source = document.createElement('source'); + const img = document.createElement('img'); + source.sizes = 'cover 10px 1000px'; + source.srcset = srcsetFor('picture-img-dims', ['', '']); + img.width = 1; + img.height = 1; + picture.appendChild(source); + picture.appendChild(img); + document.body.appendChild(picture); + check(picture, img, 'ref2'); + } + + // A source with a width or height attribute does not use the img's. + { + const picture = document.createElement('picture'); + const source = document.createElement('source'); + const img = document.createElement('img'); + source.sizes = 'cover 10px 1000px'; + source.srcset = srcsetFor('picture-source-width-only', ['', '']); + source.setAttribute('width', '1'); + img.width = 1; + img.height = 1; + picture.appendChild(source); + picture.appendChild(img); + document.body.appendChild(picture); + check(picture, img, 'ref1'); + } done(); } diff --git a/html/semantics/embedded-content/the-img-element/sizes/sizes-auto-2d.tentative.html b/html/semantics/embedded-content/the-img-element/sizes/sizes-auto-2d.tentative.html index 1cda21709ca504..c9b3a1374b7d0a 100644 --- a/html/semantics/embedded-content/the-img-element/sizes/sizes-auto-2d.tentative.html +++ b/html/semantics/embedded-content/the-img-element/sizes/sizes-auto-2d.tentative.html @@ -61,7 +61,9 @@ {loading: 'lazy', sizes: 'auto', width: '1', height: '1', style: 'object-fit: none', h: ['', ''], ref: 'ref1'}, {loading: 'lazy', sizes: 'auto', width: '1', height: '1', style: 'object-fit: contain', h: ['', ''], ref: 'ref1'}, {loading: 'lazy', sizes: 'auto', width: '1', height: '1', style: 'object-fit: scale-down', h: ['', ''], ref: 'ref1'}, - // Without 'h' descriptors, the width (10px) is used. + // With object-fit: cover and aspect ratio 1, max(10px, 100px * 1) = 100px. + {loading: 'lazy', sizes: 'auto', width: '1', height: '1', style: 'object-fit: cover', h: ['', ''], ref: 'ref2'}, + // Without width/height attributes or 'h' descriptors, the width (10px) is used. {loading: 'lazy', sizes: 'auto', style: 'object-fit: cover', h: ['', ''], ref: 'ref1'}, {loading: 'lazy', sizes: 'auto', width: '1', style: 'object-fit: cover', h: ['', ''], ref: 'ref1'}, {loading: 'lazy', sizes: 'auto', height: '1', style: 'object-fit: cover', h: ['', ''], ref: 'ref1'}, @@ -71,6 +73,9 @@ // ... and min(10px, 100px * 50) = min(10px, 100px * 25.5) = 10px. {loading: 'lazy', sizes: 'auto', style: 'object-fit: contain', h: [' 1h', ' 2h'], ref: 'ref1'}, {loading: 'lazy', sizes: 'auto', style: 'object-fit: scale-down', h: [' 1h', ' 2h'], ref: 'ref1'}, + // 'h' descriptors take precedence over the dimension attribute aspect ratio: + // aspect ratios are 50/500 = 0.1 and 51/510 = 0.1, so max(10px, 100px * 0.1) = 10px. + {loading: 'lazy', sizes: 'auto', width: '10', height: '1', style: 'object-fit: cover', h: [' 500h', ' 510h'], ref: 'ref1'}, // Non-lazy img falls back to 1px and does not use CSS object-fit. {sizes: 'auto, 1px', width: '1', height: '1', style: 'object-fit: cover', h: [' 1h', ' 2h'], ref: 'ref1'}, ]; @@ -98,6 +103,63 @@ test_img(obj, i); } + // A without width/height attributes uses the 's dimension + // attribute aspect ratio when sizes=auto. + { + const picture = document.createElement('picture'); + const source = document.createElement('source'); + const img = document.createElement('img'); + source.srcset = srcsetFor('picture-img-dims', ['', '']); + img.loading = 'lazy'; + img.sizes = 'auto'; + img.width = 1; + img.height = 1; + img.style = 'object-fit: cover'; + picture.appendChild(source); + picture.appendChild(img); + document.body.appendChild(picture); + check(picture, img, 'ref2'); + } + + // A with width and height attributes uses its own dimension + // attribute aspect ratio. + { + const picture = document.createElement('picture'); + const source = document.createElement('source'); + const img = document.createElement('img'); + source.srcset = srcsetFor('picture-source-dims', ['', '']); + source.setAttribute('width', '1'); + source.setAttribute('height', '1'); + img.loading = 'lazy'; + img.sizes = 'auto'; + img.width = 1; + img.height = 100; + img.style = 'object-fit: cover'; + picture.appendChild(source); + picture.appendChild(img); + document.body.appendChild(picture); + check(picture, img, 'ref2'); + } + + // A with only a width attribute has no dimension attribute aspect + // ratio and does not fall back to the 's. + { + const picture = document.createElement('picture'); + const source = document.createElement('source'); + const img = document.createElement('img'); + source.srcset = srcsetFor('picture-source-width-only', ['', '']); + source.setAttribute('width', '1'); + img.loading = 'lazy'; + img.sizes = 'auto'; + img.width = 1; + img.height = 1; + img.style = 'object-fit: cover'; + picture.appendChild(source); + picture.appendChild(img); + document.body.appendChild(picture); + check(picture, img, 'ref1'); + } + // Dynamic change to computed 'object-fit' on an auto-sized img triggers // source re-selection. async_promise_test(async (t) => { @@ -118,6 +180,56 @@ assert_equals(baseUrl(img.currentSrc), ref2); }, 'Changing computed object-fit on an auto-sized img updates currentSrc'); + // Dynamic change to width/height attributes on an auto-sized img triggers + // source re-selection. + async_promise_test(async (t) => { + const ref1 = document.getElementById('ref1').currentSrc.split('?')[0]; + const ref2 = document.getElementById('ref2').currentSrc.split('?')[0]; + const img = document.createElement('img'); + img.loading = 'lazy'; + img.sizes = 'auto'; + img.width = 1; + img.height = 100; + img.style = 'object-fit: cover'; + img.srcset = srcsetFor('dynamic-img-dims', ['', '']); + document.body.appendChild(img); + await waitForLoad(img); + assert_equals(baseUrl(img.currentSrc), ref1); + + // Changing a dimension attribute without changing the selected candidate + // URL should not trigger a reload or other side effects. + let unexpectedEvent = false; + img.onload = img.onerror = () => { unexpectedEvent = true; }; + img.height = 50; + await new Promise(resolve => t.step_timeout(resolve, 50)); + assert_false(unexpectedEvent); + assert_equals(baseUrl(img.currentSrc), ref1); + + const reloaded = waitForLoad(img); + img.height = 1; + await reloaded; + assert_equals(baseUrl(img.currentSrc), ref2); + }, 'Changing dimension attributes on an auto-sized img updates currentSrc'); + + // Changing a dimension attribute when the selected candidate URL does not + // change must not switch an unresolvable no-alt image to fallback layout. + async_promise_test(async (t) => { + const iframe = document.createElement('iframe'); + t.add_cleanup(() => iframe.remove()); + iframe.srcdoc = + '' + + ''; + const loaded = new Promise(resolve => { iframe.onload = resolve; }); + document.body.appendChild(iframe); + await loaded; + + const img = iframe.contentDocument.querySelector('img'); + assert_equals(img.offsetWidth, 100); + + img.width = 200; + assert_equals(img.offsetWidth, 200); + }, 'Changing dimension attributes when selected source URL is unchanged preserves layout disposition'); + done(); }