Extracted the logic for copying events from one jQuery set to another, makes it easie...
[jquery.git] / src / manipulation.js
index 1699f68..baf99d5 100644 (file)
@@ -31,19 +31,21 @@ if ( !jQuery.support.htmlSerialize ) {
 
 jQuery.fn.extend({
        text: function( text ) {
-               if ( typeof text !== "object" && text !== undefined )
+               if ( typeof text !== "object" && text !== undefined ) {
                        return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
+               }
 
                var ret = "";
 
-               jQuery.each( text || this, function(){
-                       jQuery.each( this.childNodes, function(){
-                               if ( this.nodeType !== 8 ) {
-                                       ret += this.nodeType !== 1 ?
-                                               this.nodeValue :
-                                               jQuery.fn.text( [ this ] );
-                               }
-                       });
+               jQuery.each( this, function() {
+                       // Get the text from text nodes and CDATA nodes
+                       if ( this.nodeType === 3 || this.nodeType === 4 ) {
+                               ret += this.nodeValue;
+
+                       // Traverse everything else, except comment nodes
+                       } else if ( this.nodeType !== 8 ) {
+                               ret += jQuery.fn.text.call( this.childNodes );
+                       }
                });
 
                return ret;
@@ -166,21 +168,8 @@ jQuery.fn.extend({
 
                // Copy the events from the original to the clone
                if ( events === true ) {
-                       var orig = this.find("*").andSelf(), i = 0;
-
-                       ret.find("*").andSelf().each(function(){
-                               if ( this.nodeName !== orig[i].nodeName ) { return; }
-
-                               var events = jQuery.data( orig[i], "events" );
-
-                               for ( var type in events ) {
-                                       for ( var handler in events[ type ] ) {
-                                               jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
-                                       }
-                               }
-
-                               i++;
-                       });
+                       cloneCopyEvent( this, ret );
+                       cloneCopyEvent( this.find("*"), ret.find("*") );
                }
 
                // Return the cloned set
@@ -195,7 +184,7 @@ jQuery.fn.extend({
 
                // See if we can take a shortcut and just use innerHTML
                } else if ( typeof value === "string" && !/<script/i.test( value ) &&
-                       (!jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
+                       (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
                        !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
 
                        try {
@@ -282,6 +271,24 @@ jQuery.fn.extend({
        }
 });
 
+function cloneCopyEvent(orig, ret) {
+       var i = 0;
+
+       ret.each(function(){
+               if ( this.nodeName !== orig[i].nodeName ) {
+                       return;
+               }
+
+               var events = jQuery.data( orig[i], "events" );
+
+               for ( var type in events ) {
+                       for ( var handler in events[ type ] ) {
+                               jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
+                       }
+               }
+       });
+}
+
 function buildFragment(args, nodes, scripts){
        var fragment, cacheable, cached, cacheresults, doc;