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