Skip to content

Commit b30af34

Browse files
committed
Added support for .text() on text nodes. Fixes #5525.
1 parent c084745 commit b30af34

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/manipulation.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@ if ( !jQuery.support.htmlSerialize ) {
3131

3232
jQuery.fn.extend({
3333
text: function( text ) {
34-
if ( typeof text !== "object" && text !== undefined )
34+
if ( typeof text !== "object" && text !== undefined ) {
3535
return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
36+
}
3637

3738
var ret = "";
3839

39-
jQuery.each( text || this, function(){
40-
jQuery.each( this.childNodes, function(){
41-
if ( this.nodeType !== 8 ) {
42-
ret += this.nodeType !== 1 ?
43-
this.nodeValue :
44-
jQuery.fn.text( [ this ] );
45-
}
46-
});
40+
jQuery.each( this, function() {
41+
// Get the text from text nodes and CDATA nodes
42+
if ( this.nodeType === 3 || this.nodeType === 4 ) {
43+
ret += this.nodeValue;
44+
45+
// Traverse everything else, except comment nodes
46+
} else if ( this.nodeType !== 8 ) {
47+
ret += jQuery.fn.text.call( this.childNodes );
48+
}
4749
});
4850

4951
return ret;

test/unit/manipulation.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ var bareObj = function(value) { return value; };
44
var functionReturningObj = function(value) { return (function() { return value; }); };
55

66
test("text()", function() {
7-
expect(1);
7+
expect(2);
88
var expected = "This link has class=\"blog\": Simon Willison's Weblog";
99
equals( jQuery('#sap').text(), expected, 'Check for merged text of more then one element.' );
10+
11+
// Check serialization of text values
12+
equals( jQuery(document.createTextNode("foo")).text(), "foo", "Text node was retreived from .text()." );
1013
});
1114

1215
var testWrap = function(val) {

0 commit comments

Comments
 (0)