Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(chrome-ext): preserve cursor position in generic contenteditable replacement (#2617)
David Raphiacbc05ec8821
diff --git a/packages/chrome-plugin/tests/draft.spec.ts b/packages/chrome-plugin/tests/draft.spec.tsindex 546abe791..584b6b5ef 100644--- a/packages/chrome-plugin/tests/draft.spec.ts+++ b/packages/chrome-plugin/tests/draft.spec.ts@@ -1,69 +1,18 @@-import { expect, test } from './fixtures';+import type { Locator } from '@playwright/test';import {- clickHarperHighlight,getDraftEditor,- getHarperHighlights,- randomString,- replaceEditorContent,+ testBasicSuggestionRichText,+ testCanIgnoreRichTextSuggestion,testMultipleSuggestionsAndUndo,} from './testUtils';const TEST_PAGE_URL = 'https://draftjs.org/';-test('Can apply basic suggestion.', async ({ page }) => {- await page.goto(TEST_PAGE_URL);-- const draft = getDraftEditor(page);- await draft.scrollIntoViewIfNeeded();- await draft.click();- await replaceEditorContent(draft, 'This is an test');-- await page.waitForTimeout(3000);-- await clickHarperHighlight(page);- await page.getByTitle('Replace with "a"').click();-- await page.waitForTimeout(3000);-- await expect(draft).toContainText('This is a test');-- // Verify editor state is preserved: arrow keys and backspace must work.- // Position cursor before 's' in 'test', then backspace to delete 'e'.- await draft.press('End');- await draft.press('ArrowLeft');- await draft.press('ArrowLeft');- await draft.press('Backspace');- await expect(draft).toContainText('This is a tst');-- // Verify typing still works.- await draft.pressSequentially('e');- await expect(draft).toContainText('This is a test');-});--testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getDraftEditor, async (editor) => {+async function setup(editor: Locator) {await editor.scrollIntoViewIfNeeded();await editor.click();-});--test('Can ignore suggestion.', async ({ page }) => {- await page.goto(TEST_PAGE_URL);- const draft = getDraftEditor(page);-- await draft.scrollIntoViewIfNeeded();- await draft.click();-- const cacheSalt = randomString(5);- await replaceEditorContent(draft, cacheSalt);-- await page.waitForTimeout(3000);-- const opened = await clickHarperHighlight(page);- expect(opened).toBe(true);- await page.getByTitle('Ignore this lint').click();-- await expect(getHarperHighlights(page)).toHaveCount(0);+}- // Nothing should change.- await expect(draft).toContainText(cacheSalt);- expect(await clickHarperHighlight(page)).toBe(false);-});+testBasicSuggestionRichText(TEST_PAGE_URL, getDraftEditor, setup);+testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getDraftEditor, setup);+testCanIgnoreRichTextSuggestion(TEST_PAGE_URL, getDraftEditor, setup);diff --git a/packages/chrome-plugin/tests/lexical.spec.ts b/packages/chrome-plugin/tests/lexical.spec.tsindex d7df2c0ea..7c638ab7d 100644--- a/packages/chrome-plugin/tests/lexical.spec.ts+++ b/packages/chrome-plugin/tests/lexical.spec.ts@@ -1,60 +1,12 @@-import { expect, test } from './fixtures';import {- clickHarperHighlight,- getHarperHighlights,getLexicalEditor,- randomString,- replaceEditorContent,+ testBasicSuggestionRichText,+ testCanIgnoreRichTextSuggestion,testMultipleSuggestionsAndUndo,} from './testUtils';const TEST_PAGE_URL = 'https://playground.lexical.dev/';-test('Can apply basic suggestion.', async ({ page }) => {- await page.goto(TEST_PAGE_URL);-- const lexical = getLexicalEditor(page);- await replaceEditorContent(lexical, 'This is an test');-- await page.waitForTimeout(3000);-- await clickHarperHighlight(page);- await page.getByTitle('Replace with "a"').click();-- await page.waitForTimeout(3000);-- await expect(lexical).toContainText('This is a test');-- // Verify editor state is preserved: arrow keys and backspace must work.- await lexical.press('End');- await lexical.press('ArrowLeft');- await lexical.press('ArrowLeft');- await lexical.press('Backspace');- await expect(lexical).toContainText('This is a tst');-- // Verify typing still works.- await lexical.pressSequentially('e');- await expect(lexical).toContainText('This is a test');-});-+testBasicSuggestionRichText(TEST_PAGE_URL, getLexicalEditor);testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getLexicalEditor);--test('Can ignore suggestion.', async ({ page }) => {- await page.goto(TEST_PAGE_URL);- const lexical = getLexicalEditor(page);-- const cacheSalt = randomString(5);- await replaceEditorContent(lexical, cacheSalt);-- await page.waitForTimeout(3000);-- const opened = await clickHarperHighlight(page);- expect(opened).toBe(true);- await page.getByTitle('Ignore this lint').click();-- await expect(getHarperHighlights(page)).toHaveCount(0);-- // Nothing should change.- await expect(lexical).toContainText(cacheSalt);- expect(await clickHarperHighlight(page)).toBe(false);-});+testCanIgnoreRichTextSuggestion(TEST_PAGE_URL, getLexicalEditor);diff --git a/packages/chrome-plugin/tests/prosemirror.spec.ts b/packages/chrome-plugin/tests/prosemirror.spec.tsindex 2f2338ab7..a80b67318 100644--- a/packages/chrome-plugin/tests/prosemirror.spec.ts+++ b/packages/chrome-plugin/tests/prosemirror.spec.ts@@ -1,53 +1,12 @@-import { expect, test } from './fixtures';import {- clickHarperHighlight,- getHarperHighlights,getProseMirrorEditor,- randomString,- replaceEditorContent,+ testBasicSuggestionRichText,+ testCanIgnoreRichTextSuggestion,testMultipleSuggestionsAndUndo,} from './testUtils';const TEST_PAGE_URL = 'https://prosemirror.net/';-test('Can apply basic suggestion.', async ({ page }) => {- await page.goto(TEST_PAGE_URL);-- const pm = getProseMirrorEditor(page);- await replaceEditorContent(pm, 'This is an test');-- await page.waitForTimeout(3000);-- await clickHarperHighlight(page);- await page.getByTitle('Replace with "a"').click();-- await page.waitForTimeout(3000);-- await expect(pm).toContainText('This is a test');- await pm.press('Control+ArrowDown');-- await pm.pressSequentially(' of Harper’s grammar checking.');- await expect(pm).toContainText('This is a test of Harper’s grammar checking.');-});-+testBasicSuggestionRichText(TEST_PAGE_URL, getProseMirrorEditor);testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getProseMirrorEditor);--test('Can ignore suggestion.', async ({ page }) => {- await page.goto(TEST_PAGE_URL);- const pm = getProseMirrorEditor(page);-- const cacheSalt = randomString(5);- await replaceEditorContent(pm, cacheSalt);-- await page.waitForTimeout(3000);-- const opened = await clickHarperHighlight(page);- expect(opened).toBe(true);- await page.getByTitle('Ignore this lint').click();-- await expect(getHarperHighlights(page)).toHaveCount(0);-- // Nothing should change.- await expect(pm).toContainText(cacheSalt);- expect(await clickHarperHighlight(page)).toBe(false);-});+testCanIgnoreRichTextSuggestion(TEST_PAGE_URL, getProseMirrorEditor);diff --git a/packages/chrome-plugin/tests/slate.spec.ts b/packages/chrome-plugin/tests/slate.spec.tsindex 90f66cafc..052feba2d 100644--- a/packages/chrome-plugin/tests/slate.spec.ts+++ b/packages/chrome-plugin/tests/slate.spec.ts@@ -1,61 +1,12 @@-import { expect, test } from './fixtures';import {- clickHarperHighlight,- getHarperHighlights,getSlateEditor,- randomString,- replaceEditorContent,+ testBasicSuggestionRichText,+ testCanIgnoreRichTextSuggestion,testMultipleSuggestionsAndUndo,} from './testUtils';const TEST_PAGE_URL = 'https://slatejs.org';-test('Can apply basic suggestion.', async ({ page }) => {- await page.goto(TEST_PAGE_URL);-- const slate = getSlateEditor(page);- await replaceEditorContent(slate, 'This is an test');-- await page.waitForTimeout(3000);-- await clickHarperHighlight(page);- await page.getByTitle('Replace with "a"').click();-- await page.waitForTimeout(3000);-- await expect(slate).toContainText('This is a test');-- // Verify editor state is preserved: arrow keys and backspace must work.- // Position cursor before 's' in 'test', then backspace to delete 'e'.- await page.press('body', 'End');- await page.press('body', 'ArrowLeft');- await page.press('body', 'ArrowLeft');- await page.press('body', 'Backspace');- await expect(slate).toContainText('This is a tst');-- // Verify typing still works.- await slate.pressSequentially('e');- await expect(slate).toContainText('This is a test');-});-+testBasicSuggestionRichText(TEST_PAGE_URL, getSlateEditor);testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getSlateEditor);--test('Can ignore suggestion.', async ({ page }) => {- await page.goto(TEST_PAGE_URL);- const slate = getSlateEditor(page);-- const cacheSalt = randomString(5);- await replaceEditorContent(slate, cacheSalt);-- await page.waitForTimeout(3000);-- const opened = await clickHarperHighlight(page);- expect(opened).toBe(true);- await page.getByTitle('Ignore this lint').click();-- await expect(getHarperHighlights(page)).toHaveCount(0);-- // Nothing should change.- await expect(slate).toContainText(cacheSalt);- expect(await clickHarperHighlight(page)).toBe(false);-});+testCanIgnoreRichTextSuggestion(TEST_PAGE_URL, getSlateEditor);diff --git a/packages/chrome-plugin/tests/testUtils.ts b/packages/chrome-plugin/tests/testUtils.tsindex 27cefef42..64bf07dae 100644--- a/packages/chrome-plugin/tests/testUtils.ts+++ b/packages/chrome-plugin/tests/testUtils.ts@@ -199,6 +199,76 @@ async function getSortedHighlightBoxes(page: Page) {return boxes;}+/** Test applying a basic suggestion in a rich text editor and verify editor state is preserved. */+export async function testBasicSuggestionRichText(+ testPageUrl: TestPageUrlProvider,+ getEditor: EditorLocatorProvider,+ setup?: (editor: Locator) => Promise<void>,+) {+ test('Can apply basic suggestion.', async ({ page }) => {+ const url = await resolveTestPage(testPageUrl, page);+ await page.goto(url);++ const editor = getEditor(page);+ if (setup) {+ await setup(editor);+ }+ await replaceEditorContent(editor, 'This is an test');++ await page.waitForTimeout(3000);++ await clickHarperHighlight(page);+ await page.getByTitle('Replace with "a"').click();++ await page.waitForTimeout(3000);++ await expect(editor).toContainText('This is a test');++ // Cursor should be right after "a" (pos 9). ArrowRight×3 + Backspace deletes 'e'.+ await page.press('body', 'ArrowRight');+ await page.press('body', 'ArrowRight');+ await page.press('body', 'ArrowRight');+ await page.press('body', 'Backspace');+ await expect(editor).toContainText('This is a tst');++ // Verify typing still works.+ await editor.pressSequentially('e');+ await expect(editor).toContainText('This is a test');+ });+}++/** Test ignoring a suggestion in a rich text editor. */+export async function testCanIgnoreRichTextSuggestion(+ testPageUrl: TestPageUrlProvider,+ getEditor: EditorLocatorProvider,+ setup?: (editor: Locator) => Promise<void>,+) {+ test('Can ignore suggestion.', async ({ page }) => {+ const url = await resolveTestPage(testPageUrl, page);+ await page.goto(url);++ const editor = getEditor(page);+ if (setup) {+ await setup(editor);+ }++ const cacheSalt = randomString(5);+ await replaceEditorContent(editor, cacheSalt);++ await page.waitForTimeout(3000);++ const opened = await clickHarperHighlight(page);+ expect(opened).toBe(true);+ await page.getByTitle('Ignore this lint').click();++ await expect(getHarperHighlights(page)).toHaveCount(0);++ // Nothing should change.+ await expect(editor).toContainText(cacheSalt);+ expect(await clickHarperHighlight(page)).toBe(false);+ });+}+/** Test multiline suggestion replacement and undo. */export async function testMultipleSuggestionsAndUndo(testPageUrl: TestPageUrlProvider,diff --git a/packages/lint-framework/src/lint/computeLintBoxes.ts b/packages/lint-framework/src/lint/computeLintBoxes.tsindex 0b2c50295..fbde63540 100644--- a/packages/lint-framework/src/lint/computeLintBoxes.ts+++ b/packages/lint-framework/src/lint/computeLintBoxes.ts@@ -102,7 +102,7 @@ function replaceValue() {replaceRichTextEditorValue(el, span, replacementText);} else {- replaceGenericContentEditable(el, value);+ replaceGenericContentEditable(el, value, span, replacementText);}el.dispatchEvent(new Event('change', { bubbles: true }));@@ -245,8 +245,23 @@ function replaceTextInRange(doc: Document, sel: Selection, range: Range, replace}}-function replaceGenericContentEditable(el: HTMLElement, value: string) {+function replaceGenericContentEditable(+ el: HTMLElement,+ value: string,+ span?: { start: number; end: number },+ replacementText?: string,+) {+ if (span && replacementText !== undefined) {+ const setup = selectSpanInEditor(el, span);+ if (setup) {+ const { doc, sel, range } = setup;+ replaceTextInRange(doc, sel, range, replacementText);+ el.dispatchEvent(new InputEvent('input', { bubbles: true, cancelable: false }));+ return;+ }+ }++ // Fallback: replace entire contentel.textContent = value;- el.dispatchEvent(new InputEvent('beforeinput', { bubbles: true, data: value }));el.dispatchEvent(new InputEvent('input', { bubbles: true }));}