3 if ( !jQuery.fn.width ) {
7 module("dimensions", { teardown: moduleTeardown });
20 ======== local reference =======
21 pass and fn can be used to test passing functions to setters
22 See testWidth below for an example
25 This function returns whatever value is passed in
28 Returns a function that returns the value
31 function testWidth( val ) {
35 $div = jQuery("#nothiddendiv");
36 $div.width( val(30) );
37 equal($div.width(), 30, "Test set to 30 correctly");
39 equal($div.width(), 30, "Test hidden div");
41 $div.width( val(-1) ); // handle negative numbers by setting to 0 #11604
42 equal($div.width(), 0, "Test negative width normalized to 0");
43 $div.css("padding", "20px");
44 equal($div.width(), 0, "Test padding specified with pixels");
45 $div.css("border", "2px solid #fff");
46 equal($div.width(), 0, "Test border specified with pixels");
48 $div.css({ "display": "", "border": "", "padding": "" });
50 jQuery("#nothiddendivchild").css({ "width": 20, "padding": "3px", "border": "2px solid #fff" });
51 equal(jQuery("#nothiddendivchild").width(), 20, "Test child width with border and padding");
52 jQuery("#nothiddendiv, #nothiddendivchild").css({ "border": "", "padding": "", "width": "" });
54 blah = jQuery("blah");
55 equal( blah.width( val(10) ), blah, "Make sure that setting a width on an empty set returns the set." );
56 equal( blah.width(), null, "Make sure 'null' is returned on an empty set");
58 equal( jQuery(window).width(), document.documentElement.clientWidth, "Window width is equal to width reported by window/document." );
60 QUnit.expectJqData( this, $div[0], "olddisplay" );
63 test("width()", function() {
67 test("width(Function)", function() {
71 test("width(Function(args))", function() {
74 var $div = jQuery("#nothiddendiv");
75 $div.width( 30 ).width(function(i, width) {
76 equal( width, 30, "Make sure previous value is correct." );
80 equal( $div.width(), 31, "Make sure value was modified correctly." );
83 function testHeight( val ) {
88 $div = jQuery("#nothiddendiv");
89 $div.height( val(30) );
90 equal($div.height(), 30, "Test set to 30 correctly");
92 equal($div.height(), 30, "Test hidden div");
94 $div.height( val(-1) ); // handle negative numbers by setting to 0 #11604
95 equal($div.height(), 0, "Test negative height normalized to 0");
96 $div.css("padding", "20px");
97 equal($div.height(), 0, "Test padding specified with pixels");
98 $div.css("border", "2px solid #fff");
99 equal($div.height(), 0, "Test border specified with pixels");
101 $div.css({ "display": "", "border": "", "padding": "", "height": "1px" });
103 jQuery("#nothiddendivchild").css({ "height": 20, "padding": "3px", "border": "2px solid #fff" });
104 equal(jQuery("#nothiddendivchild").height(), 20, "Test child height with border and padding");
105 jQuery("#nothiddendiv, #nothiddendivchild").css({ "border": "", "padding": "", "height": "" });
107 blah = jQuery("blah");
108 equal( blah.height( val(10) ), blah, "Make sure that setting a height on an empty set returns the set." );
109 equal( blah.height(), null, "Make sure 'null' is returned on an empty set");
111 equal( jQuery(window).height(), document.documentElement.clientHeight, "Window width is equal to width reported by window/document." );
113 QUnit.expectJqData( this, $div[0], "olddisplay" );
116 test("height()", function() {
120 test("height(Function)", function() {
124 test("height(Function(args))", function() {
127 var $div = jQuery("#nothiddendiv");
128 $div.height( 30 ).height(function(i, height) {
129 equal( height, 30, "Make sure previous value is correct." );
133 equal( $div.height(), 31, "Make sure value was modified correctly." );
136 test("innerWidth()", function() {
140 $win = jQuery( window ),
141 $doc = jQuery( document );
143 equal( jQuery( window ).innerWidth(), $win.width(), "Test on window" );
144 equal( jQuery( document ).innerWidth(), $doc.width(), "Test on document" );
146 $div = jQuery( "#nothiddendiv" );
149 "border": "2px solid #fff",
153 equal( $div.innerWidth(), 30, "Test with margin and border" );
154 $div.css( "padding", "20px" );
155 equal( $div.innerWidth(), 70, "Test with margin, border and padding" );
157 equal( $div.innerWidth(), 70, "Test hidden div" );
160 $div.css({ "display": "", "border": "", "padding": "", "width": "", "height": "" });
162 div = jQuery( "<div>" );
164 // Temporarily require 0 for backwards compat - should be auto
165 equal( div.innerWidth(), 0, "Make sure that disconnected nodes are handled." );
168 QUnit.expectJqData( this, $div[ 0 ], "olddisplay" );
171 test("innerHeight()", function() {
175 $win = jQuery( window ),
176 $doc = jQuery( document );
178 equal( jQuery( window ).innerHeight(), $win.height(), "Test on window" );
179 equal( jQuery( document ).innerHeight(), $doc.height(), "Test on document" );
181 $div = jQuery( "#nothiddendiv" );
184 "border": "2px solid #fff",
188 equal( $div.innerHeight(), 30, "Test with margin and border" );
189 $div.css( "padding", "20px" );
190 equal( $div.innerHeight(), 70, "Test with margin, border and padding" );
192 equal( $div.innerHeight(), 70, "Test hidden div" );
195 $div.css({ "display": "", "border": "", "padding": "", "width": "", "height": "" });
197 div = jQuery( "<div>" );
199 // Temporarily require 0 for backwards compat - should be auto
200 equal( div.innerHeight(), 0, "Make sure that disconnected nodes are handled." );
203 QUnit.expectJqData( this, $div[ 0 ], "olddisplay" );
206 test("outerWidth()", function() {
210 $win = jQuery( window ),
211 $doc = jQuery( document );
213 equal( jQuery( window ).outerWidth(), $win.width(), "Test on window without margin option" );
214 equal( jQuery( window ).outerWidth( true ), $win.width(), "Test on window with margin option" );
215 equal( jQuery( document ).outerWidth(), $doc.width(), "Test on document without margin option" );
216 equal( jQuery( document ).outerWidth( true ), $doc.width(), "Test on document with margin option" );
218 $div = jQuery( "#nothiddendiv" );
219 $div.css( "width", 30 );
221 equal( $div.outerWidth(), 30, "Test with only width set" );
222 $div.css( "padding", "20px" );
223 equal( $div.outerWidth(), 70, "Test with padding" );
224 $div.css( "border", "2px solid #fff" );
225 equal( $div.outerWidth(), 74, "Test with padding and border" );
226 $div.css( "margin", "10px" );
227 equal( $div.outerWidth(), 74, "Test with padding, border and margin without margin option" );
228 $div.css( "position", "absolute" );
229 equal( $div.outerWidth( true ), 94, "Test with padding, border and margin with margin option" );
231 equal( $div.outerWidth( true ), 94, "Test hidden div with padding, border and margin with margin option" );
234 $div.css({ "position": "", "display": "", "border": "", "padding": "", "width": "", "height": "" });
236 div = jQuery( "<div>" );
238 // Temporarily require 0 for backwards compat - should be auto
239 equal( div.outerWidth(), 0, "Make sure that disconnected nodes are handled." );
242 QUnit.expectJqData( this, $div[ 0 ], "olddisplay" );
245 test("child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height() see #9441 #9300", function() {
249 var $divNormal = jQuery("<div>").css({ "width": "100px", "height": "100px", "border": "10px solid white", "padding": "2px", "margin": "3px" }),
250 $divChild = $divNormal.clone(),
251 $divUnconnected = $divNormal.clone(),
252 $divHiddenParent = jQuery("<div>").css( "display", "none" ).append( $divChild ).appendTo("body");
253 $divNormal.appendTo("body");
255 // tests that child div of a hidden div works the same as a normal div
256 equal( $divChild.width(), $divNormal.width(), "child of a hidden element width() is wrong see #9441" );
257 equal( $divChild.innerWidth(), $divNormal.innerWidth(), "child of a hidden element innerWidth() is wrong see #9441" );
258 equal( $divChild.outerWidth(), $divNormal.outerWidth(), "child of a hidden element outerWidth() is wrong see #9441" );
259 equal( $divChild.outerWidth(true), $divNormal.outerWidth( true ), "child of a hidden element outerWidth( true ) is wrong see #9300" );
261 equal( $divChild.height(), $divNormal.height(), "child of a hidden element height() is wrong see #9441" );
262 equal( $divChild.innerHeight(), $divNormal.innerHeight(), "child of a hidden element innerHeight() is wrong see #9441" );
263 equal( $divChild.outerHeight(), $divNormal.outerHeight(), "child of a hidden element outerHeight() is wrong see #9441" );
264 equal( $divChild.outerHeight(true), $divNormal.outerHeight( true ), "child of a hidden element outerHeight( true ) is wrong see #9300" );
266 // tests that child div of an unconnected div works the same as a normal div
267 equal( $divUnconnected.width(), $divNormal.width(), "unconnected element width() is wrong see #9441" );
268 equal( $divUnconnected.innerWidth(), $divNormal.innerWidth(), "unconnected element innerWidth() is wrong see #9441" );
269 equal( $divUnconnected.outerWidth(), $divNormal.outerWidth(), "unconnected element outerWidth() is wrong see #9441" );
270 equal( $divUnconnected.outerWidth(true), $divNormal.outerWidth( true ), "unconnected element outerWidth( true ) is wrong see #9300" );
272 equal( $divUnconnected.height(), $divNormal.height(), "unconnected element height() is wrong see #9441" );
273 equal( $divUnconnected.innerHeight(), $divNormal.innerHeight(), "unconnected element innerHeight() is wrong see #9441" );
274 equal( $divUnconnected.outerHeight(), $divNormal.outerHeight(), "unconnected element outerHeight() is wrong see #9441" );
275 equal( $divUnconnected.outerHeight(true), $divNormal.outerHeight( true ), "unconnected element outerHeight( true ) is wrong see #9300" );
278 $divHiddenParent.remove();
282 test("getting dimensions shouldn't modify runtimeStyle see #9233", function() {
285 var $div = jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
287 runtimeStyle = div.runtimeStyle;
289 if ( runtimeStyle ) {
290 div.runtimeStyle.marginLeft = "12em";
291 div.runtimeStyle.left = "11em";
294 $div.outerWidth( true );
296 if ( runtimeStyle ) {
297 equal( div.runtimeStyle.left, "11em", "getting dimensions modifies runtimeStyle, see #9233" );
299 ok( true, "this browser doesn't support runtimeStyle, see #9233" );
305 test( "table dimensions", 2, function() {
306 var table = jQuery("<table><colgroup><col/><col/></colgroup><tbody><tr><td></td><td>a</td></tr><tr><td></td><td>a</td></tr></tbody></table>").appendTo("#qunit-fixture"),
307 tdElem = table.find("td").first(),
308 colElem = table.find("col").first().width( 300 );
310 table.find("td").css({ "margin": 0, "padding": 0 });
312 equal( tdElem.width(), tdElem.width(), "width() doesn't alter dimension values of empty cells, see #11293" );
313 equal( colElem.width(), 300, "col elements have width(), see #12243" );
316 test("box-sizing:border-box child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height() see #10413", function() {
320 var $divNormal = jQuery("<div>").css({ "boxSizing": "border-box", "width": "100px", "height": "100px", "border": "10px solid white", "padding": "2px", "margin": "3px" }),
321 $divChild = $divNormal.clone(),
322 $divUnconnected = $divNormal.clone(),
323 $divHiddenParent = jQuery("<div>").css( "display", "none" ).append( $divChild ).appendTo("body");
324 $divNormal.appendTo("body");
326 // tests that child div of a hidden div works the same as a normal div
327 equal( $divChild.width(), $divNormal.width(), "child of a hidden element width() is wrong see #10413" );
328 equal( $divChild.innerWidth(), $divNormal.innerWidth(), "child of a hidden element innerWidth() is wrong see #10413" );
329 equal( $divChild.outerWidth(), $divNormal.outerWidth(), "child of a hidden element outerWidth() is wrong see #10413" );
330 equal( $divChild.outerWidth(true), $divNormal.outerWidth( true ), "child of a hidden element outerWidth( true ) is wrong see #10413" );
332 equal( $divChild.height(), $divNormal.height(), "child of a hidden element height() is wrong see #10413" );
333 equal( $divChild.innerHeight(), $divNormal.innerHeight(), "child of a hidden element innerHeight() is wrong see #10413" );
334 equal( $divChild.outerHeight(), $divNormal.outerHeight(), "child of a hidden element outerHeight() is wrong see #10413" );
335 equal( $divChild.outerHeight(true), $divNormal.outerHeight( true ), "child of a hidden element outerHeight( true ) is wrong see #10413" );
337 // tests that child div of an unconnected div works the same as a normal div
338 equal( $divUnconnected.width(), $divNormal.width(), "unconnected element width() is wrong see #10413" );
339 equal( $divUnconnected.innerWidth(), $divNormal.innerWidth(), "unconnected element innerWidth() is wrong see #10413" );
340 equal( $divUnconnected.outerWidth(), $divNormal.outerWidth(), "unconnected element outerWidth() is wrong see #10413" );
341 equal( $divUnconnected.outerWidth(true), $divNormal.outerWidth( true ), "unconnected element outerWidth( true ) is wrong see #10413" );
343 equal( $divUnconnected.height(), $divNormal.height(), "unconnected element height() is wrong see #10413" );
344 equal( $divUnconnected.innerHeight(), $divNormal.innerHeight(), "unconnected element innerHeight() is wrong see #10413" );
345 equal( $divUnconnected.outerHeight(), $divNormal.outerHeight(), "unconnected element outerHeight() is wrong see #10413" );
346 equal( $divUnconnected.outerHeight(true), $divNormal.outerHeight( true ), "unconnected element outerHeight( true ) is wrong see #10413" );
349 $divHiddenParent.remove();
353 test("outerHeight()", function() {
357 $win = jQuery( window ),
358 $doc = jQuery( document );
360 equal( jQuery( window ).outerHeight(), $win.height(), "Test on window without margin option" );
361 equal( jQuery( window ).outerHeight( true ), $win.height(), "Test on window with margin option" );
362 equal( jQuery( document ).outerHeight(), $doc.height(), "Test on document without margin option" );
363 equal( jQuery( document ).outerHeight( true ), $doc.height(), "Test on document with margin option" );
365 $div = jQuery( "#nothiddendiv" );
366 $div.css( "height", 30 );
368 equal( $div.outerHeight(), 30, "Test with only width set" );
369 $div.css( "padding", "20px" );
370 equal( $div.outerHeight(), 70, "Test with padding" );
371 $div.css( "border", "2px solid #fff" );
372 equal( $div.outerHeight(), 74, "Test with padding and border" );
373 $div.css( "margin", "10px" );
374 equal( $div.outerHeight(), 74, "Test with padding, border and margin without margin option" );
375 equal( $div.outerHeight( true ), 94, "Test with padding, border and margin with margin option" );
377 equal( $div.outerHeight( true ), 94, "Test hidden div with padding, border and margin with margin option" );
380 $div.css({ "display": "", "border": "", "padding": "", "width": "", "height": "" });
382 div = jQuery( "<div>" );
384 // Temporarily require 0 for backwards compat - should be auto
385 equal( div.outerHeight(), 0, "Make sure that disconnected nodes are handled." );
388 QUnit.expectJqData( this, $div[ 0 ], "olddisplay" );
391 test("passing undefined is a setter #5571", function() {
393 equal(jQuery("#nothiddendiv").height(30).height(undefined).height(), 30, ".height(undefined) is chainable (#5571)");
394 equal(jQuery("#nothiddendiv").height(30).innerHeight(undefined).height(), 30, ".innerHeight(undefined) is chainable (#5571)");
395 equal(jQuery("#nothiddendiv").height(30).outerHeight(undefined).height(), 30, ".outerHeight(undefined) is chainable (#5571)");
396 equal(jQuery("#nothiddendiv").width(30).width(undefined).width(), 30, ".width(undefined) is chainable (#5571)");
399 test( "getters on non elements should return null", function() {
402 var nonElem = jQuery("notAnElement");
404 strictEqual( nonElem.width(), null, ".width() is not null (#12283)" );
405 strictEqual( nonElem.innerWidth(), null, ".innerWidth() is not null (#12283)" );
406 strictEqual( nonElem.outerWidth(), null, ".outerWidth() is not null (#12283)" );
407 strictEqual( nonElem.outerWidth( true ), null, ".outerWidth(true) is not null (#12283)" );
409 strictEqual( nonElem.height(), null, ".height() is not null (#12283)" );
410 strictEqual( nonElem.innerHeight(), null, ".innerHeight() is not null (#12283)" );
411 strictEqual( nonElem.outerHeight(), null, ".outerHeight() is not null (#12283)" );
412 strictEqual( nonElem.outerHeight( true ), null, ".outerHeight(true) is not null (#12283)" );
415 test("setters with and without box-sizing:border-box", function(){
418 // Support: Android 2.3 (-webkit-box-sizing).
419 var el_bb = jQuery("<div style='width:114px;height:114px;margin:5px;padding:3px;border:4px solid white;-webkit-box-sizing:border-box;box-sizing:border-box;'>test</div>").appendTo("#qunit-fixture"),
420 el = jQuery("<div style='width:100px;height:100px;margin:5px;padding:3px;border:4px solid white;'>test</div>").appendTo("#qunit-fixture"),
423 equal( el_bb.width( 101 ).width(), expected + 1, "test border-box width(int) by roundtripping" );
424 equal( el_bb.innerWidth( 108 ).width(), expected + 2, "test border-box innerWidth(int) by roundtripping" );
425 equal( el_bb.outerWidth( 117 ).width(), expected + 3, "test border-box outerWidth(int) by roundtripping" );
426 equal( el_bb.outerWidth( 118, false ).width(), expected + 4, "test border-box outerWidth(int, false) by roundtripping" );
427 equal( el_bb.outerWidth( 129, true ).width(), expected + 5, "test border-box innerWidth(int, true) by roundtripping" );
429 equal( el_bb.height( 101 ).height(), expected + 1, "test border-box height(int) by roundtripping" );
430 equal( el_bb.innerHeight( 108 ).height(), expected + 2, "test border-box innerHeight(int) by roundtripping" );
431 equal( el_bb.outerHeight( 117 ).height(), expected + 3, "test border-box outerHeight(int) by roundtripping" );
432 equal( el_bb.outerHeight( 118, false ).height(), expected + 4, "test border-box outerHeight(int, false) by roundtripping" );
433 equal( el_bb.outerHeight( 129, true ).height(), expected + 5, "test border-box innerHeight(int, true) by roundtripping" );
435 equal( el.width( 101 ).width(), expected + 1, "test border-box width(int) by roundtripping" );
436 equal( el.innerWidth( 108 ).width(), expected + 2, "test border-box innerWidth(int) by roundtripping" );
437 equal( el.outerWidth( 117 ).width(), expected + 3, "test border-box outerWidth(int) by roundtripping" );
438 equal( el.outerWidth( 118, false ).width(), expected + 4, "test border-box outerWidth(int, false) by roundtripping" );
439 equal( el.outerWidth( 129, true ).width(), expected + 5, "test border-box innerWidth(int, true) by roundtripping" );
441 equal( el.height( 101 ).height(), expected + 1, "test border-box height(int) by roundtripping" );
442 equal( el.innerHeight( 108 ).height(), expected + 2, "test border-box innerHeight(int) by roundtripping" );
443 equal( el.outerHeight( 117 ).height(), expected + 3, "test border-box outerHeight(int) by roundtripping" );
444 equal( el.outerHeight( 118, false ).height(), expected + 4, "test border-box outerHeight(int, false) by roundtripping" );
445 equal( el.outerHeight( 129, true ).height(), expected + 5, "test border-box innerHeight(int, true) by roundtripping" );
448 testIframe( "dimensions/documentLarge", "window vs. large document", function( jQuery, window, document ) {
451 ok( jQuery( document ).height() > jQuery( window ).height(), "document height is larger than window height" );
452 ok( jQuery( document ).width() > jQuery( window ).width(), "document width is larger than window width" );
455 test( "allow modification of coordinates argument (gh-1848)", 1, function() {
457 element = jQuery( "<div/>" ).appendTo( "#qunit-fixture" );
459 element.offset(function( index, coords ) {
465 offsetTop = element.offset().top;
466 ok( Math.abs(offsetTop - 100) < 0.02,
467 "coordinates are modified (got offset.top: " + offsetTop + ")");