Tweaked a CSS test to handle differences in font-size % support in browsers.
[jquery.git] / test / unit / css.js
1 module("css");
2
3 test("css(String|Hash)", function() {
4         expect(27);
5
6         equals( jQuery('#main').css("display"), 'none', 'Check for css property "display"');
7
8         ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible');
9         jQuery('#nothiddendiv').css({display: 'none'});
10         ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden');
11         jQuery('#nothiddendiv').css({display: 'block'});
12         ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
13
14         // handle negative numbers by ignoring #1599, #4216
15         var width = parseFloat(jQuery('#nothiddendiv').css('width')), height = parseFloat(jQuery('#nothiddendiv').css('height'));
16         jQuery('#nothiddendiv').css({ width: -1, height: -1 });
17         equals( parseFloat(jQuery('#nothiddendiv').css('width')), width, 'Test negative width ignored')
18         equals( parseFloat(jQuery('#nothiddendiv').css('height')), height, 'Test negative height ignored')
19
20         jQuery('#floatTest').css({styleFloat: 'right'});
21         equals( jQuery('#floatTest').css('styleFloat'), 'right', 'Modified CSS float using "styleFloat": Assert float is right');
22         jQuery('#floatTest').css({cssFloat: 'left'});
23         equals( jQuery('#floatTest').css('cssFloat'), 'left', 'Modified CSS float using "cssFloat": Assert float is left');
24         jQuery('#floatTest').css({'float': 'right'});
25         equals( jQuery('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right');
26         jQuery('#floatTest').css({'font-size': '30px'});
27         equals( jQuery('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px');
28
29         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
30                 jQuery('#foo').css({opacity: n});
31                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
32                 jQuery('#foo').css({opacity: parseFloat(n)});
33                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
34         });
35         jQuery('#foo').css({opacity: ''});
36         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
37
38         equals( jQuery('#empty').css('opacity'), '0', "Assert opacity is accessible via filter property set in stylesheet in IE" );
39         jQuery('#empty').css({ opacity: '1' });
40         equals( jQuery('#empty').css('opacity'), '1', "Assert opacity is taken from style attribute when set vs stylesheet in IE with filters" );
41
42         var div = jQuery('#nothiddendiv'), child = jQuery('#nothiddendivchild');
43
44         equals( parseInt(div.css("fontSize")), 16, "Verify fontSize px set." );
45         equals( parseInt(child.css("fontSize")), 16, "Verify fontSize px set." );
46
47         child.attr("class", "em");
48         equals( parseInt(child.css("fontSize")), 32, "Verify fontSize em set." );
49
50         // Have to verify this as the result depends upon the browser's CSS
51         // support for font-size percentages
52         child.attr("class", "prct");
53         var prctval = parseInt(child.css("fontSize")), checkval = 0;
54         if ( prctval === 16 || prctval === 24 ) {
55                 checkval = prctval;
56         }
57
58         equals( prctval, checkval, "Verify fontSize % set." );
59 });
60
61 test("css(String, Object)", function() {
62         expect(20);
63         ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible');
64         jQuery('#nothiddendiv').css("display", 'none');
65         ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden');
66         jQuery('#nothiddendiv').css("display", 'block');
67         ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
68
69         jQuery('#floatTest').css('styleFloat', 'left');
70         equals( jQuery('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left');
71         jQuery('#floatTest').css('cssFloat', 'right');
72         equals( jQuery('#floatTest').css('cssFloat'), 'right', 'Modified CSS float using "cssFloat": Assert float is right');
73         jQuery('#floatTest').css('float', 'left');
74         equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');
75         jQuery('#floatTest').css('font-size', '20px');
76         equals( jQuery('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');
77
78         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
79                 jQuery('#foo').css('opacity', n);
80                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
81                 jQuery('#foo').css('opacity', parseFloat(n));
82                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
83         });
84         jQuery('#foo').css('opacity', '');
85         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
86
87         // using contents will get comments regular, text, and comment nodes
88         var j = jQuery("#nonnodes").contents();
89         j.css("padding-left", "1px");
90         equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
91
92         // opera sometimes doesn't update 'display' correctly, see #2037
93         jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML
94         equals( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
95 });
96
97 if(jQuery.browser.msie) {
98   test("css(String, Object) for MSIE", function() {
99     // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
100                 jQuery('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
101         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
102
103     var filterVal = "progid:DXImageTransform.Microsoft.alpha(opacity=30) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
104     var filterVal2 = "progid:DXImageTransform.Microsoft.alpha(opacity=100) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
105     jQuery('#foo').css("filter", filterVal);
106     equals( jQuery('#foo').css("filter"), filterVal, "css('filter', val) works" );
107     jQuery('#foo').css("opacity", 1)
108     equals( jQuery('#foo').css("filter"), filterVal2, "Setting opacity in IE doesn't clobber other filters" );
109     equals( jQuery('#foo').css("opacity"), 1, "Setting opacity in IE with other filters works" )
110   });
111 }
112
113 test("css(String, Function)", function() {
114         try { 
115                 expect(3);
116                 
117                 var sizes = ["10px", "20px", "30px"];
118         
119                 jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
120                                          "<div class='cssFunction'></div>" + 
121                                          "<div class='cssFunction'></div></div>")
122                         .appendTo("body");
123         
124                 var index = 0;
125         
126                 jQuery("#cssFunctionTest div").css("font-size", function() {
127                         var size = sizes[index];
128                         index++;
129                         return size;
130                 });
131                 
132                 index = 0;
133                 
134                 jQuery("#cssFunctionTest div").each(function() {
135                         var computedSize = jQuery(this).css("font-size")
136                         var expectedSize = sizes[index]
137                         equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
138                         index++;
139                 });
140                 
141         } finally {
142                 jQuery("#cssFunctionTest").remove();
143         }
144 });
145
146 test("css(Object) where values are Functions", function() {
147         try { 
148                 expect(3);
149                 
150                 var sizes = ["10px", "20px", "30px"];
151         
152                 jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
153                                          "<div class='cssFunction'></div>" + 
154                                          "<div class='cssFunction'></div></div>")
155                         .appendTo("body");
156         
157                 var index = 0;
158         
159                 jQuery("#cssFunctionTest div").css({fontSize: function() {
160                         var size = sizes[index];
161                         index++;
162                         return size;
163                 }});
164                 
165                 index = 0;
166                 
167                 jQuery("#cssFunctionTest div").each(function() {
168                         var computedSize = jQuery(this).css("font-size")
169                         var expectedSize = sizes[index]
170                         equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
171                         index++;
172                 });
173                 
174         } finally {
175                 jQuery("#cssFunctionTest").remove();
176         }
177 });
178
179 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
180         expect(4);
181
182         var $checkedtest = jQuery("#checkedtest");
183         // IE6 was clearing "checked" in jQuery.css(elem, "height");
184         jQuery.css($checkedtest[0], "height");
185         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
186         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
187         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
188         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
189 });