Skip to content

fix(core): 修复 #892 divider 撤销后输入报错#894

Merged
cycleccc merged 1 commit into
masterfrom
fix/core/issue-892-divider-undo-input
May 31, 2026
Merged

fix(core): 修复 #892 divider 撤销后输入报错#894
cycleccc merged 1 commit into
masterfrom
fix/core/issue-892-divider-undo-input

Conversation

@cycleccc

@cycleccc cycleccc commented May 31, 2026

Copy link
Copy Markdown
Collaborator

背景

修复 issue #892:在默认 demo 中执行“插入分割线 -> 撤销 -> 重新输入”时,偶发抛出 Cannot resolve a Slate node from DOM node

变更

beforeinput 中将 targetRange -> Slate range 的映射改为容错模式:

  • 使用 suppressThrow: true\n - 对 stale DOM 导致的映射异常进行兜底(跳过重选区,不中断输入)
  • 新增回归测试:
  • target range 无法映射时不抛错且继续输入
  • target range 指向 stale DOM 时不抛错且继续输入
  • 增加 changeset(@wangeditor-next/core patch)

验证

  • pnpm --filter @wangeditor-next/core test -- __tests__/text-area/event-handlers/before-input.test.ts
  • pnpm --filter @wangeditor-next/core test -- __tests__/text-area/event-handlers/composition.test.ts __tests__/text-area/syncSelection.test.ts
  • pnpm --filter @wangeditor-next/core exec eslint src/text-area/event-handlers/beforeInput.ts __tests__/text-area/event-handlers/before-input.test.ts
  • pnpm --filter @wangeditor-next/core build

Closes #892

Summary by CodeRabbit

Bug Fixes

  • Fixed text input errors that could occur after using undo on divider operations, ensuring a smooth typing experience after these actions.
  • Improved error handling for DOM node references that may become stale during text input to prevent interruption and crashes.

beforeinput targetRange 映射改为容错模式

补充回归测试覆盖映射失败与 stale DOM 场景

增加 core patch changeset

Closes: #892
@changeset-bot

changeset-bot Bot commented May 31, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2a7b8a3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
@wangeditor-next/core Patch
@wangeditor-next/editor Patch
@wangeditor-next/demo-react Patch
@wangeditor-next/demo-vue3 Patch
@wangeditor-next/demo-yjs-react Patch
@wangeditor-next/demo-yjs-vue3 Patch
@wangeditor-next/plugin-attachment Patch
@wangeditor-next/plugin-ctrl-enter Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented May 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR hardens the beforeInput event handler to tolerate stale DOM target ranges that occur after undo operations. The fix wraps range conversion in try/catch with error suppression, skips DOM-based selection when conversion fails, and includes tests verifying both null-return and exception paths still allow text insertion to proceed.

Changes

beforeInput stale DOM tolerance fix

Layer / File(s) Summary
beforeInput try/catch guard for stale DOM ranges
packages/core/src/text-area/event-handlers/beforeInput.ts
handleBeforeInput wraps DomEditor.toSlateRange in try/catch with suppressThrow: true, sets range to null on failure, and skips Transforms.select when conversion cannot produce a valid Slate range.
Test range conversion fallback paths
packages/core/__tests__/text-area/event-handlers/before-input.test.ts
Two test cases validate that handleBeforeInput neither throws nor skips Editor.insertText when DOM-to-Slate conversion returns null or throws for stale DOM nodes.
Release note for DOM node resolution fix
.changeset/tough-snails-dance.md
Changesets entry documents a patch fix for beforeinput target range mapping after divider undo, noting the change tolerates stale DOM nodes to prevent resolution errors.

Sequence Diagram(s)

(No diagram generated; the change is a defensive guard condition, not a multi-component control-flow or state-transition sequence.)

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • wangeditor-next/wangEditor-next#849: Both PRs address DOM-to-Slate resolution failures; this PR hardens the beforeInput handler around stale DOM ranges, while that PR refines DOM element lookup scoping within the editor root to prevent cross-editor ID collisions.

Poem

🐰 A stale DOM node tried to hide,
But try/catch caught it with stride,
Text still flows where selection fails,
The editor's heart forever sails! ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ⚠️ Warning The PR description is provided in Chinese and covers background, changes, and verification steps, but does not follow the required template structure with English sections like 'Changes Overview', 'Implementation Approach', etc. Reformat the description to follow the repository's template with sections: Changes Overview, Implementation Approach, Testing Done, Verification Steps, Additional Notes, Checklist, and Related Issues.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately addresses the main change: fixing issue #892 related to divider undo input errors, which directly corresponds to the handleBeforeInput error handling improvement described in the changeset and code modifications.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/core/issue-892-divider-undo-input

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@cycleccc cycleccc merged commit 5efc385 into master May 31, 2026
12 checks passed
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