Skip to content

Commit a93ac1a

Browse files
credodannevk
authored andcommitted
Implement HTML video and audio element lazy-loading via the loading attribute
https://bugs.webkit.org/show_bug.cgi?id=303995 Reviewed by Anne van Kesteren. This patch implements a loading attribute for video and audio elements, enabling lazy loading of media sources and video poster images, and deferring autoplay. This was added to the HTML spec here: whatwg/html#11980 Synced test from upstream wpt: - Commit: web-platform-tests/wpt@7d9d48d - Test: imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-source-inserted-load-event.html * LayoutTests/TestExpectations: * LayoutTests/imported/w3c/web-platform-tests/html/dom/reflection-embedded-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/infrastructure/urls/resolving-urls/query-encoding/windows-1251_include=loading-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/infrastructure/urls/resolving-urls/query-encoding/windows-1252_include=loading-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-autoplay-deferred-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-lazy-autoplay-when-visible-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-lazy-disconnected-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-lazy-not-rendered-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-lazy-source-deferred-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-lazy-to-eager-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-load-deferred-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-load-preload-auto-deferred-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-load-preload-metadata-deferred-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-autoplay-deferred-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-eager-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-autoplay-when-visible-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-disconnected-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-in-scroller-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-in-viewport-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-load-when-visible-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-not-rendered-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-poster-when-visible-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-resume-on-loading-attribute-change-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-source-inserted-load-event-expected.txt: Added. * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-source-inserted-load-event.html: Added. * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-source-poster-deferred-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-to-eager-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-poster-deferred-expected.txt: * LayoutTests/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/w3c-import.log: * LayoutTests/imported/w3c/web-platform-tests/media/A4.webm: Added. * LayoutTests/platform/glib/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-lazy-in-viewport-expected.txt: Removed. * LayoutTests/platform/glib/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-lazy-window-onload-expected.txt: Removed. * LayoutTests/platform/glib/imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element/audio-loading-load-preload-metadata-deferred-expected.txt: Removed. * Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml: * Source/WebCore/Sources.txt: * Source/WebCore/WebCore.xcodeproj/project.pbxproj: * Source/WebCore/dom/Document.h: * Source/WebCore/html/HTMLIFrameElement.cpp: (WebCore::HTMLIFrameElement::attributeChanged): (WebCore::isFrameLazyLoadable): (WebCore::HTMLIFrameElement::shouldLoadFrameLazily): * Source/WebCore/html/HTMLImageElement.cpp: (WebCore::HTMLImageElement::bestFitSourceFromPictureElement): (WebCore::HTMLImageElement::selectImageSource): (WebCore::HTMLImageElement::attributeChanged): (WebCore::HTMLImageElement::didAttachRenderers): (WebCore::HTMLImageElement::allowsAutoSizes const): (WebCore::HTMLImageElement::hasLazyLoadableAttributeValue): Deleted. (WebCore::HTMLImageElement::isLazyLoadable const): Deleted. * Source/WebCore/html/HTMLImageElement.h: * Source/WebCore/html/HTMLMediaElement.cpp: (WebCore::HTMLMediaElement::~HTMLMediaElement): (WebCore::HTMLMediaElement::didMoveToNewDocument): (WebCore::HTMLMediaElement::attributeChanged): (WebCore::HTMLMediaElement::shouldLazyLoadMediaResource const): (WebCore::HTMLMediaElement::deferMediaResourceLoadIfNeeded): (WebCore::HTMLMediaElement::clearLazyLoadState): (WebCore::HTMLMediaElement::resumeDeferredMediaResourceLoad): (WebCore::HTMLMediaElement::runLazyLoadResumptionSteps): (WebCore::HTMLMediaElement::lazyLoadIntersectionCallbackInvoked): (WebCore::HTMLMediaElement::prepareForLoad): (WebCore::HTMLMediaElement::selectMediaResource): (WebCore::HTMLMediaElement::queueSelectMediaResourceTask): (WebCore::HTMLMediaElement::playInternal): (WebCore::HTMLMediaElement::sourceWasAdded): * Source/WebCore/html/HTMLMediaElement.h: (WebCore::HTMLMediaElement::isIntersectingViewport const): (WebCore::HTMLMediaElement::tracksViewportIntersection const): (WebCore::HTMLMediaElement::viewportIntersectionChanged): (WebCore::HTMLMediaElement::isMediaResourceLoadDeferred const): (WebCore::HTMLMediaElement::shouldObserveViewportIntersection const): * Source/WebCore/html/HTMLMediaElement.idl: * Source/WebCore/html/HTMLVideoElement.cpp: (WebCore::HTMLVideoElement::rendererIsNeeded): (WebCore::HTMLVideoElement::didAttachRenderers): (WebCore::HTMLVideoElement::computeAcceleratedRenderingStateAndUpdateMediaPlayer): (WebCore::HTMLVideoElement::attributeChanged): (WebCore::HTMLVideoElement::webkitEnterFullscreen): (WebCore::HTMLVideoElement::didMoveToNewDocument): (WebCore::HTMLVideoElement::viewportIntersectionChanged): (WebCore::HTMLVideoElement::runLazyLoadResumptionSteps): (WebCore::HTMLVideoElement::loadDeferredPosterImage): (WebCore::HTMLVideoElement::resumePosterLoadIfAlreadyVisible): (WebCore::HTMLVideoElement::~HTMLVideoElement): Deleted. (WebCore::HTMLVideoElement::lazyLoadIntersectionCallbackInvoked): Deleted. * Source/WebCore/html/HTMLVideoElement.h: * Source/WebCore/html/LazyLoadElementObserver.cpp: (WebCore::LazyLoadElementObserver::unobserve): (WebCore::LazyLoadElementObserver::unobserveIfLazyLoadOnly): (WebCore::LazyLoadElementObserver::isObserved const): Deleted. * Source/WebCore/html/LazyLoadElementObserver.h: * Source/WebCore/html/LazyLoading.cpp: Added. (WebCore::LazyLoading::hasLazyAttributeValue): (WebCore::LazyLoading::isInLazyState): (WebCore::LazyLoading::isLazyLoadingEnabled): (WebCore::LazyLoading::isLazyLoadable): * Source/WebCore/html/LazyLoading.h: Copied from Source/WebCore/html/LazyLoadElementObserver.h. * Source/WebCore/html/parser/HTMLPreloadScanner.cpp: (WebCore::TokenPreloadScanner::StartTagScanner::createPreloadRequest): (WebCore::TokenPreloadScanner::StartTagScanner::isLazyloadingImage const): (WebCore::TokenPreloadScanner::StartTagScanner::processAttribute): * Source/WebCore/loader/ImageLoader.cpp: (WebCore::ImageLoader::updateFromElement): (WebCore::ImageLoader::notifyFinished): (WebCore::ImageLoader::updatedHasPendingEvent): (WebCore::ImageLoader::decode): (WebCore::ImageLoader::resetLazyImageLoading): * Source/WebCore/rendering/RenderImage.cpp: (WebCore::RenderImage::layout): Canonical link: https://commits.webkit.org/325572@main
1 parent 74f4270 commit a93ac1a

