Skip to content

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.

fix: remove running script tags html (#890)

Myles Scolnickb1bbfc3aeaa9
diff --git a/frontend/src/components/editor/output/HtmlOutput.tsx b/frontend/src/components/editor/output/HtmlOutput.tsx
index 35a67297ad..8ed5811dca 100644
--- a/frontend/src/components/editor/output/HtmlOutput.tsx
+++ b/frontend/src/components/editor/output/HtmlOutput.tsx
@@ -1,7 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import { memo, useLayoutEffect, useRef } from "react";
-import { lookupScript, updateScriptCache } from "./scripts";
-import { Logger } from "../../../utils/Logger";
+import { memo } from "react";
import { renderHTML } from "../../../plugins/core/RenderHTML";
import { cn } from "../../../utils/cn";
@@ -13,70 +11,12 @@ interface Props {
export const HtmlOutput: React.FC<Props> = memo(
({ html, inline = false, className }) => {
- const nodeRef = useRef<HTMLDivElement>(null);
-
- useLayoutEffect(() => {
- const outputNode = nodeRef.current;
-
- if (!outputNode) {
- Logger.error("Output is not initialized");
- return;
- }
-
- // Cache script sources and run inline script tags
- async function runScripts(outputNode: HTMLDivElement) {
- // eslint-disable-next-line unicorn/prefer-spread
- const scriptNodes = Array.from(outputNode.querySelectorAll("script"));
-
- for (const scriptNode of scriptNodes) {
- if (scriptNode.src != null && scriptNode.src !== "") {
- let cachedScript = lookupScript(scriptNode.src);
-
- // Cache script
- if (cachedScript === null) {
- const scriptElement = document.createElement("script");
- scriptElement.src = scriptNode.src;
- scriptElement.type = scriptNode.type;
- cachedScript = { element: scriptElement, loaded: false };
- updateScriptCache(scriptNode.src, cachedScript);
- }
-
- // Handle load state
- if (!cachedScript?.loaded) {
- const scriptElement = cachedScript.element;
- await new Promise((resolve) => {
- document.head.append(scriptElement);
- scriptElement.addEventListener("load", resolve);
- scriptElement.addEventListener("error", resolve);
- });
- cachedScript.loaded = true;
- }
- } else {
- const code = scriptNode.innerText;
- const fnbody = `"use strict"; return (async () => {${code}})()`;
- try {
- // eslint-disable-next-line no-new-func, @typescript-eslint/no-implied-eval
- await new Function(fnbody)();
- } catch (error) {
- Logger.error("Failed to execute script ", scriptNode);
- Logger.error(error);
- }
- }
- }
- }
-
- runScripts(outputNode);
- }, [html, nodeRef]);
-
if (!html) {
return null;
}
return (
- <div
- className={cn(className, { "inline-flex": inline, block: !inline })}
- ref={nodeRef}
- >
+ <div className={cn(className, { "inline-flex": inline, block: !inline })}>
{renderHTML({ html })}
</div>
);
diff --git a/frontend/src/components/editor/output/scripts.ts b/frontend/src/components/editor/output/scripts.ts
deleted file mode 100644
index 93f6f4e6f2..0000000000
--- a/frontend/src/components/editor/output/scripts.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-/* Copyright 2024 Marimo. All rights reserved. */
-interface Script {
- element: HTMLScriptElement;
- loaded: boolean;
-}
-
-const LOADED_SCRIPTS: Record<string, Script> = {};
-
-export function lookupScript(src: string) {
- return src in LOADED_SCRIPTS ? LOADED_SCRIPTS[src] : null;
-}
-
-export function updateScriptCache(src: string, value: Script) {
- LOADED_SCRIPTS[src] = value;
-}