Bug 159475

Summary: Implement IntersectionObserver
Product: WebKit Reporter: Jeffrey Gilbert <jeffreytgilbert>
Component: WebKit Misc.Assignee: Ali Juma <ajuma>
Status: RESOLVED FIXED    
Severity: Enhancement CC: 709922234, ajuma, andrew, andrew.affinity, barraclough, bedney, bjr.roberts, cdumez, chi187, cigitia, danyao, darin, dave, dbates, dvoytenko, ews-watchlist, fred.wang, herr.ernst, jan.hadenfeldt, joaonadais, john.skibicki.iii, jshannon, kngan, mandy.pettt, manian, matiasnu, m.goleb+bugzilla, nisenbau, pauljohnknight, rik, rniwa, sam, sebastianzartner, simon.fraser, sk, tannerhodges, webkit-bug-importer, webkit, yellowydv23
Priority: P2 Keywords: InRadar, Performance, WebExposed
Version: Safari Technology Preview   
Hardware: All   
OS: All   
URL: https://w3c.github.io/IntersectionObserver/
See Also: https://bugs.webkit.org/show_bug.cgi?id=137037
Bug Depends on: 191558, 206556, 165746, 188416, 188445, 188469, 188475, 188575, 188670, 188809, 189007, 189055, 189091, 189128, 189515, 189525, 189624, 190235, 191210, 191274, 191282, 191330, 191569, 208047, 208053    
Bug Blocks:    
Attachments:
Description Flags
Work in progress
none
Step 1: Bindings support
rniwa: review+
Archive of layout-test-results from ews121 for ios-simulator-wk2
none
Work in progress patch
none
WIP
none
Archive of layout-test-results from ews102 for mac-sierra
none
Archive of layout-test-results from ews104 for mac-sierra-wk2
none
Archive of layout-test-results from ews112 for mac-sierra
none
Archive of layout-test-results from ews125 for ios-simulator-wk2
none
Archive of layout-test-results from ews126 for ios-simulator-wk2
none
WIP
none
Archive of layout-test-results from ews100 for mac-sierra
none
Archive of layout-test-results from ews112 for mac-sierra
none
Archive of layout-test-results from ews121 for ios-simulator-wk2
none
WIP
none
Archive of layout-test-results from ews123 for ios-simulator-wk2
none
WIP
ews-watchlist: commit-queue-
Archive of layout-test-results from ews121 for ios-simulator-wk2 none

Description Jeffrey Gilbert 2016-07-06 10:24:10 PDT
Working draft for implementation:
https://wicg.github.io/IntersectionObserver/

Intent to implement in Firefox by version 50
https://bugzilla.mozilla.org/show_bug.cgi?id=1243846
https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API

Chrome adopted IntersectionObserver support in version 51
https://www.chromestatus.com/feature/5695342691483648 
https://chromium.googlesource.com/chromium/src/third_party/+/master/WebKit/Source/core/dom/IntersectionObserver.cpp

MS Edge has committed to adopting support, but have not implemented yet
https://developer.microsoft.com/en-us/microsoft-edge/platform/status/intersectionobserver
https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/13407852-intersectionobserver

Rationale:
Advertisements typically use Flash, frame per second counters, or costly geometry calculations within loops to determine if an ad is in view or not. The mechanisms are difficult to implement, non-standardized, and costly to web performance. The IAB, MRC, browser vendors, and web standards bodies have attempted to solve for this by introducing first the PageVisibility 2 API draft, and now splintered off from that, the IntersectionObserver API. This pattern allows for efficient event driven code to take advantage of indicators sent directly from the browser of the area of a page or more importantly sandboxed iframe which are viewable without requiring access to the parent documents information or leaking important security details.

Chrome has introduced this early as a way of hastening the elimination of the Flash browser plugin as a necessary evil, which they plan to throttle by default at the end of the year. Firefox's intention is to introduce this functionality as part of version 50, currently in nightlies. Microsoft has commented to the IAB that they intend to introduce the functionality, but have not committed to a build version or date for releasing it. That leaves only Safari/Webkit based browsers left. This solves an obvious problem for advertisers and consumers/end users who want transparency and performance, respectively.
Comment 1 Jeffrey Gilbert 2016-08-04 10:15:31 PDT
Update: Chrome has a working implementation out now as of Chrome 51. The next Chrome release will also introduce a throttling mechanism on Flash which will greatly impact Flash functionality across site publishers. Linked in the comments are some of the implementation details for the Chrome feature.

