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