Added support for .text() on text nodes. Fixes #5525.
[jquery.git] / src / manipulation.js
index 547acc1..2a6b9dc 100644 (file)
@@ -1,10 +1,10 @@
 var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
        rleadingWhitespace = /^\s+/,
-       rxhtmlTag = /(<(\w+)[^>]*?)\/>/g,
+       rxhtmlTag = /(<([\w:]+)[^>]*?)\/>/g,
        rselfClosing = /^(?:abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i,
-       rtagName = /<(\w+)/,
+       rtagName = /<([\w:]+)/,
        rtbody = /<tbody/i,
-       rhtml = /</,
+       rhtml = /<|&\w+;/,
        fcloseTag = function(all, front, tag){
                return rselfClosing.test(tag) ?
                        all :
@@ -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;
@@ -90,6 +92,14 @@ jQuery.fn.extend({
                });
        },
 
+       unwrap: function() {
+               return this.parent().each(function(){
+                       if ( !jQuery.nodeName( this, "body" ) ) {
+                               jQuery( this ).replaceWith( this.childNodes );
+                       }
+               }).end();
+       },
+       
        append: function() {
                return this.domManip(arguments, true, function(elem){
                        if ( this.nodeType === 1 ) {
@@ -325,7 +335,7 @@ jQuery.each({
 jQuery.each({
        // keepData is for internal use only--do not document
        remove: function( selector, keepData ) {
-               if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
+               if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
                        if ( !keepData && this.nodeType === 1 ) {
                                cleanData( this.getElementsByTagName("*") );
                                cleanData( [ this ] );