make sure toggleClass does not delete classNames when forcefully removing classes...
[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(1);
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 });
78
79 test("attr(String, Object)", function() {
80         expect(21);
81         var div = jQuery("div").attr("foo", "bar"),
82                 fail = false;
83         for ( var i = 0; i < div.size(); i++ ) {
84                 if ( div.get(i).getAttribute('foo') != "bar" ){
85                         fail = i;
86                         break;
87                 }
88         }
89         equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
90
91         ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
92
93         jQuery("#name").attr('name', 'something');
94         equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
95         jQuery("#check2").attr('checked', true);
96         equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
97         jQuery("#check2").attr('checked', false);
98         equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
99         jQuery("#text1").attr('readonly', true);
100         equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
101         jQuery("#text1").attr('readonly', false);
102         equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
103         jQuery("#name").attr('maxlength', '5');
104         equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
105         jQuery("#name").attr('maxLength', '10');
106         equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
107
108         // for #1070
109         jQuery("#name").attr('someAttr', '0');
110         equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
111         jQuery("#name").attr('someAttr', 0);
112         equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
113         jQuery("#name").attr('someAttr', 1);
114         equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
115
116         // using contents will get comments regular, text, and comment nodes
117         var j = jQuery("#nonnodes").contents();
118
119         j.attr("name", "attrvalue");
120         equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
121         j.removeAttr("name");
122
123         reset();
124
125         var type = jQuery("#check2").attr('type');
126         var thrown = false;
127         try {
128                 jQuery("#check2").attr('type','hidden');
129         } catch(e) {
130                 thrown = true;
131         }
132         ok( thrown, "Exception thrown when trying to change type property" );
133         equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
134
135         var check = document.createElement("input");
136         var thrown = true;
137         try {
138                 jQuery(check).attr('type','checkbox');
139         } catch(e) {
140                 thrown = false;
141         }
142         ok( thrown, "Exception thrown when trying to change type property" );
143         equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
144
145         var check = jQuery("<input />");
146         var thrown = true;
147         try {
148                 check.attr('type','checkbox');
149         } catch(e) {
150                 thrown = false;
151         }
152         ok( thrown, "Exception thrown when trying to change type property" );
153         equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
154
155         var button = jQuery("#button");
156         var thrown = false;
157         try {
158                 button.attr('type','submit');
159         } catch(e) {
160                 thrown = true;
161         }
162         ok( thrown, "Exception thrown when trying to change type property" );
163         equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
164 });
165
166 if ( !isLocal ) {
167         test("attr(String, Object) - Loaded via XML document", function() {
168                 expect(2);
169                 stop();
170                 jQuery.get('data/dashboard.xml', function(xml) {
171                         var titles = [];
172                         jQuery('tab', xml).each(function() {
173                                 titles.push(jQuery(this).attr('title'));
174                         });
175                         equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
176                         equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
177                         start();
178                 });
179         });
180 }
181
182 test("attr('tabindex')", function() {
183         expect(8);
184
185         // elements not natively tabbable
186         equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
187         equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
188
189         // anchor with href
190         equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
191         equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
192         equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
193
194         // anchor without href
195         equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
196         equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
197         equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
198 });
199
200 test("attr('tabindex', value)", function() {
201         expect(9);
202
203         var element = jQuery('#divWithNoTabIndex');
204         equals(element.attr('tabindex'), undefined, 'start with no tabindex');
205
206         // set a positive string
207         element.attr('tabindex', '1');
208         equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
209
210         // set a zero string
211         element.attr('tabindex', '0');
212         equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
213
214         // set a negative string
215         element.attr('tabindex', '-1');
216         equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
217
218         // set a positive number
219         element.attr('tabindex', 1);
220         equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
221
222         // set a zero number
223         element.attr('tabindex', 0);
224         equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
225
226         // set a negative number
227         element.attr('tabindex', -1);
228         equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
229
230         element = jQuery('#linkWithTabIndex');
231         equals(element.attr('tabindex'), 2, 'start with tabindex 2');
232
233         element.attr('tabindex', -1);
234         equals(element.attr('tabindex'), -1, 'set negative tabindex');
235 });
236
237 test("addClass(String)", function() {
238         expect(2);
239         var div = jQuery("div");
240         div.addClass("test");
241         var pass = true;
242         for ( var i = 0; i < div.size(); i++ ) {
243          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
244         }
245         ok( pass, "Add Class" );
246
247         // using contents will get regular, text, and comment nodes
248         var j = jQuery("#nonnodes").contents();
249         j.addClass("asdf");
250         ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
251 });
252
253 test("removeClass(String) - simple", function() {
254         expect(5);
255
256         var $divs = jQuery('div');
257
258         $divs.addClass("test").removeClass("test");
259
260         ok( !$divs.is('.test'), "Remove Class" );
261
262         reset();
263
264         $divs.addClass("test").addClass("foo").addClass("bar");
265         $divs.removeClass("test").removeClass("bar").removeClass("foo");
266
267         ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
268
269         reset();
270
271         // Make sure that a null value doesn't cause problems
272         $divs.eq(0).addClass("test").removeClass(null);
273         ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
274
275         $divs.eq(0).addClass("test").removeClass("");
276         ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
277
278         // using contents will get regular, text, and comment nodes
279         var j = jQuery("#nonnodes").contents();
280         j.removeClass("asdf");
281         ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
282 });
283
284 test("toggleClass(String|boolean|undefined[, boolean])", function() {
285         expect(17);
286
287         var e = jQuery("#firstp");
288         ok( !e.is(".test"), "Assert class not present" );
289         e.toggleClass("test");
290         ok( e.is(".test"), "Assert class present" );
291         e.toggleClass("test");
292         ok( !e.is(".test"), "Assert class not present" );
293
294         // class name with a boolean
295         e.toggleClass("test", false);
296         ok( !e.is(".test"), "Assert class not present" );
297         e.toggleClass("test", true);
298         ok( e.is(".test"), "Assert class present" );
299         e.toggleClass("test", false);
300         ok( !e.is(".test"), "Assert class not present" );
301
302         // multiple class names
303         e.addClass("testA testB");
304         ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
305         e.toggleClass("testB testC");
306         ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
307         e.toggleClass("testA testC");
308         ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
309
310         // toggleClass storage
311         e.toggleClass(true);
312         ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
313         e.addClass("testD testE");
314         ok( e.is(".testD.testE"), "Assert class present" );
315         e.toggleClass();
316         ok( !e.is(".testD.testE"), "Assert class not present" );
317         ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
318         e.toggleClass();
319         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
320         e.toggleClass(false);
321         ok( !e.is(".testD.testE"), "Assert class not present" );
322         e.toggleClass(true);
323         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
324         e.toggleClass();
325         e.toggleClass(false);
326         e.toggleClass();
327         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
328         
329         
330
331         // Cleanup
332         e.removeClass("testD");
333         e.removeData('__className__');
334 });
335
336 test("removeAttr(String", function() {
337         expect(1);
338         equals( jQuery('#mark').removeAttr("class")[0].className, "", "remove class" );
339 });
340
341 test("jQuery.className", function() {
342         expect(6);
343         var x = jQuery("<p>Hi</p>")[0];
344         var c = jQuery.className;
345         c.add(x, "hi");
346         equals( x.className, "hi", "Check single added class" );
347         c.add(x, "foo bar");
348         equals( x.className, "hi foo bar", "Check more added classes" );
349         c.remove(x);
350         equals( x.className, "", "Remove all classes" );
351         c.add(x, "hi foo bar");
352         c.remove(x, "foo");
353         equals( x.className, "hi bar", "Check removal of one class" );
354         ok( c.has(x, "hi"), "Check has1" );
355         ok( c.has(x, "bar"), "Check has2" );
356 });