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 randomally ?!
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('addClass', 'css');
200 equals( elem.className, 'css', 'attr(addClass)');
202 $elem.attr('removeClass', 'css');
203 equals( jQuery.trim(elem.className), '', 'attr(removeClass)');
205 $elem.attr('css', {color:'red'});
206 ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
208 $elem.attr('height', 10);
209 equals( elem.style.height, '10px', 'attr(height)');
211 $elem.attr('each', function(){
213 ok(true, 'attr(each)');
217 // Multiple attributes
221 css:{ paddingLeft:1, paddingRight:1 }
224 equals( elem.style.width, '10px', 'attr({...})');
225 equals( elem.style.paddingLeft, '1px', 'attr({...})');
226 equals( elem.style.paddingRight, '1px', 'attr({...})');
230 test("attr(String, Object) - Loaded via XML document", function() {
233 jQuery.get('data/dashboard.xml', function(xml) {
235 jQuery('tab', xml).each(function() {
236 titles.push(jQuery(this).attr('title'));
238 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
239 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
245 test("attr('tabindex')", function() {
248 // elements not natively tabbable
249 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
250 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
253 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
254 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
255 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
257 // anchor without href
258 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
259 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
260 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
263 test("attr('tabindex', value)", function() {
266 var element = jQuery('#divWithNoTabIndex');
267 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
269 // set a positive string
270 element.attr('tabindex', '1');
271 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
274 element.attr('tabindex', '0');
275 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
277 // set a negative string
278 element.attr('tabindex', '-1');
279 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
281 // set a positive number
282 element.attr('tabindex', 1);
283 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
286 element.attr('tabindex', 0);
287 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
289 // set a negative number
290 element.attr('tabindex', -1);
291 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
293 element = jQuery('#linkWithTabIndex');
294 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
296 element.attr('tabindex', -1);
297 equals(element.attr('tabindex'), -1, 'set negative tabindex');
300 var testAddClass = function(valueObj) {
302 var div = jQuery("div");
303 div.addClass( valueObj("test") );
305 for ( var i = 0; i < div.size(); i++ ) {
306 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
308 ok( pass, "Add Class" );
310 // using contents will get regular, text, and comment nodes
311 var j = jQuery("#nonnodes").contents();
312 j.addClass( valueObj("asdf") );
313 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
316 test("addClass(String)", function() {
317 testAddClass(bareObj);
320 test("addClass(Function)", function() {
321 testAddClass(functionReturningObj);
324 var testRemoveClass = function(valueObj) {
327 var $divs = jQuery('div');
329 $divs.addClass("test").removeClass( valueObj("test") );
331 ok( !$divs.is('.test'), "Remove Class" );
334 $divs = jQuery('div');
336 $divs.addClass("test").addClass("foo").addClass("bar");
337 $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
339 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
342 $divs = jQuery('div');
344 // Make sure that a null value doesn't cause problems
345 $divs.eq(0).addClass("test").removeClass( valueObj(null) );
346 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
348 $divs.eq(0).addClass("test").removeClass( valueObj("") );
349 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
351 // using contents will get regular, text, and comment nodes
352 var j = jQuery("#nonnodes").contents();
353 j.removeClass( valueObj("asdf") );
354 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
357 test("removeClass(String) - simple", function() {
358 testRemoveClass(bareObj);
361 test("removeClass(Function) - simple", function() {
362 testRemoveClass(functionReturningObj);
365 var testToggleClass = function(valueObj) {
368 var e = jQuery("#firstp");
369 ok( !e.is(".test"), "Assert class not present" );
370 e.toggleClass( valueObj("test") );
371 ok( e.is(".test"), "Assert class present" );
372 e.toggleClass( valueObj("test") );
373 ok( !e.is(".test"), "Assert class not present" );
375 // class name with a boolean
376 e.toggleClass( valueObj("test"), false );
377 ok( !e.is(".test"), "Assert class not present" );
378 e.toggleClass( valueObj("test"), true );
379 ok( e.is(".test"), "Assert class present" );
380 e.toggleClass( valueObj("test"), false );
381 ok( !e.is(".test"), "Assert class not present" );
383 // multiple class names
384 e.addClass("testA testB");
385 ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
386 e.toggleClass( valueObj("testB testC") );
387 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
388 e.toggleClass( valueObj("testA testC") );
389 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
391 // toggleClass storage
393 ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
394 e.addClass("testD testE");
395 ok( e.is(".testD.testE"), "Assert class present" );
397 ok( !e.is(".testD.testE"), "Assert class not present" );
398 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
400 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
401 e.toggleClass(false);
402 ok( !e.is(".testD.testE"), "Assert class not present" );
404 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
406 e.toggleClass(false);
408 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
413 e.removeClass("testD");
414 e.removeData('__className__');
417 test("toggleClass(String|boolean|undefined[, boolean])", function() {
418 testToggleClass(bareObj);
421 test("toggleClass(Function[, boolean])", function() {
422 testToggleClass(functionReturningObj);
425 var testRemoveAttr = function(valueObj) {
427 equals( jQuery('#mark').removeAttr( valueObj("class") )[0].className, "", "remove class" );
430 test("removeAttr(String)", function() {
431 testRemoveAttr(bareObj);
434 test("removeAttr(Function)", function() {
435 testRemoveAttr(functionReturningObj);
438 test("addClass, removeClass, hasClass", function() {
441 var jq = jQuery("<p>Hi</p>"), x = jq[0];
444 equals( x.className, "hi", "Check single added class" );
446 jq.addClass("foo bar");
447 equals( x.className, "hi foo bar", "Check more added classes" );
450 equals( x.className, "", "Remove all classes" );
452 jq.addClass("hi foo bar");
453 jq.removeClass("foo");
454 equals( x.className, "hi bar", "Check removal of one class" );
456 ok( jq.hasClass("hi"), "Check has1" );
457 ok( jq.hasClass("bar"), "Check has2" );