Permalink
Browse files

jquery core: #2548, #2170, #2188, #2099, #1170, #2558, #2521, #2119, #…

…1271, #2453, #2537.

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)
  • Loading branch information...
flesler committed May 13, 2008
1 parent 3739459 commit e9b2781d30a325f835fbf7c0043cf855cbb6edfa
Showing with 63 additions and 61 deletions.
  1. +63 −61 src/core.js
View
@@ -154,8 +154,8 @@ jQuery.fn = jQuery.prototype = {
// Look for the case where we're accessing a style value
if ( name.constructor == String )
- if ( value == undefined )
- return this.length && jQuery[ type || "attr" ]( this[0], name ) || undefined;
+ if ( value === undefined )
+ return this[0] && jQuery[ type || "attr" ]( this[0], name );
else {
options = {};
@@ -1028,79 +1028,90 @@ jQuery.extend({
return ret;
},
-
+
attr: function( elem, name, value ) {
// don't set attributes on text and comment nodes
if (!elem || elem.nodeType == 3 || elem.nodeType == 8)
return undefined;
- var fix = jQuery.isXMLDoc( elem ) ?
- {} :
- jQuery.props;
+ var notxml = !jQuery.isXMLDoc( elem ),
+ // Whether we are setting (or getting)
+ set = value !== undefined,
+ msie = jQuery.browser.msie;
- // Safari mis-reports the default selected property of a hidden option
- // Accessing the parent's selectedIndex property fixes it
- 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;
+ // Try to normalize/fix the name
+ name = notxml && jQuery.props[ name ] || name;
- return elem[ fix[ name ] ];
+ // Only do all the following if this is a node (faster for style)
+ // IE elem.getAttribute passes even for style
+ if ( elem.tagName ) {
- } else if ( jQuery.browser.msie && name == "style" )
- return jQuery.attr( elem.style, "cssText", value );
+ // These attributes require special treatment
+ var special = /href|src|style/.test( name );
- else if ( value == undefined && jQuery.browser.msie && jQuery.nodeName( elem, "form" ) && (name == "action" || name == "method") )
- return elem.getAttributeNode( name ).nodeValue;
+ // Safari mis-reports the default selected property of a hidden option
+ // Accessing the parent's selectedIndex property fixes it
+ if ( name == "selected" && jQuery.browser.safari )
+ elem.parentNode.selectedIndex;
- // IE elem.getAttribute passes even for style
- else if ( elem.tagName ) {
+ // 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;
+ }
- if ( value != undefined ) {
- // 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";
+ // 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 ( 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 ( jQuery.browser.msie && /href|src/.test( name ) && !jQuery.isXMLDoc( elem ) )
+ if ( msie && special && notxml )
return elem.getAttribute( name, 2 );
return elem.getAttribute( name );
+ }
+
// 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
- elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) +
- (parseFloat( value ).toString() == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
- }
-
- return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
- (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100).toString() :
- "";
- }
- name = name.replace(/-([a-z])/ig, function(all, letter){
- return letter.toUpperCase();
- });
+ // IE uses filters for opacity
+ if ( msie && name == "opacity" ) {
+ if ( set ) {
+ // IE has trouble with opacity if it does not have layout
+ // Force it by setting the zoom level
+ elem.zoom = 1;
- if ( value != undefined )
- elem[ name ] = value;
+ // Set the alpha filter to set the opacity
+ elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) +
+ (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")");
+ }
- return elem[ name ];
+ return elem.filter && elem.filter.indexOf("opacity=") >= 0 ?
+ (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '':
+ "";
}
+
+ name = name.replace(/-([a-z])/ig, function(all, letter){
+ return letter.toUpperCase();
+ });
+
+ if ( set )
+ elem[ name ] = value;
+
+ return elem[ name ];
},
trim: function( text ) {
@@ -1224,18 +1235,9 @@ jQuery.extend({
"float": styleFloat,
cssFloat: styleFloat,
styleFloat: styleFloat,
- innerHTML: "innerHTML",
- className: "className",
- value: "value",
- disabled: "disabled",
- checked: "checked",
readonly: "readOnly",
- selected: "selected",
maxlength: "maxLength",
- selectedIndex: "selectedIndex",
- defaultValue: "defaultValue",
- tagName: "tagName",
- nodeName: "nodeName"
+ cellspacing: "cellSpacing"
}
});

0 comments on commit e9b2781

Please sign in to comment.