Make sure that .offset() doesn't fail against disconnected DOM nodes. Fixes #4996.
[jquery.git] / src / offset.js
1 (function( jQuery ) {
2
3 var rtable = /^t(?:able|d|h)$/i,
4         rroot = /^(?:body|html)$/i;
5
6 if ( "getBoundingClientRect" in document.documentElement ) {
7         jQuery.fn.offset = function( options ) {
8                 var elem = this[0], box;
9
10                 if ( options ) { 
11                         return this.each(function( i ) {
12                                 jQuery.offset.setOffset( this, options, i );
13                         });
14                 }
15
16                 if ( !elem || !elem.ownerDocument ) {
17                         return null;
18                 }
19
20                 if ( elem === elem.ownerDocument.body ) {
21                         return jQuery.offset.bodyOffset( elem );
22                 }
23
24                 try {
25                         box = elem.getBoundingClientRect();
26
27                 } catch(e) {
28                         box = { top: elem.offsetTop, left: elem.offsetLeft };
29                 }
30
31                 var doc = elem.ownerDocument,
32                         body = doc.body,
33                         docElem = doc.documentElement,
34                         win = getWindow(doc),
35                         clientTop  = docElem.clientTop  || body.clientTop  || 0,
36                         clientLeft = docElem.clientLeft || body.clientLeft || 0,
37                         scrollTop  = (win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop  || body.scrollTop ),
38                         scrollLeft = (win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft),
39                         top  = box.top  + scrollTop  - clientTop,
40                         left = box.left + scrollLeft - clientLeft;
41
42                 return { top: top, left: left };
43         };
44
45 } else {
46         jQuery.fn.offset = function( options ) {
47                 var elem = this[0];
48
49                 if ( options ) { 
50                         return this.each(function( i ) {
51                                 jQuery.offset.setOffset( this, options, i );
52                         });
53                 }
54
55                 if ( !elem || !elem.ownerDocument ) {
56                         return null;
57                 }
58
59                 if ( elem === elem.ownerDocument.body ) {
60                         return jQuery.offset.bodyOffset( elem );
61                 }
62
63                 jQuery.offset.initialize();
64
65                 var offsetParent = elem.offsetParent, prevOffsetParent = elem,
66                         doc = elem.ownerDocument, computedStyle, docElem = doc.documentElement,
67                         body = doc.body, defaultView = doc.defaultView,
68                         prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
69                         top = elem.offsetTop, left = elem.offsetLeft;
70
71                 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
72                         if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
73                                 break;
74                         }
75
76                         computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
77                         top  -= elem.scrollTop;
78                         left -= elem.scrollLeft;
79
80                         if ( elem === offsetParent ) {
81                                 top  += elem.offsetTop;
82                                 left += elem.offsetLeft;
83
84                                 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && rtable.test(elem.nodeName)) ) {
85                                         top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
86                                         left += parseFloat( computedStyle.borderLeftWidth ) || 0;
87                                 }
88
89                                 prevOffsetParent = offsetParent;
90                                 offsetParent = elem.offsetParent;
91                         }
92
93                         if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
94                                 top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
95                                 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
96                         }
97
98                         prevComputedStyle = computedStyle;
99                 }
100
101                 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
102                         top  += body.offsetTop;
103                         left += body.offsetLeft;
104                 }
105
106                 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
107                         top  += Math.max( docElem.scrollTop, body.scrollTop );
108                         left += Math.max( docElem.scrollLeft, body.scrollLeft );
109                 }
110
111                 return { top: top, left: left };
112         };
113 }
114
115 jQuery.offset = {
116         initialize: function() {
117                 var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop") ) || 0,
118                         html = "<div style='position:absolute;top:0;left:0;margin:0;border:5px solid #000;padding:0;width:1px;height:1px;'><div></div></div><table style='position:absolute;top:0;left:0;margin:0;border:5px solid #000;padding:0;width:1px;height:1px;' cellpadding='0' cellspacing='0'><tr><td></td></tr></table>";
119
120                 jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } );
121
122                 container.innerHTML = html;
123                 body.insertBefore( container, body.firstChild );
124                 innerDiv = container.firstChild;
125                 checkDiv = innerDiv.firstChild;
126                 td = innerDiv.nextSibling.firstChild.firstChild;
127
128                 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
129                 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
130
131                 checkDiv.style.position = "fixed";
132                 checkDiv.style.top = "20px";
133
134                 // safari subtracts parent border width here which is 5px
135                 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
136                 checkDiv.style.position = checkDiv.style.top = "";
137
138                 innerDiv.style.overflow = "hidden";
139                 innerDiv.style.position = "relative";
140
141                 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
142
143                 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
144
145                 body.removeChild( container );
146                 body = container = innerDiv = checkDiv = table = td = null;
147                 jQuery.offset.initialize = jQuery.noop;
148         },
149
150         bodyOffset: function( body ) {
151                 var top = body.offsetTop, left = body.offsetLeft;
152
153                 jQuery.offset.initialize();
154
155                 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
156                         top  += parseFloat( jQuery.css(body, "marginTop") ) || 0;
157                         left += parseFloat( jQuery.css(body, "marginLeft") ) || 0;
158                 }
159
160                 return { top: top, left: left };
161         },
162         
163         setOffset: function( elem, options, i ) {
164                 var position = jQuery.css( elem, "position" );
165
166                 // set position first, in-case top/left are set even on static elem
167                 if ( position === "static" ) {
168                         elem.style.position = "relative";
169                 }
170
171                 var curElem = jQuery( elem ),
172                         curOffset = curElem.offset(),
173                         curCSSTop = jQuery.css( elem, "top" ),
174                         curCSSLeft = jQuery.css( elem, "left" ),
175                         calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1),
176                         props = {}, curPosition = {}, curTop, curLeft;
177
178                 // need to be able to calculate position if either top or left is auto and position is absolute
179                 if ( calculatePosition ) {
180                         curPosition = curElem.position();
181                 }
182
183                 curTop  = calculatePosition ? curPosition.top  : parseInt( curCSSTop,  10 ) || 0;
184                 curLeft = calculatePosition ? curPosition.left : parseInt( curCSSLeft, 10 ) || 0;
185
186                 if ( jQuery.isFunction( options ) ) {
187                         options = options.call( elem, i, curOffset );
188                 }
189
190                 if (options.top != null) {
191                         props.top = (options.top - curOffset.top) + curTop;
192                 }
193                 if (options.left != null) {
194                         props.left = (options.left - curOffset.left) + curLeft;
195                 }
196                 
197                 if ( "using" in options ) {
198                         options.using.call( elem, props );
199                 } else {
200                         curElem.css( props );
201                 }
202         }
203 };
204
205
206 jQuery.fn.extend({
207         position: function() {
208                 if ( !this[0] ) {
209                         return null;
210                 }
211
212                 var elem = this[0],
213
214                 // Get *real* offsetParent
215                 offsetParent = this.offsetParent(),
216
217                 // Get correct offsets
218                 offset       = this.offset(),
219                 parentOffset = rroot.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
220
221                 // Subtract element margins
222                 // note: when an element has margin: auto the offsetLeft and marginLeft
223                 // are the same in Safari causing offset.left to incorrectly be 0
224                 offset.top  -= parseFloat( jQuery.css(elem, "marginTop") ) || 0;
225                 offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0;
226
227                 // Add offsetParent borders
228                 parentOffset.top  += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0;
229                 parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0;
230
231                 // Subtract the two offsets
232                 return {
233                         top:  offset.top  - parentOffset.top,
234                         left: offset.left - parentOffset.left
235                 };
236         },
237
238         offsetParent: function() {
239                 return this.map(function() {
240                         var offsetParent = this.offsetParent || document.body;
241                         while ( offsetParent && (!rroot.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
242                                 offsetParent = offsetParent.offsetParent;
243                         }
244                         return offsetParent;
245                 });
246         }
247 });
248
249
250 // Create scrollLeft and scrollTop methods
251 jQuery.each( ["Left", "Top"], function( i, name ) {
252         var method = "scroll" + name;
253
254         jQuery.fn[ method ] = function(val) {
255                 var elem = this[0], win;
256                 
257                 if ( !elem ) {
258                         return null;
259                 }
260
261                 if ( val !== undefined ) {
262                         // Set the scroll offset
263                         return this.each(function() {
264                                 win = getWindow( this );
265
266                                 if ( win ) {
267                                         win.scrollTo(
268                                                 !i ? val : jQuery(win).scrollLeft(),
269                                                  i ? val : jQuery(win).scrollTop()
270                                         );
271
272                                 } else {
273                                         this[ method ] = val;
274                                 }
275                         });
276                 } else {
277                         win = getWindow( elem );
278
279                         // Return the scroll offset
280                         return win ? ("pageXOffset" in win) ? win[ i ? "pageYOffset" : "pageXOffset" ] :
281                                 jQuery.support.boxModel && win.document.documentElement[ method ] ||
282                                         win.document.body[ method ] :
283                                 elem[ method ];
284                 }
285         };
286 });
287
288 function getWindow( elem ) {
289         return jQuery.isWindow( elem ) ?
290                 elem :
291                 elem.nodeType === 9 ?
292                         elem.defaultView || elem.parentWindow :
293                         false;
294 }
295
296 })( jQuery );