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
@@ -1020,68 +1020,8 @@
// Apply any settings from the returned JSON if available.
const settings = response.settings || ajax.settings || drupalSettings;
- // We don't know what response.data contains: it might be a string of text
- // without HTML, so don't rely on jQuery correctly interpreting
- // $(response.data) as new HTML rather than a CSS selector. Also, if
- // response.data contains top-level text nodes, they get lost with either
- // $(response.data) or $('
').replaceWith(response.data).
- // attachBehaviors() can, for the same reason, not be called with a
- // context object that includes top-level text nodes. Therefore text nodes
- // will be wrapped with a element. This also gives themers the
- // possibility to style the response.
- let $newContent;
- // We use the trimmed data to be able to detect cases when the response
- // has only top-level elements or comment nodes with extra whitespace
- // around. In that case whitespaces are removed and the response
- // should not be wrapped.
- // For example:
- // ' content
' is equivalent to 'content
' and
- // no extra wrapper will be added.
- const trimmedData = response.data.trim();
- // List of DOM nodes contained in the response for processing.
- let responseDataNodes;
-
- // When 'data' is empty or is only whitespace, manually create the node
- // array because parseHTML would return null.
- if (trimmedData === '') {
- responseDataNodes = [document.createTextNode(response.data)];
- }
- else {
- responseDataNodes = $.parseHTML(trimmedData, true);
- }
- // Check every node for it's type to decide if wrapping is necessary.
- const onlyElementNodes = responseDataNodes.every(
- element => element.nodeType === Node.ELEMENT_NODE
- || element.nodeType === Node.COMMENT_NODE
- // Ignore any TEXT_NODE that only contains whitespace.
- || (element.nodeType === Node.TEXT_NODE && element.textContent.trim().length === 0),
- );
- // Find the first node that is not COMMENT_NODE.
- const firstNonCommentNode = responseDataNodes.filter(element => element.nodeType !== Node.COMMENT_NODE)[0];
-
- // When there are only element and/or comment nodes in the response or if
- // the method is 'html' and the element can be safely converted a jQuery
- // object, no extra wrapping necessary.
- if (onlyElementNodes || (method === 'html' && firstNonCommentNode.nodeType === Node.ELEMENT_NODE)) {
- $newContent = $(trimmedData);
- }
- // When there are other types of top-level nodes, the response needs to be
- // wrapped.
- else {
- const onlyTextOrCommentNodes = responseDataNodes.every(
- element => element.nodeType === Node.COMMENT_NODE || element.nodeType === Node.TEXT_NODE,
- );
- // If response.data contains only nodes of the type TEXT_NODE or
- // COMMENT_NODE, response.data will be wrapped with SPAN.
- if (onlyTextOrCommentNodes) {
- $newContent = $('');
- }
- else {
- $newContent = $('');
- }
- // Use response.data to keep whitespace as-is.
- $newContent.html(response.data);
- }
+ // Parse response.data into an element collection.
+ const $newContent = $($.parseHTML(response.data));
// If removing content from the wrapper, detach behaviors first.
switch (method) {
diff -u b/core/misc/ajax.js b/core/misc/ajax.js
--- b/core/misc/ajax.js
+++ b/core/misc/ajax.js
@@ -481,41 +481,7 @@
var settings = response.settings || ajax.settings || drupalSettings;
- var $newContent = void 0;
-
- var trimmedData = response.data.trim();
-
- var responseDataNodes = void 0;
-
- if (trimmedData === '') {
- responseDataNodes = [document.createTextNode(response.data)];
- } else {
- responseDataNodes = $.parseHTML(trimmedData, true);
- }
-
- var onlyElementNodes = responseDataNodes.every(function (element) {
- return element.nodeType === Node.ELEMENT_NODE || element.nodeType === Node.COMMENT_NODE || element.nodeType === Node.TEXT_NODE && element.textContent.trim().length === 0;
- });
-
- var firstNonCommentNode = responseDataNodes.filter(function (element) {
- return element.nodeType !== Node.COMMENT_NODE;
- })[0];
-
- if (onlyElementNodes || method === 'html' && firstNonCommentNode.nodeType === Node.ELEMENT_NODE) {
- $newContent = $(trimmedData);
- } else {
- var onlyTextOrCommentNodes = responseDataNodes.every(function (element) {
- return element.nodeType === Node.COMMENT_NODE || element.nodeType === Node.TEXT_NODE;
- });
-
- if (onlyTextOrCommentNodes) {
- $newContent = $('');
- } else {
- $newContent = $('');
- }
-
- $newContent.html(response.data);
- }
+ var $newContent = $($.parseHTML(response.data));
switch (method) {
case 'html':