Fixed some bugs relating to the setter arg change in val and html. Also optimized...
[jquery.git] / src / attributes.js
index 794da70..1dd7084 100644 (file)
@@ -12,10 +12,27 @@ jQuery.fn.extend({
                return access( this, name, value, true, jQuery.attr );
        },
 
+       removeAttr: function( name ) {
+               if ( jQuery.isFunction( name ) ) {
+                       return this.each(function(i) {
+                               var self = jQuery(this);
+                               self.removeAttr( name.call(this, i, self.attr(name)) );
+                       });
+               }
+
+               return this.each(function(){
+                       jQuery.attr( this, name, "" );
+                       if ( this.nodeType === 1 ) {
+                               this.removeAttribute( name );
+                       }
+               });
+       },
+
        addClass: function( value ) {
                if ( jQuery.isFunction(value) ) {
-                       return this.each(function() {
-                               jQuery(this).addClass( value.call(this) );
+                       return this.each(function(i) {
+                               var self = jQuery(this);
+                               self.addClass( value.call(this, i, self.attr("class")) );
                        });
                }
 
@@ -46,8 +63,9 @@ jQuery.fn.extend({
 
        removeClass: function( value ) {
                if ( jQuery.isFunction(value) ) {
-                       return this.each(function() {
-                               jQuery(this).removeClass( value.call(this) );
+                       return this.each(function(i) {
+                               var self = jQuery(this);
+                               self.removeClass( value.call(this, i, self.attr("class")) );
                        });
                }
 
@@ -75,6 +93,41 @@ jQuery.fn.extend({
                return this;
        },
 
+       toggleClass: function( value, stateVal ) {
+               var type = typeof value, isBool = typeof stateVal === "boolean";
+
+               if ( jQuery.isFunction( value ) ) {
+                       return this.each(function(i) {
+                               var self = jQuery(this);
+                               self.toggleClass( value.call(this, i, self.attr("class")), stateVal );
+                       });
+               }
+
+               return this.each(function() {
+                       if ( type === "string" ) {
+                               // toggle individual class names
+                               var className, i = 0, self = jQuery(this),
+                                       state = stateVal,
+                                       classNames = value.split( rspace );
+
+                               while ( (className = classNames[ i++ ]) ) {
+                                       // check each className given, space seperated list
+                                       state = isBool ? state : !self.hasClass( className );
+                                       self[ state ? "addClass" : "removeClass" ]( className );
+                               }
+
+                       } else if ( type === "undefined" || type === "boolean" ) {
+                               if ( this.className ) {
+                                       // store className if set
+                                       jQuery.data( this, "__className__", this.className );
+                               }
+
+                               // toggle whole className
+                               this.className = this.className || value === false ? "" : jQuery.data( this, "__className__" ) || "";
+                       }
+               });
+       },
+
        hasClass: function( selector ) {
                var className = " " + selector + " ";
                for ( var i = 0, l = this.length; i < l; i++ ) {
@@ -142,38 +195,33 @@ jQuery.fn.extend({
                        return undefined;
                }
 
-               // Typecast once if the value is a number
-               if ( typeof value === "number" ) {
-                       value += "";
-               }
+               var isFunction = jQuery.isFunction(value);
 
-               var val = value;
+               return this.each(function(i) {
+                       var self = jQuery(this), val = value;
 
-               return this.each(function() {
-                       if ( jQuery.isFunction(value) ) {
-                               val = value.call(this);
+                       if ( this.nodeType !== 1 ) {
+                               return;
+                       }
 
-                               // Typecast each time if the value is a Function and the appended
-                               // value is therefore different each time.
-                               if ( typeof val === "number" ) {
-                                       val += "";
-                               }
+                       if ( isFunction ) {
+                               val = value.call(this, i, self.val());
                        }
 
-                       if ( this.nodeType !== 1 ) {
-                               return;
+                       // Typecast each time if the value is a Function and the appended
+                       // value is therefore different each time.
+                       if ( typeof val === "number" ) {
+                               val += "";
                        }
 
                        if ( jQuery.isArray(val) && rradiocheck.test( this.type ) ) {
-                               this.checked = jQuery.inArray( jQuery(this).val(), val ) >= 0;
+                               this.checked = jQuery.inArray( self.val(), val ) >= 0;
 
                        } else if ( jQuery.nodeName( this, "select" ) ) {
                                var values = jQuery.makeArray(val);
 
                                jQuery( "option", this ).each(function() {
-                                       // IE 6 will return "" for the value if one isn't specified, instead of the text
-                                       var node = this.getAttributeNode("value");
-                                       this.selected = jQuery.inArray( node && node.specified ? node.value : this.value || this.text, values ) >= 0;
+                                       this.selected = jQuery.inArray( jQuery(this).val(), values ) >= 0;
                                });
 
                                if ( !values.length ) {
@@ -187,50 +235,6 @@ jQuery.fn.extend({
        }
 });
 
-jQuery.each({
-       removeAttr: function( name ) {
-               jQuery.attr( this, name, "" );
-               if ( this.nodeType === 1 ) {
-                       this.removeAttribute( name );
-               }
-       },
-
-       toggleClass: function( classNames, state ) {
-               var type = typeof classNames;
-
-               if ( type === "string" ) {
-                       // toggle individual class names
-                       var isBool = typeof state === "boolean", className, i = 0,
-                               classNames = classNames.split( rspace );
-
-                       while ( (className = classNames[ i++ ]) ) {
-                               // check each className given, space seperated list
-                               state = isBool ? state : !jQuery(this).hasClass( className );
-                               jQuery(this)[ state ? "addClass" : "removeClass" ]( className );
-                       }
-
-               } else if ( type === "undefined" || type === "boolean" ) {
-                       if ( this.className ) {
-                               // store className if set
-                               jQuery.data( this, "__className__", this.className );
-                       }
-
-                       // toggle whole className
-                       this.className = this.className || classNames === false ? "" : jQuery.data( this, "__className__" ) || "";
-               }
-       }
-}, function( name, fn ) {
-       jQuery.fn[ name ] = function( val, state ) {
-               if ( jQuery.isFunction( val ) ) {
-                       return this.each(function() {
-                               jQuery(this)[ name ]( val.call(this), state );
-                       });
-               }
-
-               return this.each( fn, arguments );
-       };
-});
-
 jQuery.extend({
        attrFn: {
                val: true,
@@ -265,10 +269,18 @@ jQuery.extend({
                        // These attributes require special treatment
                        var special = rspecialurl.test( name );
 
-                       // Safari mis-reports the default selected property of a hidden option
+                       // Safari mis-reports the default selected property of an option
                        // Accessing the parent's selectedIndex property fixes it
-                       if ( name === "selected" && elem.parentNode ) {
-                               elem.parentNode.selectedIndex;
+                       if ( name === "selected" && !jQuery.support.optSelected ) {
+                               var parent = elem.parentNode;
+                               if ( parent ) {
+                                       parent.selectedIndex;
+       
+                                       // Make sure that it also works with optgroups, see #5701
+                                       if ( parent.parentNode ) {
+                                               parent.parentNode.selectedIndex;
+                                       }
+                               }
                        }
 
                        // If applicable, access the attribute via the DOM 0 way