File: get_element_region_test.html

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (245 lines) | stat: -rw-r--r-- 7,845 bytes parent folder | download | duplicates (10)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!DOCTYPE HTML>
<html>
<script src='test.js'></script>
<script src='get_element_region.js'></script>
<script>

function testNotElement() {
  try {
    getElementRegion(document);
    assert(false);
  } catch (error) {
    assertEquals(document + ' is not an element', error.message);
  }
}

function testElementWithFirstClientRect() {
  const region = getElementRegion(document.getElementById('a'));
  assertEquals(0, region.left);
  assertEquals(0, region.top);
  assertEquals(100, region.width);
  assertEquals(200, region.height);
}

function testSvgElement() {
  const ellipse = document.getElementById('e');
  const region = getElementRegion(ellipse);
  assertEquals(0, region.left);
  assertEquals(0, region.top);

  const boundingRect = ellipse.getBoundingClientRect();
  const visualViewport = window.visualViewport;

  assert(0 <= region.width);
  assert(region.width <= 90);
  if (region.width > 0 && region.width < 90) {
    assertEquals(boundingRect.left + region.width, visualViewport.width);
  }

  assert(0 <= region.height);
  assert(region.height <= 40);
  if (region.height > 0 && region.height < 40) {
    assertEquals(boundingRect.top + region.height, visualViewport.height);
  }
}

function testAreaPoly() {
  const region = getElementRegion(document.getElementById('poly'));
  assertEquals(20, region.left);
  assertEquals(10, region.top);
  assertEquals(40, region.width);
  assertEquals(45, region.height);
}

function testAreaRect() {
  const region = getElementRegion(document.getElementById('rect'));
  assertEquals(120, region.left);
  assertEquals(100, region.top);
  assertEquals(20, region.width);
  assertEquals(50, region.height);
}

function testAreaCircle() {
  const region = getElementRegion(document.getElementById('circle'));
  assertEquals(175, region.left);
  assertEquals(165, region.top);
  assertEquals(10, region.width);
  assertEquals(10, region.height);
}

function testPartialOutOfView() {
  let element = document.getElementById('partial-out-of-view');
  let region = getElementRegion(element);
  assertEquals(50, region.left);
  assertEquals(0, region.top);

  const boundingRect = element.getBoundingClientRect();
  const visualViewport = window.visualViewport;

  assert(0 <= region.width);
  assert(region.width <= 50);
  if (region.width > 0 && region.width < 50) {
    assertEquals(boundingRect.left + region.width, visualViewport.width);
  }

  assert(0 <= region.height);
  assert(region.height <= 100);
  if (region.height > 0 && region.height < 100) {
    assertEquals(boundingRect.top + region.height, visualViewport.height);
  }
}

function testScrolledOutOfView() {
  // Scroll to an offset to make element with id=a partially visible.
  const elemRect = document.getElementById("a").getBoundingClientRect();
  window.scrollTo(elemRect.left + 50, elemRect.top + 100);
  const region = getElementRegion(document.getElementById("a"));
  assertEquals(50, region.left);
  assertEquals(100, region.top);
  assertEquals(50, region.width);
  assertEquals(100, region.height);
  window.scrollTo(0, 0);
}

function testScrolledPartiallyOutOfView() {
  // Scroll to an offset to make element with id=a partially visible.
  document.body.style.paddingTop = '0.25px';
  document.body.style.paddingLeft = '0.25px';
  const elem = document.getElementById("check");
  window.scrollTo(
    elem.offsetLeft + elem.offsetWidth,
    elem.offsetTop + elem.offsetHeight,
  );
  const region = getElementRegion(elem);
  assertEquals(0, region.left);
  assertEquals(0, region.top);
  assertEquals(elem.offsetWidth, region.width);
  assertEquals(elem.offsetHeight, region.height);
  window.scrollTo(0, 0);
  document.body.style.paddingTop = '';
  document.body.style.paddingLeft = '';
}

function testFullOutOfView() {
  const region = getElementRegion(document.getElementById('out-of-view'));
  assertEquals(0, region.left);
  assertEquals(0, region.top);
  assertEquals(100, region.width);
  assertEquals(100, region.height);
}