52 files changed

Lines changed: 577 additions & 249 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎LayoutTests/TestExpectations‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8041,9 +8041,6 @@ imported/w3c/web-platform-tests/cookies/third-party-cookies/third-party-cookie-h
80418041
imported/w3c/web-platform-tests/html/semantics/embedded-content/the-canvas-element/canvas-descendants-focusability-006.html [ Skip ] # Timeout
80428042
imported/w3c/web-platform-tests/html/semantics/embedded-content/the-iframe-element/iframe_sandbox_allow_top_navigation_by_user_activation_with_user_gesture.html [ Skip ] # Timeout
80438043
imported/w3c/web-platform-tests/html/semantics/embedded-content/the-object-element/object-allowed-schemas.sub.window.html [ Skip ] # Timeout
8044-
imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-autoplay-when-visible.html [ Skip ] # Timeout
8045-
imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-in-viewport.html [ Skip ] # Timeout
8046-
imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video-loading-lazy-resume-on-loading-attribute-change.html [ Skip ] # Timeout
80478044
webkit.org/b/200128 imported/w3c/web-platform-tests/html/semantics/embedded-content/the-video-element/video_timeupdate_on_seek.html [ Skip ]
80488045
imported/w3c/web-platform-tests/cookies/samesite/sandbox-allow-same-site-none-cookies-value.tentative.https.html [ Skip ] # Different result on each platform for subtest
80498046

‎LayoutTests/imported/w3c/web-platform-tests/html/dom/reflection-embedded-expected.txt‎

Lines changed: 116 additions & 116 deletions
Large diffs are not rendered by default.

‎LayoutTests/imported/w3c/web-platform-tests/html/infrastructure/urls/resolving-urls/query-encoding/windows-1251_include=loading-expected.txt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ TIMEOUT loading image <embed src> Test timed out
77
TIMEOUT loading image <object data> Test timed out
88
PASS loading image <input src>
99
PASS loading image <video poster>
10-
TIMEOUT loading video <video> Test timed out
11-
TIMEOUT loading video <video><source> Test timed out
12-
TIMEOUT loading video <audio> Test timed out
13-
TIMEOUT loading video <audio><source> Test timed out
10+
PASS loading video <video>
11+
PASS loading video <video><source>
12+
PASS loading video <audio>
13+
PASS loading video <audio><source>
1414
PASS loading webvtt <track>
1515

‎LayoutTests/imported/w3c/web-platform-tests/html/infrastructure/urls/resolving-urls/query-encoding/windows-1252_include=loading-expected.txt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ TIMEOUT loading image <embed src> Test timed out
77
TIMEOUT loading image <object data> Test timed out
88
PASS loading image <input src>
99
PASS loading image <video poster>
10-
TIMEOUT loading video <video> Test timed out
11-
TIMEOUT loading video <video><source> Test timed out
12-
TIMEOUT loading video <audio> Test timed out
13-
TIMEOUT loading video <audio><source> Test timed out
10+
PASS loading video <video>
11+
PASS loading video <video><source>
12+
PASS loading video <audio>
13+
PASS loading video <audio><source>
1414
PASS loading webvtt <track>
1515

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11

22

3-
Harness Error (FAIL), message = Error: assert_unreached: Audio should not fire loadstart event when not visible in viewport Reached unreachable code
4-
5-
FAIL Audio with loading=lazy and autoplay that is not visible in viewport does not load audio data or autoplay assert_equals: expected 0 but got 4
3+
PASS Audio with loading=lazy and autoplay that is not visible in viewport does not load audio data or autoplay
64

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11

22

3-
Harness Error (FAIL), message = Error: assert_true: Audio should fire play event after scrolling into view expected true got false
4-
5-
FAIL Audio with loading=lazy and autoplay does not play while not visible, then plays once scrolled into view assert_true: Audio should be paused when not visible in viewport expected true got false
3+
PASS Audio with loading=lazy and autoplay does not play while not visible, then plays once scrolled into view
64

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
FAIL Audio with loading=lazy does not load when not connected to document assert_unreached: Disconnected audio with loading=lazy should not load. Reached unreachable code
2+
PASS Audio with loading=lazy does not load when not connected to document
33

Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
FAIL In-viewport audio with loading=lazy and no controls attribute, or display:none or hidden attribute should never load assert_unreached: Not-rendered audio elements with loading=lazy should not attempt to load. Reached unreachable code
2+
PASS In-viewport audio with loading=lazy and no controls attribute, or display:none or hidden attribute should never load
33

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22

3-
FAIL Audio with loading=lazy and source does not load until visible assert_false: Audio load should not start while offscreen expected false got true
3+
PASS Audio with loading=lazy and source does not load until visible
44

Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22

3-
FAIL Below-viewport audio with loading='lazy' loads when changed to loading='eager' or when loading attribute is removed assert_false: Audio should not fire loadstart when lazy and outside viewport expected false got true
3+
PASS Below-viewport audio with loading='lazy' loads when changed to loading='eager' or when loading attribute is removed
44

0 commit comments

Comments
 (0)