Skip to content

Commit

Permalink
fix(tooltip): ensure valid node before removal to prevent errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
monicawheeler committed Mar 11, 2024
1 parent 18e83f6 commit fd4b3f8
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/sage-system/lib/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fd4b3f8

Please sign in to comment.