diff -u b/core/misc/ajax.js b/core/misc/ajax.js --- b/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -1041,12 +1041,28 @@ // https://www.drupal.org/node/736066. var $responseDataWrapped = $('
').html(response.data); - var $new_content = $responseDataWrapped.contents() - .filter(function (index, value) { - return value.nodeType === 1; - }).length === 1 && $responseDataWrapped.contents().length === 1 ? - $responseDataWrapped.children() : - $('
').html(response.data); + var $new_content = $('
'); + + var createWrapper = function () { + return $('
'); + }; + + var wrapper = null; + $responseDataWrapped.contents().each(function (index, value) { + if (wrapper && value.nodeType !== 1) { + wrapper.append(value); + } + if (!wrapper && value.nodeType !== 1) { + wrapper = createWrapper(); + wrapper.append(value); + } + if (wrapper && value.nodeType === 1) { + $new_content.append(wrapper, value); + wrapper = null; + } + }); + + $new_content = $new_content.children(); // If removing content from the wrapper, detach behaviors first. switch (method) { diff -u b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php --- b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php +++ b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php @@ -63,13 +63,14 @@ break; case 'mixed': - $markup = 'outside
inside
'; + $markup = ' what? a some text

some more text

ok and now some more,

ok

'; break; } $content = [ '#title' => 'AJAX Dialog & contents', 'content' => [ - '#markup' => $markup, + '#type' => 'inline_template', + '#template' => $markup, ], ]; 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 @@ -104,7 +104,7 @@ $this->clickLink('Link 3 (mixed)'); $assert->assertWaitOnAjaxRequest(); $assert->responseContains('not-wrapped'); - $assert->responseContains('
outside
inside
'); + $assert->responseContains('whatwhat'); }