|
2 | 2 | This demonstrates the effectiveness a method for testing for visibility elements on various
|
3 | 3 | conditions.
|
4 | 4 | -->
|
5 |
| -<!DOCTYPE html> |
| 5 | +<!doctype html> |
6 | 6 | <html>
|
7 | 7 | <head>
|
8 | 8 | <title>Visibility test</title>
|
9 |
| - <link rel="icon" href="data:;base64,iVBORw0KGgo="> |
| 9 | + <link rel="icon" href="data:;base64,iVBORw0KGgo=" /> |
10 | 10 | <style type="text/css" media="screen">
|
11 | 11 | * {
|
12 | 12 | font-family: sans;
|
|
21 | 21 | table {
|
22 | 22 | border-collapse: separate;
|
23 | 23 | }
|
24 |
| - tr, td { |
| 24 | + tr, |
| 25 | + td { |
25 | 26 | border: 2px solid black;
|
26 | 27 | margin: 0;
|
27 | 28 | padding: 2px;
|
28 | 29 | }
|
29 | 30 | </style>
|
30 | 31 |
|
| 32 | + <scirpt src="../lib/dom_utils.js"></scirpt> |
| 33 | + |
31 | 34 | <script charset="utf-8">
|
32 | 35 | window.addEventListener("load", displayTests, false);
|
33 | 36 | window.addEventListener("load", checkDivsForVisibility, false);
|
34 | 37 |
|
35 | 38 | var visibilityTests = [
|
36 | 39 | {
|
37 |
| - "description": "BoundingClientRect is inside viewport", |
38 |
| - "test": function (element) { |
| 40 | + description: "BoundingClientRect is inside viewport", |
| 41 | + test: function (element) { |
39 | 42 | var rect = element.getBoundingClientRect();
|
40 |
| - if ( |
41 |
| - !rect || rect.top > window.innerHeight || rect.bottom < 0 || |
42 |
| - rect.left > window.innerWidth || rect.right < 0 |
43 |
| - ) { |
| 43 | + if (!rect || !DomUtils.isElementVisible(element,{partialCheck: true})) { |
44 | 44 | return false;
|
45 | 45 | }
|
46 | 46 | return true;
|
47 | 47 | },
|
48 | 48 | },
|
49 | 49 | {
|
50 |
| - "description": "BoundingClientRect has nonzero dimensions", |
51 |
| - "test": function (element) { |
| 50 | + description: "BoundingClientRect has nonzero dimensions", |
| 51 | + test: function (element) { |
52 | 52 | var rect = element.getBoundingClientRect();
|
53 | 53 | if (!rect || rect.width == 0 || rect.height == 0) {
|
54 | 54 | return false;
|
|
57 | 57 | },
|
58 | 58 | },
|
59 | 59 | {
|
60 |
| - "description": "Is visible and displayed", |
61 |
| - "test": function (element) { |
| 60 | + description: "Is visible and displayed", |
| 61 | + test: function (element) { |
62 | 62 | var computedStyle = window.getComputedStyle(element, null);
|
63 | 63 | if (
|
64 | 64 | computedStyle.getPropertyValue("visibility") != "visible" ||
|
|
70 | 70 | },
|
71 | 71 | },
|
72 | 72 | {
|
73 |
| - "description": "Has ClientRect", |
74 |
| - "test": function (element) { |
| 73 | + description: "Has ClientRect", |
| 74 | + test: function (element) { |
75 | 75 | var clientRect = element.getClientRects()[0];
|
76 | 76 | if (!clientRect) {
|
77 | 77 | return false;
|
|
80 | 80 | },
|
81 | 81 | },
|
82 | 82 | {
|
83 |
| - "description": "ClientRect has nonzero dimensions", |
84 |
| - "test": function (element) { |
| 83 | + description: "ClientRect has nonzero dimensions", |
| 84 | + test: function (element) { |
85 | 85 | var clientRect = element.getClientRects()[0];
|
86 |
| - if (!clientRect || clientRect.width == 0 || clientRect.height == 0) { |
| 86 | + if ( |
| 87 | + !clientRect || |
| 88 | + clientRect.width == 0 || |
| 89 | + clientRect.height == 0 |
| 90 | + ) { |
87 | 91 | return false;
|
88 | 92 | }
|
89 | 93 | return true;
|
|
93 | 97 |
|
94 | 98 | function displayTests() {
|
95 | 99 | for (var i = 0; i < visibilityTests.length; i++) {
|
96 |
| - document.getElementById("testDescriptions").innerHTML += "<b>Test " + (i + 1) + |
97 |
| - ": </b>" + visibilityTests[i].description + "<br/>"; |
| 100 | + document.getElementById("testDescriptions").innerHTML += |
| 101 | + "<b>Test " + |
| 102 | + (i + 1) + |
| 103 | + ": </b>" + |
| 104 | + visibilityTests[i].description + |
| 105 | + "<br/>"; |
98 | 106 | }
|
99 | 107 | }
|
100 | 108 |
|
|
135 | 143 | var td = makeTag("td", expectedResult);
|
136 | 144 | td.style.background = netResult === expectedResult ? "#fff" : "#ccf";
|
137 | 145 | table.appendChild(td);
|
138 |
| - table.appendChild(makeTag("td", divs[i].getAttribute("data-comment"))); |
| 146 | + table.appendChild( |
| 147 | + makeTag("td", divs[i].getAttribute("data-comment")), |
| 148 | + ); |
139 | 149 |
|
140 | 150 | // hide the test cases once we're done with them
|
141 | 151 | divs[i].style.visibility = "hidden";
|
|
153 | 163 | </table>
|
154 | 164 |
|
155 | 165 | <div id="testContainer" style="position: absolute; top: 0; left: 0">
|
156 |
| - <span class="testElement" data-expectedresult="1" data-comment="default">test</span> |
| 166 | + <span class="testElement" data-expectedresult="1" data-comment="default" |
| 167 | + >test</span |
| 168 | + > |
157 | 169 |
|
158 | 170 | <span
|
159 | 171 | class="testElement"
|
160 | 172 | data-expectedresult="0"
|
161 | 173 | style="visibility: hidden"
|
162 | 174 | data-comment="visibility: hidden"
|
163 |
| - >test</span> |
| 175 | + >test</span |
| 176 | + > |
164 | 177 |
|
165 | 178 | <div style="visibility: hidden">
|
166 | 179 | <span
|
167 | 180 | class="testElement"
|
168 | 181 | data-expectedresult="0"
|
169 | 182 | data-comment="nested in an element that has visibility: hidden"
|
170 |
| - >test</span> |
| 183 | + >test</span |
| 184 | + > |
171 | 185 | </div>
|
172 | 186 |
|
173 | 187 | <span
|
174 | 188 | class="testElement"
|
175 | 189 | data-expectedresult="0"
|
176 | 190 | style="display: none"
|
177 | 191 | data-comment="display: none"
|
178 |
| - >test</span> |
| 192 | + >test</span |
| 193 | + > |
179 | 194 |
|
180 | 195 | <div style="display: none">
|
181 | 196 | <span
|
182 | 197 | class="testElement"
|
183 | 198 | data-expectedresult="0"
|
184 | 199 | data-comment="nested in an element that has display: none"
|
185 |
| - >test</span> |
| 200 | + >test</span |
| 201 | + > |
186 | 202 | </div>
|
187 | 203 |
|
188 | 204 | <span
|
189 | 205 | class="testElement"
|
190 | 206 | data-expectedresult="0"
|
191 | 207 | style="position: absolute; top: 2000px"
|
192 | 208 | data-comment="outside viewport"
|
193 |
| - >test</span> |
| 209 | + >test</span |
| 210 | + > |
194 | 211 |
|
195 | 212 | <div style="opacity: 0">
|
196 | 213 | <span
|
197 | 214 | class="testElement"
|
198 | 215 | data-expectedresult="1"
|
199 | 216 | data-comment="nested in an element that has opacity:0"
|
200 |
| - >test</span> |
| 217 | + >test</span |
| 218 | + > |
201 | 219 | </div>
|
202 | 220 | <div
|
203 | 221 | class="testElement"
|
|
0 commit comments