From 99df8542b4b3fa60e779290db5f806b5d9d4f27e Mon Sep 17 00:00:00 2001 From: Tom Byers Date: Fri, 11 Feb 2022 12:30:07 +0000 Subject: [PATCH] Add guard against elements removed from the DOM We can't guarantee that elements we stored a reference to with `classesToPersist.remove` will still exist so we need to guard against this. Note: it checks for whether the node is still attached to the DOM rather than whether it exists because the standard way to delete a node just detaches it from the DOM and relies on garbage collection to delete it from memory. --- app/assets/javascripts/updateContent.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/updateContent.js b/app/assets/javascripts/updateContent.js index b73defac9..b53fe9ebf 100644 --- a/app/assets/javascripts/updateContent.js +++ b/app/assets/javascripts/updateContent.js @@ -26,7 +26,11 @@ }, replace: function () { this.classNames.forEach((className, index) => { - this.$els[index].addClass(className); + var $el = this.$els[index]; + + if (global.document.body.contains($el.get(0))) { + $el.addClass(className); + } }); // remove references to elements