Repository navigation
fix(markdown-preview): paint Find matches without mutating react DOM (crash 237acef1) - #8678
Conversation
Search highlighting injected <mark> by splitting react-markdown's own text nodes (and normalize()-merging them on clear). react kept stale child pointers, so the next streamed-content commit threw NotFoundError (crash 237acef1: insertBefore ... not a child of this node) when Find was open during a rewrite. Paint match Ranges with the CSS Custom Highlight API instead — zero DOM mutation of the subtree react owns. Also mark the preview body translate="no" so browser/OS page-translation can't trigger the same class of crash. Adds a happy-dom regression test that reproduces the crash: before, applying highlights injects a <mark> and a re-render throws; after, the DOM react owns is untouched and streamed re-renders are clean. Co-authored-by: Orca <[email protected]>
📝 WalkthroughWalkthroughMarkdown preview search highlighting was migrated from injected 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 4
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 5e34720d-e32d-45be-9cf8-df5767e3496a
📒 Files selected for processing (4)
src/renderer/src/assets/markdown-preview.csssrc/renderer/src/components/editor/MarkdownPreview.tsxsrc/renderer/src/components/editor/markdown-preview-search-crash.test.tsxsrc/renderer/src/components/editor/markdown-preview-search.ts
- Use --markdown-search-match(-active) theme tokens instead of raw hex. - Split match vs active highlight painting so Next/Prev navigation repaints only the active range, not the full (100k-range) match set. - Reapply the active highlight after a re-apply via a searchRevision effect dep, so a same-count streamed rerender doesn't drop the active highlight. - Strengthen the streamed-rerender test (keep highlights active across the commit) and add navigation-only-repaints + same-count active regression tests. Co-authored-by: Orca <[email protected]>
Old vs new Find-highlight stylingBefore = the old CodeRabbit review — all 4 comments addressed (commit
|
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/renderer/src/assets/main.css (1)
297-298: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueConsider adjusting the dark mode colors for optimal contrast.
The dark mode colors for
--markdown-search-matchand--markdown-search-match-activeare identical to their light mode counterparts (#facc15and#fb923c). Consider if these shades provide the intended visual comfort against the dark background (#1e1e1e), similar to how--annotation-highlightwas slightly adjusted between modes.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: ff252691-113d-4eb6-b44e-6c9b94afe1fe
📒 Files selected for processing (5)
src/renderer/src/assets/main.csssrc/renderer/src/assets/markdown-preview.csssrc/renderer/src/components/editor/MarkdownPreview.tsxsrc/renderer/src/components/editor/markdown-preview-search-crash.test.tsxsrc/renderer/src/components/editor/markdown-preview-search.ts
🚧 Files skipped from review as they are similar to previous changes (3)
- src/renderer/src/components/editor/markdown-preview-search.ts
- src/renderer/src/assets/markdown-preview.css
- src/renderer/src/components/editor/MarkdownPreview.tsx
…(crash 237acef1) (stablyai#8678) Co-authored-by: Orca <[email protected]>

What
Fixes a renderer crash —
NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node— in the markdown preview.Crash report
237acef1-e50c-4c78-bfea-623aa211b7ea(macOS, prod build,terminal.workbenchboundary):Root cause
Find-in-preview highlighting painted matches by mutating react-markdown's own DOM:
applyMarkdownPreviewSearchHighlightssplit each matchingTextnode andtextNode.replaceWith(fragment)'d in<mark>elements.clearMarkdownPreviewSearchHighlightsunwrapped them and calledroot.normalize(), merging adjacent text nodes.Both rewrite the child list of nodes React owns. React keeps fiber pointers to the original text nodes, so when streamed/external file content re-renders the paragraph (extremely common in Orca — AI writes, external edits), React's commit phase runs
insertBefore/removeChildagainst a node that is no longer a child of its parent → the crash. Trigger conditions from the report: the preview Find bar open while content was being rewritten.The fix
Paint matches with the CSS Custom Highlight API —
CSS.highlights.set(name, new Highlight(...ranges))overRanges computed from the text nodes. This highlights the exact same character ranges without mutating a single DOM node, so React's tree is never disturbed. Match offsets come from the existingfindTextMatchRanges(unchanged), which already returns original-text offsets that map straight onto eachTextnode.The API is reached through a
getHighlightApi()feature-detector, so it degrades to a no-op where the API is absent (older Chromium, jsdom/happy-dom in tests) — match count and navigation still work off the returned Ranges; only the visual paint is skipped. The<mark>CSS became::highlight(...)rules.Also added
translate="no"to the preview body so browser/OS page-translation (which swaps text nodes behind React's back) can't trigger the same class of crash.ELI5
To highlight your search matches, the app used to cut up the page's text and staple little yellow
<mark>tags into it. But React had already memorized where every piece of text was. When new content streamed in, React reached for a piece of text that the highlighter had quietly moved — and fell over. The fix paints the yellow highlight on top of the text using a browser feature made exactly for this, without ever cutting the text up. React's memory stays correct, so nothing falls over.Fix evidence (undeniable)
New happy-dom regression test
markdown-preview-search-crash.test.tsxrenders a real react-owned<p>and drives the real search functions:<mark>(body.querySelector('mark')is non-null → the "no mutation" assertion fails), and re-rendering the paragraph while highlights are active throws theNotFoundError.<mark>is injected,body.textContentis byte-for-byte intact, and a streamed re-render is clean.129 test files / 839 testsinsrc/renderer/src/components/editor/pass (incl. the existingmarkdown-preview-search.test.ts). Typecheck (web) exit 0. oxlint + oxfmt clean.Trade-offs / behavior changes
border-radius/paddingis dropped because::highlight()only styles color/background. Cosmetic.<mark>element in the a11y tree. Match count is still surfaced in the UI; this only affects the highlight decoration, not navigation.CSS.highlightsis a document-global registry. Highlights are scoped per-preview via a module-level union registry (each preview's ranges are tracked by its own token and the union is painted), so split/floating previews with Find open don't clobber each other — parity with the old per-<mark>behavior.<mark>s lostdata-activethe same way).Adversarial review
2 loops → clean.
new Highlight(...ranges)spread the entire match array as constructor args →RangeError: Maximum call stack size exceededat ~100k+ matches (large streamed doc + short common query — the same regime as the bug being fixed). Fixed: build theHighlightincrementally with.add(); no range-spread anywhere on a match-scaling path. Regression test with an arg-rejecting stubHighlight.CSS.highlightsis document-global → two previews with Find open clobbered each other. Fixed: per-instance union registry keyed by a stable per-preview token. Regression test proves A+B paint the union and closing one keeps the other.setActivere-paints the match set on navigation — both pre-existing-equivalent, neither a crash or regression).Made with Orca 🐋