From fb2e0a0c2864846af701203d3708da5dab3e6ba2 Mon Sep 17 00:00:00 2001 From: Chris Antaki Date: Wed, 30 Oct 2013 01:16:05 -0700 Subject: [PATCH] Fix #11809: Update text without creating DOM nodes. Close gh-1412. --- src/manipulation.js | 6 +++++- test/unit/manipulation.js | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/manipulation.js b/src/manipulation.js index 3de597f0f..738864293 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -314,7 +314,11 @@ jQuery.fn.extend({ return access( this, function( value ) { return value === undefined ? jQuery.text( this ) : - this.empty().append( ( this[ 0 ] && this[ 0 ].ownerDocument || document ).createTextNode( value ) ); + this.empty().each(function() { + if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) { + this.textContent = value; + } + }); }, null, value, arguments.length ); }, diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index ad89bd1be..ca84d4bc1 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -64,9 +64,9 @@ test( "text(undefined)", function() { function testText( valueObj ) { - expect( 4 ); + expect( 7 ); - var val, j; + var val, j, expected, $multipleElements, $parentDiv, $childDiv; val = valueObj("
Hello cruel world!
"); equal( jQuery("#foo").text(val)[ 0 ].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" ); @@ -79,6 +79,24 @@ function testText( valueObj ) { // Blackberry 4.6 doesn't maintain comments in the DOM equal( jQuery("#nonnodes")[ 0 ].childNodes.length < 3 ? 8 : j[ 2 ].nodeType, 8, "Check node,textnode,comment with text()" ); + + // Update multiple elements #11809 + expected = "New"; + + $multipleElements = jQuery( "
Hello
" ).add( "
World
" ); + $multipleElements.text( expected ); + + equal( $multipleElements.eq(0).text(), expected, "text() updates multiple elements (#11809)" ); + equal( $multipleElements.eq(1).text(), expected, "text() updates multiple elements (#11809)" ); + + // Prevent memory leaks #11809 + $childDiv = jQuery( "
" ); + $childDiv.data("leak", true); + $parentDiv = jQuery( "
" ); + $parentDiv.append( $childDiv ); + $parentDiv.text("Dry off"); + + equal( $childDiv.data("leak"), undefined, "Check for leaks (#11809)" ); } test( "text(String)", function() { -- 2.39.5