diff --git a/core/misc/ajax.es6.js b/core/misc/ajax.es6.js
index 739afa19747bc2fadafab2228c36d6b55e897706..1dae116a801f2aff344beb1827dbd0d01b9c406e 100644
--- a/core/misc/ajax.es6.js
+++ b/core/misc/ajax.es6.js
@@ -1010,6 +1010,60 @@
throw new Drupal.AjaxError(xmlhttprequest, uri, customMessage);
};
+ /**
+ * Provide a wrapper for new content via Ajax.
+ *
+ * Wrap the inserted markup when inserting multiple root elements with an
+ * ajax effect.
+ *
+ * @param {jQuery} $newContent
+ * Response elements after parsing.
+ * @param {Drupal.Ajax} ajax
+ * {@link Drupal.Ajax} object created by {@link Drupal.ajax}.
+ * @param {object} response
+ * The response from the Ajax request.
+ *
+ * @deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0.
+ * Use data with desired wrapper. See https://www.drupal.org/node/2974880.
+ *
+ * @todo Add deprecation warning after it is possible. For more information
+ * see: https://www.drupal.org/project/drupal/issues/2973400
+ *
+ * @see https://www.drupal.org/node/2940704
+ */
+ Drupal.theme.ajaxWrapperNewContent = ($newContent, ajax, response) => (
+ (response.effect || ajax.effect) !== 'none' &&
+ $newContent.filter(
+ i =>
+ !(
+ // We can not consider HTML comments or whitespace text as separate
+ // roots, since they do not cause visual regression with effect.
+ $newContent[i].nodeName === '#comment' ||
+ ($newContent[i].nodeName === '#text' && /^(\s|\n|\r)*$/.test($newContent[i].textContent))
+ ),
+ ).length > 1
+ ? Drupal.theme('ajaxWrapperMultipleRootElements', $newContent)
+ : $newContent
+ );
+
+ /**
+ * Provide a wrapper for multiple root elements via Ajax.
+ *
+ * @param {jQuery} $elements
+ * Response elements after parsing.
+ *
+ * @deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0.
+ * Use data with desired wrapper. See https://www.drupal.org/node/2974880.
+ *
+ * @todo Add deprecation warning after it is possible. For more information
+ * see: https://www.drupal.org/project/drupal/issues/2973400
+ *
+ * @see https://www.drupal.org/node/2940704
+ */
+ Drupal.theme.ajaxWrapperMultipleRootElements = $elements => (
+ $('
').append($elements)
+ );
+
/**
* @typedef {object} Drupal.AjaxCommands~commandDefinition
*
@@ -1056,39 +1110,24 @@
* A optional jQuery selector string.
* @param {object} [response.settings]
* An optional array of settings that will be used.
- * @param {number} [status]
- * The XMLHttpRequest status.
*/
- insert(ajax, response, status) {
+ insert(ajax, response) {
// Get information from the response. If it is not there, default to
// our presets.
const $wrapper = response.selector ? $(response.selector) : $(ajax.wrapper);
const method = response.method || ajax.method;
const effect = ajax.getEffect(response);
- let settings;
-
- // 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).
- const $newContentWrapped = $('').html(response.data);
- let $newContent = $newContentWrapped.contents();
-
- // For legacy reasons, the effects processing code assumes that
- // $newContent consists of a single top-level element. Also, it has not
- // been sufficiently tested whether attachBehaviors() can be successfully
- // called with a context object that includes top-level text nodes.
- // However, to give developers full control of the HTML appearing in the
- // page, and to enable Ajax content to be inserted in places where
- // elements are not allowed (e.g., within
,
, and
- // parents), we check if the new content satisfies the requirement
- // of a single top-level element, and only use the container
created
- // above when it doesn't. For more information, please see
- // https://www.drupal.org/node/736066.
- if ($newContent.length !== 1 || $newContent.get(0).nodeType !== 1) {
- $newContent = $newContentWrapped;
- }
+
+ // Apply any settings from the returned JSON if available.
+ const settings = response.settings || ajax.settings || drupalSettings;
+
+ // Parse response.data into an element collection.
+ let $newContent = $($.parseHTML(response.data, document, true));
+ // For backward compatibility, in some cases a wrapper will be added. This
+ // behavior will be removed before Drupal 9.0.0. If different behavior is
+ // needed, the theme functions can be overriden.
+ // @see https://www.drupal.org/node/2940704
+ $newContent = Drupal.theme('ajaxWrapperNewContent', $newContent, ajax, response);
// If removing content from the wrapper, detach behaviors first.
switch (method) {
@@ -1097,8 +1136,10 @@
case 'replaceAll':
case 'empty':
case 'remove':
- settings = response.settings || ajax.settings || drupalSettings;
Drupal.detachBehaviors($wrapper.get(0), settings);
+ break;
+ default:
+ break;
}
// Add the new content to the page.
@@ -1111,10 +1152,11 @@
// Determine which effect to use and what content will receive the
// effect, then show the new content.
- if ($newContent.find('.ajax-new-content').length > 0) {
- $newContent.find('.ajax-new-content').hide();
+ const $ajaxNewContent = $newContent.find('.ajax-new-content');
+ if ($ajaxNewContent.length) {
+ $ajaxNewContent.hide();
$newContent.show();
- $newContent.find('.ajax-new-content')[effect.showEffect](effect.showSpeed);
+ $ajaxNewContent[effect.showEffect](effect.showSpeed);
}
else if (effect.showEffect !== 'show') {
$newContent[effect.showEffect](effect.showSpeed);
@@ -1123,10 +1165,13 @@
// Attach all JavaScript behaviors to the new content, if it was
// successfully added to the page, this if statement allows
// `#ajax['wrapper']` to be optional.
- if ($newContent.parents('html').length > 0) {
- // Apply any settings from the returned JSON if available.
- settings = response.settings || ajax.settings || drupalSettings;
- Drupal.attachBehaviors($newContent.get(0), settings);
+ if ($newContent.parents('html').length) {
+ // Attach behaviors to all element nodes.
+ $newContent.each((index, element) => {
+ if (element.nodeType === Node.ELEMENT_NODE) {
+ Drupal.attachBehaviors(element, settings);
+ }
+ });
}
},
diff --git a/core/misc/ajax.js b/core/misc/ajax.js
index abe0ec2928df7cb497910d6be9b3d1cbf38e345f..ddec86d6636ada6d319bdfd7ee63eb8b65bb5556 100644
--- a/core/misc/ajax.js
+++ b/core/misc/ajax.js
@@ -490,20 +490,28 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
throw new Drupal.AjaxError(xmlhttprequest, uri, customMessage);
};
+ Drupal.theme.ajaxWrapperNewContent = function ($newContent, ajax, response) {
+ return (response.effect || ajax.effect) !== 'none' && $newContent.filter(function (i) {
+ return !($newContent[i].nodeName === '#comment' || $newContent[i].nodeName === '#text' && /^(\s|\n|\r)*$/.test($newContent[i].textContent));
+ }).length > 1 ? Drupal.theme('ajaxWrapperMultipleRootElements', $newContent) : $newContent;
+ };
+
+ Drupal.theme.ajaxWrapperMultipleRootElements = function ($elements) {
+ return $('').append($elements);
+ };
+
Drupal.AjaxCommands = function () {};
Drupal.AjaxCommands.prototype = {
- insert: function insert(ajax, response, status) {
+ insert: function insert(ajax, response) {
var $wrapper = response.selector ? $(response.selector) : $(ajax.wrapper);
var method = response.method || ajax.method;
var effect = ajax.getEffect(response);
- var settings = void 0;
- var $newContentWrapped = $('').html(response.data);
- var $newContent = $newContentWrapped.contents();
+ var settings = response.settings || ajax.settings || drupalSettings;
- if ($newContent.length !== 1 || $newContent.get(0).nodeType !== 1) {
- $newContent = $newContentWrapped;
- }
+ var $newContent = $($.parseHTML(response.data, document, true));
+
+ $newContent = Drupal.theme('ajaxWrapperNewContent', $newContent, ajax, response);
switch (method) {
case 'html':
@@ -511,8 +519,10 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
case 'replaceAll':
case 'empty':
case 'remove':
- settings = response.settings || ajax.settings || drupalSettings;
Drupal.detachBehaviors($wrapper.get(0), settings);
+ break;
+ default:
+ break;
}
$wrapper[method]($newContent);
@@ -521,17 +531,21 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
$newContent.hide();
}
- if ($newContent.find('.ajax-new-content').length > 0) {
- $newContent.find('.ajax-new-content').hide();
+ var $ajaxNewContent = $newContent.find('.ajax-new-content');
+ if ($ajaxNewContent.length) {
+ $ajaxNewContent.hide();
$newContent.show();
- $newContent.find('.ajax-new-content')[effect.showEffect](effect.showSpeed);
+ $ajaxNewContent[effect.showEffect](effect.showSpeed);
} else if (effect.showEffect !== 'show') {
$newContent[effect.showEffect](effect.showSpeed);
}
- if ($newContent.parents('html').length > 0) {
- settings = response.settings || ajax.settings || drupalSettings;
- Drupal.attachBehaviors($newContent.get(0), settings);
+ if ($newContent.parents('html').length) {
+ $newContent.each(function (index, element) {
+ if (element.nodeType === Node.ELEMENT_NODE) {
+ Drupal.attachBehaviors(element, settings);
+ }
+ });
}
},
remove: function remove(ajax, response, status) {
diff --git a/core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php b/core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php
index 9b35ef7694fc921b13249c28497b462ea0558ce9..71350305ffee40430a1dc791e911e20bbe1be09b 100644
--- a/core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php
+++ b/core/modules/system/tests/modules/ajax_forms_test/src/Callbacks.php
@@ -27,7 +27,8 @@ public function selectCallback($form, FormStateInterface $form_state) {
*/
public function dateCallback($form, FormStateInterface $form_state) {
$response = new AjaxResponse();
- $response->addCommand(new HtmlCommand('#ajax_date_value', $form_state->getValue('date')));
+ $date = $form_state->getValue('date');
+ $response->addCommand(new HtmlCommand('#ajax_date_value', sprintf('
',
+ ];
+
+ $render_info = [];
+ foreach ($render_single_root as $key => $render) {
+ $render_info[$key] = ['render' => $render, 'effect' => 'fade'];
+ }
+ foreach ($render_multiple_root as $key => $render) {
+ $render_info[$key] = ['render' => $render, 'effect' => 'none'];
+ $render_info["$key--effect"] = ['render' => $render, 'effect' => 'fade'];
+ }
+
+ return $render_info;
+ }
+
}
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormPageCacheTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormPageCacheTest.php
index 06b64af67b2281400740d93247a1549e386e6e24..5a167f729cb92263eed2968543ec0f9a70afc4ef 100644
--- a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormPageCacheTest.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxFormPageCacheTest.php
@@ -54,8 +54,8 @@ public function testSimpleAJAXFormValue() {
// Wait for the DOM to update. The HtmlCommand will update
// #ajax_selected_color to reflect the color change.
- $green_div = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('green')");
- $this->assertNotNull($green_div, 'DOM update: The selected color DIV is green.');
+ $green_span = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('green')");
+ $this->assertNotNull($green_span, 'DOM update: The selected color SPAN is green.');
// Confirm the operation of the UpdateBuildIdCommand.
$build_id_first_ajax = $this->getFormBuildId();
@@ -66,8 +66,8 @@ public function testSimpleAJAXFormValue() {
$session->getPage()->selectFieldOption('select', 'red');
// Wait for the DOM to update.
- $red_div = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('red')");
- $this->assertNotNull($red_div, 'DOM update: The selected color DIV is red.');
+ $red_span = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('red')");
+ $this->assertNotNull($red_span, 'DOM update: The selected color SPAN is red.');
// Confirm the operation of the UpdateBuildIdCommand.
$build_id_second_ajax = $this->getFormBuildId();
@@ -84,8 +84,8 @@ public function testSimpleAJAXFormValue() {
$session->getPage()->selectFieldOption('select', 'green');
// Wait for the DOM to update.
- $green_div2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('green')");
- $this->assertNotNull($green_div2, 'DOM update: After reload - the selected color DIV is green.');
+ $green_span2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('green')");
+ $this->assertNotNull($green_span2, 'DOM update: After reload - the selected color SPAN is green.');
$build_id_from_cache_first_ajax = $this->getFormBuildId();
$this->assertNotEquals($build_id_from_cache_initial, $build_id_from_cache_first_ajax, 'Build id is changed in the simpletest-DOM on first AJAX submission');
@@ -96,8 +96,8 @@ public function testSimpleAJAXFormValue() {
$session->getPage()->selectFieldOption('select', 'red');
// Wait for the DOM to update.
- $red_div2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color div:contains('red')");
- $this->assertNotNull($red_div2, 'DOM update: After reload - the selected color DIV is red.');
+ $red_span2 = $this->assertSession()->waitForElement('css', "#ajax_selected_color:contains('red')");
+ $this->assertNotNull($red_span2, 'DOM update: After reload - the selected color SPAN is red.');
$build_id_from_cache_second_ajax = $this->getFormBuildId();
$this->assertNotEquals($build_id_from_cache_first_ajax, $build_id_from_cache_second_ajax, 'Build id changes on subsequent AJAX submissions');
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php
index fa2a3e45c02fd30b2a9c0496d0a090abbe3e9b61..5d11ce49b4bc758fffdb8864cc3af79f94d4b94d 100644
--- a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php
@@ -82,4 +82,119 @@ public function testDrupalSettingsCachingRegression() {
$this->assertNotContains($fake_library, $libraries);
}
+ /**
+ * Tests that various AJAX responses with DOM elements are correctly inserted.
+ *
+ * After inserting DOM elements, Drupal JavaScript behaviors should be
+ * reattached and all top-level elements of type Node.ELEMENT_NODE need to be
+ * part of the context.
+ */
+ public function testInsertAjaxResponse() {
+ $render_single_root = [
+ 'pre-wrapped-div' => '