Issue Summary
I ran into this error using the following code in an attempt to clear any existing messages before adding another via the new JS Messages API.
var messages = new Drupal.Message();
messages.clear(); // Throws exception - Uncaught TypeError: Cannot read property 'querySelectorAll' of null
This is a result of the fact that in the constructor: the messageWrapper can be initialized as null.
Drupal.Message = class {
constructor(messageWrapper = null) {
this.messageWrapper = messageWrapper;
}
But in the clear function there is no check on a null value as exists in other functions such as add().
clear() {
Array.prototype.forEach.call(
this.messageWrapper.querySelectorAll('[data-drupal-message-id]'),
message => {
this.messageWrapper.removeChild(message);
},
);
}
Proposed Resolution
Minimally, the clear function should be updated with a defaultWrapper() call similar to add().
clear() {
if (!this.messageWrapper) {
this.messageWrapper = Drupal.Message.defaultWrapper();
}
Array.prototype.forEach.call(
this.messageWrapper.querySelectorAll('[data-drupal-message-id]'),
message => {
this.messageWrapper.removeChild(message);
},
);
}
However, it might make even more sense to always initialize the messageWrapper in the constructor so that other calls such as select() don't throw this same exception.
Drupal.Message = class {
constructor(messageWrapper = null) {
if (!messageWrapper) {
this.messageWrapper = Drupal.Message.defaultWrapper();
} else {
this.messageWrapper = messageWrapper;
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #14 | interdiff-3052526-10-14.txt | 1.3 KB | phenaproxima |
| #14 | 3052526-14.patch | 4.08 KB | phenaproxima |
| #10 | interdiff-7-10.txt | 1.01 KB | tedbow |
| #10 | 3052526-10.patch | 2.92 KB | tedbow |
| #7 | 3052526-7.patch | 2.21 KB | tedbow |
Comments
Comment #2
malcolm_p commentedPatch attached for checking this.messageWrapper in clear().
Comment #3
GrandmaGlassesRopeManComment #4
malcolm_p commentedI ran into this error again on another project, I'd love to see a fix make it into 8.8.x. Any issues with review?
Comment #5
droplet commentedI think it should be initialized in the Class Constructor
Comment #6
GrandmaGlassesRopeManI think I agree with @droplet.
We can check in the constructor to see if you haven't passed a wrapper, and if not, create the default wrapper. Thanks.
Comment #7
tedbowHere is a fix in the constructor and test coverage
Comment #9
GrandmaGlassesRopeManWe probably don't need this same check in `.add()` anymore.
Comment #10
tedbow@alwaysworking good call
Comment #11
wim leersComment #12
webchickNice catch! Looks like the JS maintainers' feedback has been addressed.
Committed and pushed to 8.8.x. Thanks!
Comment #13
webchickOops. ;) Not quite.
Comment #14
phenaproximaHopefully, this will fix it.
Comment #15
phenaproximaRestoring RTBC when testbot passes. I feel okay with this because I literally just changed a little coding style thing. Functionally, the code is identical.
Comment #16
webchickOk great, that interdiff seems straight-forward.
Committed and pushed to 8.8.x, FOR REAL THIS TIME. ;) Thanks!