Bug 159475 - Implement IntersectionObserver
Summary: Implement IntersectionObserver
Status: ASSIGNED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit Misc. (show other bugs)
Version: Safari Technology Preview
Hardware: All All
: P2 Enhancement
Assignee: Ali Juma
URL: https://w3c.github.io/IntersectionObs...
Keywords: InRadar, Performance, WebExposed
Depends on: 191558 165746 188416 188445 188469 188475 188575 188670 188809 189007 189055 189091 189128 189515 189525 189624 190235 191210 191274 191282 191330 191569
Blocks:
  Show dependency treegraph
 
Reported: 2016-07-06 10:24 PDT by Jeffrey Gilbert
Modified: 2018-11-12 16:30 PST (History)
39 users (show)

See Also:


Attachments
Work in progress (118.52 KB, patch)
2016-10-28 17:54 PDT, Simon Fraser (smfr)
no flags Details | Formatted Diff | Diff
Step 1: Bindings support (125.24 KB, patch)
2016-10-28 20:58 PDT, Simon Fraser (smfr)
rniwa: review+
Details | Formatted Diff | Diff
Archive of layout-test-results from ews121 for ios-simulator-wk2 (deleted)
2016-10-28 22:37 PDT, Build Bot
no flags Details
Work in progress patch (51.34 KB, patch)
2018-02-02 15:14 PST, Simon Fraser (smfr)
no flags Details | Formatted Diff | Diff
WIP (150.79 KB, patch)
2018-07-12 15:02 PDT, Ali Juma
no flags Details | Formatted Diff | Diff
Archive of layout-test-results from ews102 for mac-sierra (2.44 MB, application/zip)
2018-07-12 16:20 PDT, Build Bot
no flags Details
Archive of layout-test-results from ews104 for mac-sierra-wk2 (2.81 MB, application/zip)
2018-07-12 16:36 PDT, Build Bot
no flags Details
Archive of layout-test-results from ews112 for mac-sierra (3.18 MB, application/zip)
2018-07-12 16:59 PDT, Build Bot
no flags Details
Archive of layout-test-results from ews125 for ios-simulator-wk2 (2.30 MB, application/zip)
2018-07-12 17:05 PDT, Build Bot
no flags Details
Archive of layout-test-results from ews126 for ios-simulator-wk2 (2.30 MB, application/zip)
2018-07-12 19:06 PDT, Build Bot
no flags Details
WIP (193.24 KB, patch)
2018-07-25 11:50 PDT, Ali Juma
no flags Details | Formatted Diff | Diff
Archive of layout-test-results from ews100 for mac-sierra (2.47 MB, application/zip)
2018-07-25 13:10 PDT, Build Bot
no flags Details
Archive of layout-test-results from ews112 for mac-sierra (3.19 MB, application/zip)
2018-07-25 13:40 PDT, Build Bot
no flags Details
Archive of layout-test-results from ews121 for ios-simulator-wk2 (2.24 MB, application/zip)
2018-07-25 13:49 PDT, Build Bot
no flags Details
WIP (211.03 KB, patch)
2018-08-08 11:46 PDT, Ali Juma
no flags Details | Formatted Diff | Diff
Archive of layout-test-results from ews123 for ios-simulator-wk2 (2.35 MB, application/zip)
2018-08-08 13:45 PDT, Build Bot
no flags Details
WIP (100.64 KB, patch)
2018-10-09 14:59 PDT, Ali Juma
ews: commit-queue-
Details | Formatted Diff | Diff
Archive of layout-test-results from ews121 for ios-simulator-wk2 (2.57 MB, application/zip)
2018-10-09 18:03 PDT, Build Bot
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
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 13 raj yadav 2018-02-11 08:50:12 PST Comment hidden (spam)
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 Build Bot 2018-07-12 16:20:37 PDT Comment hidden (obsolete)
Comment 24 Build Bot 2018-07-12 16:20:39 PDT Comment hidden (obsolete)
Comment 25 Build Bot 2018-07-12 16:36:15 PDT Comment hidden (obsolete)
Comment 26 Build Bot 2018-07-12 16:36:17 PDT Comment hidden (obsolete)
Comment 27 Build Bot 2018-07-12 16:59:26 PDT Comment hidden (obsolete)
Comment 28 Build Bot 2018-07-12 16:59:28 PDT Comment hidden (obsolete)
Comment 29 Build Bot 2018-07-12 17:05:51 PDT Comment hidden (obsolete)
Comment 30 Build Bot 2018-07-12 17:05:54 PDT Comment hidden (obsolete)
Comment 31 Build Bot 2018-07-12 19:06:49 PDT Comment hidden (obsolete)
Comment 32 Build Bot 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 Build Bot 2018-07-25 13:10:18 PDT Comment hidden (obsolete)
Comment 35 Build Bot 2018-07-25 13:10:20 PDT Comment hidden (obsolete)
Comment 36 Build Bot 2018-07-25 13:40:39 PDT Comment hidden (obsolete)
Comment 37 Build Bot 2018-07-25 13:40:42 PDT Comment hidden (obsolete)
Comment 38 Build Bot 2018-07-25 13:49:27 PDT Comment hidden (obsolete)
Comment 39 Build Bot 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 Build Bot 2018-08-08 13:45:30 PDT Comment hidden (obsolete)
Comment 42 Build Bot 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 Build Bot 2018-10-09 18:03:02 PDT Comment hidden (obsolete)
Comment 45 Build Bot 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).