diff --git a/core/lib/Drupal/Core/Render/Element/StatusMessages.php b/core/lib/Drupal/Core/Render/Element/StatusMessages.php index 530457f..cd3bf52 100644 --- a/core/lib/Drupal/Core/Render/Element/StatusMessages.php +++ b/core/lib/Drupal/Core/Render/Element/StatusMessages.php @@ -75,19 +75,17 @@ public static function generatePlaceholder(array $element) { public static function renderMessages($type) { $render = []; $messages = drupal_get_messages($type); - if ($messages) { - // Render the messages. - $render = [ - '#theme' => 'status_messages', - // @todo Improve when https://www.drupal.org/node/2278383 lands. - '#message_list' => $messages, - '#status_headings' => [ - 'status' => t('Status message'), - 'error' => t('Error message'), - 'warning' => t('Warning message'), - ], - ]; - } + // Render the messages. + $render = [ + '#theme' => 'status_messages', + // @todo Improve when https://www.drupal.org/node/2278383 lands. + '#message_list' => $messages, + '#status_headings' => [ + 'status' => t('Status message'), + 'error' => t('Error message'), + 'warning' => t('Warning message'), + ], + ]; return $render; } diff --git a/core/misc/announce.js b/core/misc/announce.js index 0f10748..7425a43 100644 --- a/core/misc/announce.js +++ b/core/misc/announce.js @@ -107,9 +107,6 @@ * A string to indicate the priority of the message. Can be either * 'polite' or 'assertive'. * - * @return {function} - * The return of the call to debounce. - * * @see http://www.w3.org/WAI/PF/aria-practices/#liveprops */ Drupal.announce = function (text, priority) { diff --git a/core/misc/message.js b/core/misc/message.js index 77b0745..e7c13b3 100644 --- a/core/misc/message.js +++ b/core/misc/message.js @@ -4,11 +4,11 @@ */ (function ($, Drupal, debounce) { - "use strict"; + 'use strict'; - var messages = [], - messagesElement, - debouncedProcessMessages; + var messages = []; + var messagesElement; + var debouncedProcessMessages; /** * Builds a div element with the aria-live attribute and attaches it to the DOM. @@ -16,10 +16,10 @@ Drupal.behaviors.drupalMessage = { attach: function () { if (!messagesElement) { - messagesElement = $('[data-drupal-messages]'); - if (!messagesElement) { - throw new Error(Drupal.t("There is no element with a [data-drupal-messages] attribute")); + if (!$('[data-drupal-messages]').length) { + throw new Error(Drupal.t('There is no element with a [data-drupal-messages] attribute')); } + messagesElement = $('[data-drupal-messages]')[0]; } } }; @@ -28,9 +28,11 @@ * Displays all queued messages in one pass instead of one after the other. * @see debouncedProcessMessages */ - function processMessages () { - var text = [], - message, messagesIndex, messagesLength; + function processMessages() { + var text = []; + var message; + var messagesIndex; + var messagesLength; if (messages.length) { for (messagesIndex = 0, messagesLength = messages.length; messagesIndex < messagesLength; messagesIndex++) { @@ -84,7 +86,7 @@ Drupal.message.remove = function (context, type) { context = context || 'default'; type = type || 'status'; - $(".messages--" + message.type + ".js-messages-context--" + message.context).remove(); + $('.messages--' + type + '.js-messages-context--' + context).remove(); }; /** diff --git a/core/modules/system/tests/modules/js_message_test/js/js_message_test.js b/core/modules/system/tests/modules/js_message_test/js/js_message_test.js new file mode 100644 index 0000000..4f54544 --- /dev/null +++ b/core/modules/system/tests/modules/js_message_test/js/js_message_test.js @@ -0,0 +1,33 @@ +/** + * @file + * Testing behavior for JSMessageTest. + */ + +(function ($, message) { + + 'use strict'; + + /** + * @type {Drupal~behavior} + * + * @prop {Drupal~behaviorAttach} attach + * Add click listeners that show and remove links with context and type. + */ + Drupal.behaviors.js_message_test = { + attach: function (context) { + $('.show-link').once('show-msg').on('click', function (e) { + e.preventDefault(); + var type = e.currentTarget.getAttribute('data-type'); + var context = e.currentTarget.getAttribute('data-context'); + message.add('Msg-' + type + '-' + context, type, context); + }); + $('.remove-link').once('remove-msg').on('click', function (e) { + e.preventDefault(); + var type = e.currentTarget.getAttribute('data-type'); + var context = e.currentTarget.getAttribute('data-context'); + message.remove(context, type); + }); + } + }; + +})(jQuery, Drupal.message); diff --git a/core/modules/system/tests/modules/js_message_test/js_message_test.info.yml b/core/modules/system/tests/modules/js_message_test/js_message_test.info.yml new file mode 100644 index 0000000..e8bc73b --- /dev/null +++ b/core/modules/system/tests/modules/js_message_test/js_message_test.info.yml @@ -0,0 +1,6 @@ +name: 'JS Message test module' +type: module +description: 'Module for the JSMessageTest test.' +package: Testing +version: VERSION +core: 8.x diff --git a/core/modules/system/tests/modules/js_message_test/js_message_test.libraries.yml b/core/modules/system/tests/modules/js_message_test/js_message_test.libraries.yml new file mode 100644 index 0000000..33c49e0 --- /dev/null +++ b/core/modules/system/tests/modules/js_message_test/js_message_test.libraries.yml @@ -0,0 +1,6 @@ +show_message: + version: VERSION + js: + js/js_message_test.js: {} + dependencies: + - core/drupal.message diff --git a/core/modules/system/tests/modules/js_message_test/js_message_test.routing.yml b/core/modules/system/tests/modules/js_message_test/js_message_test.routing.yml new file mode 100644 index 0000000..4c325e8 --- /dev/null +++ b/core/modules/system/tests/modules/js_message_test/js_message_test.routing.yml @@ -0,0 +1,7 @@ +js_message_test.links: + path: '/js_message_test_link' + defaults: + _controller: '\Drupal\js_message_test\Controller\JSMessageTestController::messageLinks' + _title: 'JsMessageLinks' + requirements: + _access: 'TRUE' diff --git a/core/modules/system/tests/modules/js_message_test/src/Controller/JSMessageTestController.php b/core/modules/system/tests/modules/js_message_test/src/Controller/JSMessageTestController.php new file mode 100644 index 0000000..839ef4f --- /dev/null +++ b/core/modules/system/tests/modules/js_message_test/src/Controller/JSMessageTestController.php @@ -0,0 +1,56 @@ + "Show-$context-$type", + 'url' => Url::fromRoute('js_message_test.links'), + 'attributes' => [ + 'id' => "show-$context-$type", + 'data-context' => $context, + 'data-type' => $type, + 'class' => ['show-link'], + ], + ]; + $links["remove-$context-$type"] = [ + 'title' => "Remove-$context-$type", + 'url' => Url::fromRoute('js_message_test.links'), + 'attributes' => [ + 'id' => "remove-$context-$type", + 'data-context' => $context, + 'data-type' => $type, + 'class' => ['remove-link'], + ], + ]; + } + + } + return [ + '#theme' => 'links', + '#links' => $links, + '#attached' => [ + 'library' => [ + 'js_message_test/show_message', + ], + ], + ]; + } + +} diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Core/JsMessageTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Core/JsMessageTest.php new file mode 100644 index 0000000..fc8746f --- /dev/null +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/JsMessageTest.php @@ -0,0 +1,39 @@ +assertSession(); + $this->drupalGet('js_message_test_link'); + $web_assert->elementExists('css', '[data-drupal-messages]'); + $contexts = ['context1', 'context2']; + $types = ['status', 'error', 'warning']; + foreach ($contexts as $context) { + foreach ($types as $type) { + $this->clickLink("Show-$context-$type"); + $this->getSession()->wait('200'); + $selector = ".messages.messages--$type.js-messages.js-messages-context--$context"; + $msg_element = $web_assert->waitForElementVisible('css', $selector); + $this->assertNotEmpty($msg_element, "Message element visible: $selector"); + $web_assert->elementContains('css', $selector, "Msg-$type-$context"); + // @todo Test clicking remove links. + } + } + } + +}