diff -u b/core/misc/ajax.es6.js b/core/misc/ajax.es6.js --- b/core/misc/ajax.es6.js +++ b/core/misc/ajax.es6.js @@ -1062,13 +1062,13 @@ // When there are other types of top-level nodes, the response needs to be // wrapped. else { - const hasCommentNodes = responseDataNodes.some( - element => element.nodeType === Node.COMMENT_NODE, + const onlyTextOrCommentNodes = responseDataNodes.every( + element => element.nodeType === Node.COMMENT_NODE || element.nodeType === Node.TEXT_NODE, ); - // If response.data contains only one TEXT_ELEMENT, TEXT_ELEMENT and + // If response.data contains only nodes of the type TEXT_NODE or // COMMENT_NODE, or the wrapping element is not styled as a block, // response.data will be wrapped with SPAN. - if (responseDataNodes.length === 1 || hasCommentNodes || $wrapper.css('display') !== 'block') { + if (onlyTextOrCommentNodes || $wrapper.css('display') !== 'block') { $newContent = $(''); } else { diff -u b/core/misc/ajax.js b/core/misc/ajax.js --- b/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -500,11 +500,11 @@ if (onlyElementNodes) { $newContent = $(trimmedData); } else { - var hasCommentNodes = responseDataNodes.some(function (element) { - return element.nodeType === Node.COMMENT_NODE; + var onlyTextOrCommentNodes = responseDataNodes.every(function (element) { + return element.nodeType === Node.COMMENT_NODE || element.nodeType === Node.TEXT_NODE; }); - if (responseDataNodes.length === 1 || hasCommentNodes || $wrapper.css('display') !== 'block') { + if (onlyTextOrCommentNodes || $wrapper.css('display') !== 'block') { $newContent = $(''); } else { $newContent = $('
'); diff -u b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php --- b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php @@ -149,7 +149,7 @@ [ 'method' => 'html', 'render_type' => 'mixed', - 'expected' => ' foo foo barsome string
final string
', + 'expected' => 'some string
final string