Safari currently has no ability to report behavior using Flash as it has already been throttled and when it's used, it's inaccurate upon initial page load until the Flash player has been scrolled in and out of view so it's forced to change state. Geometric polling does work, but it's an expensive operation to call from javascript resulting in reduced battery life from higher cpu usage. 

Billing standards for advertisements are rapidly moving toward viewability based guarantees, so this mechanism for measurement and the impact of these calls will not be reduced until the implementation of this spec goes out in major webkit browsers(specifically Safari, mobile Safari, and embedded webviews).
Comment 2 Simon Fraser (smfr) 2016-10-28 17:54:37 PDT
Created attachment 293262 [details]
Work in progress
Comment 3 Simon Fraser (smfr) 2016-10-28 20:58:13 PDT
Created attachment 293271 [details]
Step 1: Bindings support
Comment 4 Simon Fraser (smfr) 2016-10-28 21:06:05 PDT
rdar://problem/23651605
Comment 5 Ryosuke Niwa 2016-10-28 22:16:46 PDT
Oh it looks like the bots are choking on an unrelated test that's temporarily broken.
Comment 6 Ryosuke Niwa 2016-10-28 22:21:07 PDT
Comment on attachment 293271 [details]
Step 1: Bindings support

View in context: https://bugs.webkit.org/attachment.cgi?id=293271&action=review

> LayoutTests/intersection-observer/intersection-observer-entry-interface.html:4
> +<head>
> +<script src="../resources/js-test-pre.js"></script>
> +</head>

Can we use testharness.js instead?  Ideally, we'd upstream these tests to web-platform-tests.

> LayoutTests/intersection-observer/intersection-observer-entry-interface.html:10
> +    if (window.internals)
> +        internals.settings.setIntersectionObserverEnabled(true);

For custom elements & shadow DOM, I unconditionally enabled them all in DRT & WTR.
Comment 7 Build Bot 2016-10-28 22:37:19 PDT Comment hidden (obsolete)
Comment 8 Build Bot 2016-10-28 22:37:25 PDT Comment hidden (obsolete)
Comment 9 Simon Fraser (smfr) 2016-10-31 15:16:53 PDT Comment hidden (obsolete)
Comment 10 Sebastian Zartner 2017-11-10 03:23:22 PST
It looks like all other major browsers already shipped support for this API, already. Simon, can you give a status update on this, please?

Sebastian
Comment 11 João Nadais 2017-11-25 07:26:50 PST
Is there any estimation for what version will this support be added in WebKit? All other browsers already support it...
Comment 12 Simon Fraser (smfr) 2018-02-02 15:14:29 PST
Created attachment 333008 [details]
Work in progress patch
Comment 14 Mandy Pettt 2018-05-29 12:54:38 PDT
dvertisements typically use Flash, frame per second counters, or costly geometry calculations within loops to determine if an ad is in view or not. The mechanisms are difficult to implement, non-standardized, and costly to web performance.
https://spinbikes218477221.wordpress.com/
Comment 15 William J. Edney 2018-05-30 11:25:32 PDT
At the risk of bugging Simon, any thoughts on getting the WIP to a final patch? Would beer help? :-).
Comment 16 Paul Knight 2018-06-01 17:46:02 PDT
(In reply to William J. Edney from comment #15)
> At the risk of bugging Simon, any thoughts on getting the WIP to a final
> patch? Would beer help? :-).

I would also buy Simon a big old burger if we could get this in Safari
Comment 17 Paul Knight 2018-06-01 17:47:18 PDT
Is there any idea of when this is likely to land in Safari? It's such a great API.
Comment 18 Danyao Wang 2018-06-20 08:03:50 PDT
Thanks for starting to work on this, Simon!

I ran your patch through the Intersection Observer WPT and a few demo sites. These seem to be the outstanding issues:

