X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fcore.js;h=b9a2fed49f4598a305b8f659f947e71be9590053;hb=1762dc24d66c7cbda189acffc21694884bb2ec7a;hp=f6f0bd592cb416affed841b5cd87116e81b7e0d6;hpb=f8698395acbe428bd7c3dd6dd5700a0c660deeab;p=jquery.git diff --git a/src/core.js b/src/core.js index f6f0bd5..b9a2fed 100644 --- a/src/core.js +++ b/src/core.js @@ -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 ) { @@ -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 ) { @@ -720,24 +716,26 @@ jQuery.extend({ // args is for internal usage only each: function( object, callback, args ) { + var name, i = 0, length = object.length; + if ( args ) { - if ( object.length == undefined ) { - for ( var name in object ) + if ( length == undefined ) { + for ( name in object ) if ( callback.apply( object[ name ], args ) === false ) break; } else - for ( var i = 0, length = object.length; i < length; i++ ) - if ( callback.apply( object[ i ], args ) === false ) + for ( ; i < length; ) + if ( callback.apply( object[ i++ ], args ) === false ) break; // A special, fast, case for the most common use of each } else { - if ( object.length == undefined ) { - for ( var name in object ) + if ( length == undefined ) { + for ( name in object ) if ( callback.call( object[ name ], name, object[ name ] ) === false ) break; } else - for ( var i = 0, length = object.length, value = object[0]; + for ( var value = object[0]; i < length && callback.call( value, i, value ) !== false; value = object[++i] ){} } @@ -822,7 +820,7 @@ jQuery.extend({ }, curCSS: function( elem, name, force ) { - var ret; + var ret, style = elem.style; // A helper method for determining if an element's values are broken function color( elem ) { @@ -836,7 +834,7 @@ jQuery.extend({ // We need to handle opacity special in IE if ( name == "opacity" && jQuery.browser.msie ) { - ret = jQuery.attr( elem.style, "opacity" ); + ret = jQuery.attr( style, "opacity" ); return ret == "" ? "1" : @@ -844,17 +842,17 @@ jQuery.extend({ } // Opera sometimes will give the wrong display answer, this fixes it, see #2037 if ( jQuery.browser.opera && name == "display" ) { - var save = elem.style.outline; - elem.style.outline = "0 solid black"; - elem.style.outline = save; + var save = style.outline; + style.outline = "0 solid black"; + style.outline = save; } // Make sure we're using the right name for getting the float value if ( name.match( /float/i ) ) name = styleFloat; - if ( !force && elem.style && elem.style[ name ] ) - ret = elem.style[ name ]; + if ( !force && style && style[ name ] ) + ret = style[ name ]; else if ( getComputedStyle ) { @@ -916,16 +914,16 @@ jQuery.extend({ // but a number that has a weird ending, we need to convert it to pixels if ( !/^\d+(px)?$/i.test( ret ) && /^\d/.test( ret ) ) { // Remember the original values - var style = elem.style.left, runtimeStyle = elem.runtimeStyle.left; + var left = style.left, rsLeft = elem.runtimeStyle.left; // Put in the new values to get a computed value out elem.runtimeStyle.left = elem.currentStyle.left; - elem.style.left = ret || 0; - ret = elem.style.pixelLeft + "px"; + style.left = ret || 0; + ret = style.pixelLeft + "px"; // Revert the changed values - elem.style.left = style; - elem.runtimeStyle.left = runtimeStyle; + style.left = left; + elem.runtimeStyle.left = rsLeft; } } @@ -1112,14 +1110,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; @@ -1127,7 +1125,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; @@ -1136,16 +1135,16 @@ jQuery.extend({ merge: function( first, second ) { // We have to loop this way because IE & Opera overwrite the length // expando of getElementsByTagName - + var i = 0; // Also, we need to make sure that the correct elements are being returned // (IE returns comment nodes in a '*' query) if ( jQuery.browser.msie ) { - for ( var i = 0; second[ i ]; i++ ) + for ( ; second[ i ]; i++ ) if ( second[ i ].nodeType != 8 ) first.push( second[ i ] ); } else - for ( var i = 0; second[ i ]; i++ ) + for ( ; second[ i ]; i++ ) first.push( second[ i ] ); return first; @@ -1192,15 +1191,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 ); } });