dimensions.js 20 KB

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