From 06358fd7cc500829c9b849c0ec04838f2e476d1a Mon Sep 17 00:00:00 2001 From: Ian Clelland Date: Mon, 15 Jan 2024 12:12:20 -0500 Subject: [PATCH] Rebase LCP on top of w3c/paint-timing#100 (#121) This eliminates the dependency on the still-incubating ElementTiming spec, by using concepts newly-introduced into Paint Timing. The concept of LCP-eligibility is also added, to encapsulate the various heuristics used to filter out elements which are not the primary content of the page (though which would still be considered "contentful" for purposes of calculating First Contentful Paint time.) Closes: #86, #113 --- index.bs | 193 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 129 insertions(+), 64 deletions(-) diff --git a/index.bs b/index.bs index 7b677f1..45c22a7 100644 --- a/index.bs +++ b/index.bs @@ -51,6 +51,15 @@ urlPrefix: https://html.spec.whatwg.org/multipage/webappapis.html; spec: html; text: height; url: #dom-img-height; urlPrefix: https://drafts.csswg.org/cssom-view/; spec: CSSOM-VIEW; type: dfn; text: visual viewport; url: visual-viewport; +urlPrefix: https://w3c.github.io/paint-timing/; spec: PAINT-TIMING; + type: dfn; url:#the-paint-timing-steps; text: the paint timing steps + type: dfn; url:#set-of-owned-text-nodes; text: set of owned text nodes + type: dfn; url:#exposed-for-paint-timing; text: exposed for paint timing + type: dfn; url:#pending-image-record; text: pending image record + type: dfn; for:pending image record; url:#pending-image-record-element; text: element + type: dfn; for:pending image record; url:#pending-image-record-loadtime; text: loadTime + type: dfn; for:pending image record; url:#pending-image-record-request; text: request + type: dfn; url:#timing-eligible; text: timing-eligible Introduction {#sec-intro} @@ -59,22 +68,19 @@ Introduction {#sec-intro} This section is non-normative. The LargestContentfulPaint API enables developers to gain visibility into the loading and rendering process of the web pages, in order for them to be able to optimize it. -Developers today don't have a reliable metric that correlates with their user's visual rendering experience. Existing metrics such as First Paint and First Contentful Paint focus on initial rendering, but don't take into account the importance of the painted content, and therefore may indicate times in which the user still does not consider the page useful. +Developers need a reliable metric that correlates with their user's visual rendering experience. Paint loading metrics such as First Paint and First Contentful Paint focus on initial rendering, but don't take into account the importance of the painted content, and therefore may indicate times in which the user still does not consider the page useful. -Largest Contentful Paint (LCP) aims to be a new page-load metric that: -* better correlates with user experience than the existing page-load metrics +Largest Contentful Paint (LCP) aims to be a page-load metric that: +* better correlates with user experience than First Paint and First Contentful Paint * is easy to understand and reason about * reduces the chance of gaming The largest paint during the loading process of the page is likely to signify a meaningful event from the user's perspective, and is therefore something we want to expose by default to developers, enabling performance teams, analytics providers and lab-based measurement tools to collect those metrics without requiring extra annotation work by the folks creating the content itself. -The API relies heavily on [[ELEMENT-TIMING]], which can be thought of as the low-level primitive that this high-level feature is built on top of. For cases where the content creators are willing to annotate their content and indicate the important points in the page's loading cycle, Element Timing is the API that will provide them more control over the elements that get reported. +The API relies heavily on concepts defined in [[PAINT-TIMING]], which can be thought of as the low-level primitive that this high-level feature is built on top of. For cases where the content creators are willing to annotate their content and indicate the important points in the page's loading cycle, [[ELEMENT-TIMING]] is the API that will provide them more control over the elements that get reported. -Elements exposed {#sec-elements-exposed} ------------------------- - -The Largest Contentful Paint API will only expose element types that are already exposed by the Element Timing API. In this case, there is no need to annotate them with the elementtiming attribute. +NOTE: The Largest Contentful Paint API will only expose elements that are [=timing-eligible=]. Note that unlike Element Timing, there is no need to annotate elements in order to have them be eligible for Largest Contentful Paint. Largest content {#sec-largest-content} ------------------------ @@ -109,6 +115,24 @@ The LargestContentfulPaint API is based on heuristics. As such, it is error pron * To account for image carousels, content is still considered as the largest even if it's removed. This presents problems for websites with splash screens that use large content as placeholders. +Terminology {#sec-terminology} +============================== + +A largest contentful paint candidate is a [=struct=] containing the +following members: + +* element, an [=/element=] +* request, a [=/Request=] or null + +An [=largest contentful paint candidate=] |candidate| is eligible to be largest contentful paint if it meets the +following criteria: + +* |candidate|'s [=largest contentful paint candidate/element=]'s opacity is > 0 +* |candidate|'s [=largest contentful paint candidate/element=] is a text node, or |candidate|'s [=largest contentful paint candidate/request=]'s + [=/response=]'s content length in bytes is >= |candidate|'s [=largest contentful paint candidate/element=]'s [=effective visual size=] * 0.004 + + Note: This heuristic tests whether the image resource contains sufficient data to be seen as contentful to the user. It compares the transferred file size with the number of pixels which are actually produced, after decoding and any image scaling is applied. Images which encode a very large number of pixels with in a very small number of bytes are typically low-content backgrounds, gradients, and the like, and are not considered as [=largest contentful paint candidates=]. + Largest Contentful Paint {#sec-largest-contentful-paint} ======================================= @@ -131,32 +155,36 @@ interface LargestContentfulPaint : PerformanceEntry { Each {{LargestContentfulPaint}} object has these associated concepts: -* A renderTime, initially set to 0. -* A size, initially set to 0. -* A loadTime, initially set to 0. -* An id, initially set to the empty string. -* A url, initially set to the empty string. -* An element containing the associated {{Element}}, initially set to null. +* A renderTime, initially set to 0. +* A size, initially set to 0. +* A loadTime, initially set to 0. +* An id, initially set to the empty string. +* A url, initially set to the empty string. +* An element containing the associated {{Element}}, initially set to null. The {{PerformanceEntry/entryType}} attribute's getter must return the {{DOMString}} "largest-contentful-paint". The {{PerformanceEntry/name}} attribute's getter must return the empty string. -The {{PerformanceEntry/startTime}} attribute's getter must return the value of this's renderTime if it is not 0, and the value of this's loadTime otherwise. +The {{PerformanceEntry/startTime}} attribute's getter must return the value of [=this=]'s renderTime if it is not 0, and the value of [=this=]'s [=LargestContentfulPaint/loadTime=] otherwise. The {{PerformanceEntry/duration}} attribute's getter must return 0. -The {{LargestContentfulPaint/renderTime}} attribute must return the value of this's renderTime. +The {{LargestContentfulPaint/renderTime}} attribute must return the value of [=this=]'s [=LargestContentfulPaint/renderTime=]. -The {{LargestContentfulPaint/loadTime}} attribute must return the value of this's loadTime. +The {{LargestContentfulPaint/loadTime}} attribute must return the value of [=this=]'s [=LargestContentfulPaint/loadTime=]. -The {{LargestContentfulPaint/size}} attribute must return the value of this's size. +The {{LargestContentfulPaint/size}} attribute must return the value of [=this=]'s [=LargestContentfulPaint/size=]. -The {{LargestContentfulPaint/id}} attribute must return the value of this's id. +The {{LargestContentfulPaint/id}} attribute must return the value of [=this=]'s [=LargestContentfulPaint/id=]. -The {{LargestContentfulPaint/url}} attribute must return the value of this's url. +The {{LargestContentfulPaint/url}} attribute must return the value of [=this=]'s [=LargestContentfulPaint/url=]. -The {{LargestContentfulPaint/element}} attribute's getter must return the value returned by running the get an element algorithm with element and null as inputs. +The {{LargestContentfulPaint/element}} attribute's getter must perform the following steps: +
+ 1. If [=this=]'s [=LargestContentfulPaint/element=] is not [=exposed for paint timing=] given null, return null. + 1. Return [=this=]'s [=LargestContentfulPaint/element=]. +
Note: The above algorithm defines that an element that is no longer a [=tree/descendant=] of the {{Document}} will no longer be returned by {{LargestContentfulPaint/element}}'s attribute getter, including elements that are inside a shadow DOM. @@ -168,38 +196,67 @@ Note: The user agent needs to maintain the [=content set=] so that removed conte Processing model {#sec-processing-model} ======================================== -Potentially add LargestContentfulPaint entry {#sec-add-lcp-entry} --------------------------------------------------------- +Each {{Window}} has has dispatched scroll event, a boolean which is initially set to false. -Note: A user agent implementing the Largest Contentful Paint API would need to include "largest-contentful-paint" in {{PerformanceObserver/supportedEntryTypes}} for {{Window}} contexts. -This allows developers to detect support for the API. -In order to potentially add a {{LargestContentfulPaint}} entry, the user agent must run the following steps: -
+Modifications to the DOM specification {#sec-modifications-DOM} +--------------------------------------------------------------- + +This section will be removed once the [[DOM]] specification has been modified. + +
+ We modify the event dispatch algorithm as follows. + + Right after step 1, we add the following step: + + * If |target|'s [=relevant global object=] is a {{Window}} object, event's {{Event/type}} is {{Document/scroll}} and its {{Event/isTrusted}} is true, set |target|'s [=relevant global object=]'s [=has dispatched scroll event=] to true. +
+ +Report Largest Contentful Paint {#sec-report-largest-contentful-paint} +---------------------------------------------------------------------- + +
+ When asked to report largest contentful paint given a {{Document}} |document|, a timestamp |now|, an [=ordered set=] of [=pending image records=] |paintedImages|, and an [=ordered set=] of [=/elements=] |paintedTextNodes|, perform the following steps: + + 1. [=list/For each=] |record| of |paintedImages|: + 1. Let |imageElement| be |record|'s [=pending image record/element=]. + 1. If |imageElement| is not [=exposed for paint timing=], given |document|, continue. + 1. Let |request| be |record|'s [=pending image record/request=]. + 1. Let |candidate| be (|imageElement|, |request|) + 1. Let |intersectionRect| be the value returned by the intersection rect algorithm using |imageElement| as the target and viewport as the root. + 1. Potentially add a LargestContentfulPaint entry with |candidate|, |intersectionRect|, |now|, |record|'s [=pending image record/loadTime=] and |document|. + 1. [=list/For each=] |textNode| of |paintedTextNodes|, + 1. If |textNode| is not [=exposed for paint timing=], given |document|, continue. + 1. Let |candidate| be (|textNode|, null) + 1. Let |intersectionRect| be an empty rectangle. + 1. [=set/For each=] {{Text}} node |text| of |textNode|'s set of owned text nodes: + 1. Augment |intersectionRect| to be smallest rectangle containing the border box of |text| and |intersectionRect|. + 1. Intersect |intersectionRect| with the visual viewport. + 1. Potentially add a LargestContentfulPaint entry with |candidate|, |intersectionRect|, |now|, 0, and |document|. +
+ +Determine the effective visual size of an element {#sec-effective-visual-size} +------------------------------------------------------------------------------ + +In order to determine the effective visual size of an [=/Element=], +run the following steps: + +
: Input :: |intersectionRect|, a {{DOMRectReadOnly}} :: |imageRequest|, a {{Request}} - :: |renderTime|, a DOMHighResTimestamp - :: |loadTime|, a DOMHighResTimestamp - :: |element|, an Element + :: |element|, an [=/Element=] :: |document|, a Document : Output - :: None - 1. Let |contentIdentifier| be the tuple (|element|, |imageRequest|). - 1. If |document|'s [=content set=] contains |contentIdentifier|, return. - 1. Append |contentIdentifier| to |document|'s [=content set=] - 1. Let |window| be |document|’s [=relevant global object=]. - 1. If either of |window|'s [=has dispatched scroll event=] or [=has dispatched input event=] is true, return. - 1. Let |url| be the empty string. - 1. If |imageRequest| is not null, set |url| to be |imageRequest|'s [=request URL=]. - 1. Let |id| be |element|'s element id. + :: The size to report for Largest Contentful Paint, in pixels, or null if the element should not be an LCP candidate. 1. Let |width| be |intersectionRect|'s {{DOMRectReadOnly/width}}. 1. Let |height| be |intersectionRect|'s {{DOMRectReadOnly/height}}. 1. Let |size| be |width| * |height|. 1. Let |root| be |document|'s browsing context's top-level browsing context's active document. 1. Let |rootWidth| be |root|'s visual viewport's width, excluding any scrollbars. 1. Let |rootHeight| be |root|'s visual viewport's height, excluding any scrollbars. - 1. If |size| is equal to |rootWidth| times |rootHeight|, return. + 1. If |size| is equal to |rootWidth| times |rootHeight|, return null. + 1. If |imageRequest| is not [=eligible to be largest contentful paint=], return null. 1. If |imageRequest| is not null, run the following steps to adjust for image position and upscaling: 1. Let |concreteDimensions| be |imageRequest|'s [=concrete object size=] within |element|. 1. Let |visibleDimensions| be |concreteDimensions|, adjusted for positioning by 'object-position' or 'background-position' and |element|'s [=content box=]. @@ -210,16 +267,43 @@ In order to potentially add a {{LargestContentfulPaint}} entry 1. Let |intersectingClientContentRect| be the intersection of |clientContentRect| with |intersectionRect|. 1. Set |size| to |intersectingClientContentRect|'s {{DOMRectReadOnly/width}} * |intersectingClientContentRect|'s {{DOMRectReadOnly/height}}. - Note: this ensure that we only intersect with the image itself and not with the element's decorations. + Note: this ensures that we only intersect with the image itself and not with the element's decorations. 1. Let |naturalArea| be |imageRequest|'s [=natural width=] * |imageRequest|'s [=natural height=]. - 1. If |naturalArea| is 0, then return. + 1. If |naturalArea| is 0, then return null. 1. Let |boundingClientArea| be |clientContentRect|'s {{DOMRectReadOnly/width}} * |clientContentRect|'s {{DOMRectReadOnly/height}}. 1. Let |scaleFactor| be |boundingClientArea| / |naturalArea|. 1. If |scaleFactor| is greater than 1, then divide |size| by |scaleFactor|. + 1. Return |size|. +
+ +Potentially add LargestContentfulPaint entry {#sec-add-lcp-entry} +----------------------------------------------------------------- + +Note: A user agent implementing the Largest Contentful Paint API would need to include "largest-contentful-paint" in {{PerformanceObserver/supportedEntryTypes}} for {{Window}} contexts. +This allows developers to detect support for the API. + +In order to potentially add a {{LargestContentfulPaint}} entry, the user agent must run the following steps: +
+ : Input + :: |candidate|, a [=largest contentful paint candidate=] + :: |intersectionRect|, a {{DOMRectReadOnly}} + :: |renderTime|, a DOMHighResTimestamp + :: |loadTime|, a DOMHighResTimestamp + :: |document|, a Document + : Output + :: None + 1. If |document|'s [=content set=] contains |candidate|, return. + 1. Append |candidate| to |document|'s [=content set=] + 1. Let |window| be |document|’s [=relevant global object=]. + 1. If either of |window|'s [=has dispatched scroll event=] or [=has dispatched input event=] is true, return. + 1. Let |size| be the [=effective visual size=] of |candidate|'s [=/element=] given |intersectionRect|. 1. If |size| is less than or equal to |document|'s [=largest contentful paint size=], return. - 1. Let |contentInfo| be a map with |contentInfo|["size"] = |size|, |contentInfo|["url"] = |url|, |contentInfo|["id"] = |id|, |contentInfo|["renderTime"] = |renderTime|, |contentInfo|["loadTime"] = |loadTime|, and contentInfo["element"] = |element|. + 1. Let |url| be the empty string. + 1. If |candidate|'s [=largest contentful paint candidate/request=] is not null, set |url| to be |candidate|'s [=largest contentful paint candidate/request=]'s [=request URL=]. + 1. Let |id| be |candidate|'s [=largest contentful paint candidate/element=]'s element id. + 1. Let |contentInfo| be a map with |contentInfo|["size"] = |size|, |contentInfo|["url"] = |url|, |contentInfo|["id"] = |id|, |contentInfo|["renderTime"] = |renderTime|, |contentInfo|["loadTime"] = |loadTime|, and contentInfo["element"] = |candidate|'s [=largest contentful paint candidate/element=]. 1. Create a LargestContentfulPaint entry with |contentInfo|, and |document| as inputs.
@@ -245,27 +329,8 @@ In order to create a {{LargestContentfulPaint}} entry, the user agent 1. [=Queue the PerformanceEntry=] |entry|.
-Modifications to the DOM specification {#sec-modifications-DOM} --------------------------------------------------------- - -This section will be removed once the [[DOM]] specification has been modified. - -
- We modify the event dispatch algorithm as follows. - - Right after step 1, we add the following step: - - * If |target|'s [=relevant global object=] is a {{Window}} object, event's {{Event/type}} is {{Document/scroll}} and its {{Event/isTrusted}} is true, set |target|'s [=relevant global object=]'s [=has dispatched scroll event=] to true. -
- -Modifications to the HTML specification {#sec-modifications-HTML} ----------------------------------------- -This section will be removed once the [[HTML]] specification has been modified. - -Each {{Window}} has has dispatched scroll event, a boolean which is initially set to false. - Security & privacy considerations {#sec-security} -=============================================== +================================================= -This API relies on Element Timing for its underlying primitives. LCP may expose some element not exposed by Element Timing in case that they are smaller than Element Timing's limits, but are still the largest elements to be painted up until that point in the page's loading. That does not seem to expose any sensitive information beyond what Element Timing already enables. +This API relies on Paint Timing for its underlying primitives. Unlike the similar API Element Timing, LCP may expose timing details of some elements with small sizes, if they are still the largest elements to be painted up until that point in the page's loading. That does not seem to expose any sensitive information beyond what Element Timing already enables.