A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: remove body tags when rendering custom HTML (#6421)
Myles Scolnick262c78609eb5
diff --git a/frontend/src/plugins/core/RenderHTML.tsx b/frontend/src/plugins/core/RenderHTML.tsxindex 16b2133693..19f84d51e5 100644--- a/frontend/src/plugins/core/RenderHTML.tsx+++ b/frontend/src/plugins/core/RenderHTML.tsx@@ -5,7 +5,7 @@ import parse, {Element,type HTMLReactParserOptions,} from "html-react-parser";-import React, { type JSX, type ReactNode, useId } from "react";+import React, { isValidElement, type JSX, type ReactNode, useId } from "react";import { CopyClipboardIcon } from "@/components/icons/copy-icon";type ReplacementFn = NonNullable<HTMLReactParserOptions["replace"]>;@@ -23,6 +23,36 @@ const replaceValidTags = (domNode: DOMNode) => {}};+const removeWrappingBodyTags: TransformFn = (+ reactNode: ReactNode,+ domNode: DOMNode,+) => {+ // Remove body tags and just render their children+ if (domNode instanceof Element && domNode.name === "body") {+ if (isValidElement(reactNode) && "props" in reactNode) {+ const props = reactNode.props as { children?: ReactNode };+ const children = props.children;+ return <>{children}</>; // eslint-disable-line react/jsx-no-useless-fragment+ }+ return;+ }+};++const removeWrappingHtmlTags: TransformFn = (+ reactNode: ReactNode,+ domNode: DOMNode,+) => {+ // Remove html tags and just render their children+ if (domNode instanceof Element && domNode.name === "html") {+ if (isValidElement(reactNode) && "props" in reactNode) {+ const props = reactNode.props as { children?: ReactNode };+ const children = props.children;+ return <>{children}</>; // eslint-disable-line react/jsx-no-useless-fragment+ }+ return;+ }+};+const replaceValidIframes = (domNode: DOMNode) => {// For iframe, we just want to use dangerouslySetInnerHTML so:// 1) we can remount the iframe when the src changes@@ -110,7 +140,11 @@ export const renderHTML = ({ html, additionalReplacements = [] }: Options) => {...additionalReplacements,];- const transformFunctions: TransformFn[] = [addCopyButtonToCodehilite];+ const transformFunctions: TransformFn[] = [+ addCopyButtonToCodehilite,+ removeWrappingBodyTags,+ removeWrappingHtmlTags,+ ];return parse(html, {replace: (domNode: DOMNode, index: number) => {diff --git a/frontend/src/plugins/core/__test__/RenderHTML.test.ts b/frontend/src/plugins/core/__test__/RenderHTML.test.tsindex d16ca5f1f7..abd2a23957 100644--- a/frontend/src/plugins/core/__test__/RenderHTML.test.ts+++ b/frontend/src/plugins/core/__test__/RenderHTML.test.ts@@ -121,6 +121,78 @@ describe("RenderHTML", () => {</p>`);});++ test("removes body tags but preserves children", () => {+ const html = "<body><h1>Hello</h1><p>World</p></body>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <React.Fragment>+ <h1>+ Hello+ </h1>+ <p>+ World+ </p>+ </React.Fragment>+ `);+ });++ test("removes nested body tags", () => {+ const html = "<div><body><span>Content</span></body></div>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <div>+ <React.Fragment>+ <span>+ Content+ </span>+ </React.Fragment>+ </div>+ `);+ });++ test("removes html tags but preserves children", () => {+ const html =+ "<html><head><title>Test</title></head><body><p>Content</p></body></html>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <React.Fragment>+ <head>+ <title>+ Test+ </title>+ </head>+ <React.Fragment>+ <p>+ Content+ </p>+ </React.Fragment>+ </React.Fragment>+ `);+ });++ test("removes nested html tags", () => {+ const html = "<div><html><span>Content</span></html></div>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <div>+ <React.Fragment>+ <span>+ Content+ </span>+ </React.Fragment>+ </div>+ `);+ });++ test("remove nested body in html", () => {+ const html = "<html><body><span>Content</span></body></html>";+ expect(renderHTML({ html })).toMatchInlineSnapshot(`+ <React.Fragment>+ <React.Fragment>+ <span>+ Content+ </span>+ </React.Fragment>+ </React.Fragment>+ `);+ });});describe("RenderHTML with < nad >", () => {