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(markdown): key task-list checkboxes by checked state to prevent stale render (#10114)
Hemanth Kumar Polisettic3ea089a59ff
diff --git a/frontend/src/plugins/core/RenderHTML.tsx b/frontend/src/plugins/core/RenderHTML.tsxindex 68391e4c5c..31f64a6dfb 100644--- a/frontend/src/plugins/core/RenderHTML.tsx+++ b/frontend/src/plugins/core/RenderHTML.tsx@@ -199,6 +199,37 @@ const keyImagesBySrc: TransformFn = (return cloneElement(reactNode, { key: `${src}-${index}` });};+// Decorator: applies a checked-state-based key to task-list checkbox+// <input> elements (e.g. GFM `- [ ]` / `- [x]` rendered by+// pymdownx.tasklist) so they remount when their checked-ness changes.+//+// pymdownx.tasklist omits the `checked` HTML attribute entirely for+// unchecked items (rather than emitting checked="false"). When markdown+// edits shift which item lands at a given tree position (e.g. deleting+// completed items above unchecked ones), reusing the same DOM <input>+// across a checked -> unchecked item is a controlled -> uncontrolled prop+// transition for React: React stops managing the DOM `checked` property in+// that transition instead of resetting it, leaving the previous item's+// checked state visually stuck until a full remount. Baking checked-ness+// into the key forces a fresh node instead of reusing the stale one.+const keyTaskListCheckboxesByChecked: TransformFn = (+ reactNode: ReactNode,+ domNode: DOMNode,+ index: number,+): JSX.Element | undefined => {+ if (!(domNode instanceof Element) || domNode.name !== "input") {+ return undefined;+ }+ if (domNode.attribs?.type !== "checkbox") {+ return undefined;+ }+ if (!isValidElement(reactNode)) {+ return undefined;+ }+ const checked = "checked" in domNode.attribs;+ return cloneElement(reactNode, { key: `checkbox-${index}-${checked}` });+};+// Wrap elements with data-marimo-doc attribute in a DocHoverTargetconst wrapDocHoverTargets: TransformFn = (reactNode: ReactNode,@@ -326,7 +357,10 @@ function parseHtml({// and may further wrap/clone it. Used for cross-cutting concerns that// should apply regardless of which (if any) match-and-replace transform// ran above.- const decoratorFunctions: TransformFn[] = [keyImagesBySrc];+ const decoratorFunctions: TransformFn[] = [+ keyImagesBySrc,+ keyTaskListCheckboxesByChecked,+ ];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 a048ce9f94..dce040740e 100644--- a/frontend/src/plugins/core/__test__/RenderHTML.test.ts+++ b/frontend/src/plugins/core/__test__/RenderHTML.test.ts@@ -114,6 +114,50 @@ describe("parseHtml", () => {expect(result.key).toBe("https://cdn.example.com/b.png-0");});+ test("checked task-list checkbox is keyed by index and checked state", () => {+ const html = '<li><input type="checkbox" checked disabled> item</li>';+ const result = parseHtml({ html }) as React.ReactElement<{+ children: React.ReactElement[];+ }>;+ const checkbox = result.props.children[0];+ expect(checkbox.key).toBe("checkbox-0-true");+ });++ test("unchecked task-list checkbox is keyed by index and checked state", () => {+ const html = '<li><input type="checkbox" disabled> item</li>';+ const result = parseHtml({ html }) as React.ReactElement<{+ children: React.ReactElement[];+ }>;+ const checkbox = result.props.children[0];+ expect(checkbox.key).toBe("checkbox-0-false");+ });++ test("task list: item reused at same position gets a new key when checked state flips", () => {+ // Simulates editing markdown so an unchecked item now sits where a+ // checked item used to be (e.g. an earlier checked item was deleted).+ const before = parseHtml({+ html: '<ul><li><input type="checkbox" checked disabled> a</li></ul>',+ }) as React.ReactElement<{ children: React.ReactElement }>;+ const after = parseHtml({+ html: '<ul><li><input type="checkbox" disabled> b</li></ul>',+ }) as React.ReactElement<{ children: React.ReactElement }>;+ const beforeLi = before.props.children as React.ReactElement<{+ children: React.ReactElement[];+ }>;+ const afterLi = after.props.children as React.ReactElement<{+ children: React.ReactElement[];+ }>;+ const beforeKey = beforeLi.props.children[0].key;+ const afterKey = afterLi.props.children[0].key;+ expect(beforeKey).not.toBe(afterKey);+ });++ test("non-checkbox input is left alone", () => {+ const html = '<input type="text" value="hi">';+ const result = parseHtml({ html }) as React.ReactElement;+ expect(result.key).toBeNull();+ });+test("codehilite with copy button", () => {const html ='<div class="codehilite"><pre><code>console.log("Hello");</code></pre></div>';