]> source.dussan.org Git - jquery.git/commitdiff
Core: organize prop & attr code to be similar
authorGilad Peleg <giladp007@gmail.com>
Wed, 10 Jun 2015 14:56:17 +0000 (17:56 +0300)
committerOleg Gaidarenko <markelog@gmail.com>
Tue, 23 Jun 2015 13:52:00 +0000 (16:52 +0300)
Closes gh-2384

src/attributes/attr.js
src/attributes/prop.js

index bec86c1f5ae30cebbb79e213e1d180d106eb4303..07576738a10dacee124bfa524a7ffa66b1b9b73e 100644 (file)
@@ -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;
-                               }
-                       }
-               }
        }
 });
 
index 58ed765113430a905a1e69ae4b2fe27d39c6c4ab..4c7b51e8c8fea821dda74f068e9f60d8be00f802 100644 (file)
@@ -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"
        }
 });