From 81a931815f3209a78cedfb7e076da2c4a5c25cd0 Mon Sep 17 00:00:00 2001 From: Monica Wheeler Date: Mon, 11 Mar 2024 11:25:55 -0500 Subject: [PATCH] fix(tooltip): ensure valid node before removal to prevent errors This commit fixes an issue where the `removeTooltip` function attempted to remove a tooltip element from the DOM without verifying if it was a valid child of `document.body`. The updated implementation adds a check to ensure the tooltip exists and its parent node is `document.body` before proceeding with the removal. This change prevents the "Failed to execute removeChild on Node: parameter 1 is not of type Node." error from being thrown, enhancing the robustness and reliability of tooltip removal. --- packages/sage-system/lib/tooltip.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/sage-system/lib/tooltip.js b/packages/sage-system/lib/tooltip.js index 71d3494c82..6a32466878 100644 --- a/packages/sage-system/lib/tooltip.js +++ b/packages/sage-system/lib/tooltip.js @@ -53,17 +53,18 @@ Sage.tooltip = (function() { positionTooltip(evt.target, tooltip, pos); } - // Removes tooltip from DOM function removeTooltip(evt) { - if (!evt.target.hasAttribute(DATA_ATTR) || !document.querySelector(SELECTOR) || !document.querySelector(`.${TOOLTIP_CLASS}`) || !evt.target.dataset.jsTooltip) return; + if (!evt.target.hasAttribute(DATA_ATTR) || !document.querySelector(SELECTOR) || !document.querySelector(`.${TOOLTIP_CLASS}`) || !evt.target.dataset.jsTooltip) return; window.requestAnimationFrame(function() { - document.body.removeChild(document.querySelector(`.${TOOLTIP_CLASS}`)); + var tooltip = document.querySelector(`.${TOOLTIP_CLASS}`); + if (tooltip && tooltip.parentNode === document.body) { // Ensure tooltip exists and its parent is document.body before removing + document.body.removeChild(tooltip); + } }); } - // Builds list of modifier classes from array of data-attributes function generateClasses(ele, evt) { ele.dataItems.forEach(function(item) {