Skip to content

Commit 5153b53

Browse files
pgiladmarkelog
authored andcommitted
Core: organize prop & attr code to be similar
Closes gh-2384
1 parent 90d828b commit 5153b53

File tree

2 files changed

+56
-54
lines changed

2 files changed

+56
-54
lines changed

src/attributes/attr.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
define([
22
"../core",
3-
"../var/rnotwhite",
43
"../core/access",
54
"./support",
5+
"../var/rnotwhite",
66
"../selector"
7-
], function( jQuery, rnotwhite, access, support ) {
7+
], function( jQuery, access, support, rnotwhite ) {
88

99
var boolHook,
1010
attrHandle = jQuery.expr.attrHandle;
@@ -23,10 +23,10 @@ jQuery.fn.extend({
2323

2424
jQuery.extend({
2525
attr: function( elem, name, value ) {
26-
var hooks, ret,
26+
var ret, hooks,
2727
nType = elem.nodeType;
2828

29-
// don't get/set attributes on text, comment and attribute nodes
29+
// Don't get/set attributes on text, comment and attribute nodes
3030
if ( nType === 3 || nType === 8 || nType === 2 ) {
3131
return;
3232
}
@@ -45,30 +45,43 @@ jQuery.extend({
4545
}
4646

4747
if ( value !== undefined ) {
48-
4948
if ( value === null ) {
5049
jQuery.removeAttr( elem, name );
50+
return;
51+
}
5152

52-
} else if ( hooks && "set" in hooks &&
53-
(ret = hooks.set( elem, value, name )) !== undefined ) {
54-
53+
if ( hooks && "set" in hooks &&
54+
( ret = hooks.set( elem, value, name ) ) !== undefined ) {
5555
return ret;
56-
57-
} else {
58-
elem.setAttribute( name, value + "" );
59-
return value;
6056
}
6157

62-
} else if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ) {
58+
elem.setAttribute( name, value + "" );
59+
return value;
60+
}
61+
62+
if ( hooks && "get" in hooks && ( ret = hooks.get( elem, name ) ) !== null ) {
6363
return ret;
64+
}
6465

65-
} else {
66-
ret = jQuery.find.attr( elem, name );
66+
ret = jQuery.find.attr( elem, name );
6767

68-
// Non-existent attributes return null, we normalize to undefined
69-
return ret == null ?
70-
undefined :
71-
ret;
68+
// Non-existent attributes return null, we normalize to undefined
69+
return ret == null ? undefined : ret;
70+
},
71+
72+
attrHooks: {
73+
type: {
74+
set: function( elem, value ) {
75+
if ( !support.radioValue && value === "radio" &&
76+
jQuery.nodeName( elem, "input" ) ) {
77+
var val = elem.value;
78+
elem.setAttribute( "type", value );
79+
if ( val ) {
80+
elem.value = val;
81+
}
82+
return value;
83+
}
84+
}
7285
}
7386
},
7487

@@ -78,34 +91,19 @@ jQuery.extend({
7891
attrNames = value && value.match( rnotwhite );
7992

8093
if ( attrNames && elem.nodeType === 1 ) {
81-
while ( (name = attrNames[i++]) ) {
94+
while ( ( name = attrNames[i++] ) ) {
8295
propName = jQuery.propFix[ name ] || name;
8396

8497
// Boolean attributes get special treatment (#10870)
8598
if ( jQuery.expr.match.bool.test( name ) ) {
99+
86100
// Set corresponding property to false
87101
elem[ propName ] = false;
88102
}
89103

90104
elem.removeAttribute( name );
91105
}
92106
}
93-
},
94-
95-
attrHooks: {
96-
type: {
97-
set: function( elem, value ) {
98-
if ( !support.radioValue && value === "radio" &&
99-
jQuery.nodeName( elem, "input" ) ) {
100-
var val = elem.value;
101-
elem.setAttribute( "type", value );
102-
if ( val ) {
103-
elem.value = val;
104-
}
105-
return value;
106-
}
107-
}
108-
}
109107
}
110108
});
111109

src/attributes/prop.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
define([
22
"../core",
33
"../core/access",
4-
"./support"
4+
"./support",
5+
"../selector"
56
], function( jQuery, access, support ) {
67

78
var rfocusable = /^(?:input|select|textarea|button)$/i;
@@ -19,38 +20,36 @@ jQuery.fn.extend({
1920
});
2021

2122
jQuery.extend({
22-
propFix: {
23-
"for": "htmlFor",
24-
"class": "className"
25-
},
26-
2723
prop: function( elem, name, value ) {
28-
var ret, hooks, notxml,
24+
var ret, hooks,
2925
nType = elem.nodeType;
3026

3127
// Don't get/set properties on text, comment and attribute nodes
32-
if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
28+
if ( nType === 3 || nType === 8 || nType === 2 ) {
3329
return;
3430
}
3531

36-
notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
32+
if ( nType !== 1 || !jQuery.isXMLDoc( elem ) ) {
3733

38-
if ( notxml ) {
3934
// Fix name and attach hooks
4035
name = jQuery.propFix[ name ] || name;
4136
hooks = jQuery.propHooks[ name ];
4237
}
4338

4439
if ( value !== undefined ) {
45-
return hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ?
46-
ret :
47-
( elem[ name ] = value );
48-
49-
} else {
50-
return hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== null ?
51-
ret :
52-
elem[ name ];
40+
if ( hooks && "set" in hooks &&
41+
( ret = hooks.set( elem, value, name ) ) !== undefined ) {
42+
return ret;
43+
}
44+
45+
return ( elem[ name ] = value );
5346
}
47+
48+
if ( hooks && "get" in hooks && ( ret = hooks.get( elem, name ) ) !== null ) {
49+
return ret;
50+
}
51+
52+
return elem[ name ];
5453
},
5554

5655
propHooks: {
@@ -62,6 +61,11 @@ jQuery.extend({
6261
-1;
6362
}
6463
}
64+
},
65+
66+
propFix: {
67+
"for": "htmlFor",
68+
"class": "className"
6569
}
6670
});
6771

0 commit comments

Comments
 (0)