You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2026/06/03 18:56:23 UTC

(superset) branch master updated: refactor(nvd3): extract testable generateAnnotationTooltipContent helper (#40620)

This is an automated email from the ASF dual-hosted git repository.

rusackas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 6ea4e227856 refactor(nvd3): extract testable generateAnnotationTooltipContent helper (#40620)
6ea4e227856 is described below

commit 6ea4e22785624d5cddc627df7b0b7afa3261fe5c
Author: Evan Rusackas <ev...@preset.io>
AuthorDate: Wed Jun 3 11:56:07 2026 -0700

    refactor(nvd3): extract testable generateAnnotationTooltipContent helper (#40620)
    
    Co-authored-by: Claude Opus 4.8 <no...@anthropic.com>
---
 .../plugins/legacy-preset-chart-nvd3/src/utils.ts  | 36 +++++++++---------
 .../legacy-preset-chart-nvd3/test/utils.test.ts    | 43 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 18 deletions(-)

diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.ts b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.ts
index 4474d50672e..04e235e01da 100644
--- a/superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.ts
+++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/src/utils.ts
@@ -275,29 +275,29 @@ export function wrapTooltip(chart) {
   });
 }
 
+// Builds the sanitized HTML for an annotation layer's tooltip. Title and
+// description values come from the annotation data source, so the output is
+// run through dompurify before being inserted into the DOM by d3-tip.
+export function generateAnnotationTooltipContent(layer, d) {
+  const title =
+    d[layer.titleColumn] && d[layer.titleColumn].length > 0
+      ? `${d[layer.titleColumn]} - ${layer.name}`
+      : layer.name;
+  const body = Array.isArray(layer.descriptionColumns)
+    ? layer.descriptionColumns.map(c => d[c])
+    : Object.values(d);
+
+  return dompurify.sanitize(
+    `<div><strong>${title}</strong></div><br/><div>${body.join(', ')}</div>`,
+  );
+}
+
 export function tipFactory(layer) {
   return d3tip()
     .attr('class', `d3-tip ${layer.annotationTipClass || ''}`)
     .direction('n')
     .offset([-5, 0])
-    .html(d => {
-      if (!d) {
-        return '';
-      }
-      const rawTitle =
-        d[layer.titleColumn] && d[layer.titleColumn].length > 0
-          ? `${d[layer.titleColumn]} - ${layer.name}`
-          : layer.name;
-      const rawBody = Array.isArray(layer.descriptionColumns)
-        ? layer.descriptionColumns.map(c => d[c])
-        : Object.values(d);
-
-      return dompurify.sanitize(
-        `<div><strong>${rawTitle}</strong></div><br/><div>${rawBody.join(
-          ', ',
-        )}</div>`,
-      );
-    });
+    .html(d => (d ? generateAnnotationTooltipContent(layer, d) : ''));
 }
 
 export function getMaxLabelSize(svg, axisClass) {
diff --git a/superset-frontend/plugins/legacy-preset-chart-nvd3/test/utils.test.ts b/superset-frontend/plugins/legacy-preset-chart-nvd3/test/utils.test.ts
index f2615771476..04c3f799787 100644
--- a/superset-frontend/plugins/legacy-preset-chart-nvd3/test/utils.test.ts
+++ b/superset-frontend/plugins/legacy-preset-chart-nvd3/test/utils.test.ts
@@ -24,6 +24,7 @@ import {
 
 import {
   computeYDomain,
+  generateAnnotationTooltipContent,
   generateBubbleTooltipContent,
   generateMultiLineTooltipContent,
   getTimeOrNumberFormatter,
@@ -312,4 +313,46 @@ describe('nvd3/utils', () => {
       expect(html).toContain('payload');
     });
   });
+
+  describe('generateAnnotationTooltipContent()', () => {
+    const layer = {
+      name: 'My annotations',
+      titleColumn: 'title',
+      descriptionColumns: ['description'],
+    };
+
+    test('renders the annotation title and description', () => {
+      const html = generateAnnotationTooltipContent(layer, {
+        title: 'Release',
+        description: 'Shipped v1',
+      });
+      expect(html).toContain('Release - My annotations');
+      expect(html).toContain('Shipped v1');
+    });
+
+    test('falls back to the layer name when the title column is empty', () => {
+      const html = generateAnnotationTooltipContent(layer, {
+        title: '',
+        description: 'Shipped v1',
+      });
+      expect(html).toContain('My annotations');
+    });
+
+    test('strips an event-handler payload from the title column', () => {
+      const html = generateAnnotationTooltipContent(layer, {
+        title: '<img src=x onerror="alert(1)">',
+        description: 'ok',
+      });
+      expect(html).not.toContain('onerror');
+      expect(html).not.toContain('alert(1)');
+    });
+
+    test('strips a script payload from a description column', () => {
+      const html = generateAnnotationTooltipContent(layer, {
+        title: 'Release',
+        description: '<script>alert(document.cookie)</script>',
+      });
+      expect(html).not.toContain('<script>');
+    });
+  });
 });