Bug Tracker

Changeset 5574

Show
Ignore:
Timestamp:
05/13/08 00:37:30 (7 months ago)
Author:
aflesler
Message:

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)

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/jquery/src/core.js

    r5558 r5574  
    155155        // Look for the case where we're accessing a style value 
    156156        if ( name.constructor == String ) 
    157             if ( value == undefined ) 
    158                 return this.length && jQuery[ type || "attr" ]( this[0], name ) || undefined; 
     157            if ( value === undefined ) 
     158                return this[0] && jQuery[ type || "attr" ]( this[0], name ); 
    159159 
    160160            else { 
     
    10291029        return ret; 
    10301030    }, 
    1031      
     1031 
    10321032    attr: function( elem, name, value ) { 
    10331033        // don't set attributes on text and comment nodes 
     
    10351035            return undefined; 
    10361036 
    1037         var fix = jQuery.isXMLDoc( elem ) ? 
    1038             {} : 
    1039             jQuery.props; 
    1040  
    1041         // Safari mis-reports the default selected property of a hidden option 
    1042         // Accessing the parent's selectedIndex property fixes it 
    1043         if ( name == "selected" && jQuery.browser.safari ) 
    1044             elem.parentNode.selectedIndex; 
    1045          
    1046         // Certain attributes only work when accessed via the old DOM 0 way 
    1047         if ( fix[ name ] ) { 
    1048             if ( value != undefined ) 
    1049                 elem[ fix[ name ] ] = value; 
    1050  
    1051             return elem[ fix[ name ] ]; 
    1052  
    1053         } else if ( jQuery.browser.msie && name == "style" ) 
    1054             return jQuery.attr( elem.style, "cssText", value ); 
    1055  
    1056         else if ( value == undefined && jQuery.browser.msie && jQuery.nodeName( elem, "form" ) && (name == "action" || name == "method") ) 
    1057             return elem.getAttributeNode( name ).nodeValue; 
    1058  
     1037        var notxml = !jQuery.isXMLDoc( elem ), 
     1038            // Whether we are setting (or getting) 
     1039            set = value !== undefined, 
     1040            msie = jQuery.browser.msie; 
     1041 
     1042        // Try to normalize/fix the name 
     1043        name = notxml && jQuery.props[ name ] || name; 
     1044 
     1045        // Only do all the following if this is a node (faster for style) 
    10591046        // IE elem.getAttribute passes even for style 
    1060         else if ( elem.tagName ) { 
    1061  
    1062             if ( value != undefined ) { 
    1063                 // We can't allow the type property to be changed (since it causes problems in IE) 
    1064                 if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode ) 
    1065                     throw "type property can't be changed"; 
    1066  
     1047        if ( elem.tagName ) { 
     1048 
     1049            // These attributes require special treatment 
     1050            var special = /href|src|style/.test( name ); 
     1051 
     1052            // Safari mis-reports the default selected property of a hidden option 
     1053            // Accessing the parent's selectedIndex property fixes it 
     1054            if ( name == "selected" && jQuery.browser.safari ) 
     1055                elem.parentNode.selectedIndex; 
     1056 
     1057            // If applicable, access the attribute via the DOM 0 way 
     1058            if ( notxml && !special && name in elem ) { 
     1059                if ( set ){ 
     1060                    // We can't allow the type property to be changed (since it causes problems in IE) 
     1061                    if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode ) 
     1062                        throw "type property can't be changed"; 
     1063 
     1064                    elem[ name ] = value; 
     1065                } 
     1066 
     1067                // browsers index elements by id/name on forms, give priority to attributes. 
     1068                if( jQuery.nodeName( elem, "form" ) && elem.getAttributeNode(name) ) 
     1069                    return elem.getAttributeNode( name ).nodeValue; 
     1070 
     1071                return elem[ name ]; 
     1072            } 
     1073 
     1074            if ( msie && name == "style" ) 
     1075                return jQuery.attr( elem.style, "cssText", value ); 
     1076 
     1077            if ( set ) 
    10671078                // convert the value to a string (all browsers do this but IE) see #1070 
    10681079                elem.setAttribute( name, "" + value ); 
     1080 
     1081            if ( msie && special && notxml )  
     1082                return elem.getAttribute( name, 2 ); 
     1083 
     1084            return elem.getAttribute( name ); 
     1085 
     1086        } 
     1087 
     1088        // elem is actually elem.style ... set the style 
     1089 
     1090        // IE uses filters for opacity 
     1091        if ( msie && name == "opacity" ) { 
     1092            if ( set ) { 
     1093                // IE has trouble with opacity if it does not have layout 
     1094                // Force it by setting the zoom level 
     1095                elem.zoom = 1;  
     1096 
     1097                // Set the alpha filter to set the opacity 
     1098                elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) + 
     1099                    (parseInt( value ) + '' == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"); 
    10691100            } 
    10701101 
    1071             if ( jQuery.browser.msie && /href|src/.test( name ) && !jQuery.isXMLDoc( elem ) )  
    1072                 return elem.getAttribute( name, 2 ); 
    1073  
    1074             return elem.getAttribute( name ); 
    1075  
    1076         // elem is actually elem.style ... set the style 
    1077         } else { 
    1078             // IE actually uses filters for opacity 
    1079             if ( name == "opacity" && jQuery.browser.msie ) { 
    1080                 if ( value != undefined ) { 
    1081                     // IE has trouble with opacity if it does not have layout 
    1082                     // Force it by setting the zoom level 
    1083                     elem.zoom = 1;  
    1084      
    1085                     // Set the alpha filter to set the opacity 
    1086                     elem.filter = (elem.filter || "").replace( /alpha\([^)]*\)/, "" ) + 
    1087                         (parseFloat( value ).toString() == "NaN" ? "" : "alpha(opacity=" + value * 100 + ")"); 
    1088                 } 
    1089      
    1090                 return elem.filter && elem.filter.indexOf("opacity=") >= 0 ? 
    1091                     (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100).toString() : 
    1092                     ""; 
    1093             } 
    1094  
    1095             name = name.replace(/-([a-z])/ig, function(all, letter){ 
    1096                 return letter.toUpperCase(); 
    1097             }); 
    1098  
    1099             if ( value != undefined ) 
    1100                 elem[ name ] = value; 
    1101  
    1102             return elem[ name ]; 
    1103         } 
     1102            return elem.filter && elem.filter.indexOf("opacity=") >= 0 ? 
     1103                (parseFloat( elem.filter.match(/opacity=([^)]*)/)[1] ) / 100) + '': 
     1104                ""; 
     1105        } 
     1106 
     1107        name = name.replace(/-([a-z])/ig, function(all, letter){ 
     1108            return letter.toUpperCase(); 
     1109        }); 
     1110 
     1111        if ( set ) 
     1112            elem[ name ] = value; 
     1113 
     1114        return elem[ name ]; 
    11041115    }, 
    11051116     
     
    12251236        cssFloat: styleFloat, 
    12261237        styleFloat: styleFloat, 
    1227         innerHTML: "innerHTML", 
    1228         className: "className", 
    1229         value: "value", 
    1230         disabled: "disabled", 
    1231         checked: "checked", 
    12321238        readonly: "readOnly", 
    1233         selected: "selected", 
    12341239        maxlength: "maxLength", 
    1235         selectedIndex: "selectedIndex", 
    1236         defaultValue: "defaultValue", 
    1237         tagName: "tagName", 
    1238         nodeName: "nodeName" 
     1240        cellspacing: "cellSpacing" 
    12391241    } 
    12401242});