Core: organize prop & attr code to be similar

Closes gh-2384
This commit is contained in:
Gilad Peleg
2015-06-10 17:56:17 +03:00
committed by Oleg Gaidarenko
parent 90d828bad0
commit 5153b5334e
2 changed files with 58 additions and 56 deletions

View 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,51 +45,28 @@ 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 );
// Non-existent attributes return null, we normalize to undefined
return ret == null ?
undefined :
ret;
}
},
removeAttr: function( elem, value ) {
var name, propName,
i = 0,
attrNames = value && value.match( rnotwhite );
ret = jQuery.find.attr( elem, name );
if ( attrNames && elem.nodeType === 1 ) {
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 );
}
}
// Non-existent attributes return null, we normalize to undefined
return ret == null ? undefined : ret;
},
attrHooks: {
@@ -106,6 +83,27 @@ jQuery.extend({
}
}
}
},
removeAttr: function( elem, value ) {
var name, propName,
i = 0,
attrNames = value && value.match( rnotwhite );
if ( attrNames && elem.nodeType === 1 ) {
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 );
}
}
}
});

View 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 );
if ( hooks && "set" in hooks &&
( ret = hooks.set( elem, value, name ) ) !== undefined ) {
return ret;
}
} else {
return hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ?
ret :
elem[ name ];
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"
}
});