summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/css-contain/content-visibility/content-visibility-071.html
blob: 6f267b27c7ea07f6ec4b19bef9d2dd78b4f06c76 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!doctype HTML>
<html>
<meta charset="utf8">
<title>Content Visibility: off-screen selection</title>
<link rel="author" title="Vladimir Levin" href="mailto:vmpstr@chromium.org">
<link rel="help" href="https://drafts.csswg.org/css-contain/#content-visibility">
<meta name="assert" content="content-visibility auto element remains non-skipped when elements in its subtree have selection.">

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<style>
body, html {
  padding: 0;
  margin: 0;
}

.spacer {
  height: 3000px;
}
.container {
  width: 10px;
  contain-intrinsic-size: 10px 20px;
  content-visibility: auto;
}
.child {
  width: 10px;
  height: 10px;
}
</style>

<div class=spacer></div>
<div id=container_1 class=container><div id=child_1 class=child>hello</div></div>
<div id=container_2 class=container><div id=child_2 class=child>hello</div></div>
<div id=container_3 class=container><div id=child_3 class=child>hello</div></div>
<div id=container_4 class=container><div id=child_4 class=child>hello</div></div>
<div id=container_5 class=container><div id=child_5 class=child>hello</div></div>

<script>
function isLocked(container) {
  const height = container.getBoundingClientRect().height;
  assert_true(height == 20 || height == 10);
  return container.getBoundingClientRect().height == 20;
}

const selection = window.getSelection();
function resetSelection() {
  selection.removeAllRanges();
  assert_true(isLocked(container_1));
  assert_true(isLocked(container_2));
  assert_true(isLocked(container_3));
  assert_true(isLocked(container_4));
  assert_true(isLocked(container_5));
}

test(() => {
  resetSelection();
  const range = document.createRange();
  range.selectNodeContents(child_2);
  selection.addRange(range);

  assert_true(isLocked(container_1), "1");
  assert_false(isLocked(container_2), "2");
  assert_true(isLocked(container_3), "3");
  assert_true(isLocked(container_4), "4");
  assert_true(isLocked(container_5), "5");
}, "One elements selected: ");

test(() => {
  resetSelection();
  const range = document.createRange();
  range.selectNodeContents(child_2);
  selection.addRange(range);

  assert_true(isLocked(container_1), "1");
  assert_false(isLocked(container_2), "2");
  assert_true(isLocked(container_3), "3");
  assert_true(isLocked(container_4), "4");
  assert_true(isLocked(container_5), "5");

  selection.extend(child_4, 0);

  assert_true(isLocked(container_1), "1");
  assert_false(isLocked(container_2), "2");
  assert_false(isLocked(container_3), "3");
  assert_false(isLocked(container_4), "4");
  assert_true(isLocked(container_5), "5");
}, "Range extended to cover more elements: ");

test(() => {
  resetSelection();
  const range = document.createRange();
  range.setStart(child_2, 0);
  range.setEnd(child_4, 0);
  selection.addRange(range);

  assert_true(isLocked(container_1), "1");
  assert_false(isLocked(container_2), "2");
  assert_false(isLocked(container_3), "3");
  assert_false(isLocked(container_4), "4");
  assert_true(isLocked(container_5), "5");

  selection.extend(child_2, 1);

  assert_true(isLocked(container_1), "1");
  assert_false(isLocked(container_2), "2");
  assert_true(isLocked(container_3), "3");
  assert_true(isLocked(container_4), "4");
  assert_true(isLocked(container_5), "5");
}, "Range shrunk to cover fewer elements: ");

test(() => {
  resetSelection();
  const range = document.createRange();
  range.setStart(child_3, 0);
  range.setEnd(child_3, 0);
  selection.addRange(range);
  selection.extend(child_2, 0);

  assert_true(isLocked(container_1), "1");
  assert_false(isLocked(container_2), "2");
  assert_false(isLocked(container_3), "3");
  assert_true(isLocked(container_4), "4");
  assert_true(isLocked(container_5), "5");

  selection.extend(child_4, 0);

  assert_true(isLocked(container_1), "1");
  assert_true(isLocked(container_2), "2");
  assert_false(isLocked(container_3), "3");
  assert_false(isLocked(container_4), "4");
  assert_true(isLocked(container_5), "5");
}, "Range flipped from back to front: ");

test(() => {
  resetSelection();
  const range = document.createRange();
  range.setStart(child_3, 0);
  range.setEnd(child_4, 0);
  selection.addRange(range);

  assert_true(isLocked(container_1), "1");
  assert_true(isLocked(container_2), "2");
  assert_false(isLocked(container_3), "3");
  assert_false(isLocked(container_4), "4");
  assert_true(isLocked(container_5), "5");

  selection.extend(child_2, 0);

  assert_true(isLocked(container_1), "1");
  assert_false(isLocked(container_2), "2");
  assert_false(isLocked(container_3), "3");
  assert_true(isLocked(container_4), "4");
  assert_true(isLocked(container_5), "5");
}, "Range flipped from front to back: ");

test(() => {
  resetSelection();
  const range = document.createRange();
  range.setStart(child_1, 0);
  range.setEnd(child_1, 0);
  selection.addRange(range);

  let state = 0;
  const states = [2, 4, 3, 5, 1];
  for (let i = 0; i < 10; ++i) {
    const id = states[state];
    selection.extend(document.getElementById(`child_${id}`), 1);

    for (let check_id = 1; check_id <= 5; ++check_id) {
      if (check_id <= id) {
        assert_false(
          isLocked(document.getElementById(`container_${check_id}`)),
          `test_${i}, container_${check_id}`);
      } else {
        assert_true(
          isLocked(document.getElementById(`container_${check_id}`)),
          `test_${i}, container_${check_id}`);
      }
    }
    state = (state + 1) % states.length;
  }
}, "Range goes back and forth: ");
</script>
</html>