Skip to content

Conversation

@anediaz
Copy link

@anediaz anediaz commented Dec 2, 2025

Summary

Define the ordering of LayoutShift.sources to be sorted by impact area in descending order.
This ensures that the element which contributed most to the layout shift appears first in the array.

Motivation

This change aligns the specification with:

Without this clarification, developers cannot rely on any consistent ordering of the sources array, making it harder to programmatically identify the most impactful elements causing layout shifts.

Changes

This PR adds explicit documentation and normative algorithm steps to specify that layout shift sources are sorted by impact area:

  1. Source Attribution section: Added non-normative explanation that sources are sorted by impact area in descending order
  2. Usage Example: Enhanced with comments demonstrating how to access the largest contributing element
  3. LayoutShift Interface: Added formal documentation for the sources attribute explaining the ordering guarantee
  4. Algorithm: Added normative sorting step in the "report the layout shift sources" algorithm to sort the list by node impact region area in descending order

Preview | Diff

Copy link
Collaborator

@mmocny mmocny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for also updating MDN, and the examples here.

@mmocny
Copy link
Collaborator

mmocny commented Dec 2, 2025

I've approved this but I'm not a maintainer of this wicg repo-- I'll talk to @yoavweiss

@mmocny
Copy link
Collaborator

mmocny commented Dec 4, 2025

@anediaz -- the build is failing with some errors

I think the main one is:

WARNING: Multiple elements have the same id 'dom-layoutshift-sources':
  <dfn> on line 460:5, <dfn> on line 541:25
Deduping, but this ID may not be stable across revisions.
  FATAL ERROR: Multiple local 'attribute' <dfn>s for 'LayoutShift' have the same linking text 'sources'.

Could you try to fix that first?

aarongable pushed a commit to chromium/chromium that referenced this pull request Dec 4, 2025
Sort the LayoutShift.sources array by per-source impact area in
descending order (largest first).

LayoutShift.sources currently returns up to five sources in an
undefined order. This makes it difficult for diagnostics and tooling
to reliably identify the most impactful contributor to a layout shift.
Sorting ensures that sources[0] always corresponds to the largest
impact area.

Impact area is computed as the union of previousRect and currentRect
for each source, matching the semantics used by Cumulative Layout
Shift (CLS) calculation. This provides a deterministic ordering and
makes the API more meaningful for users.

The WICG spec PR: WICG/layout-instability#126

Bug: 450950606
Change-Id: Iec3530250a8c29f44203f0e17b7789806323dd48
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7196706
Commit-Queue: Ane Diaz De Tuesta <[email protected]>
Reviewed-by: Michal Mocny <[email protected]>
Reviewed-by: Scott Haseley <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1554088}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Dec 4, 2025
Sort the LayoutShift.sources array by per-source impact area in
descending order (largest first).

LayoutShift.sources currently returns up to five sources in an
undefined order. This makes it difficult for diagnostics and tooling
to reliably identify the most impactful contributor to a layout shift.
Sorting ensures that sources[0] always corresponds to the largest
impact area.

Impact area is computed as the union of previousRect and currentRect
for each source, matching the semantics used by Cumulative Layout
Shift (CLS) calculation. This provides a deterministic ordering and
makes the API more meaningful for users.

The WICG spec PR: WICG/layout-instability#126

Bug: 450950606
Change-Id: Iec3530250a8c29f44203f0e17b7789806323dd48
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7196706
Commit-Queue: Ane Diaz De Tuesta <[email protected]>
Reviewed-by: Michal Mocny <[email protected]>
Reviewed-by: Scott Haseley <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1554088}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Dec 4, 2025
Sort the LayoutShift.sources array by per-source impact area in
descending order (largest first).

LayoutShift.sources currently returns up to five sources in an
undefined order. This makes it difficult for diagnostics and tooling
to reliably identify the most impactful contributor to a layout shift.
Sorting ensures that sources[0] always corresponds to the largest
impact area.

Impact area is computed as the union of previousRect and currentRect
for each source, matching the semantics used by Cumulative Layout
Shift (CLS) calculation. This provides a deterministic ordering and
makes the API more meaningful for users.

The WICG spec PR: WICG/layout-instability#126

Bug: 450950606
Change-Id: Iec3530250a8c29f44203f0e17b7789806323dd48
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7196706
Commit-Queue: Ane Diaz De Tuesta <[email protected]>
Reviewed-by: Michal Mocny <[email protected]>
Reviewed-by: Scott Haseley <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1554088}
@anediaz anediaz force-pushed the define-layoutshift-sources-impact-area branch from 0252b44 to ed6a3d4 Compare December 4, 2025 21:11
@anediaz
Copy link
Author

anediaz commented Dec 4, 2025

@anediaz -- the build is failing with some errors

I think the main one is:

WARNING: Multiple elements have the same id 'dom-layoutshift-sources':
  &lt;dfn> on line 460:5, &lt;dfn> on line 541:25
Deduping, but this ID may not be stable across revisions.
  FATAL ERROR: Multiple local 'attribute' &lt;dfn>s for 'LayoutShift' have the same linking text 'sources'.

Could you try to fix that first?

Thanks for the help! It seems to be passing now :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants