99bab1fe56e0a02186f1913e3dcef56d87734ed7
[jquery.git] / test / unit / css.js
1 module("css");
2
3 test("css(String|Hash)", function() {
4         expect(28);
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         jQuery('#nothiddendiv').css({ 'width': 1, 'height': 1 });
16
17         var width = parseFloat(jQuery('#nothiddendiv').css('width')), height = parseFloat(jQuery('#nothiddendiv').css('height'));
18         jQuery('#nothiddendiv').css({ width: -1, height: -1 });
19         equals( parseFloat(jQuery('#nothiddendiv').css('width')), width, 'Test negative width ignored')
20         equals( parseFloat(jQuery('#nothiddendiv').css('height')), height, 'Test negative height ignored')
21
22         jQuery('#floatTest').css({'float': 'right'});
23         equals( jQuery('#floatTest').css('float'), 'right', 'Modified CSS float using "float": Assert float is right');
24         jQuery('#floatTest').css({'font-size': '30px'});
25         equals( jQuery('#floatTest').css('font-size'), '30px', 'Modified CSS font-size: Assert font-size is 30px');
26
27         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
28                 jQuery('#foo').css({opacity: n});
29                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
30                 jQuery('#foo').css({opacity: parseFloat(n)});
31                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
32         });
33         jQuery('#foo').css({opacity: ''});
34         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
35
36         equals( jQuery('#empty').css('opacity'), '0', "Assert opacity is accessible via filter property set in stylesheet in IE" );
37         jQuery('#empty').css({ opacity: '1' });
38         equals( jQuery('#empty').css('opacity'), '1', "Assert opacity is taken from style attribute when set vs stylesheet in IE with filters" );
39
40         var div = jQuery('#nothiddendiv'), child = jQuery('#nothiddendivchild');
41
42         equals( parseInt(div.css("fontSize")), 16, "Verify fontSize px set." );
43         equals( parseInt(div.css("font-size")), 16, "Verify fontSize px set." );
44         equals( parseInt(child.css("fontSize")), 16, "Verify fontSize px set." );
45         equals( parseInt(child.css("font-size")), 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         equals( typeof child.css("width"), "string", "Make sure that a string width is returned from css('width')." );
61 });
62
63 test("css(String, Object)", function() {
64         expect(19);
65         ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible');
66         jQuery('#nothiddendiv').css("display", 'none');
67         ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden');
68         jQuery('#nothiddendiv').css("display", 'block');
69         ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
70
71         jQuery("#nothiddendiv").css("top", "-1em");
72         ok( jQuery("#nothiddendiv").css("top"), -16, "Check negative number in EMs." );
73
74         jQuery('#floatTest').css('float', 'left');
75         equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');
76         jQuery('#floatTest').css('font-size', '20px');
77         equals( jQuery('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');
78
79         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
80                 jQuery('#foo').css('opacity', n);
81                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
82                 jQuery('#foo').css('opacity', parseFloat(n));
83                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
84         });
85         jQuery('#foo').css('opacity', '');
86         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
87
88         // using contents will get comments regular, text, and comment nodes
89         var j = jQuery("#nonnodes").contents();
90         j.css("padding-left", "1px");
91         equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
92
93         // opera sometimes doesn't update 'display' correctly, see #2037
94         jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML
95         equals( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
96 });
97
98 if(jQuery.browser.msie) {
99   test("css(String, Object) for MSIE", function() {
100     // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
101                 jQuery('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
102         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
103
104     var filterVal = "progid:DXImageTransform.Microsoft.alpha(opacity=30) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
105     var filterVal2 = "progid:DXImageTransform.Microsoft.alpha(opacity=100) progid:DXImageTransform.Microsoft.Blur(pixelradius=5)";
106     jQuery('#foo').css("filter", filterVal);
107     equals( jQuery('#foo').css("filter"), filterVal, "css('filter', val) works" );
108     jQuery('#foo').css("opacity", 1)
109     equals( jQuery('#foo').css("filter"), filterVal2, "Setting opacity in IE doesn't clobber other filters" );
110     equals( jQuery('#foo').css("opacity"), 1, "Setting opacity in IE with other filters works" )
111   });
112 }
113
114 test("css(String, Function)", function() {
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         jQuery("#cssFunctionTest").remove();
142 });
143
144 test("css(String, Function) with incoming value", function() {
145         expect(3);
146                 
147         var sizes = ["10px", "20px", "30px"];
148         
149         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
150                                  "<div class='cssFunction'></div>" + 
151                                  "<div class='cssFunction'></div></div>")
152                 .appendTo("body");
153         
154         var index = 0;
155         
156         jQuery("#cssFunctionTest div").css("font-size", function() {
157                 var size = sizes[index];
158                 index++;
159                 return size;
160         });
161                 
162         index = 0;
163         
164         jQuery("#cssFunctionTest div").css("font-size", function(i, computedSize) {
165                 var expectedSize = sizes[index]
166                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
167                 index++;
168                 return computedSize;
169         });
170
171         jQuery("#cssFunctionTest").remove();
172 });
173
174 test("css(Object) where values are Functions", function() {
175         expect(3);
176                 
177         var sizes = ["10px", "20px", "30px"];
178         
179         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
180                                  "<div class='cssFunction'></div>" + 
181                                  "<div class='cssFunction'></div></div>")
182                 .appendTo("body");
183
184         var index = 0;
185         
186         jQuery("#cssFunctionTest div").css({fontSize: function() {
187                 var size = sizes[index];
188                 index++;
189                 return size;
190         }});
191                 
192         index = 0;
193                 
194         jQuery("#cssFunctionTest div").each(function() {
195                 var computedSize = jQuery(this).css("font-size")
196                 var expectedSize = sizes[index]
197                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
198                 index++;
199         });
200                 
201         jQuery("#cssFunctionTest").remove();
202 });
203
204 test("css(Object) where values are Functions with incoming values", function() {
205         expect(3);
206                 
207         var sizes = ["10px", "20px", "30px"];
208         
209         jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
210                                  "<div class='cssFunction'></div>" + 
211                                  "<div class='cssFunction'></div></div>")
212                 .appendTo("body");
213
214         var index = 0;
215         
216         jQuery("#cssFunctionTest div").css({fontSize: function() {
217                 var size = sizes[index];
218                 index++;
219                 return size;
220         }});
221                 
222         index = 0;
223                 
224         jQuery("#cssFunctionTest div").css({"font-size": function(i, computedSize) {
225                 var expectedSize = sizes[index]
226                 equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
227                 index++;
228                 return computedSize;
229         }});
230                 
231         jQuery("#cssFunctionTest").remove();
232 });
233
234 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
235         expect(4);
236
237         var $checkedtest = jQuery("#checkedtest");
238         // IE6 was clearing "checked" in jQuery.css(elem, "height");
239         jQuery.css($checkedtest[0], "height");
240         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
241         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
242         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
243         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
244 });