Added document.ready shortcut to $().
[jquery.git] / jquery / jquery.js
index 97fa48e..dad8e6d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * jQuery
+ * jQuery - New Wave Javascript
  *
  * Copyright (c) 2006 John Resig (jquery.com)
  * Licensed under the MIT License:
@@ -17,18 +17,62 @@ 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.
+        */
+
+       // Check to see if this is a possible collision case
+       if ( jQuery._$ && !c && a.constructor == String && 
+      
+               // Make sure that the expression is a colliding one
+               !/[^a-zA-Z0-9_-]/.test(a) &&
+        
+               // and that there are no elements that match it
+               // (this is the one truly ambiguous case)
+               !document.getElementsByTagName(a).length )
+
+                       // Use the default method, in case it works some voodoo
+                       return jQuery._$( a );
+
+       // Watch for when a jQuery object is passed as the selector
+       if ( a.jquery )
+               return a;
+
+       // Watch for when a jQuery object is passed at the context
+       if ( c && c.jquery )
+               return $(c.get()).find(a);
+       
+       // If the context is global, return a new object
        if ( window == this )
                return new jQuery(a,c);
-       
-       this.cur = jQuery.Select(
-               a || jQuery.context || document,
-               c && c.jquery && c.get(0) || c
-       );
+
+       // Watch for when an array is passed in
+       this.get( a.constructor == Array ?
+               // Assume that it's an array of DOM Elements
+               a :
+
+               // Find the matching elements and save them for later
+               jQuery.Select( a, c ) );
+
+       var fn = arguments[ arguments.length - 1 ];
+       if ( fn && fn.constructor == Function )
+               this.each(fn);
 }
 
