Added document.ready shortcut to $().
[jquery.git] / jquery / jquery.js
index bd2f92f..dad8e6d 100644 (file)
@@ -17,6 +17,14 @@ window.undefined = window.undefined;
  * @constructor
  */
 function jQuery(a,c) {
+
+       // Shortcut for document ready (because $(document).each() is silly)
+       if ( a && a.constructor == Function )
+               return $(document).ready(a);
+
+       // Make sure t hat a selection was provided
+       a = a || jQuery.context || document;
+
        /*
         * Handle support for overriding other $() functions. Way too many libraries
         * provide this function to simply ignore it and overwrite it.
@@ -35,9 +43,6 @@ function jQuery(a,c) {
                        // Use the default method, in case it works some voodoo
                        return jQuery._$( a );
 
-       // Make sure t hat a selection was provided
-       a = a || jQuery.context || document;
-
        // Watch for when a jQuery object is passed as the selector
        if ( a.jquery )
                return a;
@@ -51,12 +56,16 @@ function jQuery(a,c) {
                return new jQuery(a,c);
 
        // Watch for when an array is passed in
-       if ( a.constructor == Array )
+       this.get( a.constructor == Array ?
                // Assume that it's an array of DOM Elements
-               this.cur = a;
-       else
+               a :
+
                // Find the matching elements and save them for later
-               this.cur = jQuery.Select( a, c );
+               jQuery.Select( a, c ) );
+
+       var fn = arguments[ arguments.length - 1 ];
+       if ( fn && fn.constructor == Function )
+               this.each(fn);
 }
 
 // Map over the $ in case of overwrite
@@ -81,7 +90,7 @@ jQuery.fn = jQuery.prototype = {
         * @type Number
         */
        size: function() {
-               return this.get().length;
+               return this.length;
        },
        
        /**
@@ -92,12 +101,22 @@ jQuery.fn = jQuery.prototype = {
         * @type Array,DOMElement
         */
        get: function(num) {
-               return num == undefined ? this.cur : this.cur[num];
+               if ( num && num.constructor == Array ) {
+                       this.length = 0;
+                       [].push.apply( this, num );
+               } else
+                       return num == undefined ?
+
+                               // Return a 'clean' array
+                               $.map( this, function(a){ return a } ) :
+
+                               // Return just the object
+                               this[num];
        },
        
        each: function(f) {
                for ( var i = 0; i < this.size(); i++ )
-                       f.apply( this.get(i), [i] );
+                       f.apply( this[i], [i] );
                return this;
        },
        set: function(a,b) {
@@ -111,14 +130,14 @@ jQuery.fn = jQuery.prototype = {
        },
        html: function(h) {
                return h == undefined && this.size() ?
-                       this.get(0).innerHTML : this.set( "innerHTML", h );
+                       this[0].innerHTML : this.set( "innerHTML", h );
        },
        val: function(h) {
                return h == undefined && this.size() ?
-                       this.get(0).value : this.set( "value", h );
+                       this[0].value : this.set( "value", h );
        },
        text: function(e) {
-               e = e || this.get();
+               e = e || this;
                var t = "";
                for ( var j = 0; j < e.length; j++ ) {
                        var r = e[j].childNodes;
@@ -137,7 +156,7 @@ jQuery.fn = jQuery.prototype = {
                                                jQuery.attr(this.style,j,a[j]);
                                else
                                        jQuery.attr(this.style,a,b);
-                       }) : jQuery.css( this.get(0), a );
+                       }) : jQuery.css( this[0], a );
        },
        toggle: function() {
                return this.each(function(){
@@ -260,65 +279,73 @@ jQuery.fn = jQuery.prototype = {
                return this.each(function(){jQuery.event.trigger(this,t);});
        },
        
-       pushStack: function(a) {
-               if ( !this.stack ) this.stack = [];
-               this.stack.unshift( this.cur );
-               if ( a ) this.cur = a;
+       pushStack: function(a,args) {
+               var fn = args[args.length-1];
+
+               if ( !fn || fn.constructor != Function ) {
+                       if ( !this.stack ) this.stack = [];
+                       this.stack.push( this.get() );
+                       this.get( a );
+               } else {
+                       var old = this.get();
+                       this.get( a );
+                       if ( fn.constructor == Function )
+                               return this.each( fn );
+                       this.get( old );
+               }
+
                return this;
        },
-       
-       find: function(t) {
-               var ret = [];
-               this.each(function(){
-                       ret = jQuery.merge( ret, jQuery.Select(t,this) );
-               });
-               this.pushStack( ret );
+
+       end: function() {
+               this.get( this.stack.pop() );
                return this;
        },
        
-       end: function() {
-               this.cur = this.stack.shift();
-               return this;
+       find: function(t) {
+               return this.pushStack( $.map( this, function(a){
+                       return jQuery.Select(t,a);
+               }), arguments );
        },
        
        parent: function(a) {
-               var ret = jQuery.map(this.cur,"a.parentNode");
+               var ret = jQuery.map(this,"a.parentNode");
                if ( a ) ret = jQuery.filter(a,ret).r;
-               return this.pushStack(ret);
+               return this.pushStack( ret, arguments );
        },
        
        parents: function(a) {
-               var ret = jQuery.map(this.cur,jQuery.parents);
+               var ret = jQuery.map(this,jQuery.parents);
                if ( a ) ret = jQuery.filter(a,ret).r;
-               return this.pushStack(ret);
+               return this.pushStack( ret, arguments );
        },
        
        siblings: function(a) {
                // Incorrect, need to exclude current element
-               var ret = jQuery.map(this.cur,jQuery.sibling);
+               var ret = jQuery.map(this,jQuery.sibling);
                if ( a ) ret = jQuery.filter(a,ret).r;
-               return this.pushStack(ret);
+               return this.pushStack( ret, arguments );
        },
        
        filter: function(t) {
                if ( /,/.test(t) ) {
                        var p = t.split(/\s*,\s*/);
-                       return this.pushStack( $.map(this.cur,function(a){
+                       return this.pushStack( $.map(this,function(a){
                                for ( var i = 0; i < p.length; i++ )
                                        if ( jQuery.filter(p[i],[a]).r.length )
                                                return a;
-                       }) );
+                       }), arguments );
                } else
-                       return this.pushStack( jQuery.filter(t,this.cur).r );
+                       return this.pushStack( jQuery.filter(t,this).r, arguments );
        },
        not: function(t) {
                return this.pushStack( t.constructor == String ?
-                       jQuery.filter(t,this.cur,false).r :
-                       jQuery.grep(this.cur,function(a){ return a != t; }) );
+                       jQuery.filter(t,this,false).r :
+                       jQuery.grep(this,function(a){ return a != t; }), arguments );
        },
        add: function(t) {
-               return this.pushStack( jQuery.merge( this.cur, t.constructor == String ?
-                       jQuery.Select(t) : t.constructor == Array ? t : [t] ) );
+               return this.pushStack( jQuery.merge( this, t.constructor == String ?
+                       jQuery.Select(t) : t.constructor == Array ? t : [t] ), arguments );
        },
        
        /**
@@ -331,7 +358,7 @@ jQuery.fn = jQuery.prototype = {
         * @type Boolean
         */
        is: function(expr) {
-               return jQuery.filter(expr,this.cur).r.length > 0;
+               return jQuery.filter(expr,this).r.length > 0;
        },
        
        /**
@@ -550,6 +577,11 @@ jQuery.token = [
 ];
 
 jQuery.Select = function( t, context ) {
+       // Make sure that the context is a DOM Element
+       if ( context && context.getElementsByTagName == undefined )
+               context = null;
+
+       // Set the correct context (if none is provided)
        context = context || jQuery.context || document;
 
        if ( t.constructor != String ) return [t];
@@ -570,10 +602,10 @@ jQuery.Select = function( t, context ) {
        var last = null;
 
        while ( t.length > 0 && last != t ) {
-    var r = [];
+               var r = [];
                last = t;
 
-    t = jQuery.cleanSpaces(t).replace( /^\/\//i, "" );
+               t = jQuery.cleanSpaces(t).replace( /^\/\//i, "" );
                
                var foundToken = false;
                
@@ -668,7 +700,7 @@ jQuery.filter = function(t,r,not) {
                g = function(a,f) {return jQuery.grep(a,f,true);};
 
        while ( t && t.match(/^[:\\.#\\[a-zA-Z\\*]/) ) {
-               var re = /^\[ *@([a-z0-9*()_-]+) *([~!|*$^=]*) *'?"?([^'"]*)'?"? *\]/i;
+               var re = /^\[ *@([a-z*_-][a-z0-9()_-]*) *([~!|*$^=]*) *'?"?([^'"]*)'?"? *\]/i;
                var m = re.exec(t);
 
                if ( m )
@@ -811,11 +843,11 @@ jQuery.event = {
                                handlers[0] = element["on" + type];
                }
                handlers[handler.guid] = handler;
-               element["on" + type] = jQuery.event.handle;
+               element["on" + type] = this.handle;
 
-               var g = jQuery.event.global;
-               if (!g[type]) g[type] = [];
-               g[type].push( element );
+               if (!this.global[type])
+                       this.global[type] = [];
+               this.global[type].push( element );
        },
        
        guid: 1,
@@ -832,27 +864,27 @@ jQuery.event = {
                                                delete element.events[type][i];
                        else
                                for ( var j in element.events )
-                                       jQuery.event.remove( element, j );
+                                       this.remove( element, j );
        },
        
        trigger: function(type,data,element) {
                // Touch up the incoming data
                data = data || [];
 
+               // Handle a global trigger
+               if ( !element ) {
+                       var g = this.global[type];
+                       if ( g )
+                               for ( var i = 0; i < g.length; i++ )
+                                       this.trigger( type, data, g[i] );
+
                // Handle triggering a single element
-               if ( element && element["on" + type] ) {
+               } else if ( element["on" + type] ) {
                        // Pass along a fake event
-                       data.shift( jQuery.event.fix({ type: type, target: element }) );
+                       data.unshift( this.fix({ type: type, target: element }) );
 
                        // Trigger the event
                        element["on" + type].apply( element, data );
-
-               // Handle a global trigger
-               } else if ( !element ) {
-                       var g = jQuery.event.global[type];
-                       if ( g )
-                               for ( var i = 0; i < g.length; i++ )
-                                       jQuery.event.trigger( type, data, g[i] );
                }
        },