Permalink
Browse files

Core: organize prop & attr code to be similar

Closes gh-2384
  • Loading branch information...
pgilad authored and markelog committed Jun 10, 2015
1 parent 90d828b commit 5153b5334eb2c8317372b46209bd9d092a91afdc
Showing with 56 additions and 54 deletions.
  1. +34 −36 src/attributes/attr.js
  2. +22 −18 src/attributes/prop.js
View
@@ -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,34 +91,19 @@ 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;
}
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;
}
}
}
}
});
View
@@ -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"
}
});

0 comments on commit 5153b53

Please sign in to comment.