]> source.dussan.org Git - jquery.git/commitdiff
Merge branch 'modest_val_proposal' of https://github.com/timmywil/jquery into timmywi...
authorjeresig <jeresig@gmail.com>
Sun, 10 Apr 2011 20:56:56 +0000 (16:56 -0400)
committerjeresig <jeresig@gmail.com>
Sun, 10 Apr 2011 20:56:56 +0000 (16:56 -0400)
Conflicts:
src/attributes.js

1  2 
src/attributes.js

index f7c2e981871b86901c16bd998d95c67eacafd36d,9fdb1492d9c7190b0c930075a299800fc7c6d600..7868e08ed6f247fcb95c61ef10b84db6a88b2dd8
@@@ -3,11 -3,23 +3,10 @@@
  var rclass = /[\n\t\r]/g,
        rspaces = /\s+/,
        rreturn = /\r/g,
 -      rspecialurl = /^(?:href|src|style)$/,
        rtype = /^(?:button|input)$/i,
        rfocusable = /^(?:button|input|object|select|textarea)$/i,
 -      rclickable = /^a(?:rea)?$/i;
 -
 -jQuery.props = {
 -      "for": "htmlFor",
 -      "class": "className",
 -      readonly: "readOnly",
 -      maxlength: "maxLength",
 -      cellspacing: "cellSpacing",
 -      rowspan: "rowSpan",
 -      colspan: "colSpan",
 -      tabindex: "tabIndex",
 -      usemap: "useMap",
 -      frameborder: "frameBorder"
 -};
 +      rclickable = /^a(?:rea)?$/i,
