Changed "Hash" to "Map" in docs
[jquery.git] / src / jquery / jquery.js
index 14d2e0a..282741f 100644 (file)
@@ -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 <img/> <img/>
-        * @result <img src="test0.jpg"/> <img src="test1.jpg"/>
+        * @before <img/><img/>
+        * @result <img src="test0.jpg"/><img src="test1.jpg"/>
         * @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 <p>I would like to say: </p>
         * @result <p>I would like to say: <b>Hello</b></p>
         *
-        * @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 <p>I would like to say: </p><b id="foo">Hello</b>
         * @result <p>I would like to say: <b id="foo">Hello</b></p>
         *
-        * @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 <p>I would like to say: </p><b>Hello</b>
         * @result <p>I would like to say: <b>Hello</b></p>
         *
         * @name append
         * @type jQuery
-        * @param Array<Element> elems An array of elements, all of which will be appended.
+        * @param <Content> content Content to append to the target
         * @cat DOM/Manipulation
+        * @see prepend(<Content>)
+        * @see before(<Content>)
+        * @see after(<Content>)
         */
        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("<b>Hello</b>");
         * @before <p>I would like to say: </p>
         * @result <p><b>Hello</b>I would like to say: </p>
         *
-        * @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 <p>I would like to say: </p><b id="foo">Hello</b>
         * @result <p><b id="foo">Hello</b>I would like to say: </p>
-        *       
-        * @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 <p>I would like to say: </p><b>Hello</b>
         * @result <p><b>Hello</b>I would like to say: </p>
         *
         * @name prepend
         * @type jQuery
-        * @param Array<Element> elems An array of elements, all of which will be appended.
+        * @param <Content> content Content to prepend to the target.
         * @cat DOM/Manipulation
+        * @see append(<Content>)
+        * @see before(<Content>)
+        * @see after(<Content>)
         */
        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("<b>Hello</b>");
         * @before <p>I would like to say: </p>
         * @result <b>Hello</b><p>I would like to say: </p>
         *
-        * @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 <p>I would like to say: </p><b id="foo">Hello</b>
         * @result <b id="foo">Hello</b><p>I would like to say: </p>
         *
-        * @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 <p>I would like to say: </p><b>Hello</b>
         * @result <b>Hello</b><p>I would like to say: </p>
         *
         * @name before
         * @type jQuery
-        * @param Array<Element> elems An array of elements, all of which will be appended.
+        * @param <Content> content Content to insert before each target.
         * @cat DOM/Manipulation
+        * @see append(<Content>)
+        * @see prepend(<Content>)
+        * @see after(<Content>)
         */
        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("<b>Hello</b>");
         * @before <p>I would like to say: </p>
         * @result <p>I would like to say: </p><b>Hello</b>
         *
-        * @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 <b id="foo">Hello</b><p>I would like to say: </p>
         * @result <p>I would like to say: </p><b id="foo">Hello</b>
         *
-        * @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 <b>Hello</b><p>I would like to say: </p>
         * @result <p>I would like to say: </p><b>Hello</b>
         *
         * @name after
         * @type jQuery
-        * @param Array<Element> elems An array of elements, all of which will be appended.
+        * @param <Content> content Content to insert after each target.
         * @cat DOM/Manipulation
+        * @see append(<Content>)
+        * @see prepend(<Content>)
+        * @see before(<Content>)
         */
        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 <p><span>Hello</span>, how are you?</p>
         * @result $("p").find("span").end() == [ <p>...</p> ]
@@ -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 );
+               }) );
        },
 
        /**
@@ -900,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++ )
@@ -915,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 );
        },
 
        /**
@@ -947,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; }) );
        },
 
        /**
@@ -994,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] ) );
        },
 
        /**
@@ -1055,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;
        }
 };
 
@@ -1177,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 );
                        };
                });