fix(muyajs): clamp setStart offset to legal range to prevent IndexSizeError (#2800) by Jocs · Pull Request #4358 · marktext/marktext · GitHub
Skip to content

fix(muyajs): clamp setStart offset to legal range to prevent IndexSizeError (#2800) - #4358

Merged
Jocs merged 2 commits into
developfrom
fix/2800-pre-tag-selection-clamp
Jun 3, 2026
Merged

Jocs merged 2 commits into
developfrom
fix/2800-pre-tag-selection-clamp

Conversation

@Jocs

@Jocs Jocs commented Jun 3, 2026

Copy link
Copy Markdown
Member

Summary

  • Typing <pre>test</pre> (or other inline HTML opening tags) crashed the renderer because the existing setCursorRange clamp in packages/muyajs/lib/selection/index.js only guarded against unsigned-int overflow, not against real-but-out-of-range small positive offsets produced by partialRender consolidating childNodes
  • Extend the clamp to bound offset by the node's childNodes.length (or textContent.length for text nodes) before calling Range.setStart / Range.setEnd / Selection.extend
  • Regression guard: packages/desktop/test/e2e/issue-2800.spec.ts reproduces the original recipe + adjacent inline-HTML variants; red without the fix, green with it

Closes

Closes #2800

Test plan

  • pnpm -C packages/desktop exec playwright test test/e2e/issue-2800.spec.ts — 3/3 pass with this PR (verified red without the fix)
  • Existing crash-range-offset.spec.ts still green — same crash family, ensures clamp expansion doesn't regress already-guarded recipes
  • Other e2e suites (selection-change / update-paragraph / editor-input / inline-format / paragraph-blocks / source-math) still green
  • pnpm run lint, pnpm run typecheck clean

🤖 Generated with Claude Code

…eError (#2800)

Typing literal `<pre>test</pre>` (or other inline HTML opening tags) caused
the renderer to crash:
  IndexSizeError: Failed to execute 'setStart' on 'Range':
  There is no child at offset 5.

The existing clamp in selection/index.js only handled unsigned-integer
overflow (offsets near 2^32). The `<pre>` recipe produces a small positive
offset that nonetheless exceeds the post-partialRender childNodes count for
the paragraph, since the inline HTML token consolidates multiple child
spans into one. Extend the clamp to bound the offset by the node's actual
child / text length before forwarding to Range.setStart / Range.setEnd /
Selection.extend.

Regression guard: test/e2e/issue-2800.spec.ts.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions

github-actions Bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Build artifacts for PR #4358:

Run: https://github.com/marktext/marktext/actions/runs/26894198483

Artifact Size Link
marktext-windows-arm64 270.6 MB Download
marktext-linux 592.9 MB Download
marktext-windows-x64 271.9 MB Download
marktext-macos-x64 271.7 MB Download
marktext-macos-arm64 261.4 MB Download

…lampLegalOffset

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

<pre>test</pre> in typewriter mode breaks input

1 participant