jquery core: #2548, #2170, #2188, #2099, #1170, #2558, #2521, #2119, #1271, #2453...
[jquery.git] / src / core.js
index 6b497fd..6b2c36a 100644 (file)
@@ -24,7 +24,10 @@ var jQuery = window.jQuery = window.$ = function( selector, context ) {
 var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/,
 
 // Is it a simple selector
-       isSimple = /^.[^:#\[\.]*$/;
+       isSimple = /^.[^:#\[\.]*$/,
+
+// Will speed up references to undefined, and allows munging its name. 
+       undefined;
 
 jQuery.fn = jQuery.prototype = {
        init: function( selector, context ) {
@@ -36,9 +39,9 @@ jQuery.fn = jQuery.prototype = {
                        this[0] = selector;
                        this.length = 1;
                        return this;
-
+               }
                // Handle HTML strings
-               } else if ( typeof selector == "string" ) {
+               if ( typeof selector == "string" ) {
                        // Are we dealing with HTML string or an ID?
                        var match = quickExpr.exec( selector );
 
@@ -54,32 +57,27 @@ jQuery.fn = jQuery.prototype = {
                                        var elem = document.getElementById( match[3] );
 
                                        // Make sure an element was located
-                                       if ( elem )
+                                       if ( elem ){
                                                // Handle the case where IE and Opera return items
                                                // by name instead of ID
                                                if ( elem.id != match[3] )
                                                        return jQuery().find( selector );
 
                                                // Otherwise, we inject the element directly into the jQuery object
-                                               else {
-                                                       this[0] = elem;
-                                                       this.length = 1;
-                                                       return this;
-                                               }
-
-                                       else
-                                               selector = [];
+                                               return jQuery( elem );
+                                       }
+                                       selector = [];
                                }
 
                        // HANDLE: $(expr, [context])
                        // (which is just equivalent to: $(content).find(expr)
                        } else
-                               return new jQuery( context ).find( selector );
+                               return jQuery( context ).find( selector );
 
                // HANDLE: $(function)
                // Shortcut for document ready
                } else if ( jQuery.isFunction( selector ) )
-                       return new jQuery( document )[ jQuery.fn.ready ? "ready" : "load" ]( selector );
+                       return jQuery( document )[ jQuery.fn.ready ? "ready" : "load" ]( selector );
                
                return this.setArray(jQuery.makeArray(selector));
        },
@@ -145,12 +143,10 @@ jQuery.fn = jQuery.prototype = {
                var ret = -1;
 
                // Locate the position of the desired element
-               this.each(function(i){
-                       if ( this == elem )
-                               ret = i;
-               });
-
-               return ret;
+               return jQuery.inArray( 
+                       // If it receives a jQuery object, the first element is used
+                       elem && elem.jquery ? elem[0] : elem
+               , this );
        },
 
        attr: function( name, value, type ) {
@@ -158,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 = {};
@@ -344,12 +340,12 @@ jQuery.fn = jQuery.prototype = {
        },
 
        add: function( selector ) {
-               return !selector ? this : this.pushStack( jQuery.merge( 
+               return this.pushStack( jQuery.unique( jQuery.merge( 
                        this.get(),
-                       selector.constructor == String ? 
-                               jQuery( selector ).get() :
-                               selector.length != undefined && (!selector.nodeName || jQuery.nodeName(selector, "form")) ?
-                                       selector : [selector] ) );
+                       typeof selector == 'string' ? 
+                               jQuery( selector ) :
+                               jQuery.makeArray( selector )
+               )));
        },
 
        is: function( selector ) {
@@ -1032,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 ) {
@@ -1114,14 +1121,14 @@ jQuery.extend({
        makeArray: function( array ) {
                var ret = [];
 
-               if( array != undefined ){
+               if( array != null ){
                        var i = array.length;
                        //the window, strings and functions also have 'length'
-                       if( i != null && !array.split && array != window && !array.call )
+                       if( i == null || array.split || array.setInterval || array.call )
+                               ret[0] = array;
+                       else
                                while( i )
                                        ret[--i] = array[i];
-                       else
-                               ret[0] = array;
                }
 
                return ret;
@@ -1129,7 +1136,8 @@ jQuery.extend({
 
        inArray: function( elem, array ) {
                for ( var i = 0, length = array.length; i < length; i++ )
-                       if ( array[ i ] == elem )
+               // Use === because on IE, window == document
+                       if ( array[ i ] === elem )
                                return i;
 
                return -1;
@@ -1180,7 +1188,7 @@ jQuery.extend({
                // Go through the array, only saving the items
                // that pass the validator function
                for ( var i = 0, length = elems.length; i < length; i++ )
-                       if ( !inv && callback( elems[ i ], i ) || inv && !callback( elems[ i ], i ) )
+                       if ( !inv != !callback( elems[ i ], i ) )
                                ret.push( elems[ i ] );
 
                return ret;
@@ -1194,15 +1202,11 @@ jQuery.extend({
                for ( var i = 0, length = elems.length; i < length; i++ ) {
                        var value = callback( elems[ i ], i );
 
-                       if ( value !== null && value != undefined ) {
-                               if ( value.constructor != Array )
-                                       value = [ value ];
-
-                               ret = ret.concat( value );
-                       }
+                       if ( value != null )
+                               ret[ ret.length ] = value;
                }
 
-               return ret;
+               return ret.concat.apply( [], ret );
        }
 });
 
@@ -1231,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"
        }
 });