Skip to content
Permalink
Browse files

Added support for .text() on text nodes. Fixes #5525.

  • Loading branch information
jeresig committed Nov 17, 2009
1 parent c084745 commit b30af34f28074b491929445f5aad3d62c63e772f
Showing with 15 additions and 10 deletions.
  1. +11 −9 src/manipulation.js
  2. +4 −1 test/unit/manipulation.js
@@ -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;
@@ -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) {

1 comment on commit b30af34

@genezys

This comment has been minimized.

Copy link

@genezys genezys commented on b30af34 Nov 18, 2009

"Text node was retrieved from .text()."

I understand IE can bother you but this one is ok :-)

Please sign in to comment.
You can’t perform that action at this time.