Landing tweak from 'haruka' that fixes non-pixel fontSize values in IE. Fixes #760.
[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         child.attr("class", "prct");
51         equals( parseInt(child.css("fontSize")), 24, "Verify fontSize % set." );
52 });
53
54 test("css(String, Object)", function() {
55         expect(21);
56         ok( jQuery('#nothiddendiv').is(':visible'), 'Modifying CSS display: Assert element is visible');
57         jQuery('#nothiddendiv').css("display", 'none');
58         ok( !jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is hidden');
59         jQuery('#nothiddendiv').css("display", 'block');
60         ok( jQuery('#nothiddendiv').is(':visible'), 'Modified CSS display: Assert element is visible');
61
62         jQuery('#floatTest').css('styleFloat', 'left');
63         equals( jQuery('#floatTest').css('styleFloat'), 'left', 'Modified CSS float using "styleFloat": Assert float is left');
64         jQuery('#floatTest').css('cssFloat', 'right');
65         equals( jQuery('#floatTest').css('cssFloat'), 'right', 'Modified CSS float using "cssFloat": Assert float is right');
66         jQuery('#floatTest').css('float', 'left');
67         equals( jQuery('#floatTest').css('float'), 'left', 'Modified CSS float using "float": Assert float is left');
68         jQuery('#floatTest').css('font-size', '20px');
69         equals( jQuery('#floatTest').css('font-size'), '20px', 'Modified CSS font-size: Assert font-size is 20px');
70
71         jQuery.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
72                 jQuery('#foo').css('opacity', n);
73                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
74                 jQuery('#foo').css('opacity', parseFloat(n));
75                 equals( jQuery('#foo').css('opacity'), parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
76         });
77         jQuery('#foo').css('opacity', '');
78         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when set to an empty String" );
79         // for #1438, IE throws JS error when filter exists but doesn't have opacity in it
80         if (jQuery.browser.msie) {
81                 jQuery('#foo').css("filter", "progid:DXImageTransform.Microsoft.Chroma(color='red');");
82         }
83         equals( jQuery('#foo').css('opacity'), '1', "Assert opacity is 1 when a different filter is set in IE, #1438" );
84
85         // using contents will get comments regular, text, and comment nodes
86         var j = jQuery("#nonnodes").contents();
87         j.css("padding-left", "1px");
88         equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
89
90         // opera sometimes doesn't update 'display' correctly, see #2037
91         jQuery("#t2037")[0].innerHTML = jQuery("#t2037")[0].innerHTML
92         equals( jQuery("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
93 });
94
95 test("css(String, Function)", function() {
96         try { 
97                 expect(3);
98                 
99                 var sizes = ["10px", "20px", "30px"];
100         
101                 jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
102                                          "<div class='cssFunction'></div>" + 
103                                          "<div class='cssFunction'></div></div>")
104                         .appendTo("body");
105         
106                 var index = 0;
107         
108                 jQuery("#cssFunctionTest div").css("font-size", function() {
109                         var size = sizes[index];
110                         index++;
111                         return size;
112                 });
113                 
114                 index = 0;
115                 
116                 jQuery("#cssFunctionTest div").each(function() {
117                         var computedSize = jQuery(this).css("font-size")
118                         var expectedSize = sizes[index]
119                         equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
120                         index++;
121                 });
122                 
123         } finally {
124                 jQuery("#cssFunctionTest").remove();
125         }
126 });
127
128 test("css(Object) where values are Functions", function() {
129         try { 
130                 expect(3);
131                 
132                 var sizes = ["10px", "20px", "30px"];
133         
134                 jQuery("<div id='cssFunctionTest'><div class='cssFunction'></div>" + 
135                                          "<div class='cssFunction'></div>" + 
136                                          "<div class='cssFunction'></div></div>")
137                         .appendTo("body");
138         
139                 var index = 0;
140         
141                 jQuery("#cssFunctionTest div").css({fontSize: function() {
142                         var size = sizes[index];
143                         index++;
144                         return size;
145                 }});
146                 
147                 index = 0;
148                 
149                 jQuery("#cssFunctionTest div").each(function() {
150                         var computedSize = jQuery(this).css("font-size")
151                         var expectedSize = sizes[index]
152                         equals( computedSize, expectedSize, "Div #" + index + " should be " + expectedSize );
153                         index++;
154                 });
155                 
156         } finally {
157                 jQuery("#cssFunctionTest").remove();
158         }
159 });
160
161 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
162         expect(4);
163
164         var $checkedtest = jQuery("#checkedtest");
165         // IE6 was clearing "checked" in jQuery.css(elem, "height");
166         jQuery.css($checkedtest[0], "height");
167         ok( !! jQuery(":radio:first", $checkedtest).attr("checked"), "Check first radio still checked." );
168         ok( ! jQuery(":radio:last", $checkedtest).attr("checked"), "Check last radio still NOT checked." );
169         ok( !! jQuery(":checkbox:first", $checkedtest).attr("checked"), "Check first checkbox still checked." );
170         ok( ! jQuery(":checkbox:last", $checkedtest).attr("checked"), "Check last checkbox still NOT checked." );
171 });