Skip to content

Commit de6520b

Browse files
committed
Added some unit tests for position method. Fixed issue with position in IE.
1 parent 9a76522 commit de6520b

File tree

7 files changed

+71
-9
lines changed

7 files changed

+71
-9
lines changed

src/offset.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ jQuery.fn.offset = function() {
9191
}
9292

9393
function add(l, t) {
94-
left += parseInt(l) || 0;
95-
top += parseInt(t) || 0;
94+
left += parseInt(l, 10) || 0;
95+
top += parseInt(t, 10) || 0;
9696
}
9797

9898
return results;
@@ -107,17 +107,17 @@ jQuery.fn.extend({
107107
// Get *real* offsetParent
108108
var offsetParent = this.offsetParent(),
109109

110-
// Get correct offsets
111-
offset = this.offset(),
112-
parentOffset = offsetParent.offset();
110+
// Get correct offsets
111+
offset = this.offset(),
112+
parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();
113113

114114
// Subtract element margins
115-
offset.top -= num( this, 'marginTop' );
116-
offset.left -= num( this, 'marginLeft' );
115+
offset.top -= parseInt( jQuery.curCSS( this[0], 'marginTop', true ), 10 ) || 0;
116+
offset.left -= parseInt( jQuery.curCSS( this[0], 'marginLeft', true ), 10 ) || 0;
117117

118118
// Add offsetParent borders
119-
parentOffset.top += num( offsetParent, 'borderTopWidth' );
120-
parentOffset.left += num( offsetParent, 'borderLeftWidth' );
119+
parentOffset.top += parseInt( jQuery.curCSS( offsetParent[0], 'borderTopWidth', true ), 10 ) || 0;
120+
parentOffset.left += parseInt( jQuery.curCSS( offsetParent[0], 'borderLeftWidth', true ), 10 ) || 0;
121121

122122
// Subtract the two offsets
123123
results = {

test/data/offset/absolute.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,15 @@
1212
#absolute-1-1-1 { top: 1px; left: 1px; }
1313
#absolute-2 { top: 19px; left: 19px; }
1414
#marker { position: absolute; border: 2px solid #000; width: 50px; height: 50px; background: #ccc; }
15+
p.instructions { position: absolute; bottom: 0; }
1516
</style>
1617
<script type="text/javascript" src="../../../dist/jquery.js"></script>
1718
<script type="text/javascript" charset="utf-8">
1819
$(function() {
1920
$('.absolute').click(function() {
2021
$('#marker').css( $(this).offset() );
22+
var pos = $(this).position();
23+
$(this).css({ top: pos.top, left: pos.left });
2124
return false;
2225
});
2326
});
@@ -31,5 +34,6 @@
3134
</div>
3235
<div id="absolute-2" class="absolute">absolute-2</div>
3336
<div id="marker"></div>
37+
<p class="instructions">Click the white box to move the marker to it. Clicking the box also changes the position to absolute (if not already) and sets the position according to the position method.</p>
3438
</body>
3539
</html>

test/data/offset/relative.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
$(function() {
1616
$('.relative').click(function() {
1717
$('#marker').css( $(this).offset() );
18+
var pos = $(this).position();
19+
$(this).css({ position: 'absolute', top: pos.top, left: pos.left });
1820
return false;
1921
});
2022
});
@@ -24,5 +26,6 @@
2426
<div id="relative-1" class="relative"><div id="relative-1-1" class="relative"><div id="relative-1-1-1" class="relative"></div></div></div>
2527
<div id="relative-2" class="relative"></div>
2628
<div id="marker"></div>
29+
<p class="instructions">Click the white box to move the marker to it. Clicking the box also changes the position to absolute (if not already) and sets the position according to the position method.</p>
2730
</body>
2831
</html>

test/data/offset/scroll.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
</div>
3535
<div id="forceScroll"></div>
3636
<div id="marker"></div>
37+
<p class="instructions">Click the white box to move the marker to it. Clicking the box also changes the position to absolute (if not already) and sets the position according to the position method.</p>
3738
</body>
3839
</html>

test/data/offset/static.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
<div id="static-1" class="static"><div id="static-1-1" class="static"><div id="static-1-1-1" class="static"></div></div></div>
2525
<div id="static-2" class="static"></div>
2626
<div id="marker"></div>
27+
<p class="instructions">Click the white box to move the marker to it. Clicking the box also changes the position to absolute (if not already) and sets the position according to the position method.</p>
2728
</body>
2829
</html>

test/data/offset/table.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
</tbody>
3939
</table>
4040
<div id="marker"></div>
41+
<p class="instructions">Click the white box to move the marker to it. Clicking the box also changes the position to absolute (if not already) and sets the position according to the position method.</p>
4142
</body>
4243
</html>

test/unit/offset.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,39 +35,89 @@ testwin("absolute", function() {
3535
equals( $w('#absolute-2').offset().top, 20, "$('#absolute-2').offset().top" );
3636
equals( $w('#absolute-2').offset().left, 20, "$('#absolute-2').offset().left" );
3737

38+
39+
equals( $w('#absolute-1').position().top, 0, "$('#absolute-1').position().top" );
40+
equals( $w('#absolute-1').position().left, 0, "$('#absolute-1').position().left" );
41+
42+
equals( $w('#absolute-1-1').position().top, 1, "$('#absolute-1-1').position().top" );
43+
equals( $w('#absolute-1-1').position().left, 1, "$('#absolute-1-1').position().left" );
44+
45+
equals( $w('#absolute-1-1-1').position().top, 1, "$('#absolute-1-1-1').position().top" );
46+
equals( $w('#absolute-1-1-1').position().left, 1, "$('#absolute-1-1-1').position().left" );
47+
48+
equals( $w('#absolute-2').position().top, 19, "$('#absolute-2').position().top" );
49+
equals( $w('#absolute-2').position().left, 19, "$('#absolute-2').position().left" );
50+
3851
testwin["absolute"].close();
3952
});
4053

4154
testwin("relative", function() {
4255
var $w = testwin["relative"].$;
4356

57+
// IE is collapsing the top margin of 1px
4458
equals( $w('#relative-1').offset().top, $.browser.msie ? 6 : 7, "$('#relative-1').offset().top" );
4559
equals( $w('#relative-1').offset().left, 7, "$('#relative-1').offset().left" );
4660

61+
// IE is collapsing the top margin of 1px
4762
equals( $w('#relative-1-1').offset().top, $.browser.msie ? 13 : 15, "$('#relative-1-1').offset().top" );
4863
equals( $w('#relative-1-1').offset().left, 15, "$('#relative-1-1').offset().left" );
4964

65+
// IE is collapsing the top margin of 1px
5066
equals( $w('#relative-2').offset().top, $.browser.msie ? 141 : 142, "$('#relative-2').offset().top" );
5167
equals( $w('#relative-2').offset().left, 27, "$('#relative-2').offset().left" );
5268

69+
70+
// IE is collapsing the top margin of 1px
71+
equals( $w('#relative-1').position().top, $.browser.msie ? 5 : 6, "$('#relative-1').position().top" );
72+
equals( $w('#relative-1').position().left, 6, "$('#relative-1').position().left" );
73+
74+
// IE is collapsing the top margin of 1px
75+
equals( $w('#relative-1-1').position().top, $.browser.msie ? 4 : 5, "$('#relative-1-1').position().top" );
76+
equals( $w('#relative-1-1').position().left, 5, "$('#relative-1-1').position().left" );
77+
78+
// IE is collapsing the top margin of 1px
79+
equals( $w('#relative-2').position().top, $.browser.msie ? 140 : 141, "$('#relative-2').position().top" );
80+
equals( $w('#relative-2').position().left, 26, "$('#relative-2').position().left" );
81+
5382
testwin["relative"].close();
5483
});
5584

5685
testwin("static", function() {
5786
var $w = testwin["static"].$;
5887

88+
// IE is collapsing the top margin of 1px
5989
equals( $w('#static-1').offset().top, $.browser.msie ? 6 : 7, "$('#static-1').offset().top" );
6090
equals( $w('#static-1').offset().left, 7, "$('#static-1').offset().left" );
6191

92+
// IE is collapsing the top margin of 1px
6293
equals( $w('#static-1-1').offset().top, $.browser.msie ? 13 : 15, "$('#static-1-1').offset().top" );
6394
equals( $w('#static-1-1').offset().left, 15, "$('#static-1-1').offset().left" );
6495

96+
// IE is collapsing the top margin of 1px
6597
equals( $w('#static-1-1-1').offset().top, $.browser.msie ? 20 : 23, "$('#static-1-1-1').offset().top" );
6698
equals( $w('#static-1-1-1').offset().left, 23, "$('#static-1-1-1').offset().left" );
6799

100+
// IE is collapsing the top margin of 1px
68101
equals( $w('#static-2').offset().top, $.browser.msie ? 121 : 122, "$('#static-2').offset().top" );
69102
equals( $w('#static-2').offset().left, 7, "$('#static-2').offset().left" );
70103

104+
105+
// IE is collapsing the top margin of 1px
106+
equals( $w('#static-1').position().top, $.browser.msie ? 5 : 6, "$('#static-1').position().top" );
107+
equals( $w('#static-1').position().left, 6, "$('#static-1').position().left" );
108+
109+
// IE is collapsing the top margin of 1px
110+
equals( $w('#static-1-1').position().top, $.browser.msie ? 12 : 14, "$('#static-1-1').position().top" );
111+
equals( $w('#static-1-1').position().left, 14, "$('#static-1-1').position().left" );
112+
113+
// IE is collapsing the top margin of 1px
114+
equals( $w('#static-1-1-1').position().top, $.browser.msie ? 19 : 22, "$('#static-1-1-1').position().top" );
115+
equals( $w('#static-1-1-1').position().left, 22, "$('#static-1-1-1').position().left" );
116+
117+
// IE is collapsing the top margin of 1px
118+
equals( $w('#static-2').position().top, $.browser.msie ? 120 : 121, "$('#static-2').position().top" );
119+
equals( $w('#static-2').position().left, 6, "$('#static-2').position().left" );
120+
71121
testwin["static"].close();
72122
});
73123

@@ -102,9 +152,11 @@ testwin("table", function() {
102152
testwin("scroll", function() {
103153
var $w = testwin["scroll"].$;
104154

155+
// IE is collapsing the top margin of 1px
105156
equals( $w('#scroll-1').offset().top, $.browser.msie ? 6 : 7, "$('#scroll-1').offset().top" );
106157
equals( $w('#scroll-1').offset().left, 7, "$('#scroll-1').offset().left" );
107158

159+
// IE is collapsing the top margin of 1px
108160
equals( $w('#scroll-1-1').offset().top, $.browser.msie ? 9 : 11, "$('#scroll-1-1').offset().top" );
109161
equals( $w('#scroll-1-1').offset().left, 11, "$('#scroll-1-1').offset().left" );
110162

0 commit comments

Comments
 (0)