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
246
247
248
249
250
251
252
253
|
(function() {
// Test is initiated from body.onload, so explicit done() call is required.
setup({ explicit_done: true });
function checkSubtreeExpectedValues(t, parent, prefix)
{
var checkedLayout = checkExpectedValues(t, parent, prefix);
Array.prototype.forEach.call(parent.childNodes, function(node) {
checkedLayout |= checkSubtreeExpectedValues(t, node, prefix);
});
return checkedLayout;
}
function checkAttribute(output, node, attribute)
{
var result = node.getAttribute && node.getAttribute(attribute);
output.checked |= !!result;
return result;
}
function assert_tolerance(actual, expected, message)
{
if (isNaN(expected) || isNaN(actual) || Math.abs(actual - expected) >= 1) {
assert_equals(actual, Number(expected), message);
}
}
function checkDataKeys(node) {
// The purpose of this list of data-* attributes is simply to ensure typos
// in tests are caught. It is therefore "ok" to add to this list for
// specific tests.
var validData = new Set([
"data-anchor-polyfill",
"data-expected-width",
"data-expected-height",
"data-offset-x",
"data-offset-y",
"data-expected-client-width",
"data-expected-client-height",
"data-expected-scroll-width",
"data-expected-scroll-height",
"data-expected-bounding-client-rect-width",
"data-expected-bounding-client-rect-height",
"data-total-x",
"data-total-y",
"data-expected-display",
"data-expected-padding-top",
"data-expected-padding-bottom",
"data-expected-padding-left",
"data-expected-padding-right",
"data-expected-margin-top",
"data-expected-margin-bottom",
"data-expected-margin-left",
"data-expected-margin-right"
]);
if (!node || !node.getAttributeNames)
return;
// Use "data-test" prefix if you need custom-named data elements.
for (let name of node.getAttributeNames()) {
if (name.startsWith("data-") && !name.startsWith("data-test"))
assert_true(validData.has(name), name + " is a valid data attribute");
}
}
function checkExpectedValues(t, node, prefix)
{
checkDataKeys(node);
var output = { checked: false };
var expectedWidth = checkAttribute(output, node, "data-expected-width");
if (expectedWidth) {
assert_tolerance(node.offsetWidth, expectedWidth, prefix + "width");
}
var expectedHeight = checkAttribute(output, node, "data-expected-height");
if (expectedHeight) {
assert_tolerance(node.offsetHeight, expectedHeight, prefix + "height");
}
var expectedOffset = checkAttribute(output, node, "data-offset-x");
if (expectedOffset) {
assert_tolerance(node.offsetLeft, expectedOffset, prefix + "offsetLeft");
}
var expectedOffset = checkAttribute(output, node, "data-offset-y");
if (expectedOffset) {
assert_tolerance(node.offsetTop, expectedOffset, prefix + "offsetTop");
}
var expectedWidth = checkAttribute(output, node, "data-expected-client-width");
if (expectedWidth) {
assert_tolerance(node.clientWidth, expectedWidth, prefix + "clientWidth");
}
var expectedHeight = checkAttribute(output, node, "data-expected-client-height");
if (expectedHeight) {
assert_tolerance(node.clientHeight, expectedHeight, prefix + "clientHeight");
}
var expectedWidth = checkAttribute(output, node, "data-expected-scroll-width");
if (expectedWidth) {
assert_tolerance(node.scrollWidth, expectedWidth, prefix + "scrollWidth");
}
var expectedHeight = checkAttribute(output, node, "data-expected-scroll-height");
if (expectedHeight) {
assert_tolerance(node.scrollHeight, expectedHeight, prefix + "scrollHeight");
}
var expectedWidth = checkAttribute(output, node, "data-expected-bounding-client-rect-width");
if (expectedWidth) {
assert_tolerance(node.getBoundingClientRect().width, expectedWidth, prefix + "getBoundingClientRect().width");
}
var expectedHeight = checkAttribute(output, node, "data-expected-bounding-client-rect-height");
if (expectedHeight) {
assert_tolerance(node.getBoundingClientRect().height, expectedHeight, prefix + "getBoundingClientRect().height");
}
var expectedOffset = checkAttribute(output, node, "data-total-x");
if (expectedOffset) {
var totalLeft = node.clientLeft + node.offsetLeft;
assert_tolerance(totalLeft, expectedOffset, prefix +
"clientLeft+offsetLeft (" + node.clientLeft + " + " + node.offsetLeft + ")");
}
var expectedOffset = checkAttribute(output, node, "data-total-y");
if (expectedOffset) {
var totalTop = node.clientTop + node.offsetTop;
assert_tolerance(totalTop, expectedOffset, prefix +
"clientTop+offsetTop (" + node.clientTop + " + " + node.offsetTop + ")");
}
var expectedDisplay = checkAttribute(output, node, "data-expected-display");
if (expectedDisplay) {
var actualDisplay = getComputedStyle(node).display;
assert_equals(actualDisplay, expectedDisplay, prefix + "display");
}
var expectedPaddingTop = checkAttribute(output, node, "data-expected-padding-top");
if (expectedPaddingTop) {
var actualPaddingTop = getComputedStyle(node).paddingTop;
// Trim the unit "px" from the output.
actualPaddingTop = actualPaddingTop.slice(0, -2);
assert_equals(actualPaddingTop, expectedPaddingTop, prefix + "padding-top");
}
var expectedPaddingBottom = checkAttribute(output, node, "data-expected-padding-bottom");
if (expectedPaddingBottom) {
var actualPaddingBottom = getComputedStyle(node).paddingBottom;
// Trim the unit "px" from the output.
actualPaddingBottom = actualPaddingBottom.slice(0, -2);
assert_equals(actualPaddingBottom, expectedPaddingBottom, prefix + "padding-bottom");
}
var expectedPaddingLeft = checkAttribute(output, node, "data-expected-padding-left");
if (expectedPaddingLeft) {
var actualPaddingLeft = getComputedStyle(node).paddingLeft;
// Trim the unit "px" from the output.
actualPaddingLeft = actualPaddingLeft.slice(0, -2);
assert_equals(actualPaddingLeft, expectedPaddingLeft, prefix + "padding-left");
}
var expectedPaddingRight = checkAttribute(output, node, "data-expected-padding-right");
if (expectedPaddingRight) {
var actualPaddingRight = getComputedStyle(node).paddingRight;
// Trim the unit "px" from the output.
actualPaddingRight = actualPaddingRight.slice(0, -2);
assert_equals(actualPaddingRight, expectedPaddingRight, prefix + "padding-right");
}
var expectedMarginTop = checkAttribute(output, node, "data-expected-margin-top");
if (expectedMarginTop) {
var actualMarginTop = getComputedStyle(node).marginTop;
// Trim the unit "px" from the output.
actualMarginTop = actualMarginTop.slice(0, -2);
assert_equals(actualMarginTop, expectedMarginTop, prefix + "margin-top");
}
var expectedMarginBottom = checkAttribute(output, node, "data-expected-margin-bottom");
if (expectedMarginBottom) {
var actualMarginBottom = getComputedStyle(node).marginBottom;
// Trim the unit "px" from the output.
actualMarginBottom = actualMarginBottom.slice(0, -2);
assert_equals(actualMarginBottom, expectedMarginBottom, prefix + "margin-bottom");
}
var expectedMarginLeft = checkAttribute(output, node, "data-expected-margin-left");
if (expectedMarginLeft) {
var actualMarginLeft = getComputedStyle(node).marginLeft;
// Trim the unit "px" from the output.
actualMarginLeft = actualMarginLeft.slice(0, -2);
assert_equals(actualMarginLeft, expectedMarginLeft, prefix + "margin-left");
}
var expectedMarginRight = checkAttribute(output, node, "data-expected-margin-right");
if (expectedMarginRight) {
var actualMarginRight = getComputedStyle(node).marginRight;
// Trim the unit "px" from the output.
actualMarginRight = actualMarginRight.slice(0, -2);
assert_equals(actualMarginRight, expectedMarginRight, prefix + "margin-right");
}
return output.checked;
}
var testNumber = 0;
var highlightError = false; // displays outline around failed test element.
var printDomOnError = true; // prints dom when test fails.
window.checkLayout = function(selectorList, callDone = true)
{
if (!selectorList) {
console.error("You must provide a CSS selector of nodes to check.");
return;
}
var nodes = document.querySelectorAll(selectorList);
nodes = Array.prototype.slice.call(nodes);
var checkedLayout = false;
Array.prototype.forEach.call(nodes, function(node) {
const title = node.title == '' ? '' : `: ${node.title}`;
test(function(t) {
var container = node.parentNode.className == 'container' ? node.parentNode : node;
var prefix =
printDomOnError ? '\n' + container.outerHTML + '\n' : '';
var passed = false;
try {
checkedLayout |= checkExpectedValues(t, node.parentNode, prefix);
checkedLayout |= checkSubtreeExpectedValues(t, node, prefix);
passed = true;
} finally {
if (!passed && highlightError) {
if (!document.getElementById('testharness_error_css')) {
var style = document.createElement('style');
style.id = 'testharness_error_css';
style.textContent = '.testharness_error { outline: red dotted 2px !important; }';
document.body.appendChild(style);
}
if (node)
node.classList.add('testharness_error');
}
checkedLayout |= !passed;
}
}, `${selectorList} ${++testNumber}${title}`);
});
if (!checkedLayout) {
console.error("No valid data-* attributes found in selector list : " + selectorList);
}
if (callDone)
done();
};
})();
|