summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/cssom-view/scrollintoview.html
blob: 7a7ecfafccbe679a96e06f3fa934cf6d19e2c760 (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
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<title>CSSOM View - scrollIntoView</title>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1">
<link rel="author" title="Chris Wu" href="mailto:pwx.frontend@gmail.com">
<link rel="help" href="https://drafts.csswg.org/cssom-view/#dom-element-scrollintoview">
<link rel="help" href="https://webidl.spec.whatwg.org/#es-operations">
<link rel="help" href="https://webidl.spec.whatwg.org/#es-overloads">
<meta name="flags" content="dom">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<style>
body.running { margin: 0; padding: 4000px; overflow: hidden }
body.running #testDiv {
  width: 200px;
  height: 200px;
  background-color: lightgreen;
}
</style>
<body class=running>
<div id=testDiv></div>
<div id="log"></div>
<script>
var testDiv = document.getElementById('testDiv');

var expectedXLeft = 4000;
var expectedXRight = 4000 - window.innerWidth + testDiv.clientWidth;
var expectedXCenter = 4000 - (window.innerWidth / 2) + (testDiv.clientWidth / 2);

var expectedYTop = 4000;
var expectedYBottom = 4000 - window.innerHeight + testDiv.clientHeight;
var expectedYCenter = 4000 - (window.innerHeight / 2) + (testDiv.clientHeight / 2);

[
  ["omitted argument", "nearest", expectedYTop],
  [true, "nearest", expectedYTop],
  [false, "nearest", expectedYBottom],
  [undefined, "nearest", expectedYTop],
  [null, "nearest", expectedYTop],
  [{}, "nearest", expectedYTop],
  [{block: "center", inline: "center"}, expectedXCenter, expectedYCenter],
  [{block: "start", inline: "start"}, expectedXLeft, expectedYTop],
  [{block: "end", inline: "end"}, expectedXRight, expectedYBottom],
  [{block: "nearest", inline: "nearest"}, "nearest", "nearest"],
].forEach(([input, expectedX, expectedY]) => {
  test(() => {
    window.scrollTo(0, 0);
    testScrollIntoView(input);
    var x = (expectedX === "nearest") ? expectedXRight : expectedX;
    var y = (expectedY === "nearest") ? expectedYBottom : expectedY;
    assert_approx_equals(window.scrollX, x, 0.5, 'scrollX');
    assert_approx_equals(window.scrollY, y, 0.5, 'scrollY');
  }, `scrollIntoView(${format_input(input)}) starting at left,top`);

  test(() => {
    window.scrollTo(0, 12000);
    testScrollIntoView(input);
    var x = (expectedX === "nearest") ? expectedXRight : expectedX;
    var y = (expectedY === "nearest") ? expectedYTop : expectedY;
    assert_approx_equals(window.scrollX, x, 0.5, 'scrollX');
    assert_approx_equals(window.scrollY, y, 0.5, 'scrollY');
  }, `scrollIntoView(${format_input(input)}) starting at left,bottom`);

  test(() => {
    window.scrollTo(12000, 0);
    testScrollIntoView(input);
    var x = (expectedX === "nearest") ? expectedXLeft : expectedX;
    var y = (expectedY === "nearest") ? expectedYBottom : expectedY;
    assert_approx_equals(window.scrollX, x, 0.5, 'scrollX');
    assert_approx_equals(window.scrollY, y, 0.5, 'scrollY');
  }, `scrollIntoView(${format_input(input)}) starting at right,top`);

  test(() => {
    window.scrollTo(12000, 12000);
    testScrollIntoView(input);
    var x = (expectedX === "nearest") ? expectedXLeft : expectedX;
    var y = (expectedY === "nearest") ? expectedYTop : expectedY;
    assert_approx_equals(window.scrollX, x, 0.5, 'scrollX');
    assert_approx_equals(window.scrollY, y, 0.5, 'scrollY');
  }, `scrollIntoView(${format_input(input)}) starting at right,bottom`);
});

function testScrollIntoView(input) {
  if (input === "omitted argument") {
    testDiv.scrollIntoView();
  } else {
    testDiv.scrollIntoView(input);
  }
}

// This formats dict as a string suitable as test name.
// format_value() is provided by testharness.js,
// which also preserves sign for -0.
function format_dict(dict) {
  const props = [];
  for (let prop in dict) {
    props.push(`${prop}: ${format_value(dict[prop])}`);
  }
  return `{${props.join(', ')}}`;
}

function format_input(input) {
  if (input === "omitted argument") {
    return "";
  } else if (input === null || typeof input !== "object") {
    return format_value(input);
  }
  return format_dict(input);
}

document.body.classList.remove('running');
window.scrollTo(0, 0);
</script>