Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / jquery / resources / src / offset.js
blob1003c400c9223139257c10e511d3828034b47225
1 (function( jQuery ) {
3 var rtable = /^t(?:able|d|h)$/i,
4         rroot = /^(?:body|html)$/i;
6 if ( "getBoundingClientRect" in document.documentElement ) {
7         jQuery.fn.offset = function( options ) {
8                 var elem = this[0], box;
10                 if ( options ) {
11                         return this.each(function( i ) {
12                                 jQuery.offset.setOffset( this, options, i );
13                         });
14                 }
16                 if ( !elem || !elem.ownerDocument ) {
17                         return null;
18                 }
20                 if ( elem === elem.ownerDocument.body ) {
21                         return jQuery.offset.bodyOffset( elem );
22                 }
24                 try {
25                         box = elem.getBoundingClientRect();
26                 } catch(e) {}
28                 var doc = elem.ownerDocument,
29                         docElem = doc.documentElement;
31                 // Make sure we're not dealing with a disconnected DOM node
32                 if ( !box || !jQuery.contains( docElem, elem ) ) {
33                         return box ? { top: box.top, left: box.left } : { top: 0, left: 0 };
34                 }
36                 var body = doc.body,
37                         win = getWindow(doc),
38                         clientTop  = docElem.clientTop  || body.clientTop  || 0,
39                         clientLeft = docElem.clientLeft || body.clientLeft || 0,
40                         scrollTop  = (win.pageYOffset || jQuery.support.boxModel && docElem.scrollTop  || body.scrollTop ),
41                         scrollLeft = (win.pageXOffset || jQuery.support.boxModel && docElem.scrollLeft || body.scrollLeft),
42                         top  = box.top  + scrollTop  - clientTop,
43                         left = box.left + scrollLeft - clientLeft;
45                 return { top: top, left: left };
46         };
48 } else {
49         jQuery.fn.offset = function( options ) {
50                 var elem = this[0];
52                 if ( options ) {
53                         return this.each(function( i ) {
54                                 jQuery.offset.setOffset( this, options, i );
55                         });
56                 }
58                 if ( !elem || !elem.ownerDocument ) {
59                         return null;
60                 }
62                 if ( elem === elem.ownerDocument.body ) {
63                         return jQuery.offset.bodyOffset( elem );
64                 }
66                 jQuery.offset.initialize();
68                 var computedStyle,
69                         offsetParent = elem.offsetParent,
70                         prevOffsetParent = elem,
71                         doc = elem.ownerDocument,
72                         docElem = doc.documentElement,
73                         body = doc.body,
74                         defaultView = doc.defaultView,
75                         prevComputedStyle = defaultView ? defaultView.getComputedStyle( elem, null ) : elem.currentStyle,
76                         top = elem.offsetTop,
77                         left = elem.offsetLeft;
79                 while ( (elem = elem.parentNode) && elem !== body && elem !== docElem ) {
80                         if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
81                                 break;
82                         }
84                         computedStyle = defaultView ? defaultView.getComputedStyle(elem, null) : elem.currentStyle;
85                         top  -= elem.scrollTop;
86                         left -= elem.scrollLeft;
88                         if ( elem === offsetParent ) {
89                                 top  += elem.offsetTop;
90                                 left += elem.offsetLeft;
92                                 if ( jQuery.offset.doesNotAddBorder && !(jQuery.offset.doesAddBorderForTableAndCells && rtable.test(elem.nodeName)) ) {
93                                         top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
94                                         left += parseFloat( computedStyle.borderLeftWidth ) || 0;
95                                 }
97                                 prevOffsetParent = offsetParent;
98                                 offsetParent = elem.offsetParent;
99                         }
101                         if ( jQuery.offset.subtractsBorderForOverflowNotVisible && computedStyle.overflow !== "visible" ) {
102                                 top  += parseFloat( computedStyle.borderTopWidth  ) || 0;
103                                 left += parseFloat( computedStyle.borderLeftWidth ) || 0;
104                         }
106                         prevComputedStyle = computedStyle;
107                 }
109                 if ( prevComputedStyle.position === "relative" || prevComputedStyle.position === "static" ) {
110                         top  += body.offsetTop;
111                         left += body.offsetLeft;
112                 }
114                 if ( jQuery.offset.supportsFixedPosition && prevComputedStyle.position === "fixed" ) {
115                         top  += Math.max( docElem.scrollTop, body.scrollTop );
116                         left += Math.max( docElem.scrollLeft, body.scrollLeft );
117                 }
119                 return { top: top, left: left };
120         };
123 jQuery.offset = {
124         initialize: function() {
125                 var body = document.body, container = document.createElement("div"), innerDiv, checkDiv, table, td, bodyMarginTop = parseFloat( jQuery.css(body, "marginTop") ) || 0,
126                         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>";
128                 jQuery.extend( container.style, { position: "absolute", top: 0, left: 0, margin: 0, border: 0, width: "1px", height: "1px", visibility: "hidden" } );
130                 container.innerHTML = html;
131                 body.insertBefore( container, body.firstChild );
132                 innerDiv = container.firstChild;
133                 checkDiv = innerDiv.firstChild;
134                 td = innerDiv.nextSibling.firstChild.firstChild;
136                 this.doesNotAddBorder = (checkDiv.offsetTop !== 5);
137                 this.doesAddBorderForTableAndCells = (td.offsetTop === 5);
139                 checkDiv.style.position = "fixed";
140                 checkDiv.style.top = "20px";
142                 // safari subtracts parent border width here which is 5px
143                 this.supportsFixedPosition = (checkDiv.offsetTop === 20 || checkDiv.offsetTop === 15);
144                 checkDiv.style.position = checkDiv.style.top = "";
146                 innerDiv.style.overflow = "hidden";
147                 innerDiv.style.position = "relative";
149                 this.subtractsBorderForOverflowNotVisible = (checkDiv.offsetTop === -5);
151                 this.doesNotIncludeMarginInBodyOffset = (body.offsetTop !== bodyMarginTop);
153                 body.removeChild( container );
154                 body = container = innerDiv = checkDiv = table = td = null;
155                 jQuery.offset.initialize = jQuery.noop;
156         },
158         bodyOffset: function( body ) {
159                 var top = body.offsetTop,
160                         left = body.offsetLeft;
162                 jQuery.offset.initialize();
164                 if ( jQuery.offset.doesNotIncludeMarginInBodyOffset ) {
165                         top  += parseFloat( jQuery.css(body, "marginTop") ) || 0;
166                         left += parseFloat( jQuery.css(body, "marginLeft") ) || 0;
167                 }
169                 return { top: top, left: left };
170         },
172         setOffset: function( elem, options, i ) {
173                 var position = jQuery.css( elem, "position" );
175                 // set position first, in-case top/left are set even on static elem
176                 if ( position === "static" ) {
177                         elem.style.position = "relative";
178                 }
180                 var curElem = jQuery( elem ),
181                         curOffset = curElem.offset(),
182                         curCSSTop = jQuery.css( elem, "top" ),
183                         curCSSLeft = jQuery.css( elem, "left" ),
184                         calculatePosition = (position === "absolute" && jQuery.inArray('auto', [curCSSTop, curCSSLeft]) > -1),
185                         props = {}, curPosition = {}, curTop, curLeft;
187                 // need to be able to calculate position if either top or left is auto and position is absolute
188                 if ( calculatePosition ) {
189                         curPosition = curElem.position();
190                 }
192                 curTop  = calculatePosition ? curPosition.top  : parseInt( curCSSTop,  10 ) || 0;
193                 curLeft = calculatePosition ? curPosition.left : parseInt( curCSSLeft, 10 ) || 0;
195                 if ( jQuery.isFunction( options ) ) {
196                         options = options.call( elem, i, curOffset );
197                 }
199                 if (options.top != null) {
200                         props.top = (options.top - curOffset.top) + curTop;
201                 }
202                 if (options.left != null) {
203                         props.left = (options.left - curOffset.left) + curLeft;
204                 }
206                 if ( "using" in options ) {
207                         options.using.call( elem, props );
208                 } else {
209                         curElem.css( props );
210                 }
211         }
215 jQuery.fn.extend({
216         position: function() {
217                 if ( !this[0] ) {
218                         return null;
219                 }
221                 var elem = this[0],
223                 // Get *real* offsetParent
224                 offsetParent = this.offsetParent(),
226                 // Get correct offsets
227                 offset       = this.offset(),
228                 parentOffset = rroot.test(offsetParent[0].nodeName) ? { top: 0, left: 0 } : offsetParent.offset();
230                 // Subtract element margins
231                 // note: when an element has margin: auto the offsetLeft and marginLeft
232                 // are the same in Safari causing offset.left to incorrectly be 0
233                 offset.top  -= parseFloat( jQuery.css(elem, "marginTop") ) || 0;
234                 offset.left -= parseFloat( jQuery.css(elem, "marginLeft") ) || 0;
236                 // Add offsetParent borders
237                 parentOffset.top  += parseFloat( jQuery.css(offsetParent[0], "borderTopWidth") ) || 0;
238                 parentOffset.left += parseFloat( jQuery.css(offsetParent[0], "borderLeftWidth") ) || 0;
240                 // Subtract the two offsets
241                 return {
242                         top:  offset.top  - parentOffset.top,
243                         left: offset.left - parentOffset.left
244                 };
245         },
247         offsetParent: function() {
248                 return this.map(function() {
249                         var offsetParent = this.offsetParent || document.body;
250                         while ( offsetParent && (!rroot.test(offsetParent.nodeName) && jQuery.css(offsetParent, "position") === "static") ) {
251                                 offsetParent = offsetParent.offsetParent;
252                         }
253                         return offsetParent;
254                 });
255         }
259 // Create scrollLeft and scrollTop methods
260 jQuery.each( ["Left", "Top"], function( i, name ) {
261         var method = "scroll" + name;
263         jQuery.fn[ method ] = function(val) {
264                 var elem = this[0], win;
266                 if ( !elem ) {
267                         return null;
268                 }
270                 if ( val !== undefined ) {
271                         // Set the scroll offset
272                         return this.each(function() {
273                                 win = getWindow( this );
275                                 if ( win ) {
276                                         win.scrollTo(
277                                                 !i ? val : jQuery(win).scrollLeft(),
278                                                 i ? val : jQuery(win).scrollTop()
279                                         );
281                                 } else {
282                                         this[ method ] = val;
283                                 }
284                         });
285                 } else {
286                         win = getWindow( elem );
288                         // Return the scroll offset
289                         return win ? ("pageXOffset" in win) ? win[ i ? "pageYOffset" : "pageXOffset" ] :
290                                 jQuery.support.boxModel && win.document.documentElement[ method ] ||
291                                         win.document.body[ method ] :
292                                 elem[ method ];
293                 }
294         };
297 function getWindow( elem ) {
298         return jQuery.isWindow( elem ) ?
299                 elem :
300                 elem.nodeType === 9 ?
301                         elem.defaultView || elem.parentWindow :
302                         false;
305 })( jQuery );