w3c / IntersectionObserver

Intersection Observer
https://www.w3.org/TR/intersection-observer/
Other
3.62k stars 526 forks source link

Polyfill: support same-origin iframe targets #419

Closed dvoytenko closed 4 years ago

dvoytenko commented 4 years ago

The polyfill is updated to support observing targets in the same-origin iframes. This is already a spec'd behavior and only polyfill abilities are enhanced. See https://www.w3.org/TR/intersection-observer/#dom-intersectionobserver-rootmargin for more info.

Notably, the changes to do not polyfill the IntersectionObserver inside an iframe, but simply allow observing using the top-level context IntersectionObserver. The final interface binding can be done by manually:

iframe.contentWindow.IntersectionObserver = window.IntersectionObserver;
iframe.contentWindow.IntersectionObserverEntry = window.IntersectionObserverEntry;

In practice, however, I find that even when same-origin iframes are used, the root observer is used for them. The tests show this pattern.

dvoytenko commented 4 years ago

Thanks for filing this PR. These changes look pretty reasonable on first look (with a few nits). Have you looked into added a test for this new behavior?

Definitely. I added several tests in the describe('same-origin iframe'). I added tests, for three categories of things:

  1. Monitoring scrolling/mutations/etc for iframes. Similar to non-iframed case.
  2. Calculation of intersectionRect and other entry structs.
  3. Main-vs-iframe-context coordination tests.
philipwalton commented 4 years ago

Ahh, sorry, the GitHub UI was hiding the diff for that file, so I missed it. Will take a look later today.

dvoytenko commented 4 years ago

@philipwalton I fixed the tests and tested with IE11 and IE10. PTAL.

dvoytenko commented 4 years ago

Close/reopen to run CI again.