dimensions.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. if ( jQuery.fn.width ) {
  2. module("dimensions", { teardown: moduleTeardown });
  3. var pass = function( val ) {
  4. return val;
  5. };
  6. var fn = function( val ) {
  7. return function() {
  8. return val;
  9. };
  10. };
  11. /*
  12. ======== local reference =======
  13. pass and fn can be used to test passing functions to setters
  14. See testWidth below for an example
  15. pass( value );
  16. This function returns whatever value is passed in
  17. fn( value );
  18. Returns a function that returns the value
  19. */
  20. var testWidth = function( val ) {
  21. expect(9);
  22. var $div = jQuery("#nothiddendiv");
  23. $div.width( val(30) );
  24. equal($div.width(), 30, "Test set to 30 correctly");
  25. $div.hide();
  26. equal($div.width(), 30, "Test hidden div");
  27. $div.show();
  28. $div.width( val(-1) ); // handle negative numbers by setting to 0 #11604
  29. equal($div.width(), 0, "Test negative width normalized to 0");
  30. $div.css("padding", "20px");
  31. equal($div.width(), 0, "Test padding specified with pixels");
  32. $div.css("border", "2px solid #fff");
  33. equal($div.width(), 0, "Test border specified with pixels");
  34. $div.css({ "display": "", "border": "", "padding": "" });
  35. jQuery("#nothiddendivchild").css({ "width": 20, "padding": "3px", "border": "2px solid #fff" });
  36. equal(jQuery("#nothiddendivchild").width(), 20, "Test child width with border and padding");
  37. jQuery("#nothiddendiv, #nothiddendivchild").css({ "border": "", "padding": "", "width": "" });
  38. var blah = jQuery("blah");
  39. equal( blah.width( val(10) ), blah, "Make sure that setting a width on an empty set returns the set." );
  40. equal( blah.width(), null, "Make sure 'null' is returned on an empty set");
  41. equal( jQuery(window).width(), document.documentElement.clientWidth, "Window width is equal to width reported by window/document." );
  42. QUnit.expectJqData( $div[0], "olddisplay" );
  43. };
  44. test("width()", function() {
  45. testWidth( pass );
  46. });
  47. test("width(Function)", function() {
  48. testWidth( fn );
  49. });
  50. test("width(Function(args))", function() {
  51. expect( 2 );
  52. var $div = jQuery("#nothiddendiv");
  53. $div.width( 30 ).width(function(i, width) {
  54. equal( width, 30, "Make sure previous value is corrrect." );
  55. return width + 1;
  56. });
  57. equal( $div.width(), 31, "Make sure value was modified correctly." );
  58. });
  59. var testHeight = function( val ) {
  60. expect(9);
  61. var $div = jQuery("#nothiddendiv");
  62. $div.height( val(30) );
  63. equal($div.height(), 30, "Test set to 30 correctly");
  64. $div.hide();
  65. equal($div.height(), 30, "Test hidden div");
  66. $div.show();
  67. $div.height( val(-1) ); // handle negative numbers by setting to 0 #11604
  68. equal($div.height(), 0, "Test negative height normalized to 0");
  69. $div.css("padding", "20px");
  70. equal($div.height(), 0, "Test padding specified with pixels");
  71. $div.css("border", "2px solid #fff");
  72. equal($div.height(), 0, "Test border specified with pixels");
  73. $div.css({ "display": "", "border": "", "padding": "", "height": "1px" });
  74. jQuery("#nothiddendivchild").css({ "height": 20, "padding": "3px", "border": "2px solid #fff" });
  75. equal(jQuery("#nothiddendivchild").height(), 20, "Test child height with border and padding");
  76. jQuery("#nothiddendiv, #nothiddendivchild").css({ "border": "", "padding": "", "height": "" });
  77. var blah = jQuery("blah");
  78. equal( blah.height( val(10) ), blah, "Make sure that setting a height on an empty set returns the set." );
  79. equal( blah.height(), null, "Make sure 'null' is returned on an empty set");
  80. equal( jQuery(window).height(), document.documentElement.clientHeight, "Window width is equal to width reported by window/document." );
  81. QUnit.expectJqData( $div[0], "olddisplay" );
  82. };
  83. test("height()", function() {
  84. testHeight( pass );
  85. });
  86. test("height(Function)", function() {
  87. testHeight( fn );
  88. });
  89. test("height(Function(args))", function() {
  90. expect( 2 );
  91. var $div = jQuery("#nothiddendiv");
  92. $div.height( 30 ).height(function(i, height) {
  93. equal( height, 30, "Make sure previous value is corrrect." );
  94. return height + 1;
  95. });
  96. equal( $div.height(), 31, "Make sure value was modified correctly." );
  97. });
  98. test("innerWidth()", function() {
  99. expect(6);
  100. var winWidth = jQuery( window ).width(),
  101. docWidth = jQuery( document ).width();
  102. equal(jQuery(window).innerWidth(), winWidth, "Test on window");
  103. equal(jQuery(document).innerWidth(), docWidth, "Test on document");
  104. var $div = jQuery("#nothiddendiv");
  105. // set styles
  106. $div.css({
  107. "margin": 10,
  108. "border": "2px solid #fff",
  109. "width": 30
  110. });
  111. equal($div.innerWidth(), 30, "Test with margin and border");
  112. $div.css("padding", "20px");
  113. equal($div.innerWidth(), 70, "Test with margin, border and padding");
  114. $div.hide();
  115. equal($div.innerWidth(), 70, "Test hidden div");
  116. // reset styles
  117. $div.css({ "display": "", "border": "", "padding": "", "width": "", "height": "" });
  118. var div = jQuery( "<div>" );
  119. // Temporarily require 0 for backwards compat - should be auto
  120. equal( div.innerWidth(), 0, "Make sure that disconnected nodes are handled." );
  121. div.remove();
  122. QUnit.expectJqData( $div[0], "olddisplay" );
  123. });
  124. test("innerHeight()", function() {
  125. expect(6);
  126. var winHeight = jQuery( window ).height(),
  127. docHeight = jQuery( document ).height();
  128. equal(jQuery(window).innerHeight(), winHeight, "Test on window");
  129. equal(jQuery(document).innerHeight(), docHeight, "Test on document");
  130. var $div = jQuery("#nothiddendiv");
  131. // set styles
  132. $div.css({
  133. "margin": 10,
  134. "border": "2px solid #fff",
  135. "height": 30
  136. });
  137. equal($div.innerHeight(), 30, "Test with margin and border");
  138. $div.css("padding", "20px");
  139. equal($div.innerHeight(), 70, "Test with margin, border and padding");
  140. $div.hide();
  141. equal($div.innerHeight(), 70, "Test hidden div");
  142. // reset styles
  143. $div.css({ "display": "", "border": "", "padding": "", "width": "", "height": "" });
  144. var div = jQuery( "<div>" );
  145. // Temporarily require 0 for backwards compat - should be auto
  146. equal( div.innerHeight(), 0, "Make sure that disconnected nodes are handled." );
  147. div.remove();
  148. QUnit.expectJqData( $div[0], "olddisplay" );
  149. });
  150. test("outerWidth()", function() {
  151. expect(11);
  152. var winWidth = jQuery( window ).width(),
  153. docWidth = jQuery( document ).width();
  154. equal( jQuery( window ).outerWidth(), winWidth, "Test on window without margin option" );
  155. equal( jQuery( window ).outerWidth( true ), winWidth, "Test on window with margin option" );
  156. equal( jQuery( document ).outerWidth(), docWidth, "Test on document without margin option" );
  157. equal( jQuery( document ).outerWidth( true ), docWidth, "Test on document with margin option" );
  158. var $div = jQuery("#nothiddendiv");
  159. $div.css("width", 30);
  160. equal($div.outerWidth(), 30, "Test with only width set");
  161. $div.css("padding", "20px");
  162. equal($div.outerWidth(), 70, "Test with padding");
  163. $div.css("border", "2px solid #fff");
  164. equal($div.outerWidth(), 74, "Test with padding and border");
  165. $div.css("margin", "10px");
  166. equal($div.outerWidth(), 74, "Test with padding, border and margin without margin option");
  167. $div.css("position", "absolute");
  168. equal($div.outerWidth(true), 94, "Test with padding, border and margin with margin option");
  169. $div.hide();
  170. equal($div.outerWidth(true), 94, "Test hidden div with padding, border and margin with margin option");
  171. // reset styles
  172. $div.css({ "position": "", "display": "", "border": "", "padding": "", "width": "", "height": "" });
  173. var div = jQuery( "<div>" );
  174. // Temporarily require 0 for backwards compat - should be auto
  175. equal( div.outerWidth(), 0, "Make sure that disconnected nodes are handled." );
  176. div.remove();
  177. QUnit.expectJqData( $div[0], "olddisplay" );
  178. });
  179. test("child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height() see #9441 #9300", function() {
  180. expect(16);
  181. // setup html
  182. var $divNormal = jQuery("<div>").css({ "width": "100px", "height": "100px", "border": "10px solid white", "padding": "2px", "margin": "3px" }),
  183. $divChild = $divNormal.clone(),
  184. $divUnconnected = $divNormal.clone(),
  185. $divHiddenParent = jQuery("<div>").css( "display", "none" ).append( $divChild ).appendTo("body");
  186. $divNormal.appendTo("body");
  187. // tests that child div of a hidden div works the same as a normal div
  188. equal( $divChild.width(), $divNormal.width(), "child of a hidden element width() is wrong see #9441" );
  189. equal( $divChild.innerWidth(), $divNormal.innerWidth(), "child of a hidden element innerWidth() is wrong see #9441" );
  190. equal( $divChild.outerWidth(), $divNormal.outerWidth(), "child of a hidden element outerWidth() is wrong see #9441" );
  191. equal( $divChild.outerWidth(true), $divNormal.outerWidth( true ), "child of a hidden element outerWidth( true ) is wrong see #9300" );
  192. equal( $divChild.height(), $divNormal.height(), "child of a hidden element height() is wrong see #9441" );
  193. equal( $divChild.innerHeight(), $divNormal.innerHeight(), "child of a hidden element innerHeight() is wrong see #9441" );
  194. equal( $divChild.outerHeight(), $divNormal.outerHeight(), "child of a hidden element outerHeight() is wrong see #9441" );
  195. equal( $divChild.outerHeight(true), $divNormal.outerHeight( true ), "child of a hidden element outerHeight( true ) is wrong see #9300" );
  196. // tests that child div of an unconnected div works the same as a normal div
  197. equal( $divUnconnected.width(), $divNormal.width(), "unconnected element width() is wrong see #9441" );
  198. equal( $divUnconnected.innerWidth(), $divNormal.innerWidth(), "unconnected element innerWidth() is wrong see #9441" );
  199. equal( $divUnconnected.outerWidth(), $divNormal.outerWidth(), "unconnected element outerWidth() is wrong see #9441" );
  200. equal( $divUnconnected.outerWidth(true), $divNormal.outerWidth( true ), "unconnected element outerWidth( true ) is wrong see #9300" );
  201. equal( $divUnconnected.height(), $divNormal.height(), "unconnected element height() is wrong see #9441" );
  202. equal( $divUnconnected.innerHeight(), $divNormal.innerHeight(), "unconnected element innerHeight() is wrong see #9441" );
  203. equal( $divUnconnected.outerHeight(), $divNormal.outerHeight(), "unconnected element outerHeight() is wrong see #9441" );
  204. equal( $divUnconnected.outerHeight(true), $divNormal.outerHeight( true ), "unconnected element outerHeight( true ) is wrong see #9300" );
  205. // teardown html
  206. $divHiddenParent.remove();
  207. $divNormal.remove();
  208. });
  209. test("getting dimensions shouldnt modify runtimeStyle see #9233", function() {
  210. expect( 1 );
  211. var $div = jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
  212. div = $div.get( 0 ),
  213. runtimeStyle = div.runtimeStyle;
  214. if ( runtimeStyle ) {
  215. div.runtimeStyle.marginLeft = "12em";
  216. div.runtimeStyle.left = "11em";
  217. }
  218. $div.outerWidth( true );
  219. if ( runtimeStyle ) {
  220. equal( div.runtimeStyle.left, "11em", "getting dimensions modifies runtimeStyle, see #9233" );
  221. } else {
  222. ok( true, "this browser doesnt support runtimeStyle, see #9233" );
  223. }
  224. $div.remove();
  225. });
  226. test( "table dimensions", 2, function() {
  227. 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"),
  228. tdElem = table.find("tr:eq(0) td:eq(0)"),
  229. colElem = table.find("col:eq(1)").width( 300 );
  230. table.find("td").css({ "margin": 0, "padding": 0 });
  231. equal( tdElem.width(), tdElem.width(), "width() doesn't alter dimension values of empty cells, see #11293" );
  232. equal( colElem.width(), 300, "col elements have width(), see #12243" );
  233. });
  234. test("box-sizing:border-box child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height() see #10413", function() {
  235. expect(16);
  236. // setup html
  237. var $divNormal = jQuery("<div>").css({ "boxSizing": "border-box", "width": "100px", "height": "100px", "border": "10px solid white", "padding": "2px", "margin": "3px" }),
  238. $divChild = $divNormal.clone(),
  239. $divUnconnected = $divNormal.clone(),
  240. $divHiddenParent = jQuery("<div>").css( "display", "none" ).append( $divChild ).appendTo("body");
  241. $divNormal.appendTo("body");
  242. // tests that child div of a hidden div works the same as a normal div
  243. equal( $divChild.width(), $divNormal.width(), "child of a hidden element width() is wrong see #10413" );
  244. equal( $divChild.innerWidth(), $divNormal.innerWidth(), "child of a hidden element innerWidth() is wrong see #10413" );
  245. equal( $divChild.outerWidth(), $divNormal.outerWidth(), "child of a hidden element outerWidth() is wrong see #10413" );
  246. equal( $divChild.outerWidth(true), $divNormal.outerWidth( true ), "child of a hidden element outerWidth( true ) is wrong see #10413" );
  247. equal( $divChild.height(), $divNormal.height(), "child of a hidden element height() is wrong see #10413" );
  248. equal( $divChild.innerHeight(), $divNormal.innerHeight(), "child of a hidden element innerHeight() is wrong see #10413" );
  249. equal( $divChild.outerHeight(), $divNormal.outerHeight(), "child of a hidden element outerHeight() is wrong see #10413" );
  250. equal( $divChild.outerHeight(true), $divNormal.outerHeight( true ), "child of a hidden element outerHeight( true ) is wrong see #10413" );
  251. // tests that child div of an unconnected div works the same as a normal div
  252. equal( $divUnconnected.width(), $divNormal.width(), "unconnected element width() is wrong see #10413" );
  253. equal( $divUnconnected.innerWidth(), $divNormal.innerWidth(), "unconnected element innerWidth() is wrong see #10413" );
  254. equal( $divUnconnected.outerWidth(), $divNormal.outerWidth(), "unconnected element outerWidth() is wrong see #10413" );
  255. equal( $divUnconnected.outerWidth(true), $divNormal.outerWidth( true ), "unconnected element outerWidth( true ) is wrong see #10413" );
  256. equal( $divUnconnected.height(), $divNormal.height(), "unconnected element height() is wrong see #10413" );
  257. equal( $divUnconnected.innerHeight(), $divNormal.innerHeight(), "unconnected element innerHeight() is wrong see #10413" );
  258. equal( $divUnconnected.outerHeight(), $divNormal.outerHeight(), "unconnected element outerHeight() is wrong see #10413" );
  259. equal( $divUnconnected.outerHeight(true), $divNormal.outerHeight( true ), "unconnected element outerHeight( true ) is wrong see #10413" );
  260. // teardown html
  261. $divHiddenParent.remove();
  262. $divNormal.remove();
  263. });
  264. test("outerHeight()", function() {
  265. expect(11);
  266. var winHeight = jQuery( window ).height(),
  267. docHeight = jQuery( document ).height();
  268. equal( jQuery( window ).outerHeight(), winHeight, "Test on window without margin option" );
  269. equal( jQuery( window ).outerHeight( true ), winHeight, "Test on window with margin option" );
  270. equal( jQuery( document ).outerHeight(), docHeight, "Test on document without margin option" );
  271. equal( jQuery( document ).outerHeight( true ), docHeight, "Test on document with margin option" );
  272. var $div = jQuery("#nothiddendiv");
  273. $div.css("height", 30);
  274. equal($div.outerHeight(), 30, "Test with only width set");
  275. $div.css("padding", "20px");
  276. equal($div.outerHeight(), 70, "Test with padding");
  277. $div.css("border", "2px solid #fff");
  278. equal($div.outerHeight(), 74, "Test with padding and border");
  279. $div.css("margin", "10px");
  280. equal($div.outerHeight(), 74, "Test with padding, border and margin without margin option");
  281. equal($div.outerHeight(true), 94, "Test with padding, border and margin with margin option");
  282. $div.hide();
  283. equal($div.outerHeight(true), 94, "Test hidden div with padding, border and margin with margin option");
  284. // reset styles
  285. $div.css({ "display": "", "border": "", "padding": "", "width": "", "height": "" });
  286. var div = jQuery( "<div>" );
  287. // Temporarily require 0 for backwards compat - should be auto
  288. equal( div.outerHeight(), 0, "Make sure that disconnected nodes are handled." );
  289. div.remove();
  290. QUnit.expectJqData( $div[0], "olddisplay" );
  291. });
  292. test("passing undefined is a setter #5571", function() {
  293. expect(4);
  294. equal(jQuery("#nothiddendiv").height(30).height(undefined).height(), 30, ".height(undefined) is chainable (#5571)");
  295. equal(jQuery("#nothiddendiv").height(30).innerHeight(undefined).height(), 30, ".innerHeight(undefined) is chainable (#5571)");
  296. equal(jQuery("#nothiddendiv").height(30).outerHeight(undefined).height(), 30, ".outerHeight(undefined) is chainable (#5571)");
  297. equal(jQuery("#nothiddendiv").width(30).width(undefined).width(), 30, ".width(undefined) is chainable (#5571)");
  298. });
  299. test( "getters on non elements should return null", function() {
  300. expect( 8 );
  301. var nonElem = jQuery("notAnElement");
  302. strictEqual( nonElem.width(), null, ".width() is not null (#12283)" );
  303. strictEqual( nonElem.innerWidth(), null, ".innerWidth() is not null (#12283)" );
  304. strictEqual( nonElem.outerWidth(), null, ".outerWidth() is not null (#12283)" );
  305. strictEqual( nonElem.outerWidth( true ), null, ".outerWidth(true) is not null (#12283)" );
  306. strictEqual( nonElem.height(), null, ".height() is not null (#12283)" );
  307. strictEqual( nonElem.innerHeight(), null, ".innerHeight() is not null (#12283)" );
  308. strictEqual( nonElem.outerHeight(), null, ".outerHeight() is not null (#12283)" );
  309. strictEqual( nonElem.outerHeight( true ), null, ".outerHeight(true) is not null (#12283)" );
  310. });
  311. test("setters with and without box-sizing:border-box", function(){
  312. expect(20);
  313. 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"),
  314. el = jQuery("<div style='width:100px;height:100px;margin:5px;padding:3px;border:4px solid white;'>test</div>").appendTo("#qunit-fixture"),
  315. expected = 100;
  316. equal( el_bb.width( 101 ).width(), expected + 1, "test border-box width(int) by roundtripping" );
  317. equal( el_bb.innerWidth( 108 ).width(), expected + 2, "test border-box innerWidth(int) by roundtripping" );
  318. equal( el_bb.outerWidth( 117 ).width(), expected + 3, "test border-box outerWidth(int) by roundtripping" );
  319. equal( el_bb.outerWidth( 118, false ).width(), expected + 4, "test border-box outerWidth(int, false) by roundtripping" );
  320. equal( el_bb.outerWidth( 129, true ).width(), expected + 5, "test border-box innerWidth(int, true) by roundtripping" );
  321. equal( el_bb.height( 101 ).height(), expected + 1, "test border-box height(int) by roundtripping" );
  322. equal( el_bb.innerHeight( 108 ).height(), expected + 2, "test border-box innerHeight(int) by roundtripping" );
  323. equal( el_bb.outerHeight( 117 ).height(), expected + 3, "test border-box outerHeight(int) by roundtripping" );
  324. equal( el_bb.outerHeight( 118, false ).height(), expected + 4, "test border-box outerHeight(int, false) by roundtripping" );
  325. equal( el_bb.outerHeight( 129, true ).height(), expected + 5, "test border-box innerHeight(int, true) by roundtripping" );
  326. equal( el.width( 101 ).width(), expected + 1, "test border-box width(int) by roundtripping" );
  327. equal( el.innerWidth( 108 ).width(), expected + 2, "test border-box innerWidth(int) by roundtripping" );
  328. equal( el.outerWidth( 117 ).width(), expected + 3, "test border-box outerWidth(int) by roundtripping" );
  329. equal( el.outerWidth( 118, false ).width(), expected + 4, "test border-box outerWidth(int, false) by roundtripping" );
  330. equal( el.outerWidth( 129, true ).width(), expected + 5, "test border-box innerWidth(int, true) by roundtripping" );
  331. equal( el.height( 101 ).height(), expected + 1, "test border-box height(int) by roundtripping" );
  332. equal( el.innerHeight( 108 ).height(), expected + 2, "test border-box innerHeight(int) by roundtripping" );
  333. equal( el.outerHeight( 117 ).height(), expected + 3, "test border-box outerHeight(int) by roundtripping" );
  334. equal( el.outerHeight( 118, false ).height(), expected + 4, "test border-box outerHeight(int, false) by roundtripping" );
  335. equal( el.outerHeight( 129, true ).height(), expected + 5, "test border-box innerHeight(int, true) by roundtripping" );
  336. });
  337. testIframe( "dimensions/documentSmall", "window vs. small document", function( jQuery, window, document ) {
  338. // this test is practically tautological, but there is a bug in IE8
  339. // with no simple workaround, so this test exposes the bug and works around it
  340. if ( document.body.offsetWidth >= document.documentElement.offsetWidth ) {
  341. expect( 2 );
  342. equal( jQuery( document ).height(), jQuery( window ).height(), "document height matches window height" );
  343. equal( jQuery( document ).width(), jQuery( window ).width(), "document width matches window width" );
  344. } else {
  345. // all tests should have at least one assertion
  346. expect( 1 );
  347. ok( true, "skipping test (conditions not satisfied)" );
  348. }
  349. });
  350. testIframe( "dimensions/documentLarge", "window vs. large document", function( jQuery, window, document ) {
  351. expect(2);
  352. ok( jQuery( document ).height() > jQuery( window ).height(), "document height is larger than window height" );
  353. ok( jQuery( document ).width() > jQuery( window ).width(), "document width is larger than window width" );
  354. });
  355. }