function testFullOutOfViewRect() {
  const region = getElementRegion(document.getElementById('out-of-view-rect'));
  assertEquals(120, region.left);
  assertEquals(100, region.top);
  assertEquals(20, region.width);
  assertEquals(50, region.height);
}

function testFullOutOfViewCircle() {
  const region = getElementRegion(document.getElementById('out-of-view-circle'));
  assertEquals(175, region.left);
  assertEquals(165, region.top);
  assertEquals(10, region.width);
  assertEquals(10, region.height);
}

function testPartialOutOfViewRect() {
  const region = getElementRegion(document.getElementById('partial-rect'));
  assertEquals(120, region.left);
  assertEquals(10, region.top);
  assertEquals(10, region.width);
  // Height should not be truncated, even if partially visible.
  assertEquals(40, region.height);
}

function testPartialOutOfViewCircle() {
  const region = getElementRegion(document.getElementById('partial-circle'));
  assertEquals(30, region.left);
  assertEquals(0, region.top);
  // Area is treated as if it is fully in-view or fully out-of-view.
  assertEquals(60, region.width);
  assertEquals(60, region.height);
}

function testNegativeDimensions() {
  const region = getElementRegion(document.getElementById('negative-dims'));
  assertEquals(0, region.left);
  assertEquals(0, region.top);
  // Negative dimensions appear as 0, so region is 0x0.
  assertEquals(0, region.width);
  assertEquals(0, region.height);
}

function testAreaDefault() {
  try {
    getElementRegion(document.getElementById('default'));
    assert(false);
  } catch (error) {
  }
}

</script>
<body>
<div style="border: 3px coral solid;">
  <div id="a" style="background-color:orange;width:100px;height:200px">
  </div>
  <br>
  <div>
    <svg xmlns="http://www.w3.org/2000/svg" height="130px" width="300px">
      <ellipse cx="150" cy="65" rx="45" ry="20" id="e">
      </ellipse>
    </svg>
  </div>
  <br>
  <div>
    <img width="200" height="200" usemap="#imgmap">
    <map name="imgmap">
      <area id="poly" shape="poly" coords="20,20,30,10,50,20,60,40,50,50,30,55">
      <area id="rect" shape="rect" coords="120,100,140,150">
      <area id="circle" shape="circle" coords="180,170,5">
      <area id="default" shape="default">
    </map>
  </div>
  <div style="background-color:blue;width:100px;height:100px;position:absolute;top:200px;left:-50px"
    id="partial-out-of-view">
  </div>
  <div style="background-color:green;width:100px;height:100px;position:absolute;top:100px;left:-200px"
    id="out-of-view">
  </div>
  <div>
    <img width="200" height="200" usemap="#outofviewmap" style="position:absolute;top:120vh;left:50px">
    <map name="outofviewmap">
      <area id="out-of-view-rect" shape="rect" coords="120,100,140,150">
      <area id="out-of-view-circle" shape="circle" coords="180,170,5">
    </map>
  </div>
  <div>
    <img width="200" height="200" usemap="#partialmap" style="position:absolute;top:calc(100vh - 20px);left:50px">
    <map name="partialmap">
      <area id="partial-rect" shape="rect" coords="120,10,130,50">
      <area id="partial-circle" shape="circle" coords="60,5,30">
    </map>
  </div>
  <div style="background-color:blue;width:100px;height:100px;position:absolute;top:200px;left:-50px"
    id="partial-out-of-view">
  </div>
  <div style="background-color:green;width:100px;height:100px;position:absolute;top:100px;left:-200px"
    id="out-of-view">
  </div>
  <div style="background-color:yellow;width:-100px;height:-100px;position:absolute;top:200px;left:200px;"
    id="negative-dims">
  </div>
  <!-- This element adds scroll to see how script handles viewport with scroll -->
  <div style="background-color:red;width:50px;height:50px;position:absolute;top:200vh;left:200vw;">
  </div>
  <input type="checkbox" id="check">
  <div style="height:4000px"></div>

</div>
</body>
</html>

</div>
</body>
</html>