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