summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/browsers/history/the-location-interface/same-hash.html
blob: 430a57662ac34e5150b44b210fdc44cf158e2c3f (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE html>
<meta charset="utf-8">
<title>Using the location interface to navigate to the same hash as the current one</title>
<link rel="help" href="https://github.com/whatwg/html/issues/7386">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<iframe id="i" srcdoc="<div style='height: 200vh'></div><div id='te&lt;st'></div>"></iframe>

<script type="module">
setup({ explicit_done: true });
await new Promise(r => window.onload = r);

for (const value of ["#te<st", "te<st", "#te%3Cst", "te%3Cst"]) {
  promise_test(async t => {
    t.add_cleanup(() => { i.contentWindow.location.hash = ""; });
    assert_equals(i.contentWindow.scrollY, 0, "Setup: iframe starts at top");

    i.contentWindow.location.hash = "te<st";
    await delayForFragmentNavigationScrolling(t);

    assert_greater_than(i.contentWindow.scrollY, i.contentWindow.innerHeight, "First hash assignment scrolls the iframe");

    i.contentWindow.scroll({ top: 0, behavior: "instant" });
    assert_equals(i.contentWindow.scrollY, 0, "Resetting the scroll position must work");

    i.contentWindow.location.hash = value;
    await delayForFragmentNavigationScrolling(t);

    assert_equals(i.contentWindow.scrollY, 0, "Reassigning the same hash must not change the scroll position");
  }, `Using location.hash = "${value}" must not reset scroll position`);
}

// These don't canonicalize to the current value of location.hash; the post-parsing version of
// "te<st" is "te%3Cst", uppercase.
for (const value of ["#te%3cst", "te%3cst"]) {
  promise_test(async t => {
    t.add_cleanup(() => { i.contentWindow.location.hash = ""; });
    assert_equals(i.contentWindow.scrollY, 0, "Setup: iframe starts at top");

    i.contentWindow.location.hash = "te<st";
    await delayForFragmentNavigationScrolling(t);

    assert_greater_than(i.contentWindow.scrollY, i.contentWindow.innerHeight, "First hash assignment scrolls the iframe");

    i.contentWindow.scroll({ top: 0, behavior: "instant" });
    assert_equals(i.contentWindow.scrollY, 0, "Resetting the scroll position must work");

    i.contentWindow.location.hash = value;
    await delayForFragmentNavigationScrolling(t);

    assert_greater_than(i.contentWindow.scrollY, i.contentWindow.innerHeight, "Reassigning the same-ish hash scrolls the iframe");
  }, `Using location.hash = "${value}" must reset scroll position`);
}

for (const value of ["about:srcdoc#te<st", "about:srcdoc#te%3cst", "about:srcdoc#te%3Cst"]) {
  promise_test(async t => {
    t.add_cleanup(() => { i.contentWindow.location.hash = ""; });
    assert_equals(i.contentWindow.scrollY, 0, "Setup: iframe starts at top");

    i.contentWindow.location.hash = "te<st";
    await delayForFragmentNavigationScrolling(t);

    assert_greater_than(i.contentWindow.scrollY, i.contentWindow.innerHeight, "First hash assignment scrolls the iframe");

    i.contentWindow.scroll({ top: 0, behavior: "instant" });
    assert_equals(i.contentWindow.scrollY, 0, "Resetting the scroll position must work");

    i.contentWindow.location.href = value;
    await delayForFragmentNavigationScrolling(t);

    assert_greater_than(i.contentWindow.scrollY, i.contentWindow.innerHeight, "Setting href must scroll the iframe");
  }, `Using location.href = "${value}" must reset scroll position`);

  promise_test(async t => {
    t.add_cleanup(() => { i.contentWindow.location.hash = ""; });
    assert_equals(i.contentWindow.scrollY, 0, "Setup: iframe starts at top");

    i.contentWindow.location.hash = "te<st";
    await delayForFragmentNavigationScrolling(t);

    assert_greater_than(i.contentWindow.scrollY, i.contentWindow.innerHeight, "First hash assignment scrolls the iframe");

    i.contentWindow.scroll({ top: 0, behavior: "instant" });
    assert_equals(i.contentWindow.scrollY, 0, "Resetting the scroll position must work");

    i.contentWindow.location.assign(value);
    await delayForFragmentNavigationScrolling(t);

    assert_greater_than(i.contentWindow.scrollY, i.contentWindow.innerHeight, "Setting href must scroll the iframe");
  }, `Using location.assign("${value}") must reset scroll position`);
}

function delayForFragmentNavigationScrolling(t) {
  // Scroll behavior for fragment navigation is set to "auto" in the spec, so we can't guarantee it's instant.
  // In practice 10 milliseconds seems to be enough.
  return new Promise(r => t.step_timeout(r, 10));
}

done();
</script>