Offset: allow offset setter to throw for disconnected elements
[jquery.git] / src / dimensions.js
blobfa389bccf4aae247722118e6de0bce1f9df75768
1 define([
2         "./core",
3         "./core/access",
4         "./css"
5 ], function( jQuery, access ) {
7 // Create innerHeight, innerWidth, height, width, outerHeight and outerWidth methods
8 jQuery.each( { Height: "height", Width: "width" }, function( name, type ) {
9         jQuery.each( { padding: "inner" + name, content: type, "": "outer" + name },
10                 function( defaultExtra, funcName ) {
12                 // Margin is only for outerHeight, outerWidth
13                 jQuery.fn[ funcName ] = function( margin, value ) {
14                         var chainable = arguments.length && ( defaultExtra || typeof margin !== "boolean" ),
15                                 extra = defaultExtra || ( margin === true || value === true ? "margin" : "border" );
17                         return access( this, function( elem, type, value ) {
18                                 var doc;
20                                 if ( jQuery.isWindow( elem ) ) {
21                                         // As of 5/8/2012 this will yield incorrect results for Mobile Safari, but there
22                                         // isn't a whole lot we can do. See pull request at this URL for discussion:
23                                         // https://github.com/jquery/jquery/pull/764
24                                         return elem.document.documentElement[ "client" + name ];
25                                 }
27                                 // Get document width or height
28                                 if ( elem.nodeType === 9 ) {
29                                         doc = elem.documentElement;
31                                         // Either scroll[Width/Height] or offset[Width/Height] or client[Width/Height],
32                                         // whichever is greatest
33                                         return Math.max(
34                                                 elem.body[ "scroll" + name ], doc[ "scroll" + name ],
35                                                 elem.body[ "offset" + name ], doc[ "offset" + name ],
36                                                 doc[ "client" + name ]
37                                         );
38                                 }
40                                 return value === undefined ?
41                                         // Get width or height on the element, requesting but not forcing parseFloat
42                                         jQuery.css( elem, type, extra ) :
44                                         // Set width or height on the element
45                                         jQuery.style( elem, type, value, extra );
46                         }, type, chainable ? margin : undefined, chainable, null );
47                 };
48         });
49 });
51 return jQuery;
52 });