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