Problem/Motivation
Found in #3406612: Exceptions in batch no longer are shown on the page: Javascript error where we had to use olivero as the test them because the message.js does not work with the stark theme.
This is because core/modules/system/templates/status-messages.html.twig does not print anything under the top level div if there are no messages.
in core/misc/message.js we have
return wrapper.innerHTML === ''
? Drupal.Message.messageInternalWrapper(wrapper)
: wrapper.firstElementChild;
But wrapper.innerHTML !== '' because there is return character. Any other character would also make it break. So in this case wrapper.firstElementChild will be NULL.
We could change the template but any contrib or custom template with this would also break.
Steps to reproduce
- Using the stark theme (or many others)
- On a fresh page load, run
(new Drupal.Message()).clear();in the browsers JS console - The code will go this.messageWrapper.querySelectorAll, but messageWrapper will be null
Proposed resolution
Check wrapper.childElementCount === 0 instead of wrapper.innerHTML === ''
Remaining tasks
Review
Issue fork drupal-3407067
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3407067-message.js-assumes-status
changes, plain diff MR !5741
Comments
Comment #3
tedbowComment #4
tedbowComment #5
tedbowComment #6
smustgrave commentedNo test failure is probably a good sign.
Could steps to reproduce be added please.
Comment #7
tim bozeman commentedAdded testing instructions.
Comment #8
tim bozeman commentedComment #9
tim bozeman commentedComment #10
smustgrave commentedThanks for adding those! Was previously tagged for tests too in #3
Comment #11
tim bozeman commentedThank you smustgrave
I added a test and it looks like they are all passing.
Comment #12
smustgrave commentedPossible to merge this into JsMessageTest to keep things well grouped.
Comment #15
quietone commentedCame across this while working on deprecating Olivero.
For #12, I simply renamed the test file, mostly because the existing test has setup function that this new test does not need.
Comment #16
smustgrave commentedSmall comments on the MR.
Comment #17
quietone commentedComment #18
quietone commentedComment #19
quietone commentedBack to needs review for the recent changes.
Comment #20
gábor hojtsyI manually code reviewed this MR. The changes look good and simple. @smustgrave's concerns have been resolved fine.
Comment #24
godotislateI kinda feel like the test should have been appended to the end of
JsMessageTest::testAddRemoveMessages(), with the test method possibly renamed for purpose, so that we save a browser test bootstrap. But I don't think it's enough to block here, and we can refactor the test in the future if we really need to for CI run optimization.Committed and pushed 970ac36 to main and 09c7a1d to 11.x. Thanks!