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
|
<script src="../../../resources/ahem.js"></script>
<script src="../../../resources/js-test.js"></script>
<style>
body {
font: 16px Ahem;
}
.bbox {
position:absolute;
outline: 5px solid rgba(255, 0, 0, .75);
z-index: -1;
}
.box {
width: 400px;
line-height: 40px;
}
.outer {
outline: 2px solid green;
}
.inner {
display: inline-block;
width: 40px;
height: 20px;
outline: 2px solid blue;
}
#test5 {
transform: translate(50px, 100px) rotate(50deg);
}
#console {
position:absolute;
left: 500px;
}
#testArea {
width: 300px;
}
</style>
<div id="console"></div>
<div id="testArea">
<div class="box" id="test1">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<br><br>
<div class="box" id="test2">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<br><br>
<div class="box" id="test3">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<br><br>
<div class="box" id="test4">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<br><br>
<div class="box" id="test5">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
</div>
<script>
if (window.testRunner)
testRunner.dumpAsText();
else
alert("WARNING:\nThis test may show bogus FAILures if not run in DumpRenderTree, due to platform-specific differences in font metrics.");
function testClientRect(rect, expectedRect)
{
shouldBeEqualToString("rect.left.toFixed(3)", expectedRect.left.toFixed(3));
shouldBeEqualToString("rect.top.toFixed(3)", expectedRect.top.toFixed(3));
shouldBeEqualToString("rect.width.toFixed(3)", expectedRect.width.toFixed(3));
shouldBeEqualToString("rect.height.toFixed(3)", expectedRect.height.toFixed(3));
if (rect.right == Math.round(rect.right))
shouldBe("rect.right", "rect.left + rect.width");
else
shouldBe("Math.abs(rect.left + rect.width - rect.right) < 0.001", "true");
if (rect.bottom == Math.round(rect.bottom))
shouldBe("rect.bottom", "rect.top + rect.height");
else
shouldBe("Math.abs(rect.top + rect.height - rect.bottom) < 0.001", "true");
debug("");
}
function addBBoxOverClientRect(rect)
{
var bbox = document.createElement('div');
bbox.className = "bbox";
var style = "";
style += "left: " + rect.left + "px;";
style += "top: " + rect.top + "px;";
style += "width: " + (rect.right - rect.left) + "px;";
style += "height: " + (rect.bottom - rect.top) + "px;";
bbox.setAttribute("style", style);
document.documentElement.appendChild(bbox);
}
function show(range)
{
if (window.testRunner)
return;
addBBoxOverClientRect(range.getBoundingClientRect());
}
var expectedResults = [
/*1*/ { left: 8, top: 8, width: 400, height: 400 },
/*2*/ { left: 8, top: 452, width: 400, height: 376 },
/*3*/ { left: 8, top: 1044, width: 400, height: 96 },
/*4*/ { left: 0, top: 0, width: 0, height: 0 },
/*5*/ { left: -14.574, top: 1761.947, width: 504.009, height: 535.849 },
/*6*/ { left: 0, top: 0, width: 0, height: 0 },
];
// Range over entire element.
debug("Test 1")
var range1 = document.createRange();
range1.selectNode(document.getElementById('test1'));
show(range1);
rect = range1.getBoundingClientRect();
testClientRect(rect, expectedResults[1 - 1]);
// Range over entire element's contents.
debug("Test 2")
var range2 = document.createRange();
range2.selectNodeContents(document.getElementById('test2'));
show(range2);
rect = range2.getBoundingClientRect();
testClientRect(rect, expectedResults[2 - 1]);
// Range over subset of element's contents.
debug("Test 3")
var range3 = document.createRange();
range3.setStart(document.getElementById('test3').firstChild, 100);
range3.setEnd(document.getElementById('test3').lastChild, 150);
show(range3);
rect = range3.getBoundingClientRect()
testClientRect(rect, expectedResults[3 - 1]);
// Collapsed range.
debug("Test 4")
var range4 = document.createRange();
range4.selectNodeContents(document.getElementById('test4'));
range4.collapse(true);
show(range4);
rect = range4.getBoundingClientRect()
testClientRect(rect, expectedResults[4 - 1]);
// Range over transformed elements.
debug("Test 5")
var range5 = document.createRange();
range5.selectNodeContents(document.getElementById('test5'));
show(range5);
rect = range5.getBoundingClientRect()
testClientRect(rect, expectedResults[5 - 1]);
// Empty range.
debug("Test 6")
var range6 = document.createRange();
rect = range6.getBoundingClientRect()
testClientRect(rect, expectedResults[6 - 1]);
if (window.testRunner) {
var area = document.getElementById('testArea');
area.parentNode.removeChild(area);
}
</script>
|