X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=8f8a1763db88f190df240856ad4b196c2c4aa450;hb=e3263063e4ef3d2e0ee6b74b9da573bf40e87668;hp=bc4bcd1e7972de9d6faa7449fcbc9202049ee175;hpb=05d401dd8439d8a2137d3fb69a70d671c0d3a69d;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index bc4bcd1..8f8a176 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -9,9 +9,6 @@ * $Rev$ */ -// Global undefined variable -window.undefined = window.undefined; - /** * Create a new jQuery Object * @@ -22,7 +19,12 @@ window.undefined = window.undefined; * @param jQuery|Element|Array c context * @cat Core */ -var jQuery = function(a,c) { + +// Map over jQuery in case of overwrite +if ( typeof jQuery != "undefined" ) + var _jQuery = jQuery; + +var jQuery = window.jQuery = function(a,c) { // If the context is global, return a new object if ( window == this || !this.init ) return new jQuery(a,c); @@ -32,10 +34,12 @@ var jQuery = function(a,c) { // Map over the $ in case of overwrite if ( typeof $ != "undefined" ) - jQuery._$ = $; + var _$ = $; // Map the jQuery namespace to the '$' one -var $ = jQuery; +window.$ = jQuery; + +var quickExpr = /^[^<]*(<(.|\s)+>)[^>]*$|^#(\w+)$/; /** * This function accepts a string containing a CSS or @@ -153,22 +157,39 @@ jQuery.fn = jQuery.prototype = { // Make sure that a selection was provided a = a || document; - // HANDLE: $(function) - // Shortcut for document ready - if ( jQuery.isFunction(a) ) - return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a ); - // Handle HTML strings if ( typeof a == "string" ) { - // HANDLE: $(html) -> $(array) - var m = /^[^<]*(<(.|\s)+>)[^>]*$/.exec(a); - if ( m ) - a = jQuery.clean( [ m[1] ] ); + var m = quickExpr.exec(a); + if ( m && (m[1] || !c) ) { + // HANDLE: $(html) -> $(array) + if ( m[1] ) + a = jQuery.clean( [ m[1] ], c ); + + // HANDLE: $("#id") + else { + var tmp = document.getElementById( m[3] ); + if ( tmp ) + // Handle the case where IE and Opera return items + // by name instead of ID + if ( tmp.id != m[3] ) + return jQuery().find( a ); + else { + this[0] = tmp; + this.length = 1; + return this; + } + else + a = []; + } // HANDLE: $(expr) - else + } else return new jQuery( c ).find( a ); - } + + // HANDLE: $(function) + // Shortcut for document ready + } else if ( jQuery.isFunction(a) ) + return new jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a ); return this.setArray( // HANDLE: $(array) @@ -301,7 +322,7 @@ jQuery.fn = jQuery.prototype = { */ setArray: function( a ) { this.length = 0; - [].push.apply( this, a ); + Array.prototype.push.apply( this, a ); return this; }, @@ -625,27 +646,32 @@ jQuery.fn = jQuery.prototype = { * @param Element elem A DOM element that will be wrapped around the target. * @cat DOM/Manipulation */ - wrap: function() { - // The elements to wrap the target around - var a, args = arguments; - - // Wrap each of the matched elements individually - return this.each(function(){ - if ( !a ) - a = jQuery.clean(args, this.ownerDocument); + wrapAll: function(html) { + if ( this[0] ) + // The elements to wrap the target around + jQuery(html, this[0].ownerDocument) + .clone() + .insertBefore(this[0]) + .map(function(){ + var elem = this; + while ( elem.firstChild ) + elem = elem.firstChild; + return elem; + }) + .append(this); - // Clone the structure that we're using to wrap - var b = a[0].cloneNode(true); - - // Insert it before the element to be wrapped - this.parentNode.insertBefore( b, this ); + return this; + }, - // Find the deepest point in the wrap structure - while ( b.firstChild ) - b = b.firstChild; + wrapInner: function(html) { + return this.each(function(){ + jQuery(this).contents().wrapAll(html); + }); + }, - // Move the matched element to within the wrap structure - b.appendChild( this ); + wrap: function(html) { + return this.each(function(){ + jQuery(this).wrapAll(html); }); }, @@ -1160,6 +1186,20 @@ jQuery.fn = jQuery.prototype = { ( this.length ? this[0].innerHTML : null ) : this.empty().append( val ); }, + + replaceWith: function( val ) { + return this.after( val ).remove(); + }, + + slice: function() { + return this.pushStack( Array.prototype.slice.apply( this, arguments ) ); + }, + + map: function(fn){ + return this.pushStack(jQuery.map( this, function(elem,i){ + return fn.call( elem, i, elem ); + })); + }, /** * @private @@ -1252,17 +1292,39 @@ jQuery.fn = jQuery.prototype = { */ jQuery.extend = jQuery.fn.extend = function() { // copy reference to target object - var target = arguments[0], a = 1; + var target = arguments[0] || {}, a = 1, al = arguments.length, deep = false; + + // Handle a deep copy situation + if ( target.constructor == Boolean ) { + deep = target; + target = arguments[1] || {}; + } // extend jQuery itself if only one argument is passed - if ( arguments.length == 1 ) { + if ( al == 1 ) { target = this; a = 0; } + var prop; - while ( (prop = arguments[a++]) != null ) - // Extend the base object - for ( var i in prop ) target[i] = prop[i]; + + for ( ; a < al; a++ ) + // Only deal with non-null/undefined values + if ( (prop = arguments[a]) != null ) + // Extend the base object + for ( var i in prop ) { + // Prevent never-ending loop + if ( target == prop[i] ) + continue; + + // Recurse if we're merging object values + if ( deep && typeof prop[i] == 'object' && target[i] ) + jQuery.extend( target[i], prop[i] ); + + // Don't bring in undefined values + else if ( prop[i] != undefined ) + target[i] = prop[i]; + } // Return the modified object return target; @@ -1303,9 +1365,10 @@ jQuery.extend({ * @type undefined * @cat Core */ - noConflict: function() { - if ( jQuery._$ ) - $ = jQuery._$; + noConflict: function(deep) { + window.$ = _$; + if ( deep ) + window.jQuery = _jQuery; return jQuery; }, @@ -1371,12 +1434,24 @@ jQuery.extend({ */ // args is for internal usage only each: function( obj, fn, args ) { - if ( obj.length == undefined ) - for ( var i in obj ) - fn.apply( obj[i], args || [i, obj[i]] ); - else - for ( var i = 0, ol = obj.length; i < ol; i++ ) - if ( fn.apply( obj[i], args || [i, obj[i]] ) === false ) break; + if ( args ) { + if ( obj.length == undefined ) + for ( var i in obj ) + fn.apply( obj[i], args ); + else + for ( var i = 0, ol = obj.length; i < ol; i++ ) + if ( fn.apply( obj[i], args ) === false ) break; + + // A special, fast, case for the most common use of each + } else { + if ( obj.length == undefined ) + for ( var i in obj ) + fn.call( obj[i], i, obj[i] ); + else + for ( var i = 0, ol = obj.length, val = obj[0]; + i < ol && fn.call(val,i,val) !== false; val = obj[++i] ){} + } + return obj; }, @@ -1489,7 +1564,7 @@ jQuery.extend({ } if (prop.match(/float/i)) - prop = jQuery.styleFloat; + prop = styleFloat; if (!force && elem.style[prop]) ret = elem.style[prop]; @@ -1509,7 +1584,7 @@ jQuery.extend({ // then some display: none elements are involved else { // Locate all of the parent display: none elements - for ( var a = elem; color(a); a = a.parentNode ) + for ( var a = elem; a && color(a); a = a.parentNode ) stack.unshift(a); // Go through and make them visible, but in reverse @@ -1577,7 +1652,11 @@ jQuery.extend({ [3, "", "
"] || !s.indexOf("", ""] || + [2, "", "
"] || + + // IE can't serialize and