1 if ( jQuery.fn.width ) {
3 module("dimensions", { teardown: moduleTeardown });
5 var pass = function( val ) {
9 var fn = function( val ) {
10 return function(){ return val; };
14 ======== local reference =======
15 pass and fn can be used to test passing functions to setters
16 See testWidth below for an example
19 This function returns whatever value is passed in
22 Returns a function that returns the value
25 var testWidth = function( val ) {
28 var $div = jQuery("#nothiddendiv");
29 $div.width( val(30) );
30 equal($div.width(), 30, "Test set to 30 correctly");
32 equal($div.width(), 30, "Test hidden div");
34 $div.width( val(-1) ); // handle negative numbers by setting to 0 #11604
35 equal($div.width(), 0, "Test negative width normalized to 0");
36 $div.css("padding", "20px");
37 equal($div.width(), 0, "Test padding specified with pixels");
38 $div.css("border", "2px solid #fff");
39 equal($div.width(), 0, "Test border specified with pixels");
41 $div.css({ "display": "", "border": "", "padding": "" });
43 jQuery("#nothiddendivchild").css({ "width": 20, "padding": "3px", "border": "2px solid #fff" });
44 equal(jQuery("#nothiddendivchild").width(), 20, "Test child width with border and padding");
45 jQuery("#nothiddendiv, #nothiddendivchild").css({ "border": "", "padding": "", "width": "" });
47 var blah = jQuery("blah");
48 equal( blah.width( val(10) ), blah, "Make sure that setting a width on an empty set returns the set." );
49 equal( blah.width(), null, "Make sure 'null' is returned on an empty set");
51 equal( jQuery(window).width(), document.documentElement.clientWidth, "Window width is equal to width reported by window/document." );
53 jQuery._removeData( $div[0], "olddisplay" );
56 test("width()", function() {
60 test("width(Function)", function() {
64 test("width(Function(args))", function() {
67 var $div = jQuery("#nothiddendiv");
68 $div.width( 30 ).width(function(i, width) {
69 equal( width, 30, "Make sure previous value is corrrect." );
73 equal( $div.width(), 31, "Make sure value was modified correctly." );
76 var testHeight = function( val ) {
79 var $div = jQuery("#nothiddendiv");
80 $div.height( val(30) );
81 equal($div.height(), 30, "Test set to 30 correctly");
83 equal($div.height(), 30, "Test hidden div");
85 $div.height( val(-1) ); // handle negative numbers by setting to 0 #11604
86 equal($div.height(), 0, "Test negative height normalized to 0");
87 $div.css("padding", "20px");
88 equal($div.height(), 0, "Test padding specified with pixels");
89 $div.css("border", "2px solid #fff");
90 equal($div.height(), 0, "Test border specified with pixels");
92 $div.css({ "display": "", "border": "", "padding": "", "height": "1px" });
94 jQuery("#nothiddendivchild").css({ "height": 20, "padding": "3px", "border": "2px solid #fff" });
95 equal(jQuery("#nothiddendivchild").height(), 20, "Test child height with border and padding");
96 jQuery("#nothiddendiv, #nothiddendivchild").css({ "border": "", "padding": "", "height": "" });
98 var blah = jQuery("blah");
99 equal( blah.height( val(10) ), blah, "Make sure that setting a height on an empty set returns the set." );
100 equal( blah.height(), null, "Make sure 'null' is returned on an empty set");
102 equal( jQuery(window).height(), document.documentElement.clientHeight, "Window width is equal to width reported by window/document." );
104 jQuery._removeData( $div[0], "olddisplay" );
107 test("height()", function() {
111 test("height(Function)", function() {
115 test("height(Function(args))", function() {
118 var $div = jQuery("#nothiddendiv");
119 $div.height( 30 ).height(function(i, height) {
120 equal( height, 30, "Make sure previous value is corrrect." );
124 equal( $div.height(), 31, "Make sure value was modified correctly." );
127 test("innerWidth()", function() {
130 var winWidth = jQuery( window ).width(),
131 docWidth = jQuery( document ).width();
133 equal(jQuery(window).innerWidth(), winWidth, "Test on window");
134 equal(jQuery(document).innerWidth(), docWidth, "Test on document");
136 var $div = jQuery("#nothiddendiv");
140 "border": "2px solid #fff",
144 equal($div.innerWidth(), 30, "Test with margin and border");
145 $div.css("padding", "20px");
146 equal($div.innerWidth(), 70, "Test with margin, border and padding");
148 equal($div.innerWidth(), 70, "Test hidden div");
151 $div.css({ "display": "", "border": "", "padding": "", "width": "", "height": "" });
153 var div = jQuery( "<div>" );
155 // Temporarily require 0 for backwards compat - should be auto
156 equal( div.innerWidth(), 0, "Make sure that disconnected nodes are handled." );
159 jQuery._removeData( $div[0], "olddisplay" );
162 test("innerHeight()", function() {
165 var winHeight = jQuery( window ).height(),
166 docHeight = jQuery( document ).height();
168 equal(jQuery(window).innerHeight(), winHeight, "Test on window");
169 equal(jQuery(document).innerHeight(), docHeight, "Test on document");
171 var $div = jQuery("#nothiddendiv");
175 "border": "2px solid #fff",
179 equal($div.innerHeight(), 30, "Test with margin and border");
180 $div.css("padding", "20px");
181 equal($div.innerHeight(), 70, "Test with margin, border and padding");
183 equal($div.innerHeight(), 70, "Test hidden div");
186 $div.css({ "display": "", "border": "", "padding": "", "width": "", "height": "" });
188 var div = jQuery( "<div>" );
190 // Temporarily require 0 for backwards compat - should be auto
191 equal( div.innerHeight(), 0, "Make sure that disconnected nodes are handled." );
194 jQuery._removeData( $div[0], "olddisplay" );
197 test("outerWidth()", function() {
200 var winWidth = jQuery( window ).width(),
201 docWidth = jQuery( document ).width();
203 equal( jQuery( window ).outerWidth(), winWidth, "Test on window without margin option" );
204 equal( jQuery( window ).outerWidth( true ), winWidth, "Test on window with margin option" );
205 equal( jQuery( document ).outerWidth(), docWidth, "Test on document without margin option" );
206 equal( jQuery( document ).outerWidth( true ), docWidth, "Test on document with margin option" );
208 var $div = jQuery("#nothiddendiv");
209 $div.css("width", 30);
211 equal($div.outerWidth(), 30, "Test with only width set");
212 $div.css("padding", "20px");
213 equal($div.outerWidth(), 70, "Test with padding");
214 $div.css("border", "2px solid #fff");
215 equal($div.outerWidth(), 74, "Test with padding and border");
216 $div.css("margin", "10px");
217 equal($div.outerWidth(), 74, "Test with padding, border and margin without margin option");
218 $div.css("position", "absolute");
219 equal($div.outerWidth(true), 94, "Test with padding, border and margin with margin option");
221 equal($div.outerWidth(true), 94, "Test hidden div with padding, border and margin with margin option");
224 $div.css({ "position": "", "display": "", "border": "", "padding": "", "width": "", "height": "" });
226 var div = jQuery( "<div>" );
228 // Temporarily require 0 for backwards compat - should be auto
229 equal( div.outerWidth(), 0, "Make sure that disconnected nodes are handled." );
232 jQuery._removeData( $div[0], "olddisplay" );
235 test("child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height() see #9441 #9300", function() {
239 var $divNormal = jQuery("<div>").css({ "width": "100px", "height": "100px", "border": "10px solid white", "padding": "2px", "margin": "3px" }),
240 $divChild = $divNormal.clone(),
241 $divUnconnected = $divNormal.clone(),
242 $divHiddenParent = jQuery("<div>").css( "display", "none" ).append( $divChild ).appendTo("body");
243 $divNormal.appendTo("body");
245 // tests that child div of a hidden div works the same as a normal div
246 equal( $divChild.width(), $divNormal.width(), "child of a hidden element width() is wrong see #9441" );
247 equal( $divChild.innerWidth(), $divNormal.innerWidth(), "child of a hidden element innerWidth() is wrong see #9441" );
248 equal( $divChild.outerWidth(), $divNormal.outerWidth(), "child of a hidden element outerWidth() is wrong see #9441" );
249 equal( $divChild.outerWidth(true), $divNormal.outerWidth( true ), "child of a hidden element outerWidth( true ) is wrong see #9300" );
251 equal( $divChild.height(), $divNormal.height(), "child of a hidden element height() is wrong see #9441" );
252 equal( $divChild.innerHeight(), $divNormal.innerHeight(), "child of a hidden element innerHeight() is wrong see #9441" );
253 equal( $divChild.outerHeight(), $divNormal.outerHeight(), "child of a hidden element outerHeight() is wrong see #9441" );
254 equal( $divChild.outerHeight(true), $divNormal.outerHeight( true ), "child of a hidden element outerHeight( true ) is wrong see #9300" );
256 // tests that child div of an unconnected div works the same as a normal div
257 equal( $divUnconnected.width(), $divNormal.width(), "unconnected element width() is wrong see #9441" );
258 equal( $divUnconnected.innerWidth(), $divNormal.innerWidth(), "unconnected element innerWidth() is wrong see #9441" );
259 equal( $divUnconnected.outerWidth(), $divNormal.outerWidth(), "unconnected element outerWidth() is wrong see #9441" );
260 equal( $divUnconnected.outerWidth(true), $divNormal.outerWidth( true ), "unconnected element outerWidth( true ) is wrong see #9300" );
262 equal( $divUnconnected.height(), $divNormal.height(), "unconnected element height() is wrong see #9441" );
263 equal( $divUnconnected.innerHeight(), $divNormal.innerHeight(), "unconnected element innerHeight() is wrong see #9441" );
264 equal( $divUnconnected.outerHeight(), $divNormal.outerHeight(), "unconnected element outerHeight() is wrong see #9441" );
265 equal( $divUnconnected.outerHeight(true), $divNormal.outerHeight( true ), "unconnected element outerHeight( true ) is wrong see #9300" );
268 $divHiddenParent.remove();
272 test("getting dimensions shouldnt modify runtimeStyle see #9233", function() {
275 var $div = jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
277 runtimeStyle = div.runtimeStyle;
279 if ( runtimeStyle ) {
280 div.runtimeStyle.marginLeft = "12em";
281 div.runtimeStyle.left = "11em";
284 $div.outerWidth( true );
286 if ( runtimeStyle ) {
287 equal( div.runtimeStyle.left, "11em", "getting dimensions modifies runtimeStyle, see #9233" );
289 ok( true, "this browser doesnt support runtimeStyle, see #9233" );
295 test( "table dimensions", 2, function() {
296 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"),
297 tdElem = table.find("tr:eq(0) td:eq(0)"),
298 colElem = table.find("col:eq(1)").width( 300 );
300 table.find("td").css({ "margin": 0, "padding": 0 });
302 equal( tdElem.width(), tdElem.width(), "width() doesn't alter dimension values of empty cells, see #11293" );
303 equal( colElem.width(), 300, "col elements have width(), see #12243" );
306 test("box-sizing:border-box child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height() see #10413", function() {
310 var $divNormal = jQuery("<div>").css({ "boxSizing": "border-box", "width": "100px", "height": "100px", "border": "10px solid white", "padding": "2px", "margin": "3px" }),
311 $divChild = $divNormal.clone(),
312 $divUnconnected = $divNormal.clone(),
313 $divHiddenParent = jQuery("<div>").css( "display", "none" ).append( $divChild ).appendTo("body");
314 $divNormal.appendTo("body");
316 // tests that child div of a hidden div works the same as a normal div
317 equal( $divChild.width(), $divNormal.width(), "child of a hidden element width() is wrong see #10413" );
318 equal( $divChild.innerWidth(), $divNormal.innerWidth(), "child of a hidden element innerWidth() is wrong see #10413" );
319 equal( $divChild.outerWidth(), $divNormal.outerWidth(), "child of a hidden element outerWidth() is wrong see #10413" );
320 equal( $divChild.outerWidth(true), $divNormal.outerWidth( true ), "child of a hidden element outerWidth( true ) is wrong see #10413" );
322 equal( $divChild.height(), $divNormal.height(), "child of a hidden element height() is wrong see #10413" );
323 equal( $divChild.innerHeight(), $divNormal.innerHeight(), "child of a hidden element innerHeight() is wrong see #10413" );
324 equal( $divChild.outerHeight(), $divNormal.outerHeight(), "child of a hidden element outerHeight() is wrong see #10413" );
325 equal( $divChild.outerHeight(true), $divNormal.outerHeight( true ), "child of a hidden element outerHeight( true ) is wrong see #10413" );
327 // tests that child div of an unconnected div works the same as a normal div
328 equal( $divUnconnected.width(), $divNormal.width(), "unconnected element width() is wrong see #10413" );
329 equal( $divUnconnected.innerWidth(), $divNormal.innerWidth(), "unconnected element innerWidth() is wrong see #10413" );
330 equal( $divUnconnected.outerWidth(), $divNormal.outerWidth(), "unconnected element outerWidth() is wrong see #10413" );
331 equal( $divUnconnected.outerWidth(true), $divNormal.outerWidth( true ), "unconnected element outerWidth( true ) is wrong see #10413" );
333 equal( $divUnconnected.height(), $divNormal.height(), "unconnected element height() is wrong see #10413" );
334 equal( $divUnconnected.innerHeight(), $divNormal.innerHeight(), "unconnected element innerHeight() is wrong see #10413" );
335 equal( $divUnconnected.outerHeight(), $divNormal.outerHeight(), "unconnected element outerHeight() is wrong see #10413" );
336 equal( $divUnconnected.outerHeight(true), $divNormal.outerHeight( true ), "unconnected element outerHeight( true ) is wrong see #10413" );
339 $divHiddenParent.remove();
343 test("outerHeight()", function() {
346 var winHeight = jQuery( window ).height(),
347 docHeight = jQuery( document ).height();
350 equal( jQuery( window ).outerHeight(), winHeight, "Test on window without margin option" );
351 equal( jQuery( window ).outerHeight( true ), winHeight, "Test on window with margin option" );
352 equal( jQuery( document ).outerHeight(), docHeight, "Test on document without margin option" );
353 equal( jQuery( document ).outerHeight( true ), docHeight, "Test on document with margin option" );
355 var $div = jQuery("#nothiddendiv");
356 $div.css("height", 30);
358 equal($div.outerHeight(), 30, "Test with only width set");
359 $div.css("padding", "20px");
360 equal($div.outerHeight(), 70, "Test with padding");
361 $div.css("border", "2px solid #fff");
362 equal($div.outerHeight(), 74, "Test with padding and border");
363 $div.css("margin", "10px");
364 equal($div.outerHeight(), 74, "Test with padding, border and margin without margin option");
365 equal($div.outerHeight(true), 94, "Test with padding, border and margin with margin option");
367 equal($div.outerHeight(true), 94, "Test hidden div with padding, border and margin with margin option");
370 $div.css({ "display": "", "border": "", "padding": "", "width": "", "height": "" });
372 var div = jQuery( "<div>" );
374 // Temporarily require 0 for backwards compat - should be auto
375 equal( div.outerHeight(), 0, "Make sure that disconnected nodes are handled." );
378 jQuery._removeData( $div[0], "olddisplay" );
381 test("passing undefined is a setter #5571", function() {
383 equal(jQuery("#nothiddendiv").height(30).height(undefined).height(), 30, ".height(undefined) is chainable (#5571)");
384 equal(jQuery("#nothiddendiv").height(30).innerHeight(undefined).height(), 30, ".innerHeight(undefined) is chainable (#5571)");
385 equal(jQuery("#nothiddendiv").height(30).outerHeight(undefined).height(), 30, ".outerHeight(undefined) is chainable (#5571)");
386 equal(jQuery("#nothiddendiv").width(30).width(undefined).width(), 30, ".width(undefined) is chainable (#5571)");
389 test( "getters on non elements should return null", function() {
392 var nonElem = jQuery("notAnElement");
394 strictEqual( nonElem.width(), null, ".width() is not null (#12283)" );
395 strictEqual( nonElem.innerWidth(), null, ".innerWidth() is not null (#12283)" );
396 strictEqual( nonElem.outerWidth(), null, ".outerWidth() is not null (#12283)" );
397 strictEqual( nonElem.outerWidth( true ), null, ".outerWidth(true) is not null (#12283)" );
399 strictEqual( nonElem.height(), null, ".height() is not null (#12283)" );
400 strictEqual( nonElem.innerHeight(), null, ".innerHeight() is not null (#12283)" );
401 strictEqual( nonElem.outerHeight(), null, ".outerHeight() is not null (#12283)" );
402 strictEqual( nonElem.outerHeight( true ), null, ".outerHeight(true) is not null (#12283)" );
405 test("setters with and without box-sizing:border-box", function(){
408 var el_bb = jQuery("<div style='width:114px;height:114px;margin:5px;padding:3px;border:4px solid white;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;'>test</div>").appendTo("#qunit-fixture"),
409 el = jQuery("<div style='width:100px;height:100px;margin:5px;padding:3px;border:4px solid white;'>test</div>").appendTo("#qunit-fixture"),
412 equal( el_bb.width( 101 ).width(), expected + 1, "test border-box width(int) by roundtripping" );
413 equal( el_bb.innerWidth( 108 ).width(), expected + 2, "test border-box innerWidth(int) by roundtripping" );
414 equal( el_bb.outerWidth( 117 ).width(), expected + 3, "test border-box outerWidth(int) by roundtripping" );
415 equal( el_bb.outerWidth( 118, false ).width(), expected + 4, "test border-box outerWidth(int, false) by roundtripping" );
416 equal( el_bb.outerWidth( 129, true ).width(), expected + 5, "test border-box innerWidth(int, true) by roundtripping" );
418 equal( el_bb.height( 101 ).height(), expected + 1, "test border-box height(int) by roundtripping" );
419 equal( el_bb.innerHeight( 108 ).height(), expected + 2, "test border-box innerHeight(int) by roundtripping" );
420 equal( el_bb.outerHeight( 117 ).height(), expected + 3, "test border-box outerHeight(int) by roundtripping" );
421 equal( el_bb.outerHeight( 118, false ).height(), expected + 4, "test border-box outerHeight(int, false) by roundtripping" );
422 equal( el_bb.outerHeight( 129, true ).height(), expected + 5, "test border-box innerHeight(int, true) by roundtripping" );
424 equal( el.width( 101 ).width(), expected + 1, "test border-box width(int) by roundtripping" );
425 equal( el.innerWidth( 108 ).width(), expected + 2, "test border-box innerWidth(int) by roundtripping" );
426 equal( el.outerWidth( 117 ).width(), expected + 3, "test border-box outerWidth(int) by roundtripping" );
427 equal( el.outerWidth( 118, false ).width(), expected + 4, "test border-box outerWidth(int, false) by roundtripping" );
428 equal( el.outerWidth( 129, true ).width(), expected + 5, "test border-box innerWidth(int, true) by roundtripping" );
430 equal( el.height( 101 ).height(), expected + 1, "test border-box height(int) by roundtripping" );
431 equal( el.innerHeight( 108 ).height(), expected + 2, "test border-box innerHeight(int) by roundtripping" );
432 equal( el.outerHeight( 117 ).height(), expected + 3, "test border-box outerHeight(int) by roundtripping" );
433 equal( el.outerHeight( 118, false ).height(), expected + 4, "test border-box outerHeight(int, false) by roundtripping" );
434 equal( el.outerHeight( 129, true ).height(), expected + 5, "test border-box innerHeight(int, true) by roundtripping" );
437 testIframe( "dimensions/documentSmall", "window vs. small document", function( jQuery, window, document ) {
438 // this test is practically tautological, but there is a bug in IE8
439 // with no simple workaround, so this test exposes the bug and works around it
440 if ( document.body.offsetWidth >= document.documentElement.offsetWidth ) {
443 equal( jQuery( document ).height(), jQuery( window ).height(), "document height matches window height" );
444 equal( jQuery( document ).width(), jQuery( window ).width(), "document width matches window width" );
446 // all tests should have at least one assertion
448 ok( true, "skipping test (conditions not satisfied)" );
452 testIframe( "dimensions/documentLarge", "window vs. large document", function( jQuery, window, document ) {
455 ok( jQuery( document ).height() > jQuery( window ).height(), "document height is larger than window height" );
456 ok( jQuery( document ).width() > jQuery( window ).width(), "document width is larger than window width" );