Changed the style attribute test to handle hex colors (as that's what Opera returns...
[jquery.git] / test / unit / attributes.js
1 module("attributes");
2
3 test("attr(String)", function() {
4         expect(27);
5         
6         // This one sometimes fails randomally ?!
7         equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
8         
9         equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );
10         equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );
11         equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );
12         equals( jQuery('#check1').attr('type'), "checkbox", 'Check for type attribute' );
13         equals( jQuery('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' );
14         equals( jQuery('#google').attr('title'), "Google!", 'Check for title attribute' );
15         equals( jQuery('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' );
16         equals( jQuery('#en').attr('lang'), "en", 'Check for lang attribute' );
17         equals( jQuery('#simon').attr('class'), "blog link", 'Check for class attribute' );
18         equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' );
19         equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' );
20         ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
21         equals( jQuery('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );
22         equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
23         equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
24         equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
25         equals( jQuery('#foo').attr('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' );
26         equals( jQuery('#foo').attr('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' );
27
28         jQuery('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
29         equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
30
31         equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
32
33
34         // Related to [5574] and [5683]
35         var body = document.body, $body = jQuery(body);
36
37         ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );
38         ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );
39
40         body.setAttribute('foo', 'baz');
41         equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
42
43         body.foo = 'bar';
44         equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
45
46         $body.attr('foo','cool');
47         equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );
48
49         body.foo = undefined;
50         ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
51
52         body.removeAttribute('foo'); // Cleanup
53 });
54
55 if ( !isLocal ) {
56         test("attr(String) in XML Files", function() {
57                 expect(2);
58                 stop();
59                 jQuery.get("data/dashboard.xml", function(xml) {
60                         equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
61                         equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
62                         start();
63                 });
64         });
65 }
66
67 test("attr(String, Function)", function() {
68         expect(2);
69         equals( jQuery('#text1').attr('value', function() { return this.id })[0].value, "text1", "Set value from id" );
70         equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
71 });
72
73 test("attr(Hash)", function() {
74         expect(3);
75         var pass = true;
76         jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
77                 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
78         });
79         ok( pass, "Set Multiple Attributes" );
80        equals( jQuery('#text1').attr({'value': function() { return this.id; }})[0].value, "text1", "Set attribute to computed value #1" );
81        equals( jQuery('#text1').attr({'title': function(i) { return i; }}).attr('title'), "0", "Set attribute to computed value #2");
82
83 });
84
85 test("attr(String, Object)", function() {
86         expect(23);
87         var div = jQuery("div").attr("foo", "bar"),
88                 fail = false;
89         for ( var i = 0; i < div.size(); i++ ) {
90                 if ( div.get(i).getAttribute('foo') != "bar" ){
91                         fail = i;
92                         break;
93                 }
94         }
95         equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
96
97         // Fails on IE since recent changes to .attr()
98         // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
99
100         jQuery("#name").attr('name', 'something');
101         equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
102         jQuery("#check2").attr('checked', true);
103         equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
104         jQuery("#check2").attr('checked', false);
105         equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
106         jQuery("#text1").attr('readonly', true);
107         equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
108         jQuery("#text1").attr('readonly', false);
109         equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
110         jQuery("#name").attr('maxlength', '5');
111         equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
112         jQuery("#name").attr('maxLength', '10');
113         equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
114
115         var table = jQuery('#table').append("<tr><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr>"),
116                 td = table.find('td:first');
117         td.attr("rowspan", "2");
118         equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
119         td.attr("colspan", "2");
120         equals( td[0].colSpan, 2, "Check colspan is correctly set" );
121         table.attr("cellspacing", "2");
122         equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" );
123
124         // for #1070
125         jQuery("#name").attr('someAttr', '0');
126         equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
127         jQuery("#name").attr('someAttr', 0);
128         equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
129         jQuery("#name").attr('someAttr', 1);
130         equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
131
132         // using contents will get comments regular, text, and comment nodes
133         var j = jQuery("#nonnodes").contents();
134
135         j.attr("name", "attrvalue");
136         equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
137         j.removeAttr("name");
138
139         reset();
140
141         var type = jQuery("#check2").attr('type');
142         var thrown = false;
143         try {
144                 jQuery("#check2").attr('type','hidden');
145         } catch(e) {
146                 thrown = true;
147         }
148         ok( thrown, "Exception thrown when trying to change type property" );
149         equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
150
151         var check = document.createElement("input");
152         var thrown = true;
153         try {
154                 jQuery(check).attr('type','checkbox');
155         } catch(e) {
156                 thrown = false;
157         }
158         ok( thrown, "Exception thrown when trying to change type property" );
159         equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
160
161         var check = jQuery("<input />");
162         var thrown = true;
163         try {
164                 check.attr('type','checkbox');
165         } catch(e) {
166                 thrown = false;
167         }
168         ok( thrown, "Exception thrown when trying to change type property" );
169         equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
170
171         var button = jQuery("#button");
172         var thrown = false;
173         try {
174                 button.attr('type','submit');
175         } catch(e) {
176                 thrown = true;
177         }
178         ok( thrown, "Exception thrown when trying to change type property" );
179         equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
180 });
181
182 test("attr(jquery_method)", function(){
183         expect(10);
184         
185         var $elem = jQuery("<div />"),
186                 elem = $elem[0];
187         
188         // one at a time        
189         $elem.attr('html', 'foo');
190         equals( elem.innerHTML, 'foo', 'attr(html)');
191         
192         $elem.attr('text', 'bar');
193         equals( elem.innerHTML, 'bar', 'attr(text)');
194         
195         $elem.attr('addClass', 'css');
196         equals( elem.className, 'css', 'attr(addClass)');
197         
198         $elem.attr('removeClass', 'css');
199         equals( jQuery.trim(elem.className), '', 'attr(removeClass)');
200         
201         $elem.attr('css', {color:'red'});
202         ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
203         
204         $elem.attr('height', 10);
205         equals( elem.style.height, '10px', 'attr(height)');
206         
207         $elem.attr('each', function(){ 
208                 return function(){
209                         ok(true, 'attr(each)');
210                 };
211         });
212         
213         // Multiple attributes
214         
215         $elem.attr({
216                 width:10,
217                 css:{ paddingLeft:1, paddingRight:1 }
218         });
219         
220         equals( elem.style.width, '10px', 'attr({...})');
221         equals( elem.style.paddingLeft, '1px', 'attr({...})');
222         equals( elem.style.paddingRight, '1px', 'attr({...})');
223 });
224
225 if ( !isLocal ) {
226         test("attr(String, Object) - Loaded via XML document", function() {
227                 expect(2);
228                 stop();
229                 jQuery.get('data/dashboard.xml', function(xml) {
230                         var titles = [];
231                         jQuery('tab', xml).each(function() {
232                                 titles.push(jQuery(this).attr('title'));
233                         });
234                         equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
235                         equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
236                         start();
237                 });
238         });
239 }
240
241 test("attr('tabindex')", function() {
242         expect(8);
243
244         // elements not natively tabbable
245         equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
246         equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
247
248         // anchor with href
249         equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
250         equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
251         equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
252
253         // anchor without href
254         equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
255         equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
256         equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
257 });
258
259 test("attr('tabindex', value)", function() {
260         expect(9);
261
262         var element = jQuery('#divWithNoTabIndex');
263         equals(element.attr('tabindex'), undefined, 'start with no tabindex');
264
265         // set a positive string
266         element.attr('tabindex', '1');
267         equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
268
269         // set a zero string
270         element.attr('tabindex', '0');
271         equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
272
273         // set a negative string
274         element.attr('tabindex', '-1');
275         equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
276
277         // set a positive number
278         element.attr('tabindex', 1);
279         equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
280
281         // set a zero number
282         element.attr('tabindex', 0);
283         equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
284
285         // set a negative number
286         element.attr('tabindex', -1);
287         equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
288
289         element = jQuery('#linkWithTabIndex');
290         equals(element.attr('tabindex'), 2, 'start with tabindex 2');
291
292         element.attr('tabindex', -1);
293         equals(element.attr('tabindex'), -1, 'set negative tabindex');
294 });
295
296 test("addClass(String)", function() {
297         expect(2);
298         var div = jQuery("div");
299         div.addClass("test");
300         var pass = true;
301         for ( var i = 0; i < div.size(); i++ ) {
302          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
303         }
304         ok( pass, "Add Class" );
305
306         // using contents will get regular, text, and comment nodes
307         var j = jQuery("#nonnodes").contents();
308         j.addClass("asdf");
309         ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
310 });
311
312 test("removeClass(String) - simple", function() {
313         expect(5);
314
315         var $divs = jQuery('div');
316
317         $divs.addClass("test").removeClass("test");
318
319         ok( !$divs.is('.test'), "Remove Class" );
320
321         reset();
322         $divs = jQuery('div');
323
324         $divs.addClass("test").addClass("foo").addClass("bar");
325         $divs.removeClass("test").removeClass("bar").removeClass("foo");
326
327         ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
328
329         reset();
330         $divs = jQuery('div');
331
332         // Make sure that a null value doesn't cause problems
333         $divs.eq(0).addClass("test").removeClass(null);
334         ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
335
336         $divs.eq(0).addClass("test").removeClass("");
337         ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
338
339         // using contents will get regular, text, and comment nodes
340         var j = jQuery("#nonnodes").contents();
341         j.removeClass("asdf");
342         ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
343 });
344
345 test("toggleClass(String|boolean|undefined[, boolean])", function() {
346         expect(17);
347
348         var e = jQuery("#firstp");
349         ok( !e.is(".test"), "Assert class not present" );
350         e.toggleClass("test");
351         ok( e.is(".test"), "Assert class present" );
352         e.toggleClass("test");
353         ok( !e.is(".test"), "Assert class not present" );
354
355         // class name with a boolean
356         e.toggleClass("test", false);
357         ok( !e.is(".test"), "Assert class not present" );
358         e.toggleClass("test", true);
359         ok( e.is(".test"), "Assert class present" );
360         e.toggleClass("test", false);
361         ok( !e.is(".test"), "Assert class not present" );
362
363         // multiple class names
364         e.addClass("testA testB");
365         ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
366         e.toggleClass("testB testC");
367         ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
368         e.toggleClass("testA testC");
369         ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
370
371         // toggleClass storage
372         e.toggleClass(true);
373         ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
374         e.addClass("testD testE");
375         ok( e.is(".testD.testE"), "Assert class present" );
376         e.toggleClass();
377         ok( !e.is(".testD.testE"), "Assert class not present" );
378         ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
379         e.toggleClass();
380         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
381         e.toggleClass(false);
382         ok( !e.is(".testD.testE"), "Assert class not present" );
383         e.toggleClass(true);
384         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
385         e.toggleClass();
386         e.toggleClass(false);
387         e.toggleClass();
388         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
389
390
391
392         // Cleanup
393         e.removeClass("testD");
394         e.removeData('__className__');
395 });
396
397 test("removeAttr(String", function() {
398         expect(1);
399         equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );
400 });
401
402 test("addClass, removeClass, hasClass", function() {
403         expect(6);
404
405         var jq = jQuery("<p>Hi</p>"), x = jq[0];
406
407         jq.addClass("hi");
408         equals( x.className, "hi", "Check single added class" );
409
410         jq.addClass("foo bar");
411         equals( x.className, "hi foo bar", "Check more added classes" );
412
413         jq.removeClass();
414         equals( x.className, "", "Remove all classes" );
415
416         jq.addClass("hi foo bar");
417         jq.removeClass("foo");
418         equals( x.className, "hi bar", "Check removal of one class" );
419
420         ok( jq.hasClass("hi"), "Check has1" );
421         ok( jq.hasClass("bar"), "Check has2" );
422 });