Skip to content

Conversation

@kanishk6103
Copy link

Closes #8190

Describe your changes:

  • Added null checks to make sure the system gracefully handles cases where tooltip components don't have a parent element in the DOM

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Is this a notable change that will require a special callout in the release notes? For example, will this break compatibility with existing APIs or projects that consume these plugins?

Author Checklist

  • Changes address original issue?
  • Tests included and/or updated with changes?
  • Has this been smoke tested?
  • Have you associated this PR with a type: label? Note: this is not necessarily the same as the original issue.
  • Have you associated a milestone with this PR? Note: leave blank if unsure.
  • Testing instructions included in associated issue OR is this a dependency/testcase change?

Reviewer Checklist

  • Changes appear to address issue?
  • Reviewer has tested changes by following the provided instructions?
  • Changes appear not to be breaking changes?
  • Appropriate automated tests included?
  • Code style and in-line documentation are appropriate?

Copilot AI review requested due to automatic review settings November 14, 2025 20:45
Copilot finished reviewing on behalf of kanishk6103 November 14, 2025 20:49
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds null checks to tooltip components to gracefully handle cases where tooltip parent elements don't exist or have been removed from the DOM. The changes prevent potential runtime errors when tooltips attempt to interact with missing parent elements.

  • Added null checks for parent elements before creating tooltips
  • Added guard clauses in tooltip coordinate calculation to handle missing parents
  • Added validation in the tooltip show() method to verify parent existence

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/api/tooltips/tooltipMixins.js Added null check and DOM containment validation for parent elements before creating tooltips
src/api/tooltips/components/TooltipComponent.vue Added null check in coordinate computation to return default values when parent is missing
src/api/tooltips/ToolTip.js Added parent existence validation in the show() method before appending tooltip to DOM
package-lock.json Contains unrelated dependency metadata changes (peer dependency flags)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 70 to 71
const parentExists = this.tooltip && this.tooltip.parentElement &&
document.contains(this.tooltip.parentElement);
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable this.tooltip is undefined in the Tooltip class. The code attempts to access this.tooltip.parentElement, but this.tooltip is never initialized in the constructor.

The parentElement is passed to the constructor but only provided to the Vue component, not stored as an instance variable. To fix this, you should either:

  1. Store parentElement as this.parentElement in the constructor and check this.parentElement directly, or
  2. Store the entire options object as this.tooltip in the constructor

Suggested fix:

constructor({ toolTipText, toolTipLocation, parentElement, cssClasses } = { ... }) {
  super();
  this.parentElement = parentElement; // Add this line
  // ... rest of constructor
}

show() {
  const parentExists = this.parentElement && document.contains(this.parentElement);
  
  if (!parentExists) {
    console.warn('Tooltip parent does not exist anymore!');
    return;
  }
  // ... rest of method
}

Copilot uses AI. Check for mistakes.
return;
}
let parentElement = this.$refs[elementRef];
if (!parentElement || !document.contains(parentElement)) {
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method document.contains() is not a standard DOM API. To check if an element is in the document, you should use document.body.contains(parentElement) or document.documentElement.contains(parentElement) instead, which have better browser support.

Suggested fix:

if (!parentElement || !document.body.contains(parentElement)) {
  console.warn('Cannot create tooltip: parent element does not exist');
  return;
}
Suggested change
if (!parentElement || !document.contains(parentElement)) {
if (!parentElement || !document.body.contains(parentElement)) {

Copilot uses AI. Check for mistakes.
inject: ['toolTipText', 'toolTipLocation', 'parentElement', 'cssClasses'],
computed: {
toolTipCoordinates() {
if (!this.parentElement || !document.contains(this.parentElement)) {
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method document.contains() is not a standard DOM API. To check if an element is in the document, you should use document.body.contains(this.parentElement) or document.documentElement.contains(this.parentElement) instead, which have better browser support.

Suggested fix:

if (!this.parentElement || !document.body.contains(this.parentElement)) {
  return { top: 0, left: 0, height: 0, width: 0 };
}
Suggested change
if (!this.parentElement || !document.contains(this.parentElement)) {
if (!this.parentElement || !document.body.contains(this.parentElement)) {

Copilot uses AI. Check for mistakes.
Comment on lines 56 to 59
if (!parentElement || !document.contains(parentElement)) {
console.warn('Cannot create tooltip: parent element does not exist');
return;
}
Copy link

Copilot AI Nov 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new null check logic added to handle missing parent elements should be covered by tests. Consider adding test cases that verify the behavior when:

  1. parentElement is null
  2. parentElement is not in the document
  3. The tooltip is created successfully with a valid parent element

This would help ensure the null checks work as expected and prevent regressions. Other API modules in the codebase have corresponding Spec files (e.g., EditorSpec.js).

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 4 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +56 to 62
if (!parentElement || !document.body.contains(parentElement)) {
console.warn('Cannot create tooltip: parent element does not exist');
return;
}
if (Array.isArray(parentElement)) {
parentElement = parentElement[0];
}
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null check should occur after handling the Array case. Currently, if parentElement is an array, document.body.contains() will be called with an array instead of an element, which may not work as expected. Consider moving the null check to after line 62 where parentElement[0] is extracted.

Suggested change
if (!parentElement || !document.body.contains(parentElement)) {
console.warn('Cannot create tooltip: parent element does not exist');
return;
}
if (Array.isArray(parentElement)) {
parentElement = parentElement[0];
}
if (Array.isArray(parentElement)) {
parentElement = parentElement[0];
}
if (!parentElement || !document.body.contains(parentElement)) {
console.warn('Cannot create tooltip: parent element does not exist');
return;
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Visual issues with Tooltips

1 participant