forked from mootools/mootools-core-specs
/
Element.Dimensions.js
258 lines (209 loc) · 5.92 KB
/
Element.Dimensions.js
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
254
255
256
257
258
/*
Script: Element.Dimensions.js
Specs for Element.Dimensions.js
License:
MIT-style license.
*/
describe('Element.Dimensions', function(){
var div, relDiv, absDiv, scrollDiv, tallDiv, fixedDiv, fixedDivChild, staticDiv, staticDivChild, table, orphanDiv;
beforeEach(function(){
/*
// overview of element structure, please update if anything has changed
// theres also an orphanDiv
<document>
<div absolute>
<relDiv relative>
<absDiv absolute>
</absDiv>
</relDiv>
</div>
<scrollDiv absolute>
<tallDiv></tallDiv>
</scrollDiv>
<fixedDiv fixed>
<fixedDivChild></fixedDivChild>
</fixedDiv>
<table></table>
<staticDiv>
<staticDivChild absolute></staticDivChild>
</staticDiv>
</document>
*/
div = new Element('div', {
id: 'ElementDimensionsTest',
styles: {
width: 100,
height: 100,
margin: 2,
padding: 3,
border: '1px solid black',
visibility: 'hidden',
display: 'block',
position: 'absolute',
top: 100,
left: 100,
overflow: 'hidden',
zIndex: 1
}
}).inject($(document.body));
relDiv = new Element('div', {
styles: {
width: 50,
height: 50,
margin: 5,
padding: 5,
border: '1px solid green',
visibility: 'hidden',
position: 'relative',
overflow: 'hidden',
'float': 'left',
'display': 'inline'
}
}).inject(div);
absDiv = new Element('div', {
styles: {
width: 10,
height: 10,
margin: 5,
padding: 5,
border: '1px solid red',
visibility: 'hidden',
position: 'absolute',
top: 10,
left: 10,
overflow: 'hidden'
}
}).inject(relDiv);
scrollDiv = new Element('div', {
styles: {
width: 100,
height: 100,
overflow: 'scroll',
visibility: 'hidden',
position: 'absolute',
top: 0,
left: 0
}
}).inject($(document.body));
tallDiv = new Element('div', {
styles: {
width: 200,
height: 200,
visibility: 'hidden'
}
}).inject(scrollDiv);
fixedDiv = new Element('div', {
styles: {
width: 200,
height: 200,
position: 'fixed',
top: 300,
left: 300,
visibility: 'hidden'
}
}).inject($(document.body));
fixedDivChild = new Element('div', {
styles: {
width: 50,
height: 50,
visibility: 'hidden'
}
}).inject(fixedDiv);
staticDiv = new Element('div', {
styles: {
width: 50,
height: 50,
visibility: 'hidden'
}
}).inject($(document.body));
staticDivChild = new Element('div', {
styles: {
width: 50,
height: 50,
visibility: 'hidden'
}
}).inject(staticDiv);
table = new Element('table').inject($(document.body));
table.set('html','\
<tbody>\
<tr>\
<td><span>a</span><div style="position: relative">b</div></td>\
<th><span>a</span><div style="position: relative">b</div></th>\
</tr>\
</tbody>\
');
orphanDiv = new Element('div', {
styles: {
position: 'absolute',
top: 0,
left: 0,
width: 50,
height: 50,
visibility: 'hidden'
}
});
});
afterEach(function(){
[div, relDiv, absDiv, scrollDiv, tallDiv, fixedDiv, fixedDivChild, staticDiv, staticDivChild, table].each(function(el){
$(el).destroy();
});
});
describe('Element.getSize', function(){
it('should measure the width and height of the element', function(){
expect(div.getSize().x).toEqual(108);
expect(div.getSize().y).toEqual(108);
});
});
describe('Element.getPosition', function(){
it('should measure the x and y position of the element', function(){
expect(div.getPosition()).toEqual({x: 102, y: 102});
});
it('should measure the x and y position of the element relative to another', function(){
expect(relDiv.getPosition(div)).toEqual({x: 8, y: 8});
});
});
describe('Element.getCoordinates', function(){
it('should return the coordinates relative to parent', function(){
expect(absDiv.getCoordinates(relDiv)).toEqual({left:15, top:15, width:22, height:22, right:37, bottom:37});
});
});
describe('Element.getScrollSize', function(){
it('should return the scrollSize', function(){
expect(scrollDiv.getScrollSize()).toEqual({x:200, y:200});
});
});
describe('Element.scrollTo', function(){
it('should scroll the element', function(){
expect(scrollDiv.scrollTo(20, 20).getScroll()).toEqual({x:20, y:20});
});
});
// http://www.w3.org/TR/cssom-view/#offset-attributes
describe('Element.offsetParent', function(){
it('should return null for the root node, the body, fixed positioned nodes and orphan nodes', function(){
expect($(document.documentElement).getOffsetParent()).toEqual(null);
expect($(document.body).getOffsetParent()).toEqual(null);
expect(fixedDiv.getOffsetParent()).toEqual(null);
expect(orphanDiv.getOffsetParent()).toEqual(null);
});
it('should return the body as last option', function(){
expect(div.getOffsetParent()).toEqual(document.body);
expect(staticDivChild.getOffsetParent()).toEqual(document.body);
});
it('should return a table if the element is static and child of a table', function(){
var tr = table.getElement('tr');
expect(tr.getOffsetParent()).toEqual(table);
});
it('should return a td if the element is static and child of a td or the first non-static element if the element is non-static', function(){
var td = table.getElement('td');
expect(td.getOffsetParent()).toEqual(table);
expect(td.getElement('span').getOffsetParent()).toEqual(td);
expect(td.getElement('div').getOffsetParent()).toEqual(document.body);
});
it('should return a th if the element is static and child of a th or the first non-static element if the element is non-static', function(){
var th = table.getElement('th');
expect(th.getOffsetParent()).toEqual(table);
expect(th.getElement('span').getOffsetParent()).toEqual(th);
expect(th.getElement('div').getOffsetParent()).toEqual(document.body);
});
});
});