3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
6 test("attr(String)", function() {
9 // This one sometimes fails randomly ?!
10 equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
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' );
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)' );
35 equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
38 // Related to [5574] and [5683]
39 var body = document.body, $body = jQuery(body);
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' );
44 body.setAttribute('foo', 'baz');
45 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
48 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
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' );
54 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
56 body.removeAttribute('foo'); // Cleanup
60 test("attr(String) in XML Files", function() {
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" );
71 test("attr(String, Function)", function() {
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");
77 test("attr(Hash)", function() {
80 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
81 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
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");
89 test("attr(String, Object)", function() {
91 var div = jQuery("div").attr("foo", "bar"),
93 for ( var i = 0; i < div.size(); i++ ) {
94 if ( div.get(i).getAttribute('foo') != "bar" ){
99 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
101 // Fails on IE since recent changes to .attr()
102 // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
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' );
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" );
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' );
136 // using contents will get comments regular, text, and comment nodes
137 var j = jQuery("#nonnodes").contents();
139 j.attr("name", "attrvalue");
140 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
141 j.removeAttr("name");
145 var type = jQuery("#check2").attr('type');
148 jQuery("#check2").attr('type','hidden');
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" );
155 var check = document.createElement("input");
158 jQuery(check).attr('type','checkbox');
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" );
165 var check = jQuery("<input />");
168 check.attr('type','checkbox');
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" );
175 var button = jQuery("#button");
178 button.attr('type','submit');
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" );
186 test("attr(jquery_method)", function(){
189 var $elem = jQuery("<div />"),
193 $elem.attr('html', 'foo');
194 equals( elem.innerHTML, 'foo', 'attr(html)');
196 $elem.attr('text', 'bar');
197 equals( elem.innerHTML, 'bar', 'attr(text)');
199 $elem.attr('css', {color:'red'});
200 ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
202 $elem.attr('height', 10);
203 equals( elem.style.height, '10px', 'attr(height)');
205 // Multiple attributes
209 css:{ paddingLeft:1, paddingRight:1 }
212 equals( elem.style.width, '10px', 'attr({...})');
213 equals( elem.style.paddingLeft, '1px', 'attr({...})');
214 equals( elem.style.paddingRight, '1px', 'attr({...})');
218 test("attr(String, Object) - Loaded via XML document", function() {
221 jQuery.get('data/dashboard.xml', function(xml) {
223 jQuery('tab', xml).each(function() {
224 titles.push(jQuery(this).attr('title'));
226 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
227 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
233 test("attr('tabindex')", function() {
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');
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');
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');
251 test("attr('tabindex', value)", function() {
254 var element = jQuery('#divWithNoTabIndex');
255 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
257 // set a positive string
258 element.attr('tabindex', '1');
259 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
262 element.attr('tabindex', '0');
263 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
265 // set a negative string
266 element.attr('tabindex', '-1');
267 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
269 // set a positive number
270 element.attr('tabindex', 1);
271 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
274 element.attr('tabindex', 0);
275 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
277 // set a negative number
278 element.attr('tabindex', -1);
279 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
281 element = jQuery('#linkWithTabIndex');
282 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
284 element.attr('tabindex', -1);
285 equals(element.attr('tabindex'), -1, 'set negative tabindex');
288 var testAddClass = function(valueObj) {
290 var div = jQuery("div");
291 div.addClass( valueObj("test") );
293 for ( var i = 0; i < div.size(); i++ ) {
294 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
296 ok( pass, "Add Class" );
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" );
304 test("addClass(String)", function() {
305 testAddClass(bareObj);
308 test("addClass(Function)", function() {
309 testAddClass(functionReturningObj);
312 var testRemoveClass = function(valueObj) {
315 var $divs = jQuery('div');
317 $divs.addClass("test").removeClass( valueObj("test") );
319 ok( !$divs.is('.test'), "Remove Class" );
322 $divs = jQuery('div');
324 $divs.addClass("test").addClass("foo").addClass("bar");
325 $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
327 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
330 $divs = jQuery('div');
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" );
336 $divs.eq(0).addClass("test").removeClass( valueObj("") );
337 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
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" );
345 test("removeClass(String) - simple", function() {
346 testRemoveClass(bareObj);
349 test("removeClass(Function) - simple", function() {
350 testRemoveClass(functionReturningObj);
353 var testToggleClass = function(valueObj) {
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" );
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" );
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" );
379 // toggleClass storage
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" );
385 ok( !e.is(".testD.testE"), "Assert class not present" );
386 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
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" );
392 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
394 e.toggleClass(false);
396 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
401 e.removeClass("testD");
402 e.removeData('__className__');
405 test("toggleClass(String|boolean|undefined[, boolean])", function() {
406 testToggleClass(bareObj);
409 test("toggleClass(Function[, boolean])", function() {
410 testToggleClass(functionReturningObj);
413 var testRemoveAttr = function(valueObj) {
415 equals( jQuery('#mark').removeAttr( valueObj("class") )[0].className, "", "remove class" );
418 test("removeAttr(String)", function() {
419 testRemoveAttr(bareObj);
422 test("removeAttr(Function)", function() {
423 testRemoveAttr(functionReturningObj);
426 test("addClass, removeClass, hasClass", function() {
429 var jq = jQuery("<p>Hi</p>"), x = jq[0];
432 equals( x.className, "hi", "Check single added class" );
434 jq.addClass("foo bar");
435 equals( x.className, "hi foo bar", "Check more added classes" );
438 equals( x.className, "", "Remove all classes" );
440 jq.addClass("hi foo bar");
441 jq.removeClass("foo");
442 equals( x.className, "hi bar", "Check removal of one class" );
444 ok( jq.hasClass("hi"), "Check has1" );
445 ok( jq.hasClass("bar"), "Check has2" );
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" );
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" );