Skip to content

fix: #4896 guard selectLanguage against a stale code-picker block - #4911

Closed
doktorigi wants to merge 1 commit into
marktext:developfrom
doktorigi:fix/4896-code-picker-stale-block
Closed

doktorigi wants to merge 1 commit into
marktext:developfrom
doktorigi:fix/4896-code-picker-stale-block

Conversation

@doktorigi

Copy link
Copy Markdown
Contributor

Description

Fixes #4896 — selecting a code-fence language from the autocomplete with the keyboard (Arrow keys + Enter) crashed the renderer with:

TypeError: Cannot read properties of null (reading 'functionType')
    at ContentState.updateCodeLanguage
    at ContentState.selectLanguage

Root cause

The code picker's callback captures the paragraph DOM node when the picker opens (eventHandler/keyboard.js → muya-code-picker dispatch). By the time the selection is confirmed via the keyboard, that paragraph can already have been converted into a code block, so selectLanguage's this.getBlock(paragraph.id) returns null — and updateCodeLanguage dereferences it (block.functionType). The mouse path happens not to hit the stale window, which matches the report (mouse works, keyboard crashes).

This is the legacy-engine analog of #4654, where the @muyajs/core code-block language selector gained a detached-block guard (packages/muya/src/ui/codeBlockLanguageSelector). This PR applies the same bail-out to packages/muyajs.

Fix

One guard in ContentState.selectLanguage: if the picked block no longer resolves, return without mutating. This is the single place a null block can enter updateCodeLanguage — the other callers (enterCtrl, pasteCtrl) already hold non-null blocks.

Tests

test/unit/specs/code-picker-select-language.spec.ts drives selectLanguage off a minimal fake ContentState (same approach as muya's selectItem.spec.ts):

  • stale block → no crash, no mutation — fails without the guard with the exact reported TypeError
  • present block → language still applied

pnpm run lint and pnpm run typecheck pass.

🤖 Generated with Claude Code

…lock

Confirming a language from the code picker with the keyboard could fire
the picker callback after the captured paragraph had already been
converted into a code block, so getBlock returned null and
updateCodeLanguage crashed reading functionType. Bail out when the block
no longer exists — the same detached-block guard the @muyajs/core
selector gained for marktext#4654.

Co-Authored-By: Claude Fable 5 <[email protected]>
niyo-oyin added a commit to niyo-oyin/marktext-ja that referenced this pull request Jul 29, 2026
- PR marktext#4946 (includes its base PR marktext#4942): removes the quadratic costs
  that froze large documents and mounts them progressively — a 2.1 MB
  document goes from an indefinite freeze to interactive in seconds.
  Applied verbatim; upstream review addressed 19 findings with 31
  regression tests, all passing here.
- PR marktext#4931 (marktext#4926): deleting at an offset inside a surrogate pair left
  a lone surrogate that crashed text-unicode ("Invalid offset - splits
  unicode bytes"). The content.ts hunk conflicted with our Phase 1
  isComposed guards and was integrated by hand; the new cursor snap
  additionally runs only when no IME composition is open, since moving
  the DOM selection mid-composition would detach the IME anchor.
- PR marktext#4913 (marktext#4899): Shift+Tab outdent with trailing sibling blocks
  corrupted the state tree (paragraphs appended directly into a list),
  crashing the next composed op. muya hunks only — the PR's legacy
  muyajs hunks target the removed engine.
- PR marktext#4911 skipped: the equivalent detached-block guard already exists
  in @muyajs/core's language selector (marktext#4654), verified in source.
- PR marktext#4930 (marktext#4921): CodeMirror instance wrapped in a Vue proxy broke
  source-code mode; markRaw fix applied cleanly.

muya suite 1515/1515, tsc/eslint/madge clean.

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ef4yFuXFbyKNTazkwUtygY
niyo-oyin added a commit to niyo-oyin/marktext-ja that referenced this pull request Jul 29, 2026
- PR marktext#4946 (includes its base PR marktext#4942): removes the quadratic costs
  that froze large documents and mounts them progressively — a 2.1 MB
  document goes from an indefinite freeze to interactive in seconds.
  Applied verbatim; upstream review addressed 19 findings with 31
  regression tests, all passing here.
- PR marktext#4931 (marktext#4926): deleting at an offset inside a surrogate pair left
  a lone surrogate that crashed text-unicode ("Invalid offset - splits
  unicode bytes"). The content.ts hunk conflicted with our Phase 1
  isComposed guards and was integrated by hand; the new cursor snap
  additionally runs only when no IME composition is open, since moving
  the DOM selection mid-composition would detach the IME anchor.
- PR marktext#4913 (marktext#4899): Shift+Tab outdent with trailing sibling blocks
  corrupted the state tree (paragraphs appended directly into a list),
  crashing the next composed op. muya hunks only — the PR's legacy
  muyajs hunks target the removed engine.
- PR marktext#4911 skipped: the equivalent detached-block guard already exists
  in @muyajs/core's language selector (marktext#4654), verified in source.
- PR marktext#4930 (marktext#4921): CodeMirror instance wrapped in a Vue proxy broke
  source-code mode; markRaw fix applied cleanly.

muya suite 1515/1515, tsc/eslint/madge clean.
@Jocs

Jocs commented Sep 15, 2026

Copy link
Copy Markdown
Member

muyajs is no longer used.

@Jocs Jocs closed this Sep 15, 2026
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.

[Bug] Keyboard selection of code language causes renderer crash (TypeError: Cannot read properties of null (reading 'functionType'))

2 participants