Нема описа

dimensions.js 37KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. ( function() {
  2. if ( !jQuery.fn.width ) {
  3. return;
  4. }
  5. QUnit.module( "dimensions", { afterEach: 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, assert );
  19. This function returns whatever value is passed in
  20. fn( value, assert );
  21. Returns a function that returns the value
  22. */
  23. function testWidth( val, assert ) {
  24. assert.expect( 9 );
  25. var $div, $empty;
  26. $div = jQuery( "#nothiddendiv" );
  27. $div.width( val( 30 ) );
  28. assert.equal( $div.width(), 30, "Test set to 30 correctly" );
  29. $div.css( "display", "none" );
  30. assert.equal( $div.width(), 30, "Test hidden div" );
  31. $div.css( "display", "" );
  32. $div.width( val( -1 ) ); // handle negative numbers by setting to 0 #11604
  33. assert.equal( $div.width(), 0, "Test negative width normalized to 0" );
  34. $div.css( "padding", "20px" );
  35. assert.equal( $div.width(), 0, "Test padding specified with pixels" );
  36. $div.css( "border", "2px solid #fff" );
  37. assert.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. assert.equal( jQuery( "#nothiddendivchild" ).width(), 20, "Test child width with border and padding" );
  41. jQuery( "#nothiddendiv, #nothiddendivchild" ).css( { "border": "", "padding": "", "width": "" } );
  42. $empty = jQuery();
  43. assert.equal( $empty.width( val( 10 ) ), $empty, "Make sure that setting a width on an empty set returns the set." );
  44. assert.strictEqual( $empty.width(), undefined, "Make sure 'undefined' is returned on an empty set" );
  45. assert.equal( jQuery( window ).width(), document.documentElement.clientWidth, "Window width is equal to width reported by window/document." );
  46. }
  47. QUnit.test( "width()", function( assert ) {
  48. testWidth( pass, assert );
  49. } );
  50. QUnit.test( "width(Function)", function( assert ) {
  51. testWidth( fn, assert );
  52. } );
  53. QUnit.test( "width(Function(args))", function( assert ) {
  54. assert.expect( 2 );
  55. var $div = jQuery( "#nothiddendiv" );
  56. $div.width( 30 ).width( function( i, width ) {
  57. assert.equal( width, 30, "Make sure previous value is correct." );
  58. return width + 1;
  59. } );
  60. assert.equal( $div.width(), 31, "Make sure value was modified correctly." );
  61. } );
  62. function testHeight( val, assert ) {
  63. assert.expect( 9 );
  64. var $div, blah;
  65. $div = jQuery( "#nothiddendiv" );
  66. $div.height( val( 30 ) );
  67. assert.equal( $div.height(), 30, "Test set to 30 correctly" );
  68. $div.css( "display", "none" );
  69. assert.equal( $div.height(), 30, "Test hidden div" );
  70. $div.css( "display", "" );
  71. $div.height( val( -1 ) ); // handle negative numbers by setting to 0 #11604
  72. assert.equal( $div.height(), 0, "Test negative height normalized to 0" );
  73. $div.css( "padding", "20px" );
  74. assert.equal( $div.height(), 0, "Test padding specified with pixels" );
  75. $div.css( "border", "2px solid #fff" );
  76. assert.equal( $div.height(), 0, "Test border specified with pixels" );
  77. $div.css( { "display": "", "border": "", "padding": "", "height": "1px" } );
  78. jQuery( "#nothiddendivchild" ).css( { "height": 20, "padding": "3px", "border": "2px solid #fff" } );
  79. assert.equal( jQuery( "#nothiddendivchild" ).height(), 20, "Test child height with border and padding" );
  80. jQuery( "#nothiddendiv, #nothiddendivchild" ).css( { "border": "", "padding": "", "height": "" } );
  81. blah = jQuery( "blah" );
  82. assert.equal( blah.height( val( 10 ) ), blah, "Make sure that setting a height on an empty set returns the set." );
  83. assert.strictEqual( blah.height(), undefined, "Make sure 'undefined' is returned on an empty set" );
  84. assert.equal( jQuery( window ).height(), document.documentElement.clientHeight, "Window width is equal to width reported by window/document." );
  85. }
  86. QUnit.test( "height()", function( assert ) {
  87. testHeight( pass, assert );
  88. } );
  89. QUnit.test( "height(Function)", function( assert ) {
  90. testHeight( fn, assert );
  91. } );
  92. QUnit.test( "height(Function(args))", function( assert ) {
  93. assert.expect( 2 );
  94. var $div = jQuery( "#nothiddendiv" );
  95. $div.height( 30 ).height( function( i, height ) {
  96. assert.equal( height, 30, "Make sure previous value is correct." );
  97. return height + 1;
  98. } );
  99. assert.equal( $div.height(), 31, "Make sure value was modified correctly." );
  100. } );
  101. QUnit.test( "innerWidth()", function( assert ) {
  102. assert.expect( 7 );
  103. var $div, div,
  104. $win = jQuery( window ),
  105. $doc = jQuery( document );
  106. assert.equal( jQuery( window ).innerWidth(), $win.width(), "Test on window" );
  107. assert.equal( jQuery( document ).innerWidth(), $doc.width(), "Test on document" );
  108. assert.strictEqual( jQuery().innerWidth(), undefined, "Test on empty set" );
  109. $div = jQuery( "#nothiddendiv" );
  110. $div.css( {
  111. "margin": 10,
  112. "border": "2px solid #fff",
  113. "width": 30
  114. } );
  115. assert.equal( $div.innerWidth(), 30, "Test with margin and border" );
  116. $div.css( "padding", "20px" );
  117. assert.equal( $div.innerWidth(), 70, "Test with margin, border and padding" );
  118. $div.css( "display", "none" );
  119. assert.equal( $div.innerWidth(), 70, "Test hidden div" );
  120. // reset styles
  121. $div.css( { "display": "", "border": "", "padding": "", "width": "", "height": "" } );
  122. div = jQuery( "<div>" );
  123. // Temporarily require 0 for backwards compat - should be auto
  124. assert.equal( div.innerWidth(), 0, "Make sure that disconnected nodes are handled." );
  125. div.remove();
  126. } );
  127. QUnit.test( "innerHeight()", function( assert ) {
  128. assert.expect( 7 );
  129. var $div, div,
  130. $win = jQuery( window ),
  131. $doc = jQuery( document );
  132. assert.equal( jQuery( window ).innerHeight(), $win.height(), "Test on window" );
  133. assert.equal( jQuery( document ).innerHeight(), $doc.height(), "Test on document" );
  134. assert.strictEqual( jQuery().innerHeight(), undefined, "Test on empty set" );
  135. $div = jQuery( "#nothiddendiv" );
  136. $div.css( {
  137. "margin": 10,
  138. "border": "2px solid #fff",
  139. "height": 30
  140. } );
  141. assert.equal( $div.innerHeight(), 30, "Test with margin and border" );
  142. $div.css( "padding", "20px" );
  143. assert.equal( $div.innerHeight(), 70, "Test with margin, border and padding" );
  144. $div.css( "display", "none" );
  145. assert.equal( $div.innerHeight(), 70, "Test hidden div" );
  146. // reset styles
  147. $div.css( { "display": "", "border": "", "padding": "", "width": "", "height": "" } );
  148. div = jQuery( "<div>" );
  149. // Temporarily require 0 for backwards compat - should be auto
  150. assert.equal( div.innerHeight(), 0, "Make sure that disconnected nodes are handled." );
  151. div.remove();
  152. } );
  153. QUnit.test( "outerWidth()", function( assert ) {
  154. assert.expect( 12 );
  155. var $div, div,
  156. $win = jQuery( window ),
  157. $doc = jQuery( document ),
  158. winwidth = $win.prop( "innerWidth" );
  159. assert.equal( jQuery( window ).outerWidth(), winwidth, "Test on window without margin option" );
  160. assert.equal( jQuery( window ).outerWidth( true ), winwidth, "Test on window with margin option" );
  161. assert.equal( jQuery( document ).outerWidth(), $doc.width(), "Test on document without margin option" );
  162. assert.equal( jQuery( document ).outerWidth( true ), $doc.width(), "Test on document with margin option" );
  163. assert.strictEqual( jQuery().outerWidth(), undefined, "Test on empty set" );
  164. $div = jQuery( "#nothiddendiv" );
  165. $div.css( "width", 30 );
  166. assert.equal( $div.outerWidth(), 30, "Test with only width set" );
  167. $div.css( "padding", "20px" );
  168. assert.equal( $div.outerWidth(), 70, "Test with padding" );
  169. $div.css( "border", "2px solid #fff" );
  170. assert.equal( $div.outerWidth(), 74, "Test with padding and border" );
  171. $div.css( "margin", "10px" );
  172. assert.equal( $div.outerWidth(), 74, "Test with padding, border and margin without margin option" );
  173. $div.css( "position", "absolute" );
  174. assert.equal( $div.outerWidth( true ), 94, "Test with padding, border and margin with margin option" );
  175. $div.css( "display", "none" );
  176. assert.equal( $div.outerWidth( true ), 94, "Test hidden div with padding, border and margin with margin option" );
  177. // reset styles
  178. $div.css( { "position": "", "display": "", "border": "", "padding": "", "width": "", "height": "" } );
  179. div = jQuery( "<div>" );
  180. // Temporarily require 0 for backwards compat - should be auto
  181. assert.equal( div.outerWidth(), 0, "Make sure that disconnected nodes are handled." );
  182. div.remove();
  183. } );
  184. QUnit.test( "outerHeight()", function( assert ) {
  185. assert.expect( 12 );
  186. var $div, div,
  187. $win = jQuery( window ),
  188. $doc = jQuery( document ),
  189. winheight = $win.prop( "innerHeight" );
  190. assert.equal( jQuery( window ).outerHeight(), winheight, "Test on window without margin option" );
  191. assert.equal( jQuery( window ).outerHeight( true ), winheight, "Test on window with margin option" );
  192. assert.equal( jQuery( document ).outerHeight(), $doc.height(), "Test on document without margin option" );
  193. assert.equal( jQuery( document ).outerHeight( true ), $doc.height(), "Test on document with margin option" );
  194. assert.strictEqual( jQuery().outerHeight(), undefined, "Test on empty set" );
  195. $div = jQuery( "#nothiddendiv" );
  196. $div.css( "height", 30 );
  197. assert.equal( $div.outerHeight(), 30, "Test with only height set" );
  198. $div.css( "padding", "20px" );
  199. assert.equal( $div.outerHeight(), 70, "Test with padding" );
  200. $div.css( "border", "2px solid #fff" );
  201. assert.equal( $div.outerHeight(), 74, "Test with padding and border" );
  202. $div.css( "margin", "10px" );
  203. assert.equal( $div.outerHeight(), 74, "Test with padding, border and margin without margin option" );
  204. $div.css( "position", "absolute" );
  205. assert.equal( $div.outerHeight( true ), 94, "Test with padding, border and margin with margin option" );
  206. $div.css( "display", "none" );
  207. assert.equal( $div.outerHeight( true ), 94, "Test hidden div with padding, border and margin with margin option" );
  208. // reset styles
  209. $div.css( { "position": "", "display": "", "border": "", "padding": "", "width": "", "height": "" } );
  210. div = jQuery( "<div>" );
  211. // Temporarily require 0 for backwards compat - should be auto
  212. assert.equal( div.outerWidth(), 0, "Make sure that disconnected nodes are handled." );
  213. div.remove();
  214. } );
  215. QUnit.test( "child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height() see #9441 #9300", function( assert ) {
  216. assert.expect( 16 );
  217. // setup html
  218. var $divNormal = jQuery( "<div>" ).css( { "width": "100px", "height": "100px", "border": "10px solid white", "padding": "2px", "margin": "3px" } ),
  219. $divChild = $divNormal.clone(),
  220. $divUnconnected = $divNormal.clone(),
  221. $divHiddenParent = jQuery( "<div>" ).css( "display", "none" ).append( $divChild ).appendTo( "body" );
  222. $divNormal.appendTo( "body" );
  223. // tests that child div of a hidden div works the same as a normal div
  224. assert.equal( $divChild.width(), $divNormal.width(), "child of a hidden element width() is wrong see #9441" );
  225. assert.equal( $divChild.innerWidth(), $divNormal.innerWidth(), "child of a hidden element innerWidth() is wrong see #9441" );
  226. assert.equal( $divChild.outerWidth(), $divNormal.outerWidth(), "child of a hidden element outerWidth() is wrong see #9441" );
  227. assert.equal( $divChild.outerWidth( true ), $divNormal.outerWidth( true ), "child of a hidden element outerWidth( true ) is wrong see #9300" );
  228. assert.equal( $divChild.height(), $divNormal.height(), "child of a hidden element height() is wrong see #9441" );
  229. assert.equal( $divChild.innerHeight(), $divNormal.innerHeight(), "child of a hidden element innerHeight() is wrong see #9441" );
  230. assert.equal( $divChild.outerHeight(), $divNormal.outerHeight(), "child of a hidden element outerHeight() is wrong see #9441" );
  231. assert.equal( $divChild.outerHeight( true ), $divNormal.outerHeight( true ), "child of a hidden element outerHeight( true ) is wrong see #9300" );
  232. // tests that child div of an unconnected div works the same as a normal div
  233. assert.equal( $divUnconnected.width(), $divNormal.width(), "unconnected element width() is wrong see #9441" );
  234. assert.equal( $divUnconnected.innerWidth(), $divNormal.innerWidth(), "unconnected element innerWidth() is wrong see #9441" );
  235. assert.equal( $divUnconnected.outerWidth(), $divNormal.outerWidth(), "unconnected element outerWidth() is wrong see #9441" );
  236. assert.equal( $divUnconnected.outerWidth( true ), $divNormal.outerWidth( true ), "unconnected element outerWidth( true ) is wrong see #9300" );
  237. assert.equal( $divUnconnected.height(), $divNormal.height(), "unconnected element height() is wrong see #9441" );
  238. assert.equal( $divUnconnected.innerHeight(), $divNormal.innerHeight(), "unconnected element innerHeight() is wrong see #9441" );
  239. assert.equal( $divUnconnected.outerHeight(), $divNormal.outerHeight(), "unconnected element outerHeight() is wrong see #9441" );
  240. assert.equal( $divUnconnected.outerHeight( true ), $divNormal.outerHeight( true ), "unconnected element outerHeight( true ) is wrong see #9300" );
  241. // teardown html
  242. $divHiddenParent.remove();
  243. $divNormal.remove();
  244. } );
  245. QUnit.test( "getting dimensions shouldn't modify runtimeStyle see #9233", function( assert ) {
  246. assert.expect( 1 );
  247. var $div = jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
  248. div = $div.get( 0 ),
  249. runtimeStyle = div.runtimeStyle;
  250. if ( runtimeStyle ) {
  251. div.runtimeStyle.marginLeft = "12em";
  252. div.runtimeStyle.left = "11em";
  253. }
  254. $div.outerWidth( true );
  255. if ( runtimeStyle ) {
  256. assert.equal( div.runtimeStyle.left, "11em", "getting dimensions modifies runtimeStyle, see #9233" );
  257. } else {
  258. assert.ok( true, "this browser doesn't support runtimeStyle, see #9233" );
  259. }
  260. $div.remove();
  261. } );
  262. QUnit.test( "table dimensions", function( assert ) {
  263. assert.expect( 2 );
  264. var table = jQuery( "<table><colgroup><col></col><col></col></colgroup><tbody><tr><td></td><td>a</td></tr><tr><td></td><td>a</td></tr></tbody></table>" ).appendTo( "#qunit-fixture" ),
  265. tdElem = table.find( "td" ).first(),
  266. colElem = table.find( "col" ).first().width( 300 );
  267. table.find( "td" ).css( { "margin": 0, "padding": 0 } );
  268. assert.equal( tdElem.width(), tdElem.width(), "width() doesn't alter dimension values of empty cells, see #11293" );
  269. assert.equal( colElem.width(), 300, "col elements have width(), see #12243" );
  270. } );
  271. QUnit.test( "SVG dimensions (basic content-box)", function( assert ) {
  272. assert.expect( 8 );
  273. var svg = jQuery( "<svg style='width: 100px; height: 100px;'></svg>" ).appendTo( "#qunit-fixture" );
  274. assert.equal( svg.width(), 100 );
  275. assert.equal( svg.height(), 100 );
  276. assert.equal( svg.innerWidth(), 100 );
  277. assert.equal( svg.innerHeight(), 100 );
  278. assert.equal( svg.outerWidth(), 100 );
  279. assert.equal( svg.outerHeight(), 100 );
  280. assert.equal( svg.outerWidth( true ), 100 );
  281. assert.equal( svg.outerHeight( true ), 100 );
  282. svg.remove();
  283. } );
  284. QUnit.test( "SVG dimensions (content-box)", function( assert ) {
  285. assert.expect( 8 );
  286. var svg = jQuery( "<svg style='width: 100px; height: 100px; box-sizing: content-box; border: 1px solid white; padding: 2px; margin: 3px'></svg>" ).appendTo( "#qunit-fixture" );
  287. assert.equal( svg.width(), 100 );
  288. assert.equal( svg.height(), 100 );
  289. assert.equal( svg.innerWidth(), 104 );
  290. assert.equal( svg.innerHeight(), 104 );
  291. assert.equal( svg.outerWidth(), 106 );
  292. assert.equal( svg.outerHeight(), 106 );
  293. assert.equal( svg.outerWidth( true ), 112 );
  294. assert.equal( svg.outerHeight( true ), 112 );
  295. svg.remove();
  296. } );
  297. QUnit.test( "SVG dimensions (border-box)", function( assert ) {
  298. assert.expect( 8 );
  299. var svg = jQuery( "<svg style='width: 100px; height: 100px; box-sizing: border-box; border: 1px solid white; padding: 2px; margin: 3px'></svg>" ).appendTo( "#qunit-fixture" );
  300. assert.equal( svg.width(), 94, "width" );
  301. assert.equal( svg.height(), 94, "height" );
  302. assert.equal( svg.innerWidth(), 98, "innerWidth" );
  303. assert.equal( svg.innerHeight(), 98, "innerHeight" );
  304. assert.equal( svg.outerWidth(), 100, "outerWidth" );
  305. assert.equal( svg.outerHeight(), 100, "outerHeight" );
  306. assert.equal( svg.outerWidth( true ), 106, "outerWidth( true )" );
  307. assert.equal( svg.outerHeight( true ), 106, "outerHeight( true )" );
  308. svg.remove();
  309. } );
  310. QUnit.test( "box-sizing:border-box child of a hidden elem (or unconnected node) has accurate inner/outer/Width()/Height() see #10413", function( assert ) {
  311. assert.expect( 16 );
  312. // setup html
  313. var $divNormal = jQuery( "<div>" ).css( { "boxSizing": "border-box", "width": "100px", "height": "100px", "border": "10px solid white", "padding": "2px", "margin": "3px" } ),
  314. $divChild = $divNormal.clone(),
  315. $divUnconnected = $divNormal.clone(),
  316. $divHiddenParent = jQuery( "<div>" ).css( "display", "none" ).append( $divChild ).appendTo( "body" );
  317. $divNormal.appendTo( "body" );
  318. // tests that child div of a hidden div works the same as a normal div
  319. assert.equal( $divChild.width(), $divNormal.width(), "child of a hidden element width() is wrong see #10413" );
  320. assert.equal( $divChild.innerWidth(), $divNormal.innerWidth(), "child of a hidden element innerWidth() is wrong see #10413" );
  321. assert.equal( $divChild.outerWidth(), $divNormal.outerWidth(), "child of a hidden element outerWidth() is wrong see #10413" );
  322. assert.equal( $divChild.outerWidth( true ), $divNormal.outerWidth( true ), "child of a hidden element outerWidth( true ) is wrong see #10413" );
  323. assert.equal( $divChild.height(), $divNormal.height(), "child of a hidden element height() is wrong see #10413" );
  324. assert.equal( $divChild.innerHeight(), $divNormal.innerHeight(), "child of a hidden element innerHeight() is wrong see #10413" );
  325. assert.equal( $divChild.outerHeight(), $divNormal.outerHeight(), "child of a hidden element outerHeight() is wrong see #10413" );
  326. assert.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. assert.equal( $divUnconnected.width(), $divNormal.width(), "unconnected element width() is wrong see #10413" );
  329. assert.equal( $divUnconnected.innerWidth(), $divNormal.innerWidth(), "unconnected element innerWidth() is wrong see #10413" );
  330. assert.equal( $divUnconnected.outerWidth(), $divNormal.outerWidth(), "unconnected element outerWidth() is wrong see #10413" );
  331. assert.equal( $divUnconnected.outerWidth( true ), $divNormal.outerWidth( true ), "unconnected element outerWidth( true ) is wrong see #10413" );
  332. assert.equal( $divUnconnected.height(), $divNormal.height(), "unconnected element height() is wrong see #10413" );
  333. assert.equal( $divUnconnected.innerHeight(), $divNormal.innerHeight(), "unconnected element innerHeight() is wrong see #10413" );
  334. assert.equal( $divUnconnected.outerHeight(), $divNormal.outerHeight(), "unconnected element outerHeight() is wrong see #10413" );
  335. assert.equal( $divUnconnected.outerHeight( true ), $divNormal.outerHeight( true ), "unconnected element outerHeight( true ) is wrong see #10413" );
  336. // teardown html
  337. $divHiddenParent.remove();
  338. $divNormal.remove();
  339. } );
  340. QUnit.test( "passing undefined is a setter #5571", function( assert ) {
  341. assert.expect( 4 );
  342. assert.equal( jQuery( "#nothiddendiv" ).height( 30 ).height( undefined ).height(), 30, ".height(undefined) is chainable (#5571)" );
  343. assert.equal( jQuery( "#nothiddendiv" ).height( 30 ).innerHeight( undefined ).height(), 30, ".innerHeight(undefined) is chainable (#5571)" );
  344. assert.equal( jQuery( "#nothiddendiv" ).height( 30 ).outerHeight( undefined ).height(), 30, ".outerHeight(undefined) is chainable (#5571)" );
  345. assert.equal( jQuery( "#nothiddendiv" ).width( 30 ).width( undefined ).width(), 30, ".width(undefined) is chainable (#5571)" );
  346. } );
  347. QUnit.test( "setters with and without box-sizing:border-box", function( assert ) {
  348. assert.expect( 120 );
  349. var parent = jQuery( "#foo" ).css( { width: "200px", height: "200px", "font-size": "16px" } ),
  350. el_bb = jQuery( "<div style='margin:5px;padding:1px;border:2px solid black;box-sizing:border-box;'></div>" ).appendTo( parent ),
  351. el = jQuery( "<div style='margin:5px;padding:1px;border:2px solid black;'></div>" ).appendTo( parent ),
  352. el_bb_np = jQuery( "<div style='margin:5px; padding:0px; border:0px solid green;box-sizing:border-box;'></div>" ).appendTo( parent ),
  353. el_np = jQuery( "<div style='margin:5px; padding:0px; border:0px solid green;'></div>" ).appendTo( parent );
  354. jQuery.each( {
  355. "number": { set: 100, expected: 100 },
  356. "em": { set: "10em", expected: 160 },
  357. "percentage": { set: "50%", expected: 100 }
  358. }, function( units, values ) {
  359. assert.equal( el_bb.width( values.set ).width(), values.expected, "test border-box width(" + units + ") by roundtripping" );
  360. assert.equal( el_bb.innerWidth( values.set ).width(), values.expected - 2, "test border-box innerWidth(" + units + ") by roundtripping" );
  361. assert.equal( el_bb.outerWidth( values.set ).width(), values.expected - 6, "test border-box outerWidth(" + units + ") by roundtripping" );
  362. assert.equal( el_bb.outerWidth( values.set, false ).width(), values.expected - 6, "test border-box outerWidth(" + units + ", false) by roundtripping" );
  363. assert.equal( el_bb.outerWidth( values.set, true ).width(), values.expected - 16, "test border-box outerWidth(" + units + ", true) by roundtripping" );
  364. assert.equal( el_bb.height( values.set ).height(), values.expected, "test border-box height(" + units + ") by roundtripping" );
  365. assert.equal( el_bb.innerHeight( values.set ).height(), values.expected - 2, "test border-box innerHeight(" + units + ") by roundtripping" );
  366. assert.equal( el_bb.outerHeight( values.set ).height(), values.expected - 6, "test border-box outerHeight(" + units + ") by roundtripping" );
  367. assert.equal( el_bb.outerHeight( values.set, false ).height(), values.expected - 6, "test border-box outerHeight(" + units + ", false) by roundtripping" );
  368. assert.equal( el_bb.outerHeight( values.set, true ).height(), values.expected - 16, "test border-box outerHeight(" + units + ", true) by roundtripping" );
  369. assert.equal( el.width( values.set ).width(), values.expected, "test non-border-box width(" + units + ") by roundtripping" );
  370. assert.equal( el.innerWidth( values.set ).width(), values.expected - 2, "test non-border-box innerWidth(" + units + ") by roundtripping" );
  371. assert.equal( el.outerWidth( values.set ).width(), values.expected - 6, "test non-border-box outerWidth(" + units + ") by roundtripping" );
  372. assert.equal( el.outerWidth( values.set, false ).width(), values.expected - 6, "test non-border-box outerWidth(" + units + ", false) by roundtripping" );
  373. assert.equal( el.outerWidth( values.set, true ).width(), values.expected - 16, "test non-border-box outerWidth(" + units + ", true) by roundtripping" );
  374. assert.equal( el.height( values.set ).height(), values.expected, "test non-border-box height(" + units + ") by roundtripping" );
  375. assert.equal( el.innerHeight( values.set ).height(), values.expected - 2, "test non-border-box innerHeight(" + units + ") by roundtripping" );
  376. assert.equal( el.outerHeight( values.set ).height(), values.expected - 6, "test non-border-box outerHeight(" + units + ") by roundtripping" );
  377. assert.equal( el.outerHeight( values.set, false ).height(), values.expected - 6, "test non-border-box outerHeight(" + units + ", false) by roundtripping" );
  378. assert.equal( el.outerHeight( values.set, true ).height(), values.expected - 16, "test non-border-box outerHeight(" + units + ", true) by roundtripping" );
  379. assert.equal( el_bb_np.width( values.set ).width(), values.expected, "test border-box width and negative padding(" + units + ") by roundtripping" );
  380. assert.equal( el_bb_np.innerWidth( values.set ).width(), values.expected, "test border-box innerWidth and negative padding(" + units + ") by roundtripping" );
  381. assert.equal( el_bb_np.outerWidth( values.set ).width(), values.expected, "test border-box outerWidth and negative padding(" + units + ") by roundtripping" );
  382. assert.equal( el_bb_np.outerWidth( values.set, false ).width(), values.expected, "test border-box outerWidth and negative padding(" + units + ", false) by roundtripping" );
  383. assert.equal( el_bb_np.outerWidth( values.set, true ).width(), values.expected - 10, "test border-box outerWidth and negative padding(" + units + ", true) by roundtripping" );
  384. assert.equal( el_bb_np.height( values.set ).height(), values.expected, "test border-box height and negative padding(" + units + ") by roundtripping" );
  385. assert.equal( el_bb_np.innerHeight( values.set ).height(), values.expected, "test border-box innerHeight and negative padding(" + units + ") by roundtripping" );
  386. assert.equal( el_bb_np.outerHeight( values.set ).height(), values.expected, "test border-box outerHeight and negative padding(" + units + ") by roundtripping" );
  387. assert.equal( el_bb_np.outerHeight( values.set, false ).height(), values.expected, "test border-box outerHeight and negative padding(" + units + ", false) by roundtripping" );
  388. assert.equal( el_bb_np.outerHeight( values.set, true ).height(), values.expected - 10, "test border-box outerHeight and negative padding(" + units + ", true) by roundtripping" );
  389. assert.equal( el_np.width( values.set ).width(), values.expected, "test non-border-box width and negative padding(" + units + ") by roundtripping" );
  390. assert.equal( el_np.innerWidth( values.set ).width(), values.expected, "test non-border-box innerWidth and negative padding(" + units + ") by roundtripping" );
  391. assert.equal( el_np.outerWidth( values.set ).width(), values.expected, "test non-border-box outerWidth and negative padding(" + units + ") by roundtripping" );
  392. assert.equal( el_np.outerWidth( values.set, false ).width(), values.expected, "test non-border-box outerWidth and negative padding(" + units + ", false) by roundtripping" );
  393. assert.equal( el_np.outerWidth( values.set, true ).width(), values.expected - 10, "test non-border-box outerWidth and negative padding(" + units + ", true) by roundtripping" );
  394. assert.equal( el_np.height( values.set ).height(), values.expected, "test non-border-box height and negative padding(" + units + ") by roundtripping" );
  395. assert.equal( el_np.innerHeight( values.set ).height(), values.expected, "test non-border-box innerHeight and negative padding(" + units + ") by roundtripping" );
  396. assert.equal( el_np.outerHeight( values.set ).height(), values.expected, "test non-border-box outerHeight and negative padding(" + units + ") by roundtripping" );
  397. assert.equal( el_np.outerHeight( values.set, false ).height(), values.expected, "test non-border-box outerHeight and negative padding(" + units + ", false) by roundtripping" );
  398. assert.equal( el_np.outerHeight( values.set, true ).height(), values.expected - 10, "test non-border-box outerHeight and negative padding(" + units + ", true) by roundtripping" );
  399. } );
  400. } );
  401. testIframe(
  402. "window vs. large document",
  403. "dimensions/documentLarge.html",
  404. function( assert, jQuery, window, document ) {
  405. assert.expect( 2 );
  406. assert.ok( jQuery( document ).height() > jQuery( window ).height(), "document height is larger than window height" );
  407. assert.ok( jQuery( document ).width() > jQuery( window ).width(), "document width is larger than window width" );
  408. }
  409. );
  410. QUnit.test( "allow modification of coordinates argument (gh-1848)", function( assert ) {
  411. assert.expect( 1 );
  412. var offsetTop,
  413. element = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" );
  414. element.offset( function( index, coords ) {
  415. coords.top = 100;
  416. return coords;
  417. } );
  418. offsetTop = element.offset().top;
  419. assert.ok( Math.abs( offsetTop - 100 ) < 0.02,
  420. "coordinates are modified (got offset.top: " + offsetTop + ")" );
  421. } );
  422. QUnit.test( "outside view position (gh-2836)", function( assert ) {
  423. // This test ported from gh-2836 example
  424. assert.expect( 1 );
  425. var parent,
  426. html = [
  427. "<div id=div-gh-2836>",
  428. "<div></div>",
  429. "<div></div>",
  430. "<div></div>",
  431. "<div></div>",
  432. "<div></div>",
  433. "</div>"
  434. ].join( "" ),
  435. stop = assert.async();
  436. parent = jQuery( html );
  437. parent.appendTo( "#qunit-fixture" );
  438. parent.one( "scroll", function() {
  439. var pos = parent.find( "div" ).eq( 3 ).position();
  440. assert.strictEqual( pos.top, -100 );
  441. stop();
  442. } );
  443. parent.scrollTop( 400 );
  444. } );
  445. QUnit.test( "width/height on element with transform (gh-3193)", function( assert ) {
  446. assert.expect( 2 );
  447. var $elem = jQuery( "<div style='width: 200px; height: 200px; transform: scale(2);'></div>" )
  448. .appendTo( "#qunit-fixture" );
  449. assert.equal( $elem.width(), 200, "Width ignores transforms" );
  450. assert.equal( $elem.height(), 200, "Height ignores transforms" );
  451. } );
  452. QUnit.test( "width/height on an inline element with no explicitly-set dimensions (gh-3571)", function( assert ) {
  453. assert.expect( 8 );
  454. var $elem = jQuery( "<span style='border: 2px solid black;padding: 1px;margin: 3px;'>Hello, I'm some text.</span>" ).appendTo( "#qunit-fixture" );
  455. jQuery.each( [ "Width", "Height" ], function( i, method ) {
  456. var val = $elem[ method.toLowerCase() ]();
  457. assert.notEqual( val, 0, method + " should not be zero on inline element." );
  458. assert.equal( $elem[ "inner" + method ](), val + 2, "inner" + method + " should include padding" );
  459. assert.equal( $elem[ "outer" + method ](), val + 6, "outer" + method + " should include padding and border" );
  460. assert.equal( $elem[ "outer" + method ]( true ), val + 12, "outer" + method + "(true) should include padding, border, and margin" );
  461. } );
  462. } );
  463. QUnit.test( "width/height on an inline element with percentage dimensions (gh-3611)",
  464. function( assert ) {
  465. assert.expect( 4 );
  466. jQuery( "<div id='gh3611' style='width: 100px;'>" +
  467. "<span style='width: 100%; padding: 0 5px'>text</span>" +
  468. "</div>" ).appendTo( "#qunit-fixture" );
  469. var $elem = jQuery( "#gh3611 span" ),
  470. actualWidth = $elem[ 0 ].getBoundingClientRect().width,
  471. marginWidth = $elem.outerWidth( true ),
  472. borderWidth = $elem.outerWidth(),
  473. paddingWidth = $elem.innerWidth(),
  474. contentWidth = $elem.width();
  475. assert.equal( Math.round( borderWidth ), Math.round( actualWidth ),
  476. ".outerWidth(): " + borderWidth + " approximates " + actualWidth );
  477. assert.equal( marginWidth, borderWidth, ".outerWidth(true) matches .outerWidth()" );
  478. assert.equal( paddingWidth, borderWidth, ".innerWidth() matches .outerWidth()" );
  479. assert.equal( contentWidth, borderWidth - 10, ".width() excludes padding" );
  480. }
  481. );
  482. QUnit.test(
  483. "width/height on a table row with phantom borders (gh-3698)", function( assert ) {
  484. assert.expect( 4 );
  485. jQuery( "<table id='gh3698' style='border-collapse: separate; border-spacing: 0;'><tbody>" +
  486. "<tr style='margin: 0; border: 10px solid black; padding: 0'>" +
  487. "<td style='margin: 0; border: 0; padding: 0; height: 42px; width: 42px;'></td>" +
  488. "</tr>" +
  489. "</tbody></table>" ).appendTo( "#qunit-fixture" );
  490. var $elem = jQuery( "#gh3698 tr" );
  491. jQuery.each( [ "Width", "Height" ], function( i, method ) {
  492. assert.equal( $elem[ "outer" + method ](), 42,
  493. "outer" + method + " should match content dimensions" );
  494. assert.equal( $elem[ "outer" + method ]( true ), 42,
  495. "outer" + method + "(true) should match content dimensions" );
  496. } );
  497. } );
  498. QUnit.test( "interaction with scrollbars (gh-3589)", function( assert ) {
  499. assert.expect( 48 );
  500. var i,
  501. suffix = "",
  502. updater = function( adjustment ) {
  503. return function( i, old ) {
  504. return old + adjustment;
  505. };
  506. },
  507. parent = jQuery( "<div></div>" )
  508. .css( { position: "absolute", width: "1000px", height: "1000px" } )
  509. .appendTo( "#qunit-fixture" ),
  510. fraction = jQuery.support.boxSizingReliable() ?
  511. jQuery( "<div style='width:4.5px;'/>" ).appendTo( parent ).width() % 1 :
  512. 0,
  513. borderWidth = 1,
  514. padding = 2,
  515. size = 100 + fraction,
  516. plainBox = jQuery( "<div></div>" )
  517. .css( {
  518. "box-sizing": "content-box",
  519. position: "absolute",
  520. overflow: "scroll",
  521. width: size + "px",
  522. height: size + "px"
  523. } ),
  524. contentBox = plainBox
  525. .clone()
  526. .css( {
  527. border: borderWidth + "px solid blue",
  528. padding: padding + "px"
  529. } ),
  530. borderBox = contentBox
  531. .clone()
  532. .css( { "box-sizing": "border-box" } ),
  533. relativeBorderBox = borderBox
  534. .clone()
  535. .css( { position: "relative" } ),
  536. $boxes = jQuery(
  537. [ plainBox[ 0 ], contentBox[ 0 ], borderBox[ 0 ], relativeBorderBox[ 0 ] ]
  538. ).appendTo( parent ),
  539. // Support: IE 9 only
  540. // Computed width seems to report content width even with "box-sizing: border-box", and
  541. // "overflow: scroll" actually _shrinks_ the element (gh-3699).
  542. borderBoxLoss =
  543. borderBox.clone().css( { overflow: "auto" } ).appendTo( parent )[ 0 ].offsetWidth -
  544. borderBox[ 0 ].offsetWidth;
  545. if ( borderBoxLoss > 0 ) {
  546. borderBox[ 0 ].style.width = ( size + borderBoxLoss ) + "px";
  547. borderBox[ 0 ].style.height = ( size + borderBoxLoss ) + "px";
  548. }
  549. for ( i = 0; i < 3; i++ ) {
  550. if ( i === 1 ) {
  551. suffix = " after increasing inner* by " + i;
  552. size += i;
  553. $boxes.innerWidth( updater( i ) ).innerHeight( updater( i ) );
  554. } else if ( i === 2 ) {
  555. suffix = " after increasing outer* by " + i;
  556. size += i;
  557. $boxes.outerWidth( updater( i ) ).outerHeight( updater( i ) );
  558. }
  559. assert.equal( plainBox.innerWidth(), size,
  560. "plain content-box innerWidth includes scroll gutter" + suffix );
  561. assert.equal( plainBox.innerHeight(), size,
  562. "plain content-box innerHeight includes scroll gutter" + suffix );
  563. assert.equal( plainBox.outerWidth(), size,
  564. "plain content-box outerWidth includes scroll gutter" + suffix );
  565. assert.equal( plainBox.outerHeight(), size,
  566. "plain content-box outerHeight includes scroll gutter" + suffix );
  567. assert.equal( contentBox.innerWidth(), size + 2 * padding,
  568. "content-box innerWidth includes scroll gutter" + suffix );
  569. assert.equal( contentBox.innerHeight(), size + 2 * padding,
  570. "content-box innerHeight includes scroll gutter" + suffix );
  571. assert.equal( contentBox.outerWidth(), size + 2 * padding + 2 * borderWidth,
  572. "content-box outerWidth includes scroll gutter" + suffix );
  573. assert.equal( contentBox.outerHeight(), size + 2 * padding + 2 * borderWidth,
  574. "content-box outerHeight includes scroll gutter" + suffix );
  575. assert.equal( borderBox.innerWidth(), size - 2 * borderWidth,
  576. "border-box innerWidth includes scroll gutter" + suffix );
  577. assert.equal( borderBox.innerHeight(), size - 2 * borderWidth,
  578. "border-box innerHeight includes scroll gutter" + suffix );
  579. assert.equal( borderBox.outerWidth(), size,
  580. "border-box outerWidth includes scroll gutter" + suffix );
  581. assert.equal( borderBox.outerHeight(), size,
  582. "border-box outerHeight includes scroll gutter" + suffix );
  583. assert.equal( relativeBorderBox.innerWidth(), size - 2 * borderWidth,
  584. "relative border-box innerWidth includes scroll gutter" + suffix );
  585. assert.equal( relativeBorderBox.innerHeight(), size - 2 * borderWidth,
  586. "relative border-box innerHeight includes scroll gutter" + suffix );
  587. assert.equal( relativeBorderBox.outerWidth(), size,
  588. "relative border-box outerWidth includes scroll gutter" + suffix );
  589. assert.equal( relativeBorderBox.outerHeight(), size,
  590. "relative border-box outerHeight includes scroll gutter" + suffix );
  591. }
  592. } );
  593. QUnit.test( "outerWidth/Height for table cells and textarea with border-box in IE 11 (gh-4102)", function( assert ) {
  594. assert.expect( 5 );
  595. var $table = jQuery( "<table class='border-box' style='border-collapse: separate'></table>" ).appendTo( "#qunit-fixture" ),
  596. $thead = jQuery( "<thead></thead>" ).appendTo( $table ),
  597. $firstTh = jQuery( "<th style='width: 200px;padding: 5px'></th>" ),
  598. $secondTh = jQuery( "<th style='width: 190px;padding: 5px'></th>" ),
  599. $thirdTh = jQuery( "<th style='width: 180px;padding: 5px'></th>" ),
  600. // Support: Firefox 63, Edge 16-17, Android 8, iOS 7-11
  601. // These browsers completely ignore the border-box and height settings
  602. // The computed height is instead just line-height + border
  603. // Either way, what we're doing in css.js is correct
  604. $td = jQuery( "<td style='height: 20px;padding: 5px;border: 1px solid;line-height:18px'>text</td>" ),
  605. $tbody = jQuery( "<tbody></tbody>" ).appendTo( $table ),
  606. $textarea = jQuery( "<textarea style='height: 0;padding: 2px;border: 1px solid;box-sizing: border-box'></textarea>" ).appendTo( "#qunit-fixture" );
  607. jQuery( "<tr></tr>" ).appendTo( $thead ).append( $firstTh );
  608. jQuery( "<tr></tr>" ).appendTo( $thead ).append( $secondTh );
  609. jQuery( "<tr></tr>" ).appendTo( $thead ).append( $thirdTh );
  610. jQuery( "<tr><td></td></tr>" ).appendTo( $tbody ).append( $td );
  611. assert.strictEqual( $firstTh.outerWidth(), 200, "First th has outerWidth 200." );
  612. assert.strictEqual( $secondTh.outerWidth(), 200, "Second th has outerWidth 200." );
  613. assert.strictEqual( $thirdTh.outerWidth(), 200, "Third th has outerWidth 200." );
  614. // Support: Android 4.0-4.3 only
  615. // Android Browser disregards td's box-sizing, treating it like it was content-box.
  616. // Unlike in IE, offsetHeight shares the same issue so there's no easy way to workaround
  617. // the issue without incurring high size penalty. Let's at least check we get the size
  618. // as the browser sees it.
  619. if ( /android 4\.[0-3]/i.test( navigator.userAgent ) ) {
  620. assert.ok( [ 30, 32 ].indexOf( $td.outerHeight() ) > -1,
  621. "outerHeight of td with border-box should include padding." );
  622. } else {
  623. assert.strictEqual( $td.outerHeight(), 30, "outerHeight of td with border-box should include padding." );
  624. }
  625. assert.strictEqual( $textarea.outerHeight(), 6, "outerHeight of textarea with border-box should include padding and border." );
  626. } );
  627. } )();