-/**
- * The jQuery query object.
- */
+// Map over the $ in case of overwrite
+if ( $ )
+       jQuery._$ = $;
+
+// Map the jQuery namespace to the '$' one
 var $ = jQuery;
 
 jQuery.fn = jQuery.prototype = {
@@ -46,7 +90,7 @@ jQuery.fn = jQuery.prototype = {
         * @type Number
         */
        size: function() {
-               return this.get().length;
+               return this.length;
        },
        
        /**
@@ -57,17 +101,27 @@ 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) {
                return this.each(function(){
-                       if ( b == undefined )
+                       if ( b === undefined )
                                for ( var j in a )
                                        jQuery.attr(this,j,a[j]);
                        else
@@ -76,32 +130,33 @@ 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++ )
-                       for ( var i = 0; i < e[j].childNodes.length; i++ )
-                               t += e[j].childNodes[i].nodeType != 1 ?
-                                       e[j].childNodes[i].nodeValue :
-                                       jQuery.fn.text(e[j].childNodes[i].childNodes);
+               for ( var j = 0; j < e.length; j++ ) {
+                       var r = e[j].childNodes;
+                       for ( var i = 0; i < r.length; i++ )
+                               t += r[i].nodeType != 1 ?
+                                       r[i].nodeValue : jQuery.fn.text([ r[i] ]);
+               }
                return t;
        },
        
        css: function(a,b) {
-               return  a.constructor != String || b ?
+               return a.constructor != String || b ?
                        this.each(function(){
-                               if ( !b )
+                               if ( b === undefined )
                                        for ( var j in a )
                                                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(){
@@ -147,8 +202,9 @@ jQuery.fn = jQuery.prototype = {
                });
        },
        remove: function() {
-               this.each(function(){this.parentNode.removeChild( this );});
-               return this.pushStack( [] );
+               return this.each(function(){
+                       this.parentNode.removeChild( this );
+               });
        },
        
        wrap: function() {
@@ -223,57 +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,"d.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) {
-               return this.pushStack( jQuery.filter(t,this.cur).r );
+               if ( /,/.test(t) ) {
+                       var p = t.split(/\s*,\s*/);
+                       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).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 );
        },
        
        /**
@@ -286,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;
        },
        
        /**
@@ -318,6 +390,22 @@ jQuery.fn = jQuery.prototype = {
        }
 };
 
+/**
+ * Similar to the Prototype $A() function, only it allows you to
+ * forcefully pass array-like structures into $().
+ */
+jQuery.A = function(a){
+       // Create a temporary, clean, array
+        var r = [];
+
+       // and copy the old array contents over to it
+        for ( var i = 0; i < a.length; i++ )
+                r.push( a[i] );
+
+       // Return the sane jQuery object
+        return $(r);
+};
+
 jQuery.className = {
        add: function(o,c){
                if (jQuery.hasWord(o,c)) return;
@@ -345,42 +433,39 @@ jQuery.className = {
        jQuery.boxModel = ( jQuery.browser != "msie" || document.compatMode == "CSS1Compat" );
 })();
 
+$.swap = function(e,o,f) {
+       for ( var i in o ) {
+               e.style["old"+i] = e.style[i];
+               e.style[i] = o[i];
+       }
+       f.apply( e, [] );
+       for ( var i in o )
+               e.style[i] = e.style["old"+i];
+};
+
 jQuery.css = function(e,p) {
        // Adapted from Prototype 1.4.0
        if ( p == "height" || p == "width" ) {
+               var old = {}, oHeight, oWidth, d = ["Top","Bottom","Right","Left"];
 
-               // Handle extra width/height provided by the W3C box model
-               var ph = (!jQuery.boxModel ? 0 :
-                       jQuery.css(e,"paddingTop") + jQuery.css(e,"paddingBottom") +
-                       jQuery.css(e,"borderTopWidth") + jQuery.css(e,"borderBottomWidth")) || 0;
-
-               var pw = (!jQuery.boxModel ? 0 :
-                       jQuery.css(e,"paddingLeft") + jQuery.css(e,"paddingRight") +
-                       jQuery.css(e,"borderLeftWidth") + jQuery.css(e,"borderRightWidth")) || 0;
-
-               var oHeight, oWidth;
-
-               if (jQuery.css(e,"display") != 'none') {
-                       oHeight = e.offsetHeight || parseInt(e.style.height) || 0;
-                       oWidth = e.offsetWidth || parseInt(e.style.width) || 0;
-               } else {
-                       var els = e.style;
-                       var ov = els.visibility;
-                       var op = els.position;
-                       var od = els.display;
-                       els.visibility = "hidden";
-                       els.position = "absolute";
-                       els.display = "";
-                       oHeight = e.clientHeight || parseInt(e.style.height);
-                       oWidth = e.clientWidth || parseInt(e.style.width);
-                       els.display = od;
-                       els.position = op;
-                       els.visibility = ov;
+               for ( var i in d ) {
+                       old["padding" + d[i]] = 0;
+                       old["border" + d[i] + "Width"] = 0;
                }
 
-               return p == "height" ?
-                       (oHeight - ph < 0 ? 0 : oHeight - ph) :
-                       (oWidth - pw < 0 ? 0 : oWidth - pw);
+               $.swap( e, old, function() {
+                       if (jQuery.css(e,"display") != 'none') {
+                               oHeight = e.offsetHeight;
+                               oWidth = e.offsetWidth;
+                       } else
+                               $.swap( e, { visibility: 'hidden', position: 'absolute', display: '' },
+                                       function(){
+                                               oHeight = e.clientHeight;
+                                               oWidth = e.clientWidth;
+                                       });
+               });
+
+               return p == "height" ? oHeight : oWidth;
        }
        
        var r;
@@ -395,7 +480,7 @@ jQuery.css = function(e,p) {
                r = s ? s.getPropertyValue(p) : null;
        }
        
-       return /top|right|left|bottom/i.test(p) ? parseFloat( r ) : r;
+       return r;
 };
 
 jQuery.clean = function(a) {
@@ -457,9 +542,9 @@ jQuery.g = {
                empty: "!a.childNodes.length",
                root: "a==(a.ownerDocument||document).documentElement",
                contains: "(a.innerText||a.innerHTML).indexOf(m[3])!=-1",
-               visible: "(!a.type||a.type!='hidden')&&(jQuery.css(a,'display')!= 'none'&&jQuery.css(a,'visibility')!= 'hidden')",
-               hidden: "(a.type&&a.type == 'hidden')||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')== 'hidden'",
-               enabled: "a.disabled==false",
+               visible: "(!a.type||a.type!='hidden')&&(jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!= 'hidden')",
+               hidden: "(a.type&&a.type=='hidden')||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')== 'hidden'",
+               enabled: "!a.disabled",
                disabled: "a.disabled",
                checked: "a.checked"
        },
@@ -492,7 +577,13 @@ 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];
 
        if ( !t.indexOf("//") ) {
@@ -511,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;
                
@@ -609,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 )
@@ -725,7 +816,7 @@ jQuery.map = function(a,f) {
        var r = [];
        for ( var i = 0; i < a.length; i++ ) {
                var t = f(a[i],i);
-               if ( t !== null ) {
+               if ( t !== null && t != undefined ) {
                        if ( t.constructor != Array ) t = [t];
                        r = jQuery.merge( t, r );
                }
@@ -752,10 +843,15 @@ jQuery.event = {
                                handlers[0] = element["on" + type];
                }
                handlers[handler.guid] = handler;
-               element["on" + type] = jQuery.event.handle;
+               element["on" + type] = this.handle;
+
+               if (!this.global[type])
+                       this.global[type] = [];
+               this.global[type].push( element );
        },
        
        guid: 1,
+       global: {},
        
        // Detach an event or set of events from an element
        remove: function(element, type, handler) {
@@ -768,25 +864,45 @@ 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(element,type,data) {
-               data = data || [ jQuery.event.fix({ type: type }) ];
-               if ( element && element["on" + type] )
+       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
+               } else if ( element["on" + type] ) {
+                       // Pass along a fake event
+                       data.unshift( this.fix({ type: type, target: element }) );
+
+                       // Trigger the event
                        element["on" + type].apply( element, data );
+               }
        },
        
        handle: function(event) {
-               if ( !event && !window.event ) return;
+               // Handle adding events to items in IFrames, in IE
+               event = event ||
+                       jQuery.event.fix( ((this.ownerDocument || this.document || 
+                               this).parentWindow || window).event );
+
+               // If no correct event was found, fail
+               if ( !event ) return;
        
                var returnValue = true, handlers = [];
-               event = event || jQuery.event.fix(window.event);
        
                for ( var j in this.events[event.type] )
                        handlers[handlers.length] = this.events[event.type][j];
        
-               for ( var i = 0; i < handlers.length; i++ ) {
+               for ( var i = 0; i < handlers.length; i++ )
                        if ( handlers[i].constructor == Function ) {
                                this.handleEvent = handlers[i];
                                if (this.handleEvent(event) === false) {
@@ -795,18 +911,19 @@ jQuery.event = {
                                        returnValue = false;
                                }
                        }
-               }
                return returnValue;
        },
        
        fix: function(event) {
-               event.preventDefault = function() {
-                       this.returnValue = false;
-               };
+               if ( event ) {
+                       event.preventDefault = function() {
+                               this.returnValue = false;
+                       };
                
-               event.stopPropagation = function() {
-                       this.cancelBubble = true;
-               };
+                       event.stopPropagation = function() {
+                               this.cancelBubble = true;
+                       };
+               }
                
                return event;
        }