new offset method, faster and no more browser detection
[jquery.git] / test / unit / offset.js
1 module("offset");
2
3 // opens a new window to run the tests against
4 var testwin = function(name, fn) {
5         testwin[name] = load_offset_fixture(name);
6         var interval = setInterval(function() {
7                 if (testwin[name] && testwin[name].$ && testwin[name].jQuery.isReady) {
8                         clearInterval(interval);
9                         test(name, fn);
10                 }
11         }, 0);
12         
13         function load_offset_fixture(name) {
14                 var win = window.open( "./data/offset/" + name + ".html?num"+parseInt(Math.random()*1000), name, 'left=0,top=0,width=500,height=500,toolbar=1,resizable=0' );
15                 if ( !win ) { 
16                         alert("Please disable your popup blocker for the offset test suite");
17                         throw "Please disable your popup blocker for the offset test suite";
18                 }
19                 return win;
20         }
21 };
22
23 testwin("absolute", function() {
24         var $w = testwin["absolute"].$;
25         
26         equals( $w('#absolute-1').offset().top, 1, "jQuery('#absolute-1').offset().top" );
27         equals( $w('#absolute-1').offset().left, 1, "jQuery('#absolute-1').offset().left" );
28         
29         equals( $w('#absolute-1-1').offset().top, 5, "jQuery('#absolute-1-1').offset().top" );
30         equals( $w('#absolute-1-1').offset().left, 5, "jQuery('#absolute-1-1').offset().left" );
31         
32         equals( $w('#absolute-1-1-1').offset().top, 9, "jQuery('#absolute-1-1-1').offset().top" );
33         equals( $w('#absolute-1-1-1').offset().left, 9, "jQuery('#absolute-1-1-1').offset().left" );
34         
35         equals( $w('#absolute-2').offset().top, 20, "jQuery('#absolute-2').offset().top" );
36         equals( $w('#absolute-2').offset().left, 20, "jQuery('#absolute-2').offset().left" );
37         
38         
39         equals( $w('#absolute-1').position().top, 0, "jQuery('#absolute-1').position().top" );
40         equals( $w('#absolute-1').position().left, 0, "jQuery('#absolute-1').position().left" );
41         
42         equals( $w('#absolute-1-1').position().top, 1, "jQuery('#absolute-1-1').position().top" );
43         equals( $w('#absolute-1-1').position().left, 1, "jQuery('#absolute-1-1').position().left" );
44         
45         equals( $w('#absolute-1-1-1').position().top, 1, "jQuery('#absolute-1-1-1').position().top" );
46         equals( $w('#absolute-1-1-1').position().left, 1, "jQuery('#absolute-1-1-1').position().left" );
47         
48         equals( $w('#absolute-2').position().top, 19, "jQuery('#absolute-2').position().top" );
49         equals( $w('#absolute-2').position().left, 19, "jQuery('#absolute-2').position().left" );
50         
51         testwin["absolute"].close();
52 });
53
54 testwin("relative", function() {
55         var $w = testwin["relative"].$;
56         
57         // IE is collapsing the top margin of 1px
58         equals( $w('#relative-1').offset().top, jQuery.browser.msie ? 6 : 7, "jQuery('#relative-1').offset().top" );
59         equals( $w('#relative-1').offset().left, 7, "jQuery('#relative-1').offset().left" );
60         
61         // IE is collapsing the top margin of 1px
62         equals( $w('#relative-1-1').offset().top, jQuery.browser.msie ? 13 : 15, "jQuery('#relative-1-1').offset().top" );
63         equals( $w('#relative-1-1').offset().left, 15, "jQuery('#relative-1-1').offset().left" );
64         
65         // IE is collapsing the top margin of 1px
66         equals( $w('#relative-2').offset().top, jQuery.browser.msie ? 141 : 142, "jQuery('#relative-2').offset().top" );
67         equals( $w('#relative-2').offset().left, 27, "jQuery('#relative-2').offset().left" );
68         
69         
70         // IE is collapsing the top margin of 1px
71         equals( $w('#relative-1').position().top, jQuery.browser.msie ? 5 : 6, "jQuery('#relative-1').position().top" );
72         equals( $w('#relative-1').position().left, 6, "jQuery('#relative-1').position().left" );
73         
74         // IE is collapsing the top margin of 1px
75         equals( $w('#relative-1-1').position().top, jQuery.browser.msie ? 4 : 5, "jQuery('#relative-1-1').position().top" );
76         equals( $w('#relative-1-1').position().left, 5, "jQuery('#relative-1-1').position().left" );
77         
78         // IE is collapsing the top margin of 1px
79         equals( $w('#relative-2').position().top, jQuery.browser.msie ? 140 : 141, "jQuery('#relative-2').position().top" );
80         equals( $w('#relative-2').position().left, 26, "jQuery('#relative-2').position().left" );
81         
82         testwin["relative"].close();
83 });
84
85 testwin("static", function() {
86         var $w = testwin["static"].$;
87         
88         // IE is collapsing the top margin of 1px
89         equals( $w('#static-1').offset().top, jQuery.browser.msie ? 6 : 7, "jQuery('#static-1').offset().top" );
90         equals( $w('#static-1').offset().left, 7, "jQuery('#static-1').offset().left" );
91         
92         // IE is collapsing the top margin of 1px
93         equals( $w('#static-1-1').offset().top, jQuery.browser.msie ? 13 : 15, "jQuery('#static-1-1').offset().top" );
94         equals( $w('#static-1-1').offset().left, 15, "jQuery('#static-1-1').offset().left" );
95         
96         // IE is collapsing the top margin of 1px
97         equals( $w('#static-1-1-1').offset().top, jQuery.browser.msie ? 20 : 23, "jQuery('#static-1-1-1').offset().top" );
98         equals( $w('#static-1-1-1').offset().left, 23, "jQuery('#static-1-1-1').offset().left" );
99         
100         // IE is collapsing the top margin of 1px
101         equals( $w('#static-2').offset().top, jQuery.browser.msie ? 121 : 122, "jQuery('#static-2').offset().top" );
102         equals( $w('#static-2').offset().left, 7, "jQuery('#static-2').offset().left" );
103         
104         
105         // IE is collapsing the top margin of 1px
106         equals( $w('#static-1').position().top, jQuery.browser.msie ? 5 : 6, "jQuery('#static-1').position().top" );
107         equals( $w('#static-1').position().left, 6, "jQuery('#static-1').position().left" );
108         
109         // IE is collapsing the top margin of 1px
110         equals( $w('#static-1-1').position().top, jQuery.browser.msie ? 12 : 14, "jQuery('#static-1-1').position().top" );
111         equals( $w('#static-1-1').position().left, 14, "jQuery('#static-1-1').position().left" );
112         
113         // IE is collapsing the top margin of 1px
114         equals( $w('#static-1-1-1').position().top, jQuery.browser.msie ? 19 : 22, "jQuery('#static-1-1-1').position().top" );
115         equals( $w('#static-1-1-1').position().left, 22, "jQuery('#static-1-1-1').position().left" );
116         
117         // IE is collapsing the top margin of 1px
118         equals( $w('#static-2').position().top, jQuery.browser.msie ? 120 : 121, "jQuery('#static-2').position().top" );
119         equals( $w('#static-2').position().left, 6, "jQuery('#static-2').position().left" );
120         
121         testwin["static"].close();
122 });
123
124 if ( !jQuery.browser.msie || (jQuery.browser.msie && parseInt(jQuery.browser.version) > 6) )
125         testwin("fixed", function() {
126                 var $w = testwin["fixed"].$;
127         
128                 equals( $w('#fixed-1').offset().top, 1001, "jQuery('#fixed-1').offset().top" );
129                 equals( $w('#fixed-1').offset().left, 1001, "jQuery('#fixed-1').offset().left" );
130         
131                 equals( $w('#fixed-2').offset().top, 1021, "jQuery('#fixed-2').offset().top" );
132                 equals( $w('#fixed-2').offset().left, 1021, "jQuery('#fixed-2').offset().left" );
133         
134                 testwin["fixed"].close();
135         });
136
137 testwin("table", function() {
138         var $w = testwin["table"].$;
139         
140         equals( $w('#table-1').offset().top, 6, "jQuery('#table-1').offset().top" );
141         equals( $w('#table-1').offset().left, 6, "jQuery('#table-1').offset().left" );
142         
143         equals( $w('#th-1').offset().top, 10, "jQuery('#table-1').offset().top" );
144         equals( $w('#th-1').offset().left, 10, "jQuery('#table-1').offset().left" );
145         
146         equals( $w('#th-2').offset().top, 10, "jQuery('#table-1').offset().top" );
147         equals( $w('#th-2').offset().left, 116, "jQuery('#table-1').offset().left" );
148         
149         testwin["table"].close();
150 });
151
152 testwin("scroll", function() {
153         var $w = testwin["scroll"].$;
154         
155         // IE is collapsing the top margin of 1px
156         equals( $w('#scroll-1').offset().top, jQuery.browser.msie ? 6 : 7, "jQuery('#scroll-1').offset().top" );
157         equals( $w('#scroll-1').offset().left, 7, "jQuery('#scroll-1').offset().left" );
158         
159         // IE is collapsing the top margin of 1px
160         equals( $w('#scroll-1-1').offset().top, jQuery.browser.msie ? 9 : 11, "jQuery('#scroll-1-1').offset().top" );
161         equals( $w('#scroll-1-1').offset().left, 11, "jQuery('#scroll-1-1').offset().left" );
162         
163         testwin["scroll"].close();
164 });
165
166 testwin("body", function() {
167         var $w = testwin["body"].$;
168         
169         equals( $w('body').offset().top, 1, "jQuery('#body').offset().top" );
170         equals( $w('body').offset().left, 1, "jQuery('#body').offset().left" );
171         
172         testwin["body"].close();
173 });