w3c/IntersectionObserver

Spec doesn't reflect reality wrt explicit roots.

emilio opened this issue · 3 comments

This test-case:

<!doctype html>
<style>
  div {
    width: 100px;
    height: 100px;
    background: blue;
    margin: 10px
  }
</style>
<div id="target"></div>
<div id="root"></div>
<script>
  let div = document.querySelector("#target");
  new IntersectionObserver(
    function() {
      console.log(arguments);
    },
    { root: document.querySelector("#root") }
  ).observe(div);
</script>

In Gecko it doesn't queue any entry. I believe this is correct per https://w3c.github.io/IntersectionObserver/#update-intersection-observations-algo:

If the intersection root is an Element, and target is not a descendant of the intersection root in the containing block chain, skip further processing for target.

I don't think that matches Blink or WebKit though, which just report a non-intersecting entry. I think it's also a bug in the spec, because per that reasoning display: none elements etc wouldn't get reported even if they're dom descendants of the root.

cc @szager-chromium

Similar issue with step 2.2:

If the intersection root is not the implicit root, and target is not in the same document as the intersection root, skip further processing for target.

<!doctype html>
<style>
  div {
    width: 100px;
    height: 100px;
    background: blue;
    margin: 10px
  }
</style>
<div id="root"></div>
<script>
  let doc = document.implementation.createHTMLDocument("");
  let target = doc.createElement("div");
  doc.body.appendChild(target);
  new IntersectionObserver(
    function() {
      console.log(arguments);
    },
    { root: document.querySelector("#root") }
  ).observe(target);
</script>

My preference would be to make the blink/webkit behavior official. I think it's useful to send the "not intersecting" notification when a target is removed from the containing block chain.

@emilio What do you think?

Yes, I tend to agree. I aligned Gecko with WebKit/Blink in https://bugzilla.mozilla.org/show_bug.cgi?id=1670327.