summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-scroll-anchoring/adjustments-in-scroll-event-handler.tentative.html
blob: 84fd79cbcb91e2e906bfabca59941e42ea5db2d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<!DOCTYPE html>
<meta name="viewport" content="width=device-width">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="author" title="Mozilla" href="https://mozilla.org">
<link rel="help" href="https://drafts.csswg.org/css-scroll-anchoring/">
<style>
  body { margin: 0 }
  .content {
    height: 200px;
    background: lightblue;
  }
  .spacer {
    height: 300vh;
  }
</style>
<div class="content"></div>
<div class="content" style="background: green"></div>
<div class="spacer"></div>
<script>
const anchor = document.querySelectorAll(".content")[1];

const t = async_test("Scroll adjustments happen even if it's triggered from scroll event listeners");
window.addEventListener("scroll", t.step_func(function() {
  // Forcibly flush layout, this will flush the pending the node insertion.
  let scrollPosition = window.scrollY;

  requestAnimationFrame(t.step_func(function() {
    requestAnimationFrame(t.step_func(function() {
      assert_equals(window.scrollY, 400);
      t.done();
    }));
  }));
}), { once: true });

window.onload = t.step_func(function() {
  requestAnimationFrame(t.step_func(function() {
    // Scroll to the anchor node in a requestAnimationFrame callback so that
    // it queues a scroll event which will be fired in the next event loop.
    anchor.scrollIntoView({ behavior: "instant" });

    // Then in a setTimeout callback insert an element just right before the
    // anchor node, it will run before firing the scroll event.
    t.step_timeout(function() {
      const content = document.createElement("div");
      content.classList.add("content");
      content.style.background = "red";
      anchor.before(content);
    }, 0);
  }));
});

</script>