X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;f=src%2Fjquery%2Fjquery.js;h=282741fd75941ea72bce48bb51688d960c7552f3;hb=2daf49296abd4fcdf526a2c2814cd24e8f3eaff0;hp=8b28fdbccfcf06a57090e95688600a21edcbfae5;hpb=c3d8cb0c09c2d7d5f0fc3a78f8b4cef9b0adc640;p=jquery.git diff --git a/src/jquery/jquery.js b/src/jquery/jquery.js index 8b28fdb..282741f 100644 --- a/src/jquery/jquery.js +++ b/src/jquery/jquery.js @@ -21,13 +21,13 @@ window.undefined = window.undefined; * @cat Core */ var jQuery = function(a,c) { - - // Shortcut for document ready - if ( a && typeof a == "function" && jQuery.fn.ready && !a.nodeType && a[0] == undefined ) // Safari reports typeof on DOM NodeLists as a function - return jQuery(document).ready(a); - // Make sure that a selection was provided a = a || document; + + // Shortcut for document ready + // Safari reports typeof on DOM NodeLists as a function + if ( typeof a == "function" && !a.nodeType && a[0] == undefined ) + return jQuery(document)[ jQuery.fn.ready ? "ready" : "load" ]( a ); // Watch for when a jQuery object is passed as the selector if ( a.jquery ) @@ -48,21 +48,12 @@ var jQuery = function(a,c) { } // Watch for when an array is passed in - this.set( a.constructor == Array || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType ? + return this.setArray( a.constructor == Array || a.length && a != window && !a.nodeType && a[0] != undefined && a[0].nodeType ? // Assume that it is an array of DOM Elements jQuery.makeArray( a ) : // Find the matching elements and save them for later jQuery.find( a, c ) ); - - // See if an extra function was provided - var fn = arguments[ arguments.length - 1 ]; - - // If so, execute it in context - if ( fn && typeof fn == "function" ) - this.each(fn); - - return this; }; // Map over the $ in case of overwrite @@ -229,6 +220,8 @@ jQuery.fn = jQuery.prototype = { size: function() { return this.length; }, + + length: 0, /** * Access all matched elements. This serves as a backwards-compatible @@ -268,7 +261,8 @@ jQuery.fn = jQuery.prototype = { }, /** - * Set the jQuery object to an array of elements. + * Set the jQuery object to an array of elements, while maintaining + * the stack. * * @example $("img").set([ document.body ]); * @result $("img").set() == [ document.body ] @@ -279,11 +273,29 @@ jQuery.fn = jQuery.prototype = { * @param Elements elems An array of elements * @cat Core */ - set: function( array ) { - // Use a tricky hack to make the jQuery object - // look and feel like an array + set: function( a ) { + var ret = jQuery(this); + ret.prevObject = this; + return ret.setArray( a ); + }, + + /** + * Set the jQuery object to an array of elements. This operation is + * completely destructive - be sure to use .set() if you wish to maintain + * the jQuery stack. + * + * @example $("img").setArray([ document.body ]); + * @result $("img").setArray() == [ document.body ] + * + * @private + * @name setArray + * @type jQuery + * @param Elements elems An array of elements + * @cat Core + */ + setArray: function( a ) { this.length = 0; - [].push.apply( this, array ); + [].push.apply( this, a ); return this; }, @@ -300,8 +312,8 @@ jQuery.fn = jQuery.prototype = { * @example $("img").each(function(i){ * this.src = "test" + i + ".jpg"; * }); - * @before - * @result + * @before + * @result * @desc Iterates over two images and sets their src property * * @name each @@ -359,7 +371,8 @@ jQuery.fn = jQuery.prototype = { */ /** - * Set a hash of key/value object properties to all matched elements. + * Set a key/value object as properties to all matched elements. + * * This serves as the best way to set a large number of properties * on all matched elements. * @@ -369,7 +382,7 @@ jQuery.fn = jQuery.prototype = { * * @name attr * @type jQuery - * @param Hash prop A set of key/value pairs to set as object properties. + * @param Map properties Key/value pairs to set as object properties. * @cat DOM */ @@ -377,7 +390,7 @@ jQuery.fn = jQuery.prototype = { * Set a single property to a value, on all matched elements. * * Note that you can't set the name property of input elements in IE. - * Use $(html) or $().append(html) or $().html(html) to create elements + * Use $(html) or .append(html) or .html(html) to create elements * on the fly including the name property. * * @example $("img").attr("src","test.jpg"); @@ -442,7 +455,8 @@ jQuery.fn = jQuery.prototype = { */ /** - * Set a hash of key/value style properties to all matched elements. + * Set a key/value object as style properties to all matched elements. + * * This serves as the best way to set a large number of style properties * on all matched elements. * @@ -452,7 +466,7 @@ jQuery.fn = jQuery.prototype = { * * @name css * @type jQuery - * @param Hash prop A set of key/value pairs to set as style properties. + * @param Map properties Key/value pairs to set as style properties. * @cat CSS */ @@ -588,8 +602,8 @@ jQuery.fn = jQuery.prototype = { }, /** - * Append any number of elements to the inside of every matched elements, - * generated from the provided HTML. + * Append content to the inside of every matched element. + * * This operation is similar to doing an appendChild to all the * specified elements, adding them into the document. * @@ -597,40 +611,21 @@ jQuery.fn = jQuery.prototype = { * @before

I would like to say:

* @result

I would like to say: Hello

* - * @name append - * @type jQuery - * @param String html A string of HTML, that will be created on the fly and appended to the target. - * @cat DOM/Manipulation - */ - - /** - * Append an element to the inside of all matched elements. - * This operation is similar to doing an appendChild to all the - * specified elements, adding them into the document. - * * @example $("p").append( $("#foo")[0] ); * @before

I would like to say:

Hello * @result

I would like to say: Hello

* - * @name append - * @type jQuery - * @param Element elem A DOM element that will be appended. - * @cat DOM/Manipulation - */ - - /** - * Append any number of elements to the inside of all matched elements. - * This operation is similar to doing an appendChild to all the - * specified elements, adding them into the document. - * * @example $("p").append( $("b") ); * @before

I would like to say:

Hello * @result

I would like to say: Hello

* * @name append * @type jQuery - * @param Array elems An array of elements, all of which will be appended. + * @param content Content to append to the target * @cat DOM/Manipulation + * @see prepend() + * @see before() + * @see after() */ append: function() { return this.domManip(arguments, true, 1, function(a){ @@ -639,94 +634,59 @@ jQuery.fn = jQuery.prototype = { }, /** - * Prepend any number of elements to the inside of every matched elements, - * generated from the provided HTML. - * This operation is the best way to insert dynamically created elements - * inside, at the beginning, of all the matched element. + * Prepend content to the inside of every matched element. + * + * This operation is the best way to insert elements + * inside, at the beginning, of all matched elements. * * @example $("p").prepend("Hello"); * @before

I would like to say:

* @result

HelloI would like to say:

* - * @name prepend - * @type jQuery - * @param String html A string of HTML, that will be created on the fly and appended to the target. - * @cat DOM/Manipulation - */ - - /** - * Prepend an element to the inside of all matched elements. - * This operation is the best way to insert an element inside, at the - * beginning, of all the matched element. - * * @example $("p").prepend( $("#foo")[0] ); * @before

I would like to say:

Hello * @result

HelloI would like to say:

- * - * @name prepend - * @type jQuery - * @param Element elem A DOM element that will be appended. - * @cat DOM/Manipulation - */ - - /** - * Prepend any number of elements to the inside of all matched elements. - * This operation is the best way to insert a set of elements inside, at the - * beginning, of all the matched element. - * + * * @example $("p").prepend( $("b") ); * @before

I would like to say:

Hello * @result

HelloI would like to say:

* * @name prepend * @type jQuery - * @param Array elems An array of elements, all of which will be appended. + * @param content Content to prepend to the target. * @cat DOM/Manipulation + * @see append() + * @see before() + * @see after() */ prepend: function() { return this.domManip(arguments, true, -1, function(a){ this.insertBefore( a, this.firstChild ); }); }, - + /** - * Insert any number of dynamically generated elements before each of the - * matched elements. + * Insert content before each of the matched elements. * * @example $("p").before("Hello"); * @before

I would like to say:

* @result Hello

I would like to say:

* - * @name before - * @type jQuery - * @param String html A string of HTML, that will be created on the fly and appended to the target. - * @cat DOM/Manipulation - */ - - /** - * Insert an element before each of the matched elements. - * * @example $("p").before( $("#foo")[0] ); * @before

I would like to say:

Hello * @result Hello

I would like to say:

* - * @name before - * @type jQuery - * @param Element elem A DOM element that will be appended. - * @cat DOM/Manipulation - */ - - /** - * Insert any number of elements before each of the matched elements. - * * @example $("p").before( $("b") ); * @before

I would like to say:

Hello * @result Hello

I would like to say:

* * @name before * @type jQuery - * @param Array elems An array of elements, all of which will be appended. + * @param content Content to insert before each target. * @cat DOM/Manipulation + * @see append() + * @see prepend() + * @see after() */ before: function() { return this.domManip(arguments, false, 1, function(a){ @@ -735,43 +695,27 @@ jQuery.fn = jQuery.prototype = { }, /** - * Insert any number of dynamically generated elements after each of the - * matched elements. + * Insert content after each of the matched elements. * * @example $("p").after("Hello"); * @before

I would like to say:

* @result

I would like to say:

Hello * - * @name after - * @type jQuery - * @param String html A string of HTML, that will be created on the fly and appended to the target. - * @cat DOM/Manipulation - */ - - /** - * Insert an element after each of the matched elements. - * * @example $("p").after( $("#foo")[0] ); * @before Hello

I would like to say:

* @result

I would like to say:

Hello * - * @name after - * @type jQuery - * @param Element elem A DOM element that will be appended. - * @cat DOM/Manipulation - */ - - /** - * Insert any number of elements after each of the matched elements. - * * @example $("p").after( $("b") ); * @before Hello

I would like to say:

* @result

I would like to say:

Hello * * @name after * @type jQuery - * @param Array elems An array of elements, all of which will be appended. + * @param content Content to insert after each target. * @cat DOM/Manipulation + * @see append() + * @see prepend() + * @see before() */ after: function() { return this.domManip(arguments, false, -1, function(a){ @@ -784,6 +728,8 @@ jQuery.fn = jQuery.prototype = { * back to its previous state. After an end operation, the list of matched elements will * revert to the last state of matched elements. * + * If there was no destructive operation before, an empty set is returned. + * * @example $("p").find("span").end(); * @before

Hello, how are you?

* @result $("p").find("span").end() == [

...

] @@ -793,9 +739,7 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Traversing */ end: function() { - if( !(this.stack && this.stack.length) ) - return this; - return this.set( this.stack.pop() ); + return this.prevObject || jQuery([]); }, /** @@ -816,9 +760,9 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Traversing */ find: function(t) { - return this.pushStack( jQuery.map( this, function(a){ + return this.set( jQuery.map( this, function(a){ return jQuery.find(t,a); - }), arguments ); + }) ); }, /** @@ -837,9 +781,9 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Manipulation */ clone: function(deep) { - return this.pushStack( jQuery.map( this, function(a){ + return this.set( jQuery.map( this, function(a){ return a.cloneNode( deep != undefined ? deep : true ); - }), arguments ); + }) ); }, /** @@ -852,13 +796,34 @@ jQuery.fn = jQuery.prototype = { * * @example $("p").filter(".selected") * @before

Hello

How are you?

- * @result $("p").filter(".selected") == [

Hello

] + * @result [

Hello

] * * @name filter * @type jQuery * @param String expr An expression to search with. * @cat DOM/Traversing */ + + /** + * Removes all elements from the set of matched elements that do not + * pass the specified filter. This method is used to narrow down + * the results of a search. + * + * The elements to filter are passed as the first argument, their + * index inside the set as the second. + * + * @example $("p").filter(function(element, index) { + * return $("ol", element).length == 0; + * }) + * @before

  1. Hello

How are you?

+ * @result [

How are you?

] + * @desc Remove all elements that have a child ol element + * + * @name filter + * @type jQuery + * @param Function filter A function to use for filtering + * @cat DOM/Traversing + */ /** * Removes all elements from the set of matched elements that do not @@ -871,7 +836,7 @@ jQuery.fn = jQuery.prototype = { * * @example $("p").filter([".selected", ":first"]) * @before

Hello

Hello Again

And Again

- * @result $("p").filter([".selected", ":first"]) == [

Hello

,

And Again

] + * @result [

Hello

,

And Again

] * * @name filter * @type jQuery @@ -879,7 +844,7 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Traversing */ filter: function(t) { - return this.pushStack( + return this.set( t.constructor == Array && jQuery.map(this,function(a){ for ( var i = 0, tl = t.length; i < tl; i++ ) @@ -894,7 +859,7 @@ jQuery.fn = jQuery.prototype = { typeof t == "function" && jQuery.grep( this, t ) || - jQuery.filter(t,this).r, arguments ); + jQuery.filter(t,this).r ); }, /** @@ -926,9 +891,9 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Traversing */ not: function(t) { - return this.pushStack( typeof t == "string" ? + return this.set( typeof t == "string" ? jQuery.filter(t,this,true).r : - jQuery.grep(this,function(a){ return a != t; }), arguments ); + jQuery.grep(this,function(a){ return a != t; }) ); }, /** @@ -973,10 +938,10 @@ jQuery.fn = jQuery.prototype = { * @cat DOM/Traversing */ add: function(t) { - return this.pushStack( jQuery.merge( + return this.set( jQuery.merge( this.get(), typeof t == "string" ? jQuery.find(t) : - t.constructor == Array ? t : [t] ), arguments ); + t.constructor == Array ? t : [t] ) ); }, /** @@ -1034,40 +999,6 @@ jQuery.fn = jQuery.prototype = { fn.apply( obj, [ clone ? a[i].cloneNode(true) : a[i] ] ); }); - }, - - /** - * - * - * @private - * @name pushStack - * @param Array a - * @param Array args - * @type jQuery - * @cat Core - */ - pushStack: function(a,args) { - var fn = args && args.length > 1 && args[args.length-1]; - var fn2 = args && args.length > 2 && args[args.length-2]; - - if ( fn && fn.constructor != Function ) fn = null; - if ( fn2 && fn2.constructor != Function ) fn2 = null; - - if ( !fn ) { - if ( !this.stack ) this.stack = []; - this.stack.push( this.get() ); - this.set( a ); - } else { - var old = this.get(); - this.set( a ); - - if ( fn2 && a.length || !fn2 ) - this.each( fn2 || fn ).set( old ); - else - this.set( old ).each( fn ); - } - - return this; } }; @@ -1156,7 +1087,7 @@ jQuery.extend({ var ret = jQuery.map(this,n); if ( a && typeof a == "string" ) ret = jQuery.filter(a,ret).r; - return this.pushStack( ret, arguments ); + return this.set( ret ); }; });