Core: Fix regression in jQuery.text() on HTMLDocument objects

Fixes gh-5264
Closes gh-5265

(cherry picked from commit 44c56f87a3)
This commit is contained in:
Timo Tijhof 2023-06-12 22:12:15 +01:00 committed by Michał Gołębiowski-Owczarek
parent 338de35990
commit a75d6b52fa
No known key found for this signature in database
2 changed files with 12 additions and 4 deletions

View File

@ -271,9 +271,14 @@ jQuery.extend( {
// Do not traverse comment nodes // Do not traverse comment nodes
ret += jQuery.text( node ); ret += jQuery.text( node );
} }
} else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) { }
if ( nodeType === 1 || nodeType === 11 ) {
return elem.textContent; return elem.textContent;
} else if ( nodeType === 3 || nodeType === 4 ) { }
if ( nodeType === 9 ) {
return elem.documentElement.textContent;
}
if ( nodeType === 3 || nodeType === 4 ) {
return elem.nodeValue; return elem.nodeValue;
} }

View File

@ -30,9 +30,9 @@ function manipulationFunctionReturningObj( value ) {
QUnit.test( "text()", function( assert ) { QUnit.test( "text()", function( assert ) {
assert.expect( 5 ); assert.expect( 6 );
var expected, frag, $newLineTest; var expected, frag, $newLineTest, doc;
expected = "This link has class=\"blog\": Simon Willison's Weblog"; expected = "This link has class=\"blog\": Simon Willison's Weblog";
assert.equal( jQuery( "#sap" ).text(), expected, "Check for merged text of more then one element." ); assert.equal( jQuery( "#sap" ).text(), expected, "Check for merged text of more then one element." );
@ -52,6 +52,9 @@ QUnit.test( "text()", function( assert ) {
assert.equal( $newLineTest.text(), "test\ntesty", "text() does not remove new lines (trac-11153)" ); assert.equal( $newLineTest.text(), "test\ntesty", "text() does not remove new lines (trac-11153)" );
$newLineTest.remove(); $newLineTest.remove();
doc = new DOMParser().parseFromString( "<span>example</span>", "text/html" );
assert.equal( jQuery( doc ).text(), "example", "text() on HTMLDocument (gh-5264)" );
} ); } );
QUnit.test( "text(undefined)", function( assert ) { QUnit.test( "text(undefined)", function( assert ) {