Fixed the issue with .text() returning incorrect results.
[jquery.git] / jquery / jquery.js
index f54d05e..06d7504 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * jQuery
+ * jQuery - New Wave Javascript
  *
  * Copyright (c) 2006 John Resig (jquery.com)
  * Licensed under the MIT License:
 // Global undefined variable
 window.undefined = window.undefined;
 
+// Map over the $ in case of overwrite
+if ( $ ) var _$ = $;
+
 /**
  * Create a new jQuery Object
  * @constructor
  */
-function jQuery(a,c) {
+var $ = jQuery = function(a,c) {
+       /*
+       * 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 ( _$ && !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 ) ||
+
+               // Watch for an array being passed in (Prototype 1.5)
+               a.constructor == Array )
+
+                       // Use the default method, in case it works some voodoo
+                       return _$( a );
+
+       // Watch for when a jQuery object is passed in as an arg
+       if ( a && a.jquery )
+               return a;
+       
+       // If the context is global, return a new object
        if ( window == this )
                return new jQuery(a,c);
        
+       // Find the matching elements and save them for later
        this.cur = jQuery.Select(
                a || jQuery.context || document,
                c && c.jquery && c.get(0) || c
        );
 }
 
-/**
- * The jQuery query object.
- */
-var $ = jQuery;
-
 jQuery.fn = jQuery.prototype = {
        /**
         * The current SVN version of jQuery.
@@ -67,7 +92,7 @@ jQuery.fn = jQuery.prototype = {
        },
        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
@@ -85,18 +110,19 @@ jQuery.fn = jQuery.prototype = {
        text: function(e) {
                e = e || this.get();
                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
@@ -129,21 +155,21 @@ jQuery.fn = jQuery.prototype = {
        },
        addClass: function(c) {
                return this.each(function(){
-                       jQuery.class.add(this,c);
+                       jQuery.className.add(this,c);
                });
        },
        removeClass: function(c) {
                return this.each(function(){
-                       jQuery.class.remove(this,c);
+                       jQuery.className.remove(this,c);
                });
        },
 
        toggleClass: function(c) {
                return this.each(function(){
                        if (jQuery.hasWord(this,c))
-                               jQuery.class.remove(this,c);
+                               jQuery.className.remove(this,c);
                        else
-                               jQuery.class.add(this,c);
+                               jQuery.className.add(this,c);
                });
        },
        remove: function() {
@@ -318,10 +344,10 @@ jQuery.fn = jQuery.prototype = {
        }
 };
 
-jQuery.class = {
+jQuery.className = {
        add: function(o,c){
                if (jQuery.hasWord(o,c)) return;
-               o.className += ( o.className.length > 0 ? " " : "" ) + c;
+               o.className += ( o.className ? " " : "" ) + c;
        },
        remove: function(o,c){
                o.className = !c ? "" :
@@ -457,9 +483,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"
        },
@@ -493,7 +519,8 @@ jQuery.token = [
 
 jQuery.Select = function( t, context ) {
        context = context || jQuery.context || document;
-       if ( t.constructor != String ) return [t];
+       if ( t.constructor != String )
+               return t.constructor == Array ? t : [t];
 
        if ( !t.indexOf("//") ) {
                context = context.documentElement;
@@ -725,7 +752,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 );
                }
@@ -778,15 +805,20 @@ jQuery.event = {
        },
        
        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 +827,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;
        }