From 6e0694156a5e6c4a8ee4c2038f5c6da244910160 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Thu, 1 Jun 2023 20:16:35 +0100 Subject: [PATCH] Core: Fix regression in jQuery.text() on HTMLDocument objects Fixes https://github.com/jquery/jquery/issues/5264. --- src/core.js | 9 +++++++-- test/unit/manipulation.js | 7 +++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/core.js b/src/core.js index 8fde2dc453..268b0431f0 100644 --- a/src/core.js +++ b/src/core.js @@ -289,9 +289,14 @@ jQuery.extend( { // Do not traverse comment nodes ret += jQuery.text( node ); } - } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) { + } + if ( nodeType === 1 || nodeType === 11 ) { return elem.textContent; - } else if ( nodeType === 3 || nodeType === 4 ) { + } + if ( nodeType === 9 ) { + return elem.documentElement.textContent; + } + if ( nodeType === 3 || nodeType === 4 ) { return elem.nodeValue; } diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index f330a43e76..8f7e8f663b 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -30,9 +30,9 @@ function manipulationFunctionReturningObj( value ) { 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"; 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)" ); $newLineTest.remove(); + + doc = new DOMParser().parseFromString( "example", "text/html" ); + assert.equal( jQuery( doc ).text(), "example", "text() on HTMLDocument (gh-5264)" ); } ); QUnit.test( "text(undefined)", function( assert ) {