Skip to content

Use createDiv for widget root (plugin-review Finding 2 revisited) - #12

Merged
nikoneex merged 1 commit into
devfrom
fix/widget-create-div
Sep 4, 2026
Merged

nikoneex merged 1 commit into
devfrom
fix/widget-create-div

Conversation

@nikoneex

@nikoneex nikoneex commented Sep 4, 2026

Copy link
Copy Markdown
Owner

Summary

The plugin-review scanner is still flagging document.createElement at src/editor-extension.tsx:81 — the one call our 1.4.1 pass left in place. Turns out the earlier "no change" resolution was based on a wrong assumption.

What the old comment claimed: all of Obsidian's createDiv helpers append to a receiver, so we can't use them for a detached CodeMirror widget root.

What's actually true: the Node-prototype createDiv appends, but the global createDiv (declared at obsidian.d.ts:193) returns a detached HTMLDivElement. That's the API the scanner wants and it satisfies CodeMirror's toDOM() contract just fine.

Change

-    // ...comment justifying why we can't use createDiv...
-    const container = view.dom.ownerDocument.createElement('div');
-    container.className = 'kaper-block';
+    // Obsidian's global `createDiv` returns a detached element — matches
+    // CodeMirror's `toDOM()` contract (CM inserts the widget itself; Node
+    // prototype's `createDiv` would append and can't be used here).
+    const container = createDiv({ cls: 'kaper-block' });

docs/obsidian-review-warnings.md Finding 2 flipped from "No change" to "Fixed" with the corrected reasoning.

Notes

  • main.js still contains 5 createElement calls after build — those come from React 18's runtime internals, not our source. The scanner cited a source line, so a source-level fix is the right target.
  • No document-context concern in practice. The global createDiv creates in the main window's document; if a viewer opens the editor in a popout, browsers adopt the node into the popout's document when CodeMirror appends it. Event listeners we attach (focusin stopPropagation) survive adoption.
  • Ships in the next patch (1.4.2).

Test plan

  • grep -c createElement src/editor-extension.tsx → 0
  • npm run lint clean
  • npm test — 92/92 green
  • npm run build clean
  • Manual: open a recipe in a popout window, confirm the widget renders and inputs receive focus without the doc-cursor-jump regression.

Obsidian's global createDiv returns a detached element and satisfies
both CodeMirror's toDOM() contract and the plugin-review scanner's
prefer-create-el rule.

The earlier "no change" resolution for this finding was based on a
wrong assumption that all createDiv helpers append their new element.
The Node-prototype method does, but the global helper doesn't
(obsidian.d.ts:193). Updates docs/obsidian-review-warnings.md
accordingly.
@nikoneex
nikoneex merged commit ab5a7bb into dev Sep 4, 2026
1 check passed
@nikoneex nikoneex mentioned this pull request Sep 4, 2026
5 tasks
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.

1 participant