css.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. if ( jQuery.css ) {
  2. module("css", { teardown: moduleTeardown });
  3. test("css(String|Hash)", function() {
  4. expect( 41 );
  5. equal( jQuery("#qunit-fixture").css("display"), "block", "Check for css property \"display\"" );
  6. var $child = jQuery("#nothiddendivchild").css({ "width": "20%", "height": "20%" });
  7. notEqual( $child.css("width"), "20px", "Retrieving a width percentage on the child of a hidden div returns percentage" );
  8. notEqual( $child.css("height"), "20px", "Retrieving a height percentage on the child of a hidden div returns percentage" );
  9. var div = jQuery( "<div>" );
  10. // These should be "auto" (or some better value)
  11. // temporarily provide "0px" for backwards compat
  12. equal( div.css("width"), "0px", "Width on disconnected node." );
  13. equal( div.css("height"), "0px", "Height on disconnected node." );
  14. div.css({ "width": 4, "height": 4 });
  15. equal( div.css("width"), "4px", "Width on disconnected node." );
  16. equal( div.css("height"), "4px", "Height on disconnected node." );
  17. var div2 = jQuery( "<div style='display:none;'><input type='text' style='height:20px;'/><textarea style='height:20px;'/><div style='height:20px;'></div></div>").appendTo("body");
  18. equal( div2.find("input").css("height"), "20px", "Height on hidden input." );
  19. equal( div2.find("textarea").css("height"), "20px", "Height on hidden textarea." );
  20. equal( div2.find("div").css("height"), "20px", "Height on hidden textarea." );
  21. div2.remove();
  22. // handle negative numbers by setting to zero #11604
  23. jQuery("#nothiddendiv").css( {"width": 1, "height": 1} );
  24. var width = parseFloat(jQuery("#nothiddendiv").css("width")), height = parseFloat(jQuery("#nothiddendiv").css("height"));
  25. jQuery("#nothiddendiv").css({ "overflow":"hidden", "width": -1, "height": -1 });
  26. equal( parseFloat(jQuery("#nothiddendiv").css("width")), 0, "Test negative width set to 0");
  27. equal( parseFloat(jQuery("#nothiddendiv").css("height")), 0, "Test negative height set to 0");
  28. equal( jQuery("<div style='display: none;'>").css("display"), "none", "Styles on disconnected nodes");
  29. jQuery("#floatTest").css({"float": "right"});
  30. equal( jQuery("#floatTest").css("float"), "right", "Modified CSS float using \"float\": Assert float is right");
  31. jQuery("#floatTest").css({"font-size": "30px"});
  32. equal( jQuery("#floatTest").css("font-size"), "30px", "Modified CSS font-size: Assert font-size is 30px");
  33. jQuery.each("0,0.25,0.5,0.75,1".split(","), function(i, n) {
  34. jQuery("#foo").css({"opacity": n});
  35. equal( jQuery("#foo").css("opacity"), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
  36. jQuery("#foo").css({"opacity": parseFloat(n)});
  37. equal( jQuery("#foo").css("opacity"), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
  38. });
  39. jQuery("#foo").css({"opacity": ""});
  40. equal( jQuery("#foo").css("opacity"), "1", "Assert opacity is 1 when set to an empty String" );
  41. equal( jQuery("#empty").css("opacity"), "0", "Assert opacity is accessible via filter property set in stylesheet in IE" );
  42. jQuery("#empty").css({ "opacity": "1" });
  43. equal( jQuery("#empty").css("opacity"), "1", "Assert opacity is taken from style attribute when set vs stylesheet in IE with filters" );
  44. jQuery.support.opacity ?
  45. ok(true, "Requires the same number of tests"):
  46. ok( ~jQuery("#empty")[0].currentStyle.filter.indexOf("gradient"), "Assert setting opacity doesn't overwrite other filters of the stylesheet in IE" );
  47. div = jQuery("#nothiddendiv");
  48. var child = jQuery("#nothiddendivchild");
  49. equal( parseInt(div.css("fontSize"), 10), 16, "Verify fontSize px set." );
  50. equal( parseInt(div.css("font-size"), 10), 16, "Verify fontSize px set." );
  51. equal( parseInt(child.css("fontSize"), 10), 16, "Verify fontSize px set." );
  52. equal( parseInt(child.css("font-size"), 10), 16, "Verify fontSize px set." );
  53. child.css("height", "100%");
  54. equal( child[0].style.height, "100%", "Make sure the height is being set correctly." );
  55. child.attr("class", "em");
  56. equal( parseInt(child.css("fontSize"), 10), 32, "Verify fontSize em set." );
  57. // Have to verify this as the result depends upon the browser's CSS
  58. // support for font-size percentages
  59. child.attr("class", "prct");
  60. var prctval = parseInt(child.css("fontSize"), 10), checkval = 0;
  61. if ( prctval === 16 || prctval === 24 ) {
  62. checkval = prctval;
  63. }
  64. equal( prctval, checkval, "Verify fontSize % set." );
  65. equal( typeof child.css("width"), "string", "Make sure that a string width is returned from css('width')." );
  66. var old = child[0].style.height;
  67. // Test NaN
  68. child.css("height", parseFloat("zoo"));
  69. equal( child[0].style.height, old, "Make sure height isn't changed on NaN." );
  70. // Test null
  71. child.css("height", null);
  72. equal( child[0].style.height, old, "Make sure height isn't changed on null." );
  73. old = child[0].style.fontSize;
  74. // Test NaN
  75. child.css("font-size", parseFloat("zoo"));
  76. equal( child[0].style.fontSize, old, "Make sure font-size isn't changed on NaN." );
  77. // Test null
  78. child.css("font-size", null);
  79. equal( child[0].style.fontSize, old, "Make sure font-size isn't changed on null." );
  80. });
  81. test("css() explicit and relative values", function() {
  82. expect(29);
  83. var $elem = jQuery("#nothiddendiv");
  84. $elem.css({ "width": 1, "height": 1, "paddingLeft": "1px", "opacity": 1 });
  85. equal( $elem.css("width"), "1px", "Initial css set or width/height works (hash)" );
  86. equal( $elem.css("paddingLeft"), "1px", "Initial css set of paddingLeft works (hash)" );
  87. equal( $elem.css("opacity"), "1", "Initial css set of opacity works (hash)" );
  88. $elem.css({ width: "+=9" });
  89. equal( $elem.css("width"), "10px", "'+=9' on width (hash)" );
  90. $elem.css({ "width": "-=9" });
  91. equal( $elem.css("width"), "1px", "'-=9' on width (hash)" );
  92. $elem.css({ "width": "+=9px" });
  93. equal( $elem.css("width"), "10px", "'+=9px' on width (hash)" );
  94. $elem.css({ "width": "-=9px" });
  95. equal( $elem.css("width"), "1px", "'-=9px' on width (hash)" );
  96. $elem.css( "width", "+=9" );
  97. equal( $elem.css("width"), "10px", "'+=9' on width (params)" );
  98. $elem.css( "width", "-=9" ) ;
  99. equal( $elem.css("width"), "1px", "'-=9' on width (params)" );
  100. $elem.css( "width", "+=9px" );
  101. equal( $elem.css("width"), "10px", "'+=9px' on width (params)" );
  102. $elem.css( "width", "-=9px" );
  103. equal( $elem.css("width"), "1px", "'-=9px' on width (params)" );
  104. $elem.css( "width", "-=-9px" );
  105. equal( $elem.css("width"), "10px", "'-=-9px' on width (params)" );
  106. $elem.css( "width", "+=-9px" );
  107. equal( $elem.css("width"), "1px", "'+=-9px' on width (params)" );
  108. $elem.css({ "paddingLeft": "+=4" });
  109. equal( $elem.css("paddingLeft"), "5px", "'+=4' on paddingLeft (hash)" );
  110. $elem.css({ "paddingLeft": "-=4" });
  111. equal( $elem.css("paddingLeft"), "1px", "'-=4' on paddingLeft (hash)" );
  112. $elem.css({ "paddingLeft": "+=4px" });
  113. equal( $elem.css("paddingLeft"), "5px", "'+=4px' on paddingLeft (hash)" );
  114. $elem.css({ "paddingLeft": "-=4px" });
  115. equal( $elem.css("paddingLeft"), "1px", "'-=4px' on paddingLeft (hash)" );
  116. $elem.css({ "padding-left": "+=4" });
  117. equal( $elem.css("paddingLeft"), "5px", "'+=4' on padding-left (hash)" );
  118. $elem.css({ "padding-left": "-=4" });
  119. equal( $elem.css("paddingLeft"), "1px", "'-=4' on padding-left (hash)" );
  120. $elem.css({ "padding-left": "+=4px" });
  121. equal( $elem.css("paddingLeft"), "5px", "'+=4px' on padding-left (hash)" );
  122. $elem.css({ "padding-left": "-=4px" });
  123. equal( $elem.css("paddingLeft"), "1px", "'-=4px' on padding-left (hash)" );
  124. $elem.css( "paddingLeft", "+=4" );
  125. equal( $elem.css("paddingLeft"), "5px", "'+=4' on paddingLeft (params)" );
  126. $elem.css( "paddingLeft", "-=4" );
  127. equal( $elem.css("paddingLeft"), "1px", "'-=4' on paddingLeft (params)" );
  128. $elem.css( "padding-left", "+=4px" );
  129. equal( $elem.css("paddingLeft"), "5px", "'+=4px' on padding-left (params)" );
  130. $elem.css( "padding-left", "-=4px" );
  131. equal( $elem.css("paddingLeft"), "1px", "'-=4px' on padding-left (params)" );
  132. $elem.css({ "opacity": "-=0.5" });
  133. equal( $elem.css("opacity"), "0.5", "'-=0.5' on opacity (hash)" );
  134. $elem.css({ "opacity": "+=0.5" });
  135. equal( $elem.css("opacity"), "1", "'+=0.5' on opacity (hash)" );
  136. $elem.css( "opacity", "-=0.5" );
  137. equal( $elem.css("opacity"), "0.5", "'-=0.5' on opacity (params)" );
  138. $elem.css( "opacity", "+=0.5" );
  139. equal( $elem.css("opacity"), "1", "'+=0.5' on opacity (params)" );
  140. });
  141. test("css(String, Object)", function() {
  142. expect( 19 );
  143. jQuery("#nothiddendiv").css("top", "-1em");
  144. ok( jQuery("#nothiddendiv").css("top"), -16, "Check negative number in EMs." );
  145. jQuery("#floatTest").css("float", "left");
  146. equal( jQuery("#floatTest").css("float"), "left", "Modified CSS float using \"float\": Assert float is left");
  147. jQuery("#floatTest").css("font-size", "20px");
  148. equal( jQuery("#floatTest").css("font-size"), "20px", "Modified CSS font-size: Assert font-size is 20px");
  149. jQuery.each("0,0.25,0.5,0.75,1".split(","), function(i, n) {
  150. jQuery("#foo").css("opacity", n);
  151. equal( jQuery("#foo").css("opacity"), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
  152. jQuery("#foo").css("opacity", parseFloat(n));
  153. equal( jQuery("#foo").css("opacity"), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
  154. });
  155. jQuery("#foo").css("opacity", "");
  156. equal( jQuery("#foo").css("opacity"), "1", "Assert opacity is 1 when set to an empty String" );
  157. // using contents will get comments regular, text, and comment nodes
  158. var j = jQuery("#nonnodes").contents();
  159. j.css("overflow", "visible");
  160. equal( j.css("overflow"), "visible", "Check node,textnode,comment css works" );
  161. // opera sometimes doesn't update 'display' correctly, see #2037
  162. jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML;
  163. equal( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
  164. var div = jQuery("#nothiddendiv"),
  165. display = div.css("display"),
  166. ret = div.css("display", undefined);
  167. equal( ret, div, "Make sure setting undefined returns the original set." );
  168. equal( div.css("display"), display, "Make sure that the display wasn't changed." );
  169. // Test for Bug #5509
  170. var success = true;
  171. try {
  172. jQuery("#foo").css("backgroundColor", "rgba(0, 0, 0, 0.1)");
  173. }
  174. catch (e) {
  175. success = false;
  176. }
  177. ok( success, "Setting RGBA values does not throw Error" );
  178. });
  179. test( "css(Array)", function() {
  180. expect( 2 );
  181. var expectedMany = {
  182. "overflow": "visible",
  183. "width": "16px"
  184. },
  185. expectedSingle = {
  186. "width": "16px"
  187. },
  188. elem = jQuery("<div></div>").appendTo("#qunit-fixture");
  189. deepEqual( elem.css( expectedMany ).css([ "overflow", "width" ]), expectedMany, "Getting multiple element array" );
  190. deepEqual( elem.css( expectedSingle ).css([ "width" ]), expectedSingle, "Getting single element array" );
  191. });
  192. if ( !jQuery.support.opacity ) {
  193. test("css(String, Object) for MSIE", function() {
  194. expect( 5 );
  195. // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
  196. jQuery("#foo").css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
  197. equal( jQuery("#foo").css("opacity"), "1", "Assert opacity is 1 when a different filter is set in IE, #1438" );
  198. var filterVal = "progid:DXImageTransform.Microsoft.Alpha(opacity=30) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
  199. var filterVal2 = "progid:DXImageTransform.Microsoft.alpha(opacity=100) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
  200. var filterVal3 = "progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
  201. jQuery("#foo").css("filter", filterVal);
  202. equal( jQuery("#foo").css("filter"), filterVal, "css('filter', val) works" );
  203. jQuery("#foo").css("opacity", 1);
  204. equal( jQuery("#foo").css("filter"), filterVal2, "Setting opacity in IE doesn't duplicate opacity filter" );
  205. equal( jQuery("#foo").css("opacity"), 1, "Setting opacity in IE with other filters works" );
  206. jQuery("#foo").css("filter", filterVal3).css("opacity", 1);
  207. ok( jQuery("#foo").css("filter").indexOf(filterVal3) !== -1, "Setting opacity in IE doesn't clobber other filters" );
  208. });
  209. test( "Setting opacity to 1 properly removes filter: style (#6652)", function() {
  210. var rfilter = /filter:[^;]*/i,
  211. test = jQuery( "#t6652" ).css( "opacity", 1 ),
  212. test2 = test.find( "div" ).css( "opacity", 1 );
  213. function hasFilter( elem ) {
  214. var match = rfilter.exec( elem[0].style.cssText );
  215. if ( match ) {
  216. return true;
  217. }
  218. return false;
  219. }
  220. expect( 2 );
  221. ok( !hasFilter( test ), "Removed filter attribute on element without filter in stylesheet" );
  222. ok( hasFilter( test2 ), "Filter attribute remains on element that had filter in stylesheet" );
  223. });
  224. }
  225. test("css(String, Function)", function() {
  226. expect(3);
  227. var sizes = ["10px", "20px", "30px"];
  228. jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" +
  229. "<div class='cssFunction'></div>" +
  230. "<div class='cssFunction'></div></div>")
  231. .appendTo("body");
  232. var index = 0;
  233. jQuery("#cssFunctionTest div").css("font-size", function() {
  234. var size = sizes[index];
  235. index++;
  236. return size;
  237. });
  238. index = 0;
  239. jQuery("#cssFunctionTest div").each(function() {
  240. var computedSize = jQuery(this).css("font-size");
  241. var expectedSize = sizes[index];
  242. equal( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
  243. index++;
  244. });
  245. jQuery("#cssFunctionTest").remove();
  246. });
  247. test("css(String, Function) with incoming value", function() {
  248. expect(3);
  249. var sizes = ["10px", "20px", "30px"];
  250. jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" +
  251. "<div class='cssFunction'></div>" +
  252. "<div class='cssFunction'></div></div>")
  253. .appendTo("body");
  254. var index = 0;
  255. jQuery("#cssFunctionTest div").css("font-size", function() {
  256. var size = sizes[index];
  257. index++;
  258. return size;
  259. });
  260. index = 0;
  261. jQuery("#cssFunctionTest div").css("font-size", function(i, computedSize) {
  262. var expectedSize = sizes[index];
  263. equal( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
  264. index++;
  265. return computedSize;
  266. });
  267. jQuery("#cssFunctionTest").remove();
  268. });
  269. test("css(Object) where values are Functions", function() {
  270. expect(3);
  271. var sizes = ["10px", "20px", "30px"];
  272. jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" +
  273. "<div class='cssFunction'></div>" +
  274. "<div class='cssFunction'></div></div>")
  275. .appendTo("body");
  276. var index = 0;
  277. jQuery("#cssFunctionTest div").css({"fontSize": function() {
  278. var size = sizes[index];
  279. index++;
  280. return size;
  281. }});
  282. index = 0;
  283. jQuery("#cssFunctionTest div").each(function() {
  284. var computedSize = jQuery(this).css("font-size");
  285. var expectedSize = sizes[index];
  286. equal( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
  287. index++;
  288. });
  289. jQuery("#cssFunctionTest").remove();
  290. });
  291. test("css(Object) where values are Functions with incoming values", function() {
  292. expect(3);
  293. var sizes = ["10px", "20px", "30px"];
  294. jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" +
  295. "<div class='cssFunction'></div>" +
  296. "<div class='cssFunction'></div></div>")
  297. .appendTo("body");
  298. var index = 0;
  299. jQuery("#cssFunctionTest div").css({"fontSize": function() {
  300. var size = sizes[index];
  301. index++;
  302. return size;
  303. }});
  304. index = 0;
  305. jQuery("#cssFunctionTest div").css({"font-size": function(i, computedSize) {
  306. var expectedSize = sizes[index];
  307. equal( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
  308. index++;
  309. return computedSize;
  310. }});
  311. jQuery("#cssFunctionTest").remove();
  312. });
  313. test("show(); hide()", function() {
  314. expect(22);
  315. var hiddendiv = jQuery("div.hidden");
  316. hiddendiv.hide();
  317. equal( hiddendiv.css("display"), "none", "Non-detached div hidden" );
  318. hiddendiv.show();
  319. equal( hiddendiv.css("display"), "block", "Pre-hidden div shown" );
  320. var div = jQuery("<div>").hide();
  321. equal( div.css("display"), "none", "Detached div hidden" );
  322. div.appendTo("#qunit-fixture").show();
  323. equal( div.css("display"), "block", "Pre-hidden div shown" );
  324. QUnit.reset();
  325. hiddendiv = jQuery("div.hidden");
  326. equal(jQuery.css( hiddendiv[0], "display"), "none", "hiddendiv is display: none");
  327. hiddendiv.css("display", "block");
  328. equal(jQuery.css( hiddendiv[0], "display"), "block", "hiddendiv is display: block");
  329. hiddendiv.show();
  330. equal(jQuery.css( hiddendiv[0], "display"), "block", "hiddendiv is display: block");
  331. hiddendiv.css("display","");
  332. var pass = true;
  333. div = jQuery("#qunit-fixture div");
  334. div.show().each(function(){
  335. if ( this.style.display == "none" ) {
  336. pass = false;
  337. }
  338. });
  339. ok( pass, "Show" );
  340. // #show-tests * is set display: none in CSS
  341. jQuery("#qunit-fixture").append("<div id='show-tests'><div><p><a href='#'></a></p><code></code><pre></pre><span></span></div><table><thead><tr><th></th></tr></thead><tbody><tr><td></td></tr></tbody></table><ul><li></li></ul></div><table id='test-table'></table>");
  342. var old = jQuery("#test-table").show().css("display") !== "table";
  343. jQuery("#test-table").remove();
  344. var test = {
  345. "div" : "block",
  346. "p" : "block",
  347. "a" : "inline",
  348. "code" : "inline",
  349. "pre" : "block",
  350. "span" : "inline",
  351. "table" : old ? "block" : "table",
  352. "thead" : old ? "block" : "table-header-group",
  353. "tbody" : old ? "block" : "table-row-group",
  354. "tr" : old ? "block" : "table-row",
  355. "th" : old ? "block" : "table-cell",
  356. "td" : old ? "block" : "table-cell",
  357. "ul" : "block",
  358. "li" : old ? "block" : "list-item"
  359. };
  360. jQuery.each(test, function(selector, expected) {
  361. var elem = jQuery(selector, "#show-tests").show();
  362. equal( elem.css("display"), expected, "Show using correct display type for " + selector );
  363. });
  364. // Make sure that showing or hiding a text node doesn't cause an error
  365. jQuery("<div>test</div> text <span>test</span>").show().remove();
  366. jQuery("<div>test</div> text <span>test</span>").hide().remove();
  367. });
  368. test("show() resolves correct default display #8099", function() {
  369. expect(7);
  370. var tt8099 = jQuery("<tt/>").appendTo("body"),
  371. dfn8099 = jQuery("<dfn/>", { "html": "foo"}).appendTo("body");
  372. equal( tt8099.css("display"), "none", "default display override for all tt" );
  373. equal( tt8099.show().css("display"), "inline", "Correctly resolves display:inline" );
  374. equal( jQuery("#foo").hide().show().css("display"), "block", "Correctly resolves display:block after hide/show" );
  375. equal( tt8099.hide().css("display"), "none", "default display override for all tt" );
  376. equal( tt8099.show().css("display"), "inline", "Correctly resolves display:inline" );
  377. equal( dfn8099.css("display"), "none", "default display override for all dfn" );
  378. equal( dfn8099.show().css("display"), "inline", "Correctly resolves display:inline" );
  379. tt8099.remove();
  380. dfn8099.remove();
  381. });
  382. test( "show() resolves correct default display for detached nodes", function(){
  383. expect( 13 );
  384. var div, span, tr, trDisplay;
  385. div = jQuery("<div class='hidden'>");
  386. div.show().appendTo("#qunit-fixture");
  387. equal( div.css("display"), "block", "Make sure a detached, pre-hidden( through stylesheets ) div is visible." );
  388. div = jQuery("<div style='display: none'>");
  389. div.show().appendTo("#qunit-fixture");
  390. equal( div.css("display"), "block", "Make sure a detached, pre-hidden( through inline style ) div is visible." );
  391. span = jQuery("<span class='hidden'/>");
  392. span.show().appendTo("#qunit-fixture");
  393. equal( span.css("display"), "inline", "Make sure a detached, pre-hidden( through stylesheets ) span has default display." );
  394. span = jQuery("<span style='display: inline'/>");
  395. span.show().appendTo("#qunit-fixture");
  396. equal( span.css("display"), "inline", "Make sure a detached, pre-hidden( through inline style ) span has default display." );
  397. div = jQuery("<div><div class='hidden'></div></div>").children("div");
  398. div.show().appendTo("#qunit-fixture");
  399. equal( div.css("display"), "block", "Make sure a detached, pre-hidden( through stylesheets ) div inside another visible div is visible." );
  400. div = jQuery("<div><div style='display: none'></div></div>").children("div");
  401. div.show().appendTo("#qunit-fixture");
  402. equal( div.css("display"), "block", "Make sure a detached, pre-hidden( through inline style ) div inside another visible div is visible." );
  403. div = jQuery("div.hidden");
  404. div.detach().show();
  405. equal( div.css("display"), "block", "Make sure a detached( through detach() ), pre-hidden div is visible." );
  406. div.remove();
  407. span = jQuery("<span>");
  408. span.appendTo("#qunit-fixture").detach().show().appendTo("#qunit-fixture" );
  409. equal( span.css("display"), "inline", "Make sure a detached( through detach() ), pre-hidden span has default display." );
  410. span.remove();
  411. div = jQuery("<div>");
  412. div.show().appendTo("#qunit-fixture");
  413. ok( !!div.get( 0 ).style.display, "Make sure not hidden div has a inline style." );
  414. div.remove();
  415. div = jQuery( document.createElement("div") );
  416. div.show().appendTo("#qunit-fixture");
  417. equal( div.css("display"), "block", "Make sure a pre-created element has default display." );
  418. div.remove();
  419. div = jQuery("<div style='display: inline'/>");
  420. div.show().appendTo("#qunit-fixture");
  421. equal( div.css("display"), "inline", "Make sure that element has same display when it was created." );
  422. div.remove();
  423. tr = jQuery("<tr/>");
  424. jQuery("#table").append( tr );
  425. trDisplay = tr.css( "display" );
  426. tr.detach().hide().show();
  427. equal( tr[ 0 ].style.display, trDisplay, "For detached tr elements, display should always be like for attached trs" );
  428. tr.remove();
  429. span = span = jQuery("<span/>").hide().show();
  430. equal( span[ 0 ].style.display, "inline", "For detached span elements, display should always be inline" );
  431. span.remove();
  432. });
  433. test("show() resolves correct default display #10227", function() {
  434. expect(2);
  435. var body = jQuery("body");
  436. body.append(
  437. "<p id='ddisplay'>a<style>body{display:none}</style></p>"
  438. );
  439. equal( body.css("display"), "none", "Initial display: none" );
  440. body.show();
  441. equal( body.css("display"), "block", "Correct display: block" );
  442. jQuery("#ddisplay").remove();
  443. QUnit.expectJqData( body[0], "olddisplay" );
  444. });
  445. test("show() resolves correct default display when iframe display:none #12904", function() {
  446. expect(2);
  447. var ddisplay = jQuery(
  448. "<p id='ddisplay'>a<style>p{display:none}iframe{display:none !important}</style></p>"
  449. ).appendTo("body");
  450. equal( ddisplay.css("display"), "none", "Initial display: none" );
  451. ddisplay.show();
  452. equal( ddisplay.css("display"), "block", "Correct display: block" );
  453. ddisplay.remove();
  454. });
  455. test("toggle()", function() {
  456. expect(9);
  457. var div,
  458. x = jQuery("#foo");
  459. ok( x.is(":visible"), "is visible" );
  460. x.toggle();
  461. ok( x.is(":hidden"), "is hidden" );
  462. x.toggle();
  463. ok( x.is(":visible"), "is visible again" );
  464. x.toggle(true);
  465. ok( x.is(":visible"), "is visible" );
  466. x.toggle(false);
  467. ok( x.is(":hidden"), "is hidden" );
  468. x.toggle(true);
  469. ok( x.is(":visible"), "is visible again" );
  470. div = jQuery("<div style='display:none'><div></div></div>").appendTo("#qunit-fixture");
  471. x = div.find("div");
  472. strictEqual( x.toggle().css( "display" ), "none", "is hidden" );
  473. strictEqual( x.toggle().css( "display" ), "block", "is visible" );
  474. // Ensure hide() is called when toggled (#12148)
  475. var oldHide = jQuery.fn.hide;
  476. jQuery.fn.hide = function() {
  477. ok( true, name + " method called on toggle" );
  478. return oldHide.apply( this, arguments );
  479. };
  480. x.toggle( name === "show" );
  481. jQuery.fn.hide = oldHide;
  482. });
  483. test("hide hidden elements (bug #7141)", function() {
  484. expect(3);
  485. QUnit.reset();
  486. var div = jQuery("<div style='display:none'></div>").appendTo("#qunit-fixture");
  487. equal( div.css("display"), "none", "Element is hidden by default" );
  488. div.hide();
  489. ok( !jQuery._data(div, "olddisplay"), "olddisplay is undefined after hiding an already-hidden element" );
  490. div.show();
  491. equal( div.css("display"), "block", "Show a double-hidden element" );
  492. div.remove();
  493. });
  494. test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
  495. expect(4);
  496. var $checkedtest = jQuery("#checkedtest");
  497. // IE6 was clearing "checked" in jQuery.css(elem, "height");
  498. jQuery.css($checkedtest[0], "height");
  499. ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
  500. ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
  501. ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
  502. ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
  503. });
  504. test("internal ref to elem.runtimeStyle (bug #7608)", function () {
  505. expect(1);
  506. var result = true;
  507. try {
  508. jQuery("#foo").css( { "width": "0%" } ).css("width");
  509. } catch (e) {
  510. result = false;
  511. }
  512. ok( result, "elem.runtimeStyle does not throw exception" );
  513. });
  514. test("marginRight computed style (bug #3333)", function() {
  515. expect(1);
  516. var $div = jQuery("#foo");
  517. $div.css({
  518. "width": "1px",
  519. "marginRight": 0
  520. });
  521. equal($div.css("marginRight"), "0px", "marginRight correctly calculated with a width and display block");
  522. });
  523. test("box model properties incorrectly returning % instead of px, see #10639 and #12088", function() {
  524. expect( 2 );
  525. var container = jQuery("<div/>").width( 400 ).appendTo("#qunit-fixture"),
  526. el = jQuery("<div/>").css({ "width": "50%", "marginRight": "50%" }).appendTo( container ),
  527. el2 = jQuery("<div/>").css({ "width": "50%", "minWidth": "300px", "marginLeft": "25%" }).appendTo( container );
  528. equal( el.css("marginRight"), "200px", "css('marginRight') returning % instead of px, see #10639" );
  529. equal( el2.css("marginLeft"), "100px", "css('marginLeft') returning incorrect pixel value, see #12088" );
  530. });
  531. test("jQuery.cssProps behavior, (bug #8402)", function() {
  532. expect( 2 );
  533. var div = jQuery( "<div>" ).appendTo(document.body).css({
  534. "position": "absolute",
  535. "top": 0,
  536. "left": 10
  537. });
  538. jQuery.cssProps.top = "left";
  539. equal( div.css("top"), "10px", "the fixed property is used when accessing the computed style");
  540. div.css("top", "100px");
  541. equal( div[0].style.left, "100px", "the fixed property is used when setting the style");
  542. // cleanup jQuery.cssProps
  543. jQuery.cssProps.top = undefined;
  544. });
  545. test("widows & orphans #8936", function () {
  546. var $p = jQuery("<p>").appendTo("#qunit-fixture");
  547. if ( "widows" in $p[0].style ) {
  548. expect(4);
  549. $p.css({
  550. "widows": 0,
  551. "orphans": 0
  552. });
  553. equal( $p.css("widows") || jQuery.style( $p[0], "widows" ), 0, "widows correctly start with value 0");
  554. equal( $p.css("orphans") || jQuery.style( $p[0], "orphans" ), 0, "orphans correctly start with value 0");
  555. $p.css({
  556. "widows": 3,
  557. "orphans": 3
  558. });
  559. equal( $p.css("widows") || jQuery.style( $p[0], "widows" ), 3, "widows correctly set to 3");
  560. equal( $p.css("orphans") || jQuery.style( $p[0], "orphans" ), 3, "orphans correctly set to 3");
  561. } else {
  562. expect(1);
  563. ok( true, "jQuery does not attempt to test for style props that definitely don't exist in older versions of IE");
  564. }
  565. $p.remove();
  566. });
  567. test("can't get css for disconnected in IE<9, see #10254 and #8388", function() {
  568. expect( 2 );
  569. var span = jQuery( "<span/>" ).css( "background-image", "url(data/1x1.jpg)" );
  570. notEqual( span.css( "background-image" ), null, "can't get background-image in IE<9, see #10254" );
  571. var div = jQuery( "<div/>" ).css( "top", 10 );
  572. equal( div.css( "top" ), "10px", "can't get top in IE<9, see #8388" );
  573. });
  574. test("can't get background-position in IE<9, see #10796", function() {
  575. var div = jQuery( "<div/>" ).appendTo( "#qunit-fixture" ),
  576. units = [
  577. "0 0",
  578. "12px 12px",
  579. "13px 12em",
  580. "12em 13px",
  581. "12em center",
  582. "+12em center",
  583. "12.2em center",
  584. "center center"
  585. ],
  586. l = units.length,
  587. i = 0;
  588. expect( l );
  589. for( ; i < l; i++ ) {
  590. div.css( "background-position", units [ i ] );
  591. ok( div.css( "background-position" ), "can't get background-position in IE<9, see #10796" );
  592. }
  593. });
  594. test("percentage properties for bottom and right in IE<9 should not be incorrectly transformed to pixels, see #11311", function() {
  595. expect( 1 );
  596. var div = jQuery("<div style='position: absolute; width: 1px; height: 20px; bottom:50%;'></div>").appendTo( "#qunit-fixture" );
  597. ok( window.getComputedStyle || div.css( "bottom" ) === "50%", "position properties get incorrectly transformed in IE<8, see #11311" );
  598. });
  599. if ( jQuery.fn.offset ) {
  600. test("percentage properties for left and top should be transformed to pixels, see #9505", function() {
  601. expect( 2 );
  602. var parent = jQuery("<div style='position:relative;width:200px;height:200px;margin:0;padding:0;border-width:0'></div>").appendTo( "#qunit-fixture" ),
  603. div = jQuery("<div style='position: absolute; width: 20px; height: 20px; top:50%; left:50%'></div>").appendTo( parent );
  604. equal( div.css("top"), "100px", "position properties not transformed to pixels, see #9505" );
  605. equal( div.css("left"), "100px", "position properties not transformed to pixels, see #9505" );
  606. });
  607. }
  608. test("Do not append px (#9548, #12990)", function() {
  609. expect( 2 );
  610. var $div = jQuery("<div>").appendTo("#qunit-fixture");
  611. $div.css( "fill-opacity", 1 );
  612. equal( $div.css("fill-opacity"), 1, "Do not append px to 'fill-opacity'" );
  613. $div.css( "column-count", 1 );
  614. if ( $div.css("column-count") ) {
  615. equal( $div.css("column-count"), 1, "Do not append px to 'column-count'" );
  616. } else {
  617. ok( true, "No support for column-count CSS property" );
  618. }
  619. });
  620. test("css('width') and css('height') should respect box-sizing, see #11004", function() {
  621. expect( 4 );
  622. var el_dis = jQuery("<div style='width:300px;height:300px;margin:2px;padding:2px;-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;'>test</div>"),
  623. el = el_dis.clone().appendTo("#qunit-fixture");
  624. equal( el.css("width"), el.css("width", el.css("width")).css("width"), "css('width') is not respecting box-sizing, see #11004");
  625. equal( el_dis.css("width"), el_dis.css("width", el_dis.css("width")).css("width"), "css('width') is not respecting box-sizing for disconnected element, see #11004");
  626. equal( el.css("height"), el.css("height", el.css("height")).css("height"), "css('height') is not respecting box-sizing, see #11004");
  627. equal( el_dis.css("height"), el_dis.css("height", el_dis.css("height")).css("height"), "css('height') is not respecting box-sizing for disconnected element, see #11004");
  628. });
  629. test("certain css values of 'normal' should be convertable to a number, see #8627", function() {
  630. expect ( 2 );
  631. var el = jQuery("<div style='letter-spacing:normal;font-weight:normal;'>test</div>").appendTo("#qunit-fixture");
  632. ok( jQuery.isNumeric( parseFloat( el.css("letterSpacing") ) ), "css('letterSpacing') not convertable to number, see #8627" );
  633. ok( jQuery.isNumeric( parseFloat( el.css("fontWeight") ) ), "css('fontWeight') not convertable to number, see #8627" );
  634. });
  635. // only run this test in IE9
  636. if ( document.documentMode === 9 ) {
  637. test( ".css('filter') returns a string in IE9, see #12537", 1, function() {
  638. equal( jQuery("<div style='-ms-filter:\"progid:DXImageTransform.Microsoft.gradient(startColorstr=#FFFFFF, endColorstr=#ECECEC)\";'></div>").css("filter"), "progid:DXImageTransform.Microsoft.gradient(startColorstr=#FFFFFF, endColorstr=#ECECEC)", "IE9 returns the correct value from css('filter')." );
  639. });
  640. }
  641. test( "cssHooks - expand", function() {
  642. expect( 15 );
  643. var result,
  644. properties = {
  645. margin: [ "marginTop", "marginRight", "marginBottom", "marginLeft" ],
  646. borderWidth: [ "borderTopWidth", "borderRightWidth", "borderBottomWidth", "borderLeftWidth"],
  647. padding: [ "paddingTop", "paddingRight", "paddingBottom", "paddingLeft" ]
  648. };
  649. jQuery.each( properties, function( property, keys ) {
  650. var hook = jQuery.cssHooks[ property ],
  651. expected = {};
  652. jQuery.each( keys, function( _, key ) {
  653. expected[ key ] = 10;
  654. });
  655. result = hook.expand( 10 );
  656. deepEqual( result, expected, property + " expands properly with a number" );
  657. jQuery.each( keys, function( _, key ) {
  658. expected[ key ] = "10px";
  659. });
  660. result = hook.expand( "10px" );
  661. deepEqual( result, expected, property + " expands properly with '10px'" );
  662. expected[ keys[1] ] = expected[ keys[3] ] = "20px";
  663. result = hook.expand( "10px 20px" );
  664. deepEqual( result, expected, property + " expands properly with '10px 20px'" );
  665. expected[ keys[2] ] = "30px";
  666. result = hook.expand( "10px 20px 30px" );
  667. deepEqual( result, expected, property + " expands properly with '10px 20px 30px'" );
  668. expected[ keys[3] ] = "40px";
  669. result = hook.expand( "10px 20px 30px 40px" );
  670. deepEqual( result, expected, property + " expands properly with '10px 20px 30px 40px'" );
  671. });
  672. });
  673. test( "css opacity consistency across browsers (#12685)", function() {
  674. expect( 4 );
  675. var fixture = jQuery("#qunit-fixture"),
  676. style = jQuery("<style>.opacityWithSpaces_t12685 { opacity: 0.1; filter: alpha(opacity = 10); } .opacityNoSpaces_t12685 { opacity: 0.2; filter: alpha(opacity=20); }</style>").appendTo(fixture),
  677. el = jQuery("<div class='opacityWithSpaces_t12685'></div>").appendTo(fixture);
  678. equal( Math.round( el.css("opacity") * 100 ), 10, "opacity from style sheet (filter:alpha with spaces)" );
  679. el.removeClass("opacityWithSpaces_t12685").addClass("opacityNoSpaces_t12685");
  680. equal( Math.round( el.css("opacity") * 100 ), 20, "opacity from style sheet (filter:alpha without spaces)" );
  681. el.css( "opacity", 0.3 );
  682. equal( Math.round( el.css("opacity") * 100 ), 30, "override opacity" );
  683. el.css( "opacity", "" );
  684. equal( Math.round( el.css("opacity") * 100 ), 20, "remove opacity override" );
  685. });
  686. test( ":visible/:hidden selectors", function() {
  687. expect( 13 );
  688. ok( jQuery("#nothiddendiv").is(":visible"), "Modifying CSS display: Assert element is visible" );
  689. jQuery("#nothiddendiv").css({ display: "none" });
  690. ok( !jQuery("#nothiddendiv").is(":visible"), "Modified CSS display: Assert element is hidden" );
  691. jQuery("#nothiddendiv").css({"display": "block"});
  692. ok( jQuery("#nothiddendiv").is(":visible"), "Modified CSS display: Assert element is visible");
  693. ok( jQuery(window).is(":visible"), "Calling is(':visible') on window does not throw an error in IE.");
  694. ok( jQuery(document).is(":visible"), "Calling is(':visible') on document does not throw an error in IE.");
  695. ok( jQuery("#nothiddendiv").is(":visible"), "Modifying CSS display: Assert element is visible");
  696. jQuery("#nothiddendiv").css("display", "none");
  697. ok( !jQuery("#nothiddendiv").is(":visible"), "Modified CSS display: Assert element is hidden");
  698. jQuery("#nothiddendiv").css("display", "block");
  699. ok( jQuery("#nothiddendiv").is(":visible"), "Modified CSS display: Assert element is visible");
  700. // ok( !jQuery("#siblingspan").is(":visible"), "Span with no content not visible (#13132)" );
  701. // var $newDiv = jQuery("<div><span></span></div>").appendTo("#qunit-fixture");
  702. // equal( $newDiv.find(":visible").length, 0, "Span with no content not visible (#13132)" );
  703. // var $br = jQuery("<br/>").appendTo("#qunit-fixture");
  704. // ok( !$br.is(":visible"), "br element not visible (#10406)");
  705. var $table = jQuery("#table");
  706. $table.html("<tr><td style='display:none'>cell</td><td>cell</td></tr>");
  707. equal(jQuery("#table td:visible").length, 1, "hidden cell is not perceived as visible (#4512). Works on table elements");
  708. $table.css("display", "none").html("<tr><td>cell</td><td>cell</td></tr>");
  709. equal(jQuery("#table td:visible").length, 0, "hidden cell children not perceived as visible (#4512)");
  710. t( "Is Visible", "#qunit-fixture div:visible:lt(2)", ["foo", "nothiddendiv"] );
  711. t( "Is Not Hidden", "#qunit-fixture:hidden", [] );
  712. t( "Is Hidden", "#form input:hidden", ["hidden1","hidden2"] );
  713. });
  714. asyncTest( "Clearing a Cloned Element's Style Shouldn't Clear the Original Element's Style (#8908)", 24, function() {
  715. var baseUrl = document.location.href.replace( /([^\/]*)$/, "" ),
  716. styles = [{
  717. name: "backgroundAttachment",
  718. value: ["fixed"],
  719. expected: [ "scroll" ]
  720. },{
  721. name: "backgroundColor",
  722. value: [ "rgb(255, 0, 0)", "rgb(255,0,0)", "#ff0000" ],
  723. expected: ["transparent"]
  724. }, {
  725. // Firefox returns auto's value
  726. name: "backgroundImage",
  727. value: [ "url('test.png')", "url(" + baseUrl + "test.png)", "url(\"" + baseUrl + "test.png\")" ],
  728. expected: [ "none", "url(\"http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif\")" ]
  729. }, {
  730. name: "backgroundPosition",
  731. value: ["5% 5%"],
  732. expected: [ "0% 0%", "-1000px 0px", "-1000px 0%" ]
  733. }, {
  734. // Firefox returns no-repeat
  735. name: "backgroundRepeat",
  736. value: ["repeat-y"],
  737. expected: [ "repeat", "no-repeat" ]
  738. }, {
  739. name: "backgroundClip",
  740. value: ["padding-box"],
  741. expected: ["border-box"]
  742. }, {
  743. name: "backgroundOrigin",
  744. value: ["content-box"],
  745. expected: ["padding-box"]
  746. }, {
  747. name: "backgroundSize",
  748. value: ["80px 60px"],
  749. expected: [ "auto auto" ]
  750. }];
  751. jQuery.each(styles, function( index, style ) {
  752. var $clone, $clonedChildren,
  753. $source = jQuery( "#firstp" ),
  754. source = $source[ 0 ],
  755. $children = $source.children();
  756. style.expected = style.expected.concat( [ "", "auto" ] );
  757. if ( source.style[ style.name ] === undefined ) {
  758. ok( true, style.name + ": style isn't supported and therefore not an issue" );
  759. ok( true );
  760. ok( true );
  761. return true;
  762. }
  763. $source.css( style.name, style.value[ 0 ] );
  764. $children.css( style.name, style.value[ 0 ] );
  765. $clone = $source.clone();
  766. $clonedChildren = $clone.children();
  767. $clone.css( style.name, "" );
  768. $clonedChildren.css( style.name, "" );
  769. window.setTimeout(function() {
  770. notEqual( $clone.css( style.name ), style.value[ 0 ], "Cloned css was changed" );
  771. ok( jQuery.inArray( $source.css( style.name ) !== -1, style.value ),
  772. "Clearing clone.css() doesn't affect source.css(): " + style.name +
  773. "; result: " + $source.css( style.name ) +
  774. "; expected: " + style.value.join( "," ) );
  775. ok( jQuery.inArray( $children.css( style.name ) !== -1, style.value ),
  776. "Clearing clonedChildren.css() doesn't affect children.css(): " + style.name +
  777. "; result: " + $children.css( style.name ) +
  778. "; expected: " + style.value.join( "," ) );
  779. }, 100 );
  780. });
  781. window.setTimeout( start, 1000 );
  782. });
  783. asyncTest( "Make sure initialized display value for disconnected nodes is correct (#13310)", 4, function() {
  784. var display = jQuery("#display").css("display"),
  785. div = jQuery("<div/>");
  786. equal( div.css( "display", "inline" ).hide().show().appendTo("body").css( "display" ), "inline", "Initialized display value has returned" );
  787. div.remove();
  788. div.css( "display", "none" ).hide();
  789. equal( jQuery._data( div[ 0 ], "olddisplay" ), undefined, "olddisplay is undefined after hiding a detached and hidden element" );
  790. div.remove();
  791. div.css( "display", "inline-block" ).hide().appendTo("body").fadeIn(function() {
  792. equal( div.css( "display" ), "inline-block", "Initialized display value has returned" );
  793. div.remove();
  794. start();
  795. });
  796. equal( jQuery._data( jQuery("#display").css( "display", "inline" ).hide()[ 0 ], "olddisplay" ), display,
  797. "display: * !Important value should used as initialized display" );
  798. jQuery._removeData( jQuery("#display")[ 0 ] );
  799. });
  800. }