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