From 5153b5334eb2c8317372b46209bd9d092a91afdc Mon Sep 17 00:00:00 2001 From: Gilad Peleg Date: Wed, 10 Jun 2015 17:56:17 +0300 Subject: [PATCH] Core: organize prop & attr code to be similar Closes gh-2384 --- src/attributes/attr.js | 70 ++++++++++++++++++++---------------------- src/attributes/prop.js | 40 +++++++++++++----------- 2 files changed, 56 insertions(+), 54 deletions(-) diff --git a/src/attributes/attr.js b/src/attributes/attr.js index bec86c1f5..07576738a 100644 --- a/src/attributes/attr.js +++ b/src/attributes/attr.js @@ -1,10 +1,10 @@ define([ "../core", - "../var/rnotwhite", "../core/access", "./support", + "../var/rnotwhite", "../selector" -], function( jQuery, rnotwhite, access, support ) { +], function( jQuery, access, support, rnotwhite ) { var boolHook, attrHandle = jQuery.expr.attrHandle; @@ -23,10 +23,10 @@ jQuery.fn.extend({ jQuery.extend({ attr: function( elem, name, value ) { - var hooks, ret, + var ret, hooks, nType = elem.nodeType; - // don't get/set attributes on text, comment and attribute nodes + // Don't get/set attributes on text, comment and attribute nodes if ( nType === 3 || nType === 8 || nType === 2 ) { return; } @@ -45,30 +45,43 @@ jQuery.extend({ } if ( value !== undefined ) { - if ( value === null ) { jQuery.removeAttr( elem, name ); + return; + } - } else if ( hooks && "set" in hooks && - (ret = hooks.set( elem, value, name )) !== undefined ) { - + if ( hooks && "set" in hooks && + ( ret = hooks.set( elem, value, name ) ) !== undefined ) { return ret; - - } else { - elem.setAttribute( name, value + "" ); - return value; } - } else if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) { + elem.setAttribute( name, value + "" ); + return value; + } + + if ( hooks && "get" in hooks && ( ret = hooks.get( elem, name ) ) !== null ) { return ret; + } - } else { - ret = jQuery.find.attr( elem, name ); + ret = jQuery.find.attr( elem, name ); - // Non-existent attributes return null, we normalize to undefined - return ret == null ? - undefined : - ret; + // Non-existent attributes return null, we normalize to undefined + return ret == null ? undefined : ret; + }, + + attrHooks: { + type: { + set: function( elem, value ) { + if ( !support.radioValue && value === "radio" && + jQuery.nodeName( elem, "input" ) ) { + var val = elem.value; + elem.setAttribute( "type", value ); + if ( val ) { + elem.value = val; + } + return value; + } + } } }, @@ -78,11 +91,12 @@ jQuery.extend({ attrNames = value && value.match( rnotwhite ); if ( attrNames && elem.nodeType === 1 ) { - while ( (name = attrNames[i++]) ) { + while ( ( name = attrNames[i++] ) ) { propName = jQuery.propFix[ name ] || name; // Boolean attributes get special treatment (#10870) if ( jQuery.expr.match.bool.test( name ) ) { + // Set corresponding property to false elem[ propName ] = false; } @@ -90,22 +104,6 @@ jQuery.extend({ elem.removeAttribute( name ); } } - }, - - attrHooks: { - type: { - set: function( elem, value ) { - if ( !support.radioValue && value === "radio" && - jQuery.nodeName( elem, "input" ) ) { - var val = elem.value; - elem.setAttribute( "type", value ); - if ( val ) { - elem.value = val; - } - return value; - } - } - } } }); diff --git a/src/attributes/prop.js b/src/attributes/prop.js index 58ed76511..4c7b51e8c 100644 --- a/src/attributes/prop.js +++ b/src/attributes/prop.js @@ -1,7 +1,8 @@ define([ "../core", "../core/access", - "./support" + "./support", + "../selector" ], function( jQuery, access, support ) { var rfocusable = /^(?:input|select|textarea|button)$/i; @@ -19,38 +20,36 @@ jQuery.fn.extend({ }); jQuery.extend({ - propFix: { - "for": "htmlFor", - "class": "className" - }, - prop: function( elem, name, value ) { - var ret, hooks, notxml, + var ret, hooks, nType = elem.nodeType; // Don't get/set properties on text, comment and attribute nodes - if ( !elem || nType === 3 || nType === 8 || nType === 2 ) { + if ( nType === 3 || nType === 8 || nType === 2 ) { return; } - notxml = nType !== 1 || !jQuery.isXMLDoc( elem ); + if ( nType !== 1 || !jQuery.isXMLDoc( elem ) ) { - if ( notxml ) { // Fix name and attach hooks name = jQuery.propFix[ name ] || name; hooks = jQuery.propHooks[ name ]; } if ( value !== undefined ) { - return hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ? - ret : - ( elem[ name ] = value ); - - } else { - return hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ? - ret : - elem[ name ]; + if ( hooks && "set" in hooks && + ( ret = hooks.set( elem, value, name ) ) !== undefined ) { + return ret; + } + + return ( elem[ name ] = value ); } + + if ( hooks && "get" in hooks && ( ret = hooks.get( elem, name ) ) !== null ) { + return ret; + } + + return elem[ name ]; }, propHooks: { @@ -62,6 +61,11 @@ jQuery.extend({ -1; } } + }, + + propFix: { + "for": "htmlFor", + "class": "className" } }); -- 2.39.5