-       rradiocheck = /^(?:radio|checkbox)$/i,
 +      formHook;
  
  jQuery.fn.extend({
        attr: function( name, value ) {
@@@ -303,229 -290,128 +310,250 @@@ jQuery.extend(
                }
  
                if ( pass && name in jQuery.attrFn ) {
 -                      return jQuery(elem)[name](value);
 +                      return jQuery( elem )[ name ]( value );
                }
 +              
 +              var ret, hooks,
 +                      notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
 +              
 +              // Normalize the name if needed
 +              name = notxml && jQuery.attrFix[ name ] || name;
  
 -              var notxml = elem.nodeType !== 1 || !jQuery.isXMLDoc( elem ),
 -                      // Whether we are setting (or getting)
 -                      set = value !== undefined;
 +              // Get the appropriate hook, or the formHook
 +              // if getSetAttribute is not supported and we have form objects in IE6/7
 +              hooks = jQuery.attrHooks[ name ] || ( elem.nodeName === "FORM" && formHook );
  
 -              // Try to normalize/fix the name
 -              name = notxml && jQuery.props[ name ] || name;
 +              if ( value !== undefined ) {
  
 -              // Only do all the following if this is a node (faster for style)
 -              if ( elem.nodeType === 1 ) {
 -                      // These attributes require special treatment
 -                      var special = rspecialurl.test( name );
 -
 -                      // Safari mis-reports the default selected property of an option
 -                      // Accessing the parent's selectedIndex property fixes it
 -                      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 ( value === null ) {
 +                              jQuery.removeAttr( elem, name );
 +                              return undefined;
 +
 +                      } else if ( hooks && "set" in hooks && notxml && (ret = hooks.set( elem, value, name )) !== undefined ) {
 +                              return ret;
 +
 +                      } else {
 +                              elem.setAttribute( name, "" + value );
 +                              return value;
                        }
  
 -                      // If applicable, access the attribute via the DOM 0 way
 -                      // 'in' checks fail in Blackberry 4.7 #6931
 -                      if ( (name in elem || elem[ name ] !== undefined) && notxml && !special ) {
 -                              if ( set ) {
 -                                      // We can't allow the type property to be changed (since it causes problems in IE)
 -                                      if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
 -                                              jQuery.error( "type property can't be changed" );
 -                                      }
 +              } else {
  
 -                                      if ( value === null ) {
 -                                              if ( elem.nodeType === 1 ) {
 -                                                      elem.removeAttribute( name );
 -                                              }
 +                      if ( hooks && "get" in hooks && notxml ) {
 +                              return hooks.get( elem, name );
  
 -                                      } else {
 -                                              elem[ name ] = value;
 -                                      }
 -                              }
 +                      } else {
  
 -                              // 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;
 -                              }
 +                              ret = elem.getAttribute( name );
  
 +                              // Non-existent attributes return null, we normalize to undefined
 +                              return ret === null ?
 +                                      undefined :
 +                                      ret;
 +                      }
 +              }
 +      },
 +      
 +      removeAttr: function( elem, name ) {
 +              if ( elem.nodeType === 1 ) {
 +                      name = jQuery.attrFix[ name ] || name;
 +              
 +                      if ( jQuery.support.getSetAttribute ) {
 +                              // Use removeAttribute in browsers that support it
 +                              elem.removeAttribute( name );
 +                      } else {
 +                              jQuery.attr( elem, name, "" );
 +                              elem.removeAttributeNode( elem.getAttributeNode( name ) );
 +                      }
 +              }
 +      },
 +
 +      attrHooks: {
 +              type: {
 +                      set: function( elem, value ) {
 +                              // We can't allow the type property to be changed (since it causes problems in IE)
 +                              if ( rtype.test( elem.nodeName ) && elem.parentNode ) {
 +                                      jQuery.error( "type property can't be changed" );
 +                              }
 +                      }
 +              },
 +              tabIndex: {
 +                      get: function( elem ) {
                                // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set
                                // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/
 -                              if ( name === "tabIndex" ) {
 -                                      var attributeNode = elem.getAttributeNode( "tabIndex" );
 -
 -                                      return attributeNode && attributeNode.specified ?
 -                                              attributeNode.value :
 -                                              rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
 -                                                      0 :
 -                                                      undefined;
 -                              }
 +                              var attributeNode = elem.getAttributeNode("tabIndex");
  
 +                              return attributeNode && attributeNode.specified ?
 +                                      parseInt( attributeNode.value, 10 ) :
 +                                      rfocusable.test( elem.nodeName ) || rclickable.test( elem.nodeName ) && elem.href ?
 +                                              0 :
 +                                              undefined;
 +                      }
 +              }
 +      },
 +      
 +      propFix: {},
 +      
 +      prop: function( elem, name, value ) {
 +              var nType = elem.nodeType;
 +              
 +              // don't get/set properties on text, comment and attribute nodes
 +              if ( !elem || nType === 3 || nType === 8 || nType === 2 ) {
 +                      return undefined;
 +              }
 +              
 +              var ret, hooks,
 +                      notxml = nType !== 1 || !jQuery.isXMLDoc( elem );
 +              
 +              // Try to normalize/fix the name
 +              name = notxml && jQuery.propFix[ name ] || name;
 +              
 +              hooks = jQuery.propHooks[ name ];
 +              
 +              if ( value !== undefined ) {
 +                      if ( hooks && "set" in hooks && (ret = hooks.set( elem, value, name )) !== undefined ) {
 +                              return ret;
 +                      
 +                      } else {
 +                              return (elem[ name ] = value);
 +                      }
 +              
 +              } else {
 +                      if ( hooks && "get" in hooks && (ret = hooks.get( elem, name )) !== undefined ) {
 +                              return ret;
 +                              
 +                      } else {
                                return elem[ name ];
                        }
 +              }
 +      },
 +      
 +      propHooks: {}
 +});
  
 -                      if ( !jQuery.support.style && notxml && name === "style" ) {
 -                              if ( set ) {
 -                                      elem.style.cssText = "" + value;
 -                              }
 -
 -                              return elem.style.cssText;
 +// IE6/7 do not support getting/setting some attributes with get/setAttribute
 +if ( !jQuery.support.getSetAttribute ) {
 +      jQuery.attrFix = jQuery.extend( jQuery.attrFix, {
 +              "for": "htmlFor",
 +              "class": "className",
 +              maxlength: "maxLength",
 +              cellspacing: "cellSpacing",
 +              rowspan: "rowSpan",
 +              colspan: "colSpan",
 +              usemap: "useMap",
 +              frameborder: "frameBorder"
 +      });
 +      
 +      // Use this for any attribute on a form in IE6/7
 +      // And the name attribute
 +      formHook = jQuery.attrHooks.name = {
 +              get: function( elem, name ) {
 +                      var ret = elem.getAttributeNode( name );
 +                      // Return undefined if not specified instead of empty string
 +                      return ret && ret.specified ?
 +                              ret.nodeValue :
 +                              undefined;
 +              },
 +              set: function( elem, value, name ) {
 +                      // Check form objects in IE (multiple bugs related)
 +                      // Only use nodeValue if the attribute node exists on the form
 +                      var ret = elem.getAttributeNode( name );
 +                      if ( ret ) {
 +                              ret.nodeValue = value;
 +                              return value;
                        }
 +              }
 +      };
  
 -                      if ( set ) {
 -                              // convert the value to a string (all browsers do this but IE) see #1070
 -                              elem.setAttribute( name, "" + value );
 +      // Set width and height to auto instead of 0 on empty string( Bug #8150 )
 +      // This is for removals
 +      jQuery.each([ "width", "height" ], function( i, name ) {
 +              jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], {
 +                      set: function( elem, value ) {
 +                              if ( value === "" ) {
 +                                      elem.setAttribute( name, "auto" );
 +                                      return value;
 +                              }
                        }
 +              });
 +      });
 +}
  
 -                      // Ensure that missing attributes return undefined
 -                      // Blackberry 4.7 returns "" from getAttribute #6938
 -                      if ( !elem.attributes[ name ] && (elem.hasAttribute && !elem.hasAttribute( name )) ) {
 -                              return undefined;
 +// Remove certain attrs if set to false
 +jQuery.each([ "selected", "checked", "readOnly", "disabled" ], function( i, name ) {
 +      jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], {
 +              set: function( elem, value ) {
 +                      if ( value === false ) {
 +                              jQuery.removeAttr( elem, name );
 +                              return value;
                        }
 +              }
 +      });
 +});
  
 -                      var attr = !jQuery.support.hrefNormalized && notxml && special ?
 -                                      // Some attributes require a special call on IE
 -                                      elem.getAttribute( name, 2 ) :
 -                                      elem.getAttribute( name );
 +// Some attributes require a special call on IE
 +if ( !jQuery.support.hrefNormalized ) {
 +      jQuery.each([ "href", "src", "width", "height", "list" ], function( i, name ) {
 +              jQuery.attrHooks[ name ] = jQuery.extend( jQuery.attrHooks[ name ], {
 +                      get: function( elem ) {
 +                              var ret = elem.getAttribute( name, 2 );
 +                              return ret === null ? undefined : ret;
 +                      }
 +              });
 +      });
 +}
  
 -                      // Non-existent attributes return null, we normalize to undefined
 -                      return attr === null ? undefined : attr;
 +if ( !jQuery.support.style ) {
 +      jQuery.attrHooks.style = {
 +              get: function( elem ) {
 +                      // Return undefined in the case of empty string
 +                      // Normalize to lowercase since IE uppercases css property names
 +                      return elem.style.cssText.toLowerCase() || undefined;
 +              },
 +              set: function( elem, value ) {
 +                      return (elem.style.cssText = "" + value);
                }
 -              // Handle everything which isn't a DOM element node
 -              if ( set ) {
 -                      elem[ name ] = value;
 +      };
 +}
 +
 +// Safari mis-reports the default selected property of an option
 +// Accessing the parent's selectedIndex property fixes it
 +if ( !jQuery.support.optSelected ) {
 +      jQuery.propHooks.selected = jQuery.extend( jQuery.propHooks.selected, {
 +              get: function( elem ) {
 +                      var parent = elem.parentNode;
 +
 +                      if ( parent ) {
 +                              parent.selectedIndex;
 +
 +                              // Make sure that it also works with optgroups, see #5701
 +                              if ( parent.parentNode ) {
 +                                      parent.parentNode.selectedIndex;
 +                              }
 +                      }
                }
 -              return elem[ name ];
 -      }
 -});
 +      });
 +}
  
+ // Radios and checkboxes getter/setter
+ if ( !jQuery.support.checkOn ) {
+       jQuery.each([ "radio", "checkbox" ], function() {
+               jQuery.valHooks[ this ] = {
+                       get: function( elem ) {
+                               // Handle the case where in Webkit "" is returned instead of "on" if a value isn't specified
+                               return elem.getAttribute("value") === null ? "on" : elem.value;
+                       }
+               };
+       });
+ }
+ jQuery.each([ "radio", "checkbox" ], function() {
+       jQuery.valHooks[ this ] = jQuery.extend( jQuery.valHooks[ this ], {
+               set: function( elem, value ) {
+                       if ( jQuery.isArray( value ) ) {
+                               return (elem.checked = jQuery.inArray( jQuery(elem).val(), value ) >= 0);
+                       }
+               }
+       });
+ });
  })( jQuery );