-
Notifications
You must be signed in to change notification settings - Fork 132
fix: backward replace insert text #2172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/super-editor/src/core/extensions/editable.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
| import { TextSelection } from 'prosemirror-state'; | ||
| import { initTestEditor } from '@tests/helpers/helpers.js'; | ||
|
|
||
| const findTextRange = (doc, text) => { | ||
| let range = null; | ||
| doc.descendants((node, pos) => { | ||
| if (node.isText && node.text === text) { | ||
| range = { | ||
| from: pos, | ||
| to: pos + node.text.length, | ||
| }; | ||
| return false; | ||
| } | ||
| return true; | ||
| }); | ||
| return range; | ||
| }; | ||
|
|
||
| describe('Editable extension backward replace handling', () => { | ||
| let editor = null; | ||
|
|
||
| afterEach(() => { | ||
| editor?.destroy(); | ||
| editor = null; | ||
| }); | ||
|
|
||
| it('replaces backward non-empty selection on beforeinput insertText', () => { | ||
| ({ editor } = initTestEditor({ | ||
| mode: 'text', | ||
| content: '<p>PREAMBLE</p>', | ||
| })); | ||
|
|
||
| const range = findTextRange(editor.state.doc, 'PREAMBLE'); | ||
| expect(range).not.toBeNull(); | ||
|
|
||
| const backwardSelection = TextSelection.create(editor.state.doc, range.to, range.from); | ||
| editor.view.dispatch(editor.state.tr.setSelection(backwardSelection)); | ||
|
|
||
| const beforeInputEvent = new InputEvent('beforeinput', { | ||
| data: 'Z', | ||
| inputType: 'insertText', | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
| editor.view.dom.dispatchEvent(beforeInputEvent); | ||
|
|
||
| expect(editor.state.doc.textContent).toBe('Z'); | ||
| }); | ||
| }); |
86 changes: 86 additions & 0 deletions
86
tests/behavior/tests/formatting/replace-text-preserves-style.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { test, expect } from '../../fixtures/superdoc.js'; | ||
|
|
||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const DOC_PATH = path.resolve(__dirname, '../../test-data/basic/sd-site-doc-2026.docx'); | ||
|
|
||
| test.skip(!fs.existsSync(DOC_PATH), 'Test document not available — run pnpm corpus:pull'); | ||
|
|
||
| /** | ||
| * SD-1951: Highlighting text right-to-left (backward selection) and typing a | ||
| * replacement should preserve the original text's styling. On main this was | ||
| * broken because the backward selection caused ProseMirror to misinterpret the | ||
| * insertText input as deleteContentBackward. | ||
| */ | ||
| test('backward-selected text replacement preserves original style (SD-1951)', async ({ superdoc }) => { | ||
| await superdoc.loadDocument(DOC_PATH); | ||
| await superdoc.waitForStable(); | ||
|
|
||
| const titleText = 'Mutual Agreement for Document Excellence'; | ||
| await superdoc.assertTextContains(titleText); | ||
|
|
||
| // Capture the title's marks before replacement | ||
| const titleStart = await superdoc.findTextPos(titleText); | ||
| const titleEnd = titleStart + titleText.length; | ||
| const originalMarks = await superdoc.getMarkAttrsAtPos(titleStart); | ||
|
|
||
| // Create a BACKWARD selection (right-to-left) and dispatch a beforeinput | ||
| // event to simulate typing. This exercises the exact code path from the | ||
| // SD-1951 fix: the editable extension's beforeinput handler intercepts | ||
| // backward selection + insertText and replaces text with correct marks. | ||
| // | ||
| // We dispatch via view.dom so ProseMirror's event pipeline processes it | ||
| // naturally through runCustomHandler → handleDOMEvents → our handler. | ||
| const result = await superdoc.page.evaluate( | ||
| ({ from, to }) => { | ||
| const { state, view } = (window as any).editor; | ||
| const TextSelectionClass = state.selection.constructor; | ||
| const backward = TextSelectionClass.create(state.doc, to, from); | ||
| view.dispatch(state.tr.setSelection(backward)); | ||
|
|
||
| // Dispatch a native beforeinput event on view.dom | ||
| const event = new InputEvent('beforeinput', { | ||
| inputType: 'insertText', | ||
| data: 'Z', | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
| view.dom.dispatchEvent(event); | ||
|
|
||
| return { | ||
| prevented: event.defaultPrevented, | ||
| docText: (window as any).editor.state.doc.textContent.substring(0, 80), | ||
| }; | ||
| }, | ||
| { from: titleStart, to: titleEnd }, | ||
| ); | ||
| await superdoc.waitForStable(); | ||
|
|
||
| // On the fix branch, the handler intercepts and replaces text (preventDefault). | ||
| // On main, the handler doesn't intercept backward selection + insertText. | ||
| expect(result.prevented).toBe(true); | ||
| expect(result.docText).toContain('Z'); | ||
| expect(result.docText).not.toContain(titleText); | ||
|
|
||
| // The replacement character must appear in the document | ||
| await superdoc.assertTextContains('Z'); | ||
| await superdoc.assertTextNotContains(titleText); | ||
|
|
||
| // The replacement must retain the original title's marks (font family, size), | ||
| // not inherit from the previous paragraph or document defaults. | ||
| const zPos = await superdoc.findTextPos('Z'); | ||
| const replacementMarks = await superdoc.getMarkAttrsAtPos(zPos); | ||
|
|
||
| const getTextStyleAttrs = (marks: Array<{ name: string; attrs: Record<string, unknown> }>) => | ||
| marks.find((m) => m.name === 'textStyle')?.attrs; | ||
|
|
||
| const originalStyle = getTextStyleAttrs(originalMarks); | ||
| const replacementStyle = getTextStyleAttrs(replacementMarks); | ||
|
|
||
| expect(originalStyle).toBeDefined(); | ||
| expect(replacementStyle).toBeDefined(); | ||
| expect(replacementStyle!.fontFamily).toBe(originalStyle!.fontFamily); | ||
| expect(replacementStyle!.fontSize).toBe(originalStyle!.fontSize); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
selection.anchorandselection.headare always defined - the ?? fallbacks never trigger.could simplify to just
selection.anchor > selection.head