1
0

css.js 36 KB

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