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