mirror of
https://github.com/jquery/jquery.git
synced 2025-01-10 18:24:24 +00:00
Fix #11809: Update text without creating DOM nodes. Close gh-1412.
This commit is contained in:
parent
19c1b6109a
commit
fb2e0a0c28
@ -314,7 +314,11 @@ jQuery.fn.extend({
|
|||||||
return access( this, function( value ) {
|
return access( this, function( value ) {
|
||||||
return value === undefined ?
|
return value === undefined ?
|
||||||
jQuery.text( this ) :
|
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 );
|
}, null, value, arguments.length );
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -64,9 +64,9 @@ test( "text(undefined)", function() {
|
|||||||
|
|
||||||
function testText( valueObj ) {
|
function testText( valueObj ) {
|
||||||
|
|
||||||
expect( 4 );
|
expect( 7 );
|
||||||
|
|
||||||
var val, j;
|
var val, j, expected, $multipleElements, $parentDiv, $childDiv;
|
||||||
|
|
||||||
val = valueObj("<div><b>Hello</b> cruel world!</div>");
|
val = valueObj("<div><b>Hello</b> cruel world!</div>");
|
||||||
equal( jQuery("#foo").text(val)[ 0 ].innerHTML.replace(/>/g, ">"), "<div><b>Hello</b> cruel world!</div>", "Check escaped text" );
|
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
|
// 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()" );
|
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( "<div>Hello</div>" ).add( "<div>World</div>" );
|
||||||
|
$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( "<div/>" );
|
||||||
|
$childDiv.data("leak", true);
|
||||||
|
$parentDiv = jQuery( "<div/>" );
|
||||||
|
$parentDiv.append( $childDiv );
|
||||||
|
$parentDiv.text("Dry off");
|
||||||
|
|
||||||
|
equal( $childDiv.data("leak"), undefined, "Check for leaks (#11809)" );
|
||||||
}
|
}
|
||||||
|
|
||||||
test( "text(String)", function() {
|
test( "text(String)", function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user