1. Notifications don't seem to be firing, either when scrolling or when new element is added to the DOM (see assert_equals failures on entries.length)
2. Should be able to observe target in a same-origin iframe, but throws WrongDocumentException instead
3. rootMargin parsing is not up-to-spec. Specifically, step 5 doesn't seem to be implemented.
4. Doesn't throw expected exceptions on invalid constructor arguments
5. (seen in demo) IntersectionObserverEntry.isIntersecting attribute is not implemented (from https://hacks.mozilla.org/2017/08/intersection-observer-comes-to-firefox/)
6. (seen in demo) When user scrolls using the trackpad or by holding and dragging the scrollbar, intersection events are not fired as frequently when threshold is fine grained. This is clear for the top-most box in the MDN demo: Safari video vs. Chrome video.

Here's the WPT result summary: https://danyao.github.io/intersection-observer/report/all.html#test-file-20

Is there anything I can help to land all or parts of this patch?
Comment 19 Darin Adler 2018-07-06 09:09:04 PDT
I don’t think Simon is going to be picking this up any time soon, so it would be great if someone else did.

A good start would be to add tests to the patch to cover the issues mentioned above, then rebase the patch, and refine until all the tests pass. If you do work on it, please communicate with others here about the fact that you are doing so, and attach work in progress patches from time to time so someone else can pick up where you left off if you drop it.
Comment 20 Ali Juma 2018-07-06 09:28:50 PDT
(In reply to Darin Adler from comment #19)
> I don’t think Simon is going to be picking this up any time soon, so it
> would be great if someone else did.
> 
> A good start would be to add tests to the patch to cover the issues
> mentioned above, then rebase the patch, and refine until all the tests pass.
> If you do work on it, please communicate with others here about the fact
> that you are doing so, and attach work in progress patches from time to time
> so someone else can pick up where you left off if you drop it.

Sounds good, I'll upload WIP patches as I go along.
Comment 21 Ali Juma 2018-07-12 15:02:45 PDT
Created attachment 344885 [details]
WIP
Comment 22 Ali Juma 2018-07-12 15:09:15 PDT
(In reply to Ali Juma from comment #21)
> Created attachment 344885 [details]
> WIP

This passes 16/24 web platform tests (the web platform tests cover all the issues mentioned in Comment 18).

The main work still to be done includes:
1) Dealing with zero-area intersections
2) Cross-document observation
3) Checking if behavior with pinch zoom and page zoom is reasonable
4) Correctly handling lifetime of IntersectionObservers and their associated roots and targets (so they're not destroyed too early, but also don't leak)

(1) and (2) are covered by the 8 remaining web platform test failures.
Comment 23 EWS Watchlist 2018-07-12 16:20:37 PDT Comment hidden (obsolete)
Comment 24 EWS Watchlist 2018-07-12 16:20:39 PDT Comment hidden (obsolete)
Comment 25 EWS Watchlist 2018-07-12 16:36:15 PDT Comment hidden (obsolete)
Comment 26 EWS Watchlist 2018-07-12 16:36:17 PDT Comment hidden (obsolete)
Comment 27 EWS Watchlist 2018-07-12 16:59:26 PDT Comment hidden (obsolete)
Comment 28 EWS Watchlist 2018-07-12 16:59:28 PDT Comment hidden (obsolete)
Comment 29 EWS Watchlist 2018-07-12 17:05:51 PDT Comment hidden (obsolete)
Comment 30 EWS Watchlist 2018-07-12 17:05:54 PDT Comment hidden (obsolete)
Comment 31 EWS Watchlist 2018-07-12 19:06:49 PDT Comment hidden (obsolete)
Comment 32 EWS Watchlist 2018-07-12 19:06:51 PDT Comment hidden (obsolete)
Comment 33 Ali Juma 2018-07-25 11:50:14 PDT Comment hidden (obsolete)
Comment 34 EWS Watchlist 2018-07-25 13:10:18 PDT Comment hidden (obsolete)
Comment 35 EWS Watchlist 2018-07-25 13:10:20 PDT Comment hidden (obsolete)
Comment 36 EWS Watchlist 2018-07-25 13:40:39 PDT Comment hidden (obsolete)
Comment 37 EWS Watchlist 2018-07-25 13:40:42 PDT Comment hidden (obsolete)
Comment 38 EWS Watchlist 2018-07-25 13:49:27 PDT Comment hidden (obsolete)
Comment 39 EWS Watchlist 2018-07-25 13:49:29 PDT Comment hidden (obsolete)
Comment 40 Ali Juma 2018-08-08 11:46:37 PDT Comment hidden (obsolete)
Comment 41 EWS Watchlist 2018-08-08 13:45:30 PDT Comment hidden (obsolete)
Comment 42 EWS Watchlist 2018-08-08 13:45:33 PDT Comment hidden (obsolete)
Comment 43 Ali Juma 2018-10-09 14:59:42 PDT Comment hidden (obsolete)
Comment 44 EWS Watchlist 2018-10-09 18:03:02 PDT Comment hidden (obsolete)
Comment 45 EWS Watchlist 2018-10-09 18:03:05 PDT Comment hidden (obsolete)
Comment 46 Ali Juma 2018-11-07 12:45:15 PST
We're now down to a single web platform test failure on trunk (target-in-different-window.html, which covers the case of a single observer with targets in two different browser windows).
Comment 47 Simon Fraser (smfr) 2019-05-30 16:31:46 PDT
Can this be closed now?