diff --git a/core/misc/ajax.es6.js b/core/misc/ajax.es6.js index 72da2af326..8c59d13989 100644 --- a/core/misc/ajax.es6.js +++ b/core/misc/ajax.es6.js @@ -988,6 +988,9 @@ * {@link Drupal.Ajax} object created by {@link Drupal.ajax}. * @param {object} response * The response from the Ajax request. + * + * @todo: Mark the function as deprecated after it will be possible. See + * https://www.drupal.org/project/drupal/issues/2973400 */ Drupal.theme.ajaxWrapperNewContent = ($newContent, ajax, response) => ( (response.effect || ajax.effect) !== 'none' && @@ -1008,6 +1011,9 @@ * * @param {jQuery} $elements * Response elements after parsing. + * + * @todo: Mark the function as deprecated after it will be possible. See + * https://www.drupal.org/project/drupal/issues/2973400 */ Drupal.theme.ajaxWrapperMultipleRootElements = ($elements) => ( $('
').append($elements) @@ -1070,7 +1076,7 @@ // Apply any settings from the returned JSON if available. const settings = response.settings || ajax.settings || drupalSettings; - // Parse response.data into an element collection. + // Parse response.data to get element collection. let $newContent = $('
').html(response.data).contents(); $newContent = Drupal.theme('ajaxWrapperNewContent', $newContent, ajax, response); diff --git a/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php index eccc259874..68c85ad3d9 100644 --- a/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php +++ b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php @@ -327,25 +327,33 @@ public function dialogClose() { * Render types. */ protected function getRenderTypes() { - $render_types = [ + $render_single_root = [ 'pre-wrapped-div' => '
pre-wrapped
', 'pre-wrapped-span' => 'pre-wrapped', 'pre-wrapped-whitespace' => '
pre-wrapped-whitespace
' . "\r\n", 'not-wrapped' => 'not-wrapped', 'comment-string-not-wrapped' => 'comment-string-not-wrapped', 'comment-not-wrapped' => '
comment-not-wrapped
', + 'svg' => '', + 'empty' => '', + ]; + $render_multiple_root = [ 'mixed' => ' foo foo bar

some string

additional not wrapped strings,

final string

', 'top-level-only' => '
element #1
element #2
', 'top-level-only-pre-whitespace' => '
element #1
element #2
', 'top-level-only-middle-whitespace-span' => 'element #1 element #2', 'top-level-only-middle-whitespace-div' => '
element #1
element #2
', - 'svg' => '', - 'empty' => '', ]; - foreach ($render_types as $key => $render) { + + $render_info = []; + foreach ($render_single_root as $key => $render) { $render_info[$key] = ['render' => $render, 'effect' => 'fade']; - $render_info["$key--effect-none"] = ['render' => $render, 'effect' => 'none']; } + 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/AjaxTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php index 16b6e11aa2..80bddca2b7 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php @@ -12,6 +12,9 @@ */ class AjaxTest extends JavascriptTestBase { + /** + * {@inheritdoc} + */ protected $minkDefaultDriverClass = DrupalSelenium2Driver::class; /** @@ -93,67 +96,40 @@ public function testDrupalSettingsCachingRegression() { * part of the context. */ public function testInsertAjaxResponse() { - // Test that no additional wrapper is added when inserting already wrapped - // response data and all top-level node elements (context) are processed - // correctly. - $test_cases['pre-wrapped-div'] = '
pre-wrapped
'; - $test_cases['pre-wrapped-span'] = 'pre-wrapped'; - // Test that no additional empty leading div is added when the return - // value had a leading space and all top-level node elements (context) are - // processed correctly. - $test_cases['pre-wrapped-whitespace'] = "
pre-wrapped-whitespace
\n"; - // Test that not wrapped response data (text node) is inserted not wrapped - // and all top-level node elements (context) are processed correctly. - $test_cases['not-wrapped'] = 'not-wrapped'; - // Test that not wrapped response data (text node and comment node) is - // inserted wrapped and all top-level node elements - // (context) are processed correctly. - $test_cases['comment-string-not-wrapped'] = 'comment-string-not-wrapped'; - // Test that top-level comments (which are not lead by text nodes) are - // inserted without wrapper. - $test_cases['comment-not-wrapped'] = '
comment-not-wrapped
'; - // Test that mixed inline & block level elements and comments response data - // is inserted correctly. - $test_cases['mixed'] = ' foo foo bar

some string

additional not wrapped strings,

final string

