Skip to content

Ensure that img dimensions are derived synchronously - #13057

Merged
zcorpan merged 11 commits into
whatwg:mainfrom
TimvdLippe:fix-dimensions-of-images
Nov 6, 2026
Merged

zcorpan merged 11 commits into
whatwg:mainfrom
TimvdLippe:fix-dimensions-of-images

Conversation

@TimvdLippe

@TimvdLippe TimvdLippe commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Browsers apply the width and height attributes of a matching source element in a picture element to the img element as soon as the DOM changes, so the computed style reflects them right away. The spec only updated the img element's dimension attribute source when selecting an image source, which happens in a microtask.

Update the image data now updates the source set synchronously, before any early return. Update the source set now resets the dimension attribute source to the img element before looking at source elements, so removing the source element or moving the img element out of the picture element reverts it. When obtaining images on demand, relevant mutations also update the source set.

Selecting the image URL stays in the microtask, since moving it could fire the error event too soon.

Fixes #8207.

(See WHATWG Working Mode: Changes for more details.)


/images.html ( diff )

Browsers immediately resolve dimensions for an `<img>` that is a child
of a `<picture>` with a preceding `<source>` sibling. This means that
adding a `<source>` and then computing the style of the `<img>` should
use the dimensions of the `<source>`.

However, the spec didn't account for this, as it first schedules a task. That
way, only after the task would be resolved, the dimensions would be
computed.

Instead, the sourceset should synchronously be updated. In the task the
sourceset selects an image source, which again updates the sourceset.
But moving the "select a source set" before the task could mean that
an "error" event would be fired to soon.

As such, only updating the sourceset but leaving the rest as-is ensures
the relevant new tests pass and no regressions are introduced.
@zcorpan zcorpan changed the title Ensure that img dimensions are derived synchronously Ensure that img dimensions are derived synchronously Oct 12, 2026
@zcorpan

zcorpan commented Oct 12, 2026

Copy link
Copy Markdown
Member

Thanks for the PR!

AI review

Reviewed at dca1812.

Blockers

  • source:33799 (update the source set, the "child is el" branch): when no source matches, nothing resets dimension attribute source back to the img. So doing this synchronously still doesn't make the spec match picture-aspect-ratio.html's "Make sure style gets invalidated correctly when the source gets removed". After removeChild(source), the img keeps the detached source as its dimension attribute source, which gives "auto 200 / 100" where the test expects "auto 250 / 50". Consider setting dimension attribute source to el at the start of update the source set, which is what Chromium does (source_ = nullptr in FindBestFitImageFromPictureParent, html_image_element.cc:510).

Suggestions

  • source:33237-33243: the new step comes after the "cannot support images" return (source:33156) and after the list-of-available-images abort (source:33231), so neither path updates the source set. Case: <picture><source srcset=x width=100 height=100><img src=a></picture> with a cached. Moving the img out of the picture aborts early, and the stale source keeps mapping width/height. With images disabled, source width/height never apply at all. Chromium runs SelectSourceURL (which sets source_) before ImageLoader::UpdateFromElement, so images being disabled doesn't affect it there. Consider moving the step up to just after step 1.
  • source:32980: when obtaining images on demand, update the image data doesn't run on relevant mutations, so the dimension attribute source stays stale there too. Either update the source set directly from the relevant-mutations hook, or confirm the on-demand case is intentionally left out.
  • source:33240-33242: the note links img-dimensions, which is the width/height getters algorithm. What actually changes is the dimension attribute source mapping that rendering and getComputedStyle() see. Consider linking concept-img-dimension-attribute-source instead.

Nits

  • Commit message: "fired to soon" should be "too soon", "sourceset" should be "source set".

("when obtaining images on demand" is basically images disabled, but the user can choose to load specific images from the context menu or similar.)

@TimvdLippe

Copy link
Copy Markdown
Contributor Author

Confirmed with servo/servo#48902 that indeed those fixes makes all tests in /html/rendering/replaced-elements/attributes-for-embedded-content-and-images/picture-aspect-ratio.html pass. Running the full test suite to see if it regresses in any other way, but I think we are good to go.

aniebietafia pushed a commit to aniebietafia/servo that referenced this pull request Oct 14, 2026
…ved (servo#48902)

Based on reviewer feedback in
whatwg/html#13057

Testing: new WPT passes

Signed-off-by: Tim van der Lippe <[email protected]>
@zcorpan

zcorpan commented Nov 4, 2026

Copy link
Copy Markdown
Member
AI review

Reviewed at 46c7352

All four points from the previous review are fixed. Lint passes.

Suggestions

  • source:33787 and source:33809-33810 (update the source set): the PR adds "Let img be null" and sets it inside the new img branch, but the old "Let img be el if el is an img element, otherwise null" step is still there. That declares img twice. Consider deleting the old step.
  • source:32985: "update the source set for <var>img</var>" has no img variable in that paragraph, so the build will warn about a single-use var. Consider "for the img element", and maybe split the sentence so the source set update and the state reset read as two separate requirements.

Nits

  • source:33795-33797: the "Set el's dimension attribute source to el" step is missing </li>.
  • source:33888-33891: the step now ends <var>child</var>.</li>, so it's missing </p>.
  • source:33799-33806: the nested "If el's parent node is a picture element:" has only one sub-step. It could be inline: "If …, then replace …".
  • source:33160-33162: "immediately is set to a source element sibling" should be "is immediately set". Also, the step now resets the dimension attribute source to the img as well, so maybe say "up to date" rather than only mentioning the source case.

@zcorpan
zcorpan merged commit 5fcc231 into whatwg:main Nov 6, 2026
2 checks passed
aphrody-dev pushed a commit to aphrody-labs/browser that referenced this pull request Nov 16, 2026
…ved (#48902)

Based on reviewer feedback in
whatwg/html#13057

Testing: new WPT passes

Signed-off-by: Tim van der Lippe <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Should we select an image source synchronously when updating the image data?

2 participants