mass refactoring of $.attr (#2548) * Changes - undefined was returned for falsy values. - expando attributes are used when possible. - one $.isXmlDoc instead of 2. - $.attr( style, ... ) goes thru less useless checks. - reduced code size of recurrent accessed variables/checks. - $.props doesn't need redundant sets of key value anymore. - added cellSpacing to $.props (#2521)
This commit is contained in:
parent
37394598cb
commit
e9b2781d30
1 changed files with 67 additions and 65 deletions
126
src/core.js
126
src/core.js
|
@ -154,8 +154,8 @@ jQuery.fn = jQuery.prototype = {
|
||||||
|
|
||||||
// Look for the case where we're accessing a style value
|
// Look for the case where we're accessing a style value
|
||||||
if ( name.constructor == String )
|
if ( name.constructor == String )
|
||||||
if ( value == undefined )
|
if ( value === undefined )
|
||||||
return this.length && jQuery[ type || "attr" ]( this[0], name ) || undefined;
|
return this[0] && jQuery[ type || "attr" ]( this[0], name );
|
||||||
|
|
||||||
else {
|
else {
|
||||||
options = {};
|
options = {};
|
||||||
|
@ -1034,73 +1034,84 @@ jQuery.extend({
|
||||||
if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
|
if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|
||||||
var fix = jQuery.isXMLDoc( elem ) ?
|
var notxml = !jQuery.isXMLDoc( elem ),
|
||||||
{} :
|
// Whether we are setting (or getting)
|
||||||
jQuery.props;
|
set = value !== undefined,
|
||||||
|
msie = jQuery.browser.msie;
|
||||||
|
|
||||||
// Safari mis-reports the default selected property of a hidden option
|
// Try to normalize/fix the name
|
||||||
// Accessing the parent's selectedIndex property fixes it
|
name = notxml && jQuery.props[ name ] || name;
|
||||||
if ( name == "selected" && jQuery.browser.safari )
|
|
||||||
elem.parentNode.selectedIndex;
|
|
||||||
|
|
||||||
// Certain attributes only work when accessed via the old DOM 0 way
|
|
||||||
if ( fix[ name ] ) {
|
|
||||||
if ( value != undefined )
|
|
||||||
elem[ fix[ name ] ] = value;
|
|
||||||
|
|
||||||
return elem[ fix[ name ] ];
|
|
||||||
|
|
||||||
} else if ( jQuery.browser.msie && name == "style" )
|
|
||||||
return jQuery.attr( elem.style, "cssText", value );
|
|
||||||
|
|
||||||
else if ( value == undefined && jQuery.browser.msie && jQuery.nodeName( elem, "form" ) && (name == "action" || name == "method") )
|
|
||||||
return elem.getAttributeNode( name ).nodeValue;
|
|
||||||
|
|
||||||
|
// Only do all the following if this is a node (faster for style)
|
||||||
// IE elem.getAttribute passes even for style
|
// IE elem.getAttribute passes even for style
|
||||||
else if ( elem.tagName ) {
|
if ( elem.tagName ) {
|
||||||
|
|
||||||
if ( value != undefined ) {
|
// These attributes require special treatment
|
||||||
// We can't allow the type property to be changed (since it causes problems in IE)
|
var special = /href|src|style/.test( name );
|
||||||
if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
|
|
||||||
throw "type property can't be changed";
|
|
||||||
|
|
||||||
// convert the value to a string (all browsers do this but IE) see #1070
|
// Safari mis-reports the default selected property of a hidden option
|
||||||
elem.setAttribute( name, "" + value );
|
// Accessing the parent's selectedIndex property fixes it
|
||||||
|
if ( name == "selected" && jQuery.browser.safari )
|
||||||
|
elem.parentNode.selectedIndex;
|
||||||
|
|
||||||
|
// If applicable, access the attribute via the DOM 0 way
|
||||||
|
if ( notxml && !special && name in elem ) {
|
||||||
|
if ( set ){
|
||||||
|
// We can't allow the type property to be changed (since it causes problems in IE)
|
||||||
|
if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
|
||||||
|
throw "type property can't be changed";
|
||||||
|
|
||||||
|
elem[ name ] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
// browsers index elements by id/name on forms, give priority to attributes.
|
||||||
|
if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) )
|
||||||
|
return elem.getAttributeNode( name ).nodeValue;
|
||||||
|
|
||||||
|
return elem[ name ];
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( jQuery.browser.msie && /href|src/.test( name ) && !jQuery.isXMLDoc( elem ) )
|
if ( msie && name == "style" )
|
||||||
|
return jQuery.attr( elem.style, "cssText", value );
|
||||||
|
|
||||||
|
if ( set )
|
||||||
|
// convert the value to a string (all browsers do this but IE) see #1070
|
||||||
|
elem.setAttribute( name, "" + value );
|
||||||
|
|
||||||
|
if ( msie && special && notxml )
|
||||||
return elem.getAttribute( name, 2 );
|
return elem.getAttribute( name, 2 );
|
||||||
|
|
||||||
return elem.getAttribute( name );
|
return elem.getAttribute( name );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// elem is actually elem.style ... set the style
|
// elem is actually elem.style ... set the style
|
||||||
} else {
|
|
||||||
// IE actually uses filters for opacity
|
|
||||||
if ( name == "opacity" && jQuery.browser.msie ) {
|
|
||||||
if ( value != undefined ) {
|
|
||||||
// IE has trouble with opacity if it does not have layout
|
|
||||||
// Force it by setting the zoom level
|
|
||||||
elem.zoom = 1;
|
|
||||||
|
|
||||||
// Set the alpha filter to set the opacity
|
// IE uses filters for opacity
|
||||||
elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) +
|
if ( msie && name == "opacity" ) {
|
||||||
(parseFloat( value ).toString() == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
|
if ( set ) {
|
||||||
}
|
// IE has trouble with opacity if it does not have layout
|
||||||
|
// Force it by setting the zoom level
|
||||||
|
elem.zoom = 1;
|
||||||
|
|
||||||
return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
|
// Set the alpha filter to set the opacity
|
||||||
(parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100).toString() :
|
elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) +
|
||||||
"";
|
(parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
name = name.replace(/-([a-z])/ig, function(all, letter){
|
return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
|
||||||
return letter.toUpperCase();
|
(parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '':
|
||||||
});
|
"";
|
||||||
|
|
||||||
if ( value != undefined )
|
|
||||||
elem[ name ] = value;
|
|
||||||
|
|
||||||
return elem[ name ];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
name = name.replace(/-([a-z])/ig, function(all, letter){
|
||||||
|
return letter.toUpperCase();
|
||||||
|
});
|
||||||
|
|
||||||
|
if ( set )
|
||||||
|
elem[ name ] = value;
|
||||||
|
|
||||||
|
return elem[ name ];
|
||||||
},
|
},
|
||||||
|
|
||||||
trim: function( text ) {
|
trim: function( text ) {
|
||||||
|
@ -1224,18 +1235,9 @@ jQuery.extend({
|
||||||
"float": styleFloat,
|
"float": styleFloat,
|
||||||
cssFloat: styleFloat,
|
cssFloat: styleFloat,
|
||||||
styleFloat: styleFloat,
|
styleFloat: styleFloat,
|
||||||
innerHTML: "innerHTML",
|
|
||||||
className: "className",
|
|
||||||
value: "value",
|
|
||||||
disabled: "disabled",
|
|
||||||
checked: "checked",
|
|
||||||
readonly: "readOnly",
|
readonly: "readOnly",
|
||||||
selected: "selected",
|
|
||||||
maxlength: "maxLength",
|
maxlength: "maxLength",
|
||||||
selectedIndex: "selectedIndex",
|
cellspacing: "cellSpacing"
|
||||||
defaultValue: "defaultValue",
|
|
||||||
tagName: "tagName",
|
|
||||||
nodeName: "nodeName"
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue