FIX: insertAdjacentHTML is a better choice here

FIX: insertAdjacentHTML is a better choice here

diff --git a/assets/javascripts/initializers/add-holiday-flair.js.es6 b/assets/javascripts/initializers/add-holiday-flair.js.es6
index 310def8..7ebf7fb 100644
--- a/assets/javascripts/initializers/add-holiday-flair.js.es6
+++ b/assets/javascripts/initializers/add-holiday-flair.js.es6
@@ -6,17 +6,12 @@ function applyFlairOnMention(element, username) {
   if (!element) return;
 
   const href = `${Discourse.BaseUri}/u/${username}`;
-
-  const mentions = element.querySelectorAll(
-    `a.mention[href="${href}"]:not(.on-holiday)`
-  );
-
-  const parser = new DOMParser();
-  const doc = parser.parseFromString(iconHTML("calendar-alt"), "text/html");
-  const iconDocument = doc.body.firstChild;
+  const mentions = element.querySelectorAll(`a.mention[href="${href}"]`);
 
   mentions.forEach(mention => {
-    mention.appendChild(iconDocument);
+    if (!mention.querySelector(".d-icon-calendar-alt")) {
+      mention.insertAdjacentHTML("beforeend", iconHTML("calendar-alt"));
+    }
     mention.classList.add("on-holiday");
   });
 }

GitHub sha: b4bf9b29

1 Like