'; - // Test that when the response has only top-level node elements, they - // are processed properly without extra wrapping. - $test_cases['top-level-only'] = '
element #1
element #2
'; - // Test that whitespaces at start or end don't wrap the response when - // there are multiple top-level nodes. - $test_cases['top-level-only-pre-whitespace'] = '
element #1
element #2
'; - // Test when there are whitespaces between top-level divs. - $test_cases['top-level-only-middle-whitespace-div'] = '
element #1
element #2
'; - // Test when there are whitespaces between top-level spans. - $test_cases['top-level-only-middle-whitespace-span'] = 'element #1 element #2'; - // Test that svg parsed correctly. - $test_cases['svg'] = ''; - // Test that empty response data. - $test_cases['empty'] = ''; - - foreach ($test_cases as $key => $render) { - $test_all_cases["$key--effect-none"] = $render; - } - $test_cases += $test_all_cases; - - $multiple_root_cases = [ - 'mixed', - 'top-level-only', - 'top-level-only-pre-whitespace', - 'top-level-only-middle-whitespace-div', - 'top-level-only-middle-whitespace-span', + $render_single_root = [ + 'pre-wrapped-div' => '
pre-wrapped
', + 'pre-wrapped-span' => 'pre-wrapped', + 'pre-wrapped-whitespace' => '
pre-wrapped-whitespace
' . "\n", + 'not-wrapped' => 'not-wrapped', + 'comment-string-not-wrapped' => 'comment-string-not-wrapped', + 'comment-not-wrapped' => '
comment-not-wrapped
', + 'svg' => '', + 'empty' => '', ]; + $render_multiple_root_unwrapper = [ + 'mixed' => ' foo foo bar

some string

additional not wrapped strings,

final string

', + 'top-level-only' => '
element #1
element #2
', + 'top-level-only-pre-whitespace' => '
element #1
element #2
', + 'top-level-only-middle-whitespace-span' => 'element #1 element #2', + 'top-level-only-middle-whitespace-div' => '
element #1
element #2
', + ]; + $render_multiple_root_wrapper = []; + foreach ($render_multiple_root_unwrapper as $key => $render) { + $render_multiple_root_wrapper["$key--effect"] = '
' . $render . '
'; + } - foreach ($test_cases as $render_type => $expected) { - if (in_array($render_type, $multiple_root_cases, TRUE)) { - $unprocessed_expected = str_replace([' class="processed"', ' processed'], '', $expected); - $expected = '
' . $unprocessed_expected . '
'; - } - // Checking default process of wrapping Ajax content. - $this->assertInsertBlock($render_type, $expected); - $this->assertInsertInline($render_type, $expected); + $expected_renders = array_merge( + $render_single_root, + $render_multiple_root_wrapper, + $render_multiple_root_unwrapper + ); + + // Checking default process of wrapping Ajax content. + foreach ($expected_renders as $render_type => $expected) { + $this->assertInsert($render_type, $expected); } - // Checking custom logic for wrapping Ajax content with multiple root. + // Checking custom ajaxWrapperMultipleRootElements wrapping. $custom_wrapper_multiple_root = <<assertInsertBlock('top-level-only-middle-whitespace-span', $expected, $custom_wrapper_multiple_root); - $this->assertInsertInline('top-level-only-middle-whitespace-span', $expected, $custom_wrapper_multiple_root); + $expected = '
element #1 element #2
'; + $this->assertInsert('top-level-only-middle-whitespace-span--effect', $expected, $custom_wrapper_multiple_root); - // Checking that we can fully control the process of wrapping Ajax content. + // Checking custom ajaxWrapperNewContent wrapping. $custom_wrapper_new_content = <<assertInsertBlock('empty--effect-none', $expected, $custom_wrapper_new_content); - $this->assertInsertInline('empty--effect-none', $expected, $custom_wrapper_new_content); + $expected = '
'; + $this->assertInsert('empty', $expected, $custom_wrapper_new_content); } /** - * Assert block insert. + * Assert insert. * * @param string $render_type * Render type. * @param string $expected * Expected result. * @param string $script - * Script for theming multiple root elements. + * Script for additional theming. */ - public function assertInsertBlock($render_type, $expected, $script = '') { + public function assertInsert($render_type, $expected, $script = '') { + // Check insert to block element. $this->drupalGet('ajax-test/insert-block-wrapper'); $this->getSession()->executeScript($script); $this->clickLink("Link html $render_type"); @@ -198,19 +173,8 @@ public function assertInsertBlock($render_type, $expected, $script = '') { $this->getSession()->executeScript($script); $this->clickLink("Link replaceWith $render_type"); $this->assertWaitPageContains('
' . $expected . '
'); - } - /** - * Assert inline insert. - * - * @param string $render_type - * Render type. - * @param string $expected - * Expected result. - * @param string $script - * Script for theming multiple root elements. - */ - public function assertInsertInline($render_type, $expected, $script = '') { + // Check insert to inline element. $this->drupalGet('ajax-test/insert-inline-wrapper'); $this->getSession()->executeScript($script); $this->clickLink("Link html $render_type"); @@ -231,8 +195,8 @@ public function assertInsertInline($render_type, $expected, $script = '') { protected function assertWaitPageContains($expected) { $page = $this->getSession()->getPage(); $this->assertTrue($page->waitFor(10, function () use ($page, $expected) { - // After the effects can remain empty styles. - $content = str_replace(' style=""', '', $page->getContent()); + // Clear content from empty styles and "processed" classes after effect. + $content = str_replace([' class="processed"', ' processed', ' style=""'], '', $page->getContent()); return stripos($content, $expected) !== FALSE; }), "Page contains expected value: $expected"); }