]> source.dussan.org Git - jquery.git/commitdiff
Added support for .text() on text nodes. Fixes #5525.
authorJohn Resig <jeresig@gmail.com>
Tue, 17 Nov 2009 19:52:08 +0000 (14:52 -0500)
committerJohn Resig <jeresig@gmail.com>
Tue, 17 Nov 2009 19:52:08 +0000 (14:52 -0500)
src/manipulation.js
test/unit/manipulation.js

index 1699f680189012a72ea94334ea1e82affe6bf8de..2a6b9dc3009064efa1e995bfe117690158bad5e2 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;
index 0f11de41c304f2882b7b8091b96393210b6d08c4..49976345d55e4e3c1ccbe9171706405c5b43dbb7 100644 (file)
@@ -4,9 +4,12 @@ var bareObj = function(value) { return value; };
 var functionReturningObj = function(value) { return (function() { return value; }); };
 
 test("text()", function() {
-       expect(1);
+       expect(2);
        var expected = "This link has class=\"blog\": Simon Willison's Weblog";
        equals( jQuery('#sap').text(), expected, 'Check for merged text of more then one element.' );
+
+       // Check serialization of text values
+       equals( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retreived from .text()." );
 });
 
 var testWrap = function(val) {