Skip to content

fix(muya): guard detached code block in language selector (#4654) - #4677

Merged
Jocs merged 3 commits into
developfrom
fix/codeblock-lang-selector-null-parent
Jun 24, 2026
Merged

Jocs merged 3 commits into
developfrom
fix/codeblock-lang-selector-null-parent

Conversation

@Jocs

@Jocs Jocs commented Jun 24, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #4654 (renderer crash when selecting a language for a code block).

When the language picker is open and its target code block is detached from
the document — e.g. the code block is converted back to a paragraph via
Backspace at offset 0 of the code body, orphaning its language-input child —
selecting a language crashes the renderer.

selectItem's first mutation, block.text = name, runs the Content text
setter, which re-computes an OT path for the edit. For a language-input that
path walks up to its code block, whose path getter dereferences its own (now
null) parent:

TypeError: Cannot destructure property 'path' of 'this.parent' as it is null.
    at get path (codeBlock/index.ts)
    at get path (content.ts)
    at set text (content.ts)
    at CodeBlockLanguageSelector.selectItem

On the retired muyajs engine the same class surfaced as
Cannot read properties of null (reading 'functionType') at
ContentState.updateCodeLanguage.

Fix

Bail out of selectItem when block.outMostBlock is null — the block (or any
ancestor) is no longer connected to the document root — before mutating its
text/lang. outMostBlock walks the full parent chain, so it catches the
detached-grandparent case (the language-input's immediate parent still exists,
but the code block above it is detached) that a shallow block.parent check
misses.

Tests

  • e2e (muya-e2e) — code-block-language-selector-orphan-4654.spec.ts
    drives the real picker float in Chromium: opens it on a code block nested in a
    list, detaches the block via the actual codeBlockContent.backspaceHandler,
    clicks a language item, and asserts no uncaught pageerror. This is the
    authoritative reproduction — it fails on the pre-fix engine with the
    path-destructure crash and passes with the guard.
  • unit — selectItem.spec.ts pins the guard contract (a detached block,
    outMostBlock null, is left untouched) and the attached happy path.

Note

The first revision of this PR guarded the wrong line (block.parent.lang).
End-to-end verification in the real engine showed the crash actually fires one
statement earlier, at block.text = name, and one ancestor level up
(immediate parent present, code block detached) — so the fix was reworked to
the outMostBlock guard above and backed by the e2e reproduction.

🤖 Generated with Claude Code

@Jocs
Jocs force-pushed the fix/codeblock-lang-selector-null-parent branch from 0b90f59 to f5ef557 Compare June 24, 2026 12:40
@Jocs Jocs changed the title fix(muya): guard null parent in code block language selector (#4654) fix(muya): guard detached code block in language selector (#4654) Jun 24, 2026
Jocs and others added 3 commits June 24, 2026 22:46
Selecting a language from the code-block language picker crashed the
renderer when the picker's target block had been detached from the
document while the picker stayed open (e.g. the code block was converted
back to a paragraph via Backspace at offset 0 of the code body, orphaning
its language-input child).

`selectItem`'s `block.text = name` runs the Content text setter, which
re-computes an OT path for the edit operation. For a language-input that
path walks up to its code block, whose `path` getter dereferences its own
(now null) parent — throwing `Cannot destructure property 'path' of
'this.parent'`. The retired muyajs engine hit the same class at
`ContentState.updateCodeLanguage` (`reading 'functionType'` of null).

Bail out of `selectItem` when `block.outMostBlock` is null — the block
(or any ancestor) is no longer connected to the document root — before
mutating its text/lang. `outMostBlock` walks the full parent chain, so it
catches the detached-grandparent case a shallow `block.parent` check misses.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Drives the real CodeBlockLanguageSelector float in Chromium: opens the
picker on a code block nested in a list, detaches the block, then clicks a
language item and asserts no uncaught renderer pageerror. Fails on the
pre-fix engine with `Cannot destructure property 'path' of 'this.parent'`.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The language picker only hid on a non-matching query or a document click,
so moving the caret out of the language input (e.g. Left/Right arrow) left
it open over a stale target. Subscribe to `selection-change` and hide the
picker when the caret's anchorBlock is no longer its target block — the
same self-perceiving pattern InlineFormatToolbar uses. This also closes the
window that let the target block be detached from under the picker (#4654);
the selectItem outMostBlock guard remains as defense in depth.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@Jocs
Jocs force-pushed the fix/codeblock-lang-selector-null-parent branch from f5ef557 to e43c0d0 Compare June 24, 2026 14:50
@Jocs

Jocs commented Jun 24, 2026

Copy link
Copy Markdown
Member Author

Added a third commit: feat(muya): auto-hide code language picker when caret leaves its block.

The picker now subscribes to selection-change and hides when the caret's anchorBlock is no longer its target (mirroring InlineFormatToolbar). This closes the window that let the code block be detached from under an open picker — the root cause of #4654 — while the outMostBlock guard in selectItem stays as defense in depth. Covered by a new real-Chromium e2e (the picker hides when the caret leaves the language input); the orphan regression now detaches via block.remove() so it isolates the guard rather than the auto-hide. Branch rebased onto current develop.

@Jocs
Jocs merged commit 09c4046 into develop Jun 24, 2026
7 checks passed
@Jocs
Jocs deleted the fix/codeblock-lang-selector-null-parent branch June 24, 2026 15:11
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