@@ -151,7 +151,9 @@ Source attribution {#source-attribution}
151151
152152In addition to the layout shift value, the API reports a sampling of up to five
153153DOM elements whose layout shifts most substantially contributed to the layout
154- shift value for an animation frame.
154+ shift value for an animation frame. The <a href="#dom-layoutshift-sources">sources</a>
155+ are sorted by impact area in descending order, so the first element represents
156+ the element that contributed most to the layout shift.
155157
156158It is possible that the true "root cause" of instability will be only
157159indirectly related to the DOM element that experiences a layout shift.
@@ -186,6 +188,12 @@ Usage example {#example}
186188 timestamp: entry.startTime
187189 });
188190 cumulativeLayoutShiftScore += entry.value;
191+
192+ // Sources are sorted by impact area in descending order.
193+ // The first element contributed most to the layout shift.
194+ if (entry.sources && entry.sources.length > 0) {
195+ console.log('Largest contributing element:' , entry.sources[0] .node);
196+ }
189197 }
190198 }
191199
@@ -449,6 +457,11 @@ events are also not excluding inputs.
449457All attributes have the values which are assigned to them by the steps to
450458<a>report the layout shift</a> .
451459
460+ The <dfn attribute for=LayoutShift>sources</dfn> attribute returns a
461+ {{FrozenArray}} of {{LayoutShiftAttribution}} objects, sorted in descending order
462+ by impact area. The first element has the largest <a>node impact region</a> and
463+ represents the element that contributed most to the layout shift.
464+
452465A user agent implementing the Layout Instability API must include
453466<code> "layout-shift"</code> in {{PerformanceObserver/supportedEntryTypes}} for
454467<a href="https://html.spec.whatwg.org/multipage/window-object.html#the-window-object">Window</a>
@@ -525,7 +538,7 @@ When asked to <dfn export>report the layout shift</dfn> for an active
525538 1. Set |newEntry|'s <dfn attribute for=LayoutShift>hadRecentInput</dfn>
526539 attribute to <code> true</code> if {{LayoutShift/lastInputTime}} is less
527540 than 500 milliseconds in the past, and <code> false</code> otherwise.
528- 1. Set |newEntry|'s <dfn attribute for= LayoutShift>sources</dfn> attribute
541+ 1. Set |newEntry|'s {{ LayoutShift/sources}} attribute
529542 to the result of invoking the algorithm to <a>report the layout shift
530543 sources</a> for |D|.
531544 1. <a href="https://w3c.github.io/performance-timeline/#dfn-queue-a-performanceentry">Queue the PerformanceEntry</a>
@@ -568,6 +581,15 @@ When asked to <dfn>report the layout shift sources</dfn> for an active
568581 <a href="https://infra.spec.whatwg.org/#list-replace">replace</a>
569582 |smallest| with |N| in |C|.
570583
584+ 1. <a href="https://infra.spec.whatwg.org/#list-sort-in-descending-order">Sort</a>
585+ |C| in descending order, with |a| being less than |b| if the area
586+ of the <a>node impact region</a> of |a| is less than the area
587+ of the <a>node impact region</a> of |b|.
588+
589+ NOTE: This ensures that the <a href="#dom-layoutshift-sources">sources</a>
590+ attribute exposes layout shift sources in descending order by impact area,
591+ with the element that contributed most to the layout shift appearing first.
592+
5715931. Return a {{FrozenArray}} of {{LayoutShiftAttribution}} objects created
572594 by running the algorithm to <a>create the attribution</a> once
573595 for each member of |C|.
0 commit comments