diff --git a/src/attributes.js b/src/attributes.js index e5425a051..cb8128d25 100644 --- a/src/attributes.js +++ b/src/attributes.js @@ -36,10 +36,10 @@ jQuery.fn.extend({ }, addClass: function( value ) { - if ( jQuery.isFunction(value) ) { + if ( jQuery.isFunction( value ) ) { return this.each(function(i) { var self = jQuery(this); - self.addClass( value.call(this, i, self.attr("class")) ); + self.addClass( value.call(this, i, self.attr("class") || "") ); }); } @@ -278,19 +278,10 @@ jQuery.extend({ // TODO: Check to see if any of these are needed anymore? // If not, it may be good to standardize on all-lowercase names instead attrFix: { - "for": "htmlFor", - "class": "className", - readonly: "readOnly", - maxlength: "maxLength", - cellspacing: "cellSpacing", - rowspan: "rowSpan", - colspan: "colSpan", - tabindex: "tabIndex", - usemap: "useMap", - frameborder: "frameBorder" }, attr: function( elem, name, value, pass ) { + // don't get/set attributes on text, comment and attribute nodes if ( !elem || elem.nodeType === 3 || elem.nodeType === 8 || elem.nodeType === 2 ) { return undefined; @@ -314,6 +305,10 @@ jQuery.extend({ if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value )) !== undefined ) { return ret; + } else if ( value === null ) { + elem.removeAttribute( name ); + return undefined; + } else { // convert the value to a string (all browsers do this but IE) see #1070 value = "" + value; @@ -329,7 +324,7 @@ jQuery.extend({ return ret; } else { - + if ( !jQuery.hasAttr( elem, name ) ) { return undefined; } @@ -337,16 +332,14 @@ jQuery.extend({ var attr = elem.getAttribute( name ); // Non-existent attributes return null, we normalize to undefined - return attr === null ? - undefined : - attr; + return attr === null ? undefined : attr; } } }, hasAttr: function( elem, name ) { // Blackberry 4.7 returns "" from getAttribute #6938 - return elem.attributes[ name ] || (elem.hasAttribute && elem.hasAttribute( name )); + return elem && elem.attributes[ name ] || (elem.hasAttribute && elem.hasAttribute( name )); }, attrHooks: { @@ -361,7 +354,7 @@ jQuery.extend({ // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/ - tabIndex: { + tabindex: { get: function( elem ) { var attributeNode = elem.getAttributeNode( "tabIndex" ); @@ -375,10 +368,11 @@ jQuery.extend({ }, // TODO: Check to see if we really need any here. - propFix: {}, + propFix: { + }, prop: function( elem, name, value ) { - var ret, hooks; + var ret, hooks, notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ); // Try to normalize/fix the name name = notxml && jQuery.propFix[ name ] || name; @@ -406,6 +400,20 @@ jQuery.extend({ propHooks: {} }); +// Remove certain attrs if set to false +jQuery.each([ "selected", "checked", "readonly", "disabled" ], function( i, name ) { + jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], { + set: function( elem, value ) { + if ( !value ) { // '', undefined, false, null will remove attr + elem.removeAttribute( name ); + return false; + } + elem.setAttribute( name, value ); + return value; + } + }); +}); + // Some attributes require a special call on IE if ( !jQuery.support.hrefNormalized ) { jQuery.each([ "href", "src", "style" ], function( i, name ) { diff --git a/src/manipulation.js b/src/manipulation.js index 27f81cc24..81a55cb89 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -377,7 +377,7 @@ function cloneCopyEvent( src, dest ) { } } -function cloneFixAttributes(src, dest) { +function cloneFixAttributes( src, dest ) { // We do not need to do anything for non-Elements if ( dest.nodeType !== 1 ) { return; diff --git a/test/unit/attributes.js b/test/unit/attributes.js index 8cf47bed6..d4284556b 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -3,38 +3,64 @@ module("attributes", { teardown: moduleTeardown }); var bareObj = function(value) { return value; }; var functionReturningObj = function(value) { return (function() { return value; }); }; -test("jQuery.props: itegrity test", function() { - - expect(1); - - // This must be maintained and equal jQuery.props - // Ensure that accidental or erroneous property - // overwrites don't occur - // This is simply for better code coverage and future proofing. - var propsShouldBe = { - "for": "htmlFor", - "class": "className", - readonly: "readOnly", - maxlength: "maxLength", - cellspacing: "cellSpacing", - rowspan: "rowSpan", - colspan: "colSpan", - tabindex: "tabIndex", - usemap: "useMap", - frameborder: "frameBorder" - }; - - same(propsShouldBe, jQuery.props, "jQuery.props passes integrity check"); +// test("jQuery.props: integrity test", function() { +// +// expect(1); +// +// // This must be maintained and equal jQuery.props +// // Ensure that accidental or erroneous property +// // overwrites don't occur +// // This is simply for better code coverage and future proofing. +// var propsShouldBe = { +// "for": "htmlFor", +// "class": "className", +// readonly: "readOnly", +// maxlength: "maxLength", +// cellspacing: "cellSpacing", +// rowspan: "rowSpan", +// colspan: "colSpan", +// tabindex: "tabIndex", +// usemap: "useMap", +// frameborder: "frameBorder" +// }; +// +// same(propsShouldBe, jQuery.props, "jQuery.props passes integrity check"); +// +// }); +test("prop", function() { + equals( jQuery('#text1').prop('value'), "Test", 'Check for value attribute' ); + equals( jQuery('#text1').prop('value', "Test2").prop('defaultValue'), "Test", 'Check for defaultValue attribute' ); + equals( jQuery('#select2').prop('selectedIndex'), 3, 'Check for selectedIndex attribute' ); + equals( jQuery('#foo').prop('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' ); + equals( jQuery('#foo').prop('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' ); + equals( jQuery("