Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(lint-framework): preserve cursor position in textarea after applying suggestion (#2650)

David Raphif57bd908f9cb
diff --git a/packages/chrome-plugin/tests/draft.spec.ts b/packages/chrome-plugin/tests/draft.spec.ts
index 584b6b5ef..7675341f0 100644
--- a/packages/chrome-plugin/tests/draft.spec.ts
+++ b/packages/chrome-plugin/tests/draft.spec.ts
@@ -1,18 +1,20 @@
-import type { Locator } from '@playwright/test';
+import type { Locator, Page } from '@playwright/test';
import {
getDraftEditor,
- testBasicSuggestionRichText,
- testCanIgnoreRichTextSuggestion,
+ testBasicSuggestion,
+ testCanBlockRuleSuggestion,
+ testCanIgnoreSuggestion,
testMultipleSuggestionsAndUndo,
} from './testUtils';
const TEST_PAGE_URL = 'https://draftjs.org/';
-async function setup(editor: Locator) {
+async function setup(_page: Page, editor: Locator) {
await editor.scrollIntoViewIfNeeded();
await editor.click();
}
-testBasicSuggestionRichText(TEST_PAGE_URL, getDraftEditor, setup);
+testBasicSuggestion(TEST_PAGE_URL, getDraftEditor, setup);
+testCanIgnoreSuggestion(TEST_PAGE_URL, getDraftEditor, setup);
+testCanBlockRuleSuggestion(TEST_PAGE_URL, getDraftEditor, setup);
testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getDraftEditor, setup);
-testCanIgnoreRichTextSuggestion(TEST_PAGE_URL, getDraftEditor, setup);
diff --git a/packages/chrome-plugin/tests/github.spec.ts b/packages/chrome-plugin/tests/github.spec.ts
index c43642f8e..a0e346014 100644
--- a/packages/chrome-plugin/tests/github.spec.ts
+++ b/packages/chrome-plugin/tests/github.spec.ts
@@ -1,18 +1,26 @@
+import type { Page } from '@playwright/test';
import { test } from './fixtures';
import {
assertHarperHighlightBoxes,
getTextarea,
replaceEditorContent,
- testBasicSuggestionTextarea,
- testCanBlockRuleTextareaSuggestion,
- testCanIgnoreTextareaSuggestion,
+ testBasicSuggestion,
+ testCanBlockRuleSuggestion,
+ testCanIgnoreSuggestion,
+ testMultipleSuggestionsAndUndo,
} from './testUtils';
const TEST_PAGE_URL = 'http://localhost:8081/github_textarea.html';
-testBasicSuggestionTextarea(TEST_PAGE_URL);
-testCanIgnoreTextareaSuggestion(TEST_PAGE_URL);
-testCanBlockRuleTextareaSuggestion(TEST_PAGE_URL);
+async function textareaSetup(page: Page) {
+ await page.waitForTimeout(2000);
+ await page.reload();
+}
+
+testBasicSuggestion(TEST_PAGE_URL, getTextarea, textareaSetup);
+testCanIgnoreSuggestion(TEST_PAGE_URL, getTextarea, textareaSetup);
+testCanBlockRuleSuggestion(TEST_PAGE_URL, getTextarea);
+testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getTextarea, textareaSetup);
test('Wraps correctly', async ({ page }) => {
await page.goto(TEST_PAGE_URL);
diff --git a/packages/chrome-plugin/tests/hn.spec.ts b/packages/chrome-plugin/tests/hn.spec.ts
index afcb6bc10..557d4eaa4 100644
--- a/packages/chrome-plugin/tests/hn.spec.ts
+++ b/packages/chrome-plugin/tests/hn.spec.ts
@@ -4,9 +4,10 @@ import {
assertHarperHighlightBoxes,
getTextarea,
replaceEditorContent,
- testBasicSuggestionTextarea,
- testCanBlockRuleTextareaSuggestion,
- testCanIgnoreTextareaSuggestion,
+ testBasicSuggestion,
+ testCanBlockRuleSuggestion,
+ testCanIgnoreSuggestion,
+ testMultipleSuggestionsAndUndo,
} from './testUtils';
/** Must be computed. */
@@ -19,9 +20,10 @@ async function getTestPageUrl(page: Page) {
return page.url();
}
-testBasicSuggestionTextarea(getTestPageUrl);
-testCanIgnoreTextareaSuggestion(getTestPageUrl);
-testCanBlockRuleTextareaSuggestion(getTestPageUrl);
+testBasicSuggestion(getTestPageUrl, getTextarea);
+testCanIgnoreSuggestion(getTestPageUrl, getTextarea);
+testCanBlockRuleSuggestion(getTestPageUrl, getTextarea);
+testMultipleSuggestionsAndUndo(getTestPageUrl, getTextarea);
test('Hacker News wraps correctly', async ({ page }) => {
await page.goto(await getTestPageUrl(page));
diff --git a/packages/chrome-plugin/tests/lexical.spec.ts b/packages/chrome-plugin/tests/lexical.spec.ts
index 7c638ab7d..b0f55a43e 100644
--- a/packages/chrome-plugin/tests/lexical.spec.ts
+++ b/packages/chrome-plugin/tests/lexical.spec.ts
@@ -1,12 +1,14 @@
import {
getLexicalEditor,
- testBasicSuggestionRichText,
- testCanIgnoreRichTextSuggestion,
+ testBasicSuggestion,
+ testCanBlockRuleSuggestion,
+ testCanIgnoreSuggestion,
testMultipleSuggestionsAndUndo,
} from './testUtils';
const TEST_PAGE_URL = 'https://playground.lexical.dev/';
-testBasicSuggestionRichText(TEST_PAGE_URL, getLexicalEditor);
+testBasicSuggestion(TEST_PAGE_URL, getLexicalEditor);
+testCanIgnoreSuggestion(TEST_PAGE_URL, getLexicalEditor);
+testCanBlockRuleSuggestion(TEST_PAGE_URL, getLexicalEditor);
testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getLexicalEditor);
-testCanIgnoreRichTextSuggestion(TEST_PAGE_URL, getLexicalEditor);
diff --git a/packages/chrome-plugin/tests/prosemirror.spec.ts b/packages/chrome-plugin/tests/prosemirror.spec.ts
index a80b67318..42d32c4fa 100644
--- a/packages/chrome-plugin/tests/prosemirror.spec.ts
+++ b/packages/chrome-plugin/tests/prosemirror.spec.ts
@@ -1,12 +1,14 @@
import {
getProseMirrorEditor,
- testBasicSuggestionRichText,
- testCanIgnoreRichTextSuggestion,
+ testBasicSuggestion,
+ testCanBlockRuleSuggestion,
+ testCanIgnoreSuggestion,
testMultipleSuggestionsAndUndo,
} from './testUtils';
const TEST_PAGE_URL = 'https://prosemirror.net/';
-testBasicSuggestionRichText(TEST_PAGE_URL, getProseMirrorEditor);
+testBasicSuggestion(TEST_PAGE_URL, getProseMirrorEditor);
+testCanIgnoreSuggestion(TEST_PAGE_URL, getProseMirrorEditor);
+testCanBlockRuleSuggestion(TEST_PAGE_URL, getProseMirrorEditor);
testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getProseMirrorEditor);
-testCanIgnoreRichTextSuggestion(TEST_PAGE_URL, getProseMirrorEditor);
diff --git a/packages/chrome-plugin/tests/simple_textarea.spec.ts b/packages/chrome-plugin/tests/simple_textarea.spec.ts
index 1e482da33..6a86bc967 100644
--- a/packages/chrome-plugin/tests/simple_textarea.spec.ts
+++ b/packages/chrome-plugin/tests/simple_textarea.spec.ts
@@ -5,16 +5,18 @@ import {
clickHarperHighlight,
getTextarea,
replaceEditorContent,
- testBasicSuggestionTextarea,
- testCanBlockRuleTextareaSuggestion,
- testCanIgnoreTextareaSuggestion,
+ testBasicSuggestion,
+ testCanBlockRuleSuggestion,
+ testCanIgnoreSuggestion,
+ testMultipleSuggestionsAndUndo,
} from './testUtils';
const TEST_PAGE_URL = 'http://localhost:8081/simple_textarea.html';
-testBasicSuggestionTextarea(TEST_PAGE_URL);
-testCanIgnoreTextareaSuggestion(TEST_PAGE_URL);
-testCanBlockRuleTextareaSuggestion(TEST_PAGE_URL);
+testBasicSuggestion(TEST_PAGE_URL, getTextarea);
+testCanIgnoreSuggestion(TEST_PAGE_URL, getTextarea);
+testCanBlockRuleSuggestion(TEST_PAGE_URL, getTextarea);
+testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getTextarea);
test('Wraps correctly', async ({ page }, testInfo) => {
await page.goto(TEST_PAGE_URL);
diff --git a/packages/chrome-plugin/tests/slate.spec.ts b/packages/chrome-plugin/tests/slate.spec.ts
index 052feba2d..650810d48 100644
--- a/packages/chrome-plugin/tests/slate.spec.ts
+++ b/packages/chrome-plugin/tests/slate.spec.ts
@@ -1,12 +1,14 @@
import {
getSlateEditor,
- testBasicSuggestionRichText,
- testCanIgnoreRichTextSuggestion,
+ testBasicSuggestion,
+ testCanBlockRuleSuggestion,
+ testCanIgnoreSuggestion,
testMultipleSuggestionsAndUndo,
} from './testUtils';
const TEST_PAGE_URL = 'https://slatejs.org';
-testBasicSuggestionRichText(TEST_PAGE_URL, getSlateEditor);
+testBasicSuggestion(TEST_PAGE_URL, getSlateEditor);
+testCanIgnoreSuggestion(TEST_PAGE_URL, getSlateEditor);
+testCanBlockRuleSuggestion(TEST_PAGE_URL, getSlateEditor);
testMultipleSuggestionsAndUndo(TEST_PAGE_URL, getSlateEditor);
-testCanIgnoreRichTextSuggestion(TEST_PAGE_URL, getSlateEditor);
diff --git a/packages/chrome-plugin/tests/testUtils.ts b/packages/chrome-plugin/tests/testUtils.ts
index 64bf07dae..f03903b4b 100644
--- a/packages/chrome-plugin/tests/testUtils.ts
+++ b/packages/chrome-plugin/tests/testUtils.ts
@@ -107,15 +107,43 @@ async function resolveTestPage(prov: TestPageUrlProvider, page: Page): Promise<s
}
}
-export async function testBasicSuggestionTextarea(testPageUrl: TestPageUrlProvider) {
+/** Exact-match assertion: `toHaveValue` for form elements, `toHaveText` for contenteditable. */
+async function assertEditorText(editor: Locator, text: string) {
+ if (await isFormElement(editor)) {
+ await expect(editor).toHaveValue(text);
+ } else {
+ await expect(editor).toHaveText(text);
+ }
+}
+
+/** Substring assertion: `inputValue` for form elements, `toContainText` for contenteditable. */
+async function assertEditorContains(editor: Locator, text: string) {
+ if (await isFormElement(editor)) {
+ const value = await editor.inputValue();
+ expect(value).toContain(text);
+ } else {
+ await expect(editor).toContainText(text);
+ }
+}
+
+async function isFormElement(editor: Locator): Promise<boolean> {
+ return editor.evaluate((el) => el.tagName === 'TEXTAREA' || el.tagName === 'INPUT');
+}
+
+/** Test applying a basic suggestion and verify cursor position after replacement. */
+export async function testBasicSuggestion(
+ testPageUrl: TestPageUrlProvider,
+ getEditor: EditorLocatorProvider,
+ setup?: (page: Page, editor: Locator) => Promise<void>,
+) {
test('Can apply basic suggestion.', async ({ page }) => {
const url = await resolveTestPage(testPageUrl, page);
await page.goto(url);
- await page.waitForTimeout(2000);
- await page.reload();
-
- const editor = getTextarea(page);
+ const editor = getEditor(page);
+ if (setup) {
+ await setup(page, editor);
+ }
await replaceEditorContent(editor, 'This is an test');
await page.waitForTimeout(6000);
@@ -125,20 +153,35 @@ export async function testBasicSuggestionTextarea(testPageUrl: TestPageUrlProvid
await page.waitForTimeout(3000);
- expect(editor).toHaveValue('This is a test');
- await assertLocatorIsFocused(page, editor);
+ await assertEditorText(editor, 'This is a test');
+
+ // Cursor should be right after "a" (pos 9). ArrowRight×3 + Backspace deletes 'e'.
+ await page.keyboard.press('ArrowRight');
+ await page.keyboard.press('ArrowRight');
+ await page.keyboard.press('ArrowRight');
+ await page.keyboard.press('Backspace');
+ await assertEditorText(editor, 'This is a tst');
+
+ // Verify typing still works.
+ await editor.pressSequentially('e');
+ await assertEditorText(editor, 'This is a test');
});
}
-export async function testCanIgnoreTextareaSuggestion(testPageUrl: TestPageUrlProvider) {
+/** Test ignoring a suggestion. */
+export async function testCanIgnoreSuggestion(
+ testPageUrl: TestPageUrlProvider,
+ getEditor: EditorLocatorProvider,
+ setup?: (page: Page, editor: Locator) => Promise<void>,
+) {
test('Can ignore suggestion.', async ({ page }) => {
const url = await resolveTestPage(testPageUrl, page);
await page.goto(url);
- await page.waitForTimeout(2000);
- await page.reload();
-
- const editor = getTextarea(page);
+ const editor = getEditor(page);
+ if (setup) {
+ await setup(page, editor);
+ }
const cacheSalt = randomString(5);
await replaceEditorContent(editor, cacheSalt);
@@ -154,18 +197,26 @@ export async function testCanIgnoreTextareaSuggestion(testPageUrl: TestPageUrlPr
await expect(getHarperHighlights(page)).toHaveCount(0);
// Nothing should change.
- expect(editor).toHaveValue(cacheSalt);
+ await assertEditorText(editor, cacheSalt);
expect(await clickHarperHighlight(page)).toBe(false);
await assertLocatorIsFocused(page, editor);
});
}
-export async function testCanBlockRuleTextareaSuggestion(testPageUrl: TestPageUrlProvider) {
+/** Test disabling a lint rule via the block button. */
+export async function testCanBlockRuleSuggestion(
+ testPageUrl: TestPageUrlProvider,
+ getEditor: EditorLocatorProvider,
+ setup?: (page: Page, editor: Locator) => Promise<void>,
+) {
test('Can hide with rule block button', async ({ page }) => {
const url = await resolveTestPage(testPageUrl, page);
await page.goto(url);
- const editor = getTextarea(page);
+ const editor = getEditor(page);
+ if (setup) {
+ await setup(page, editor);
+ }
await replaceEditorContent(editor, 'This is an test.');
await page.waitForTimeout(6000);
@@ -199,81 +250,11 @@ 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,
getEditor: EditorLocatorProvider,
- setup?: (editor: Locator) => Promise<void>,
+ setup?: (page: Page, editor: Locator) => Promise<void>,
) {
test('Multiple suggestions and undo.', async ({ page }) => {
const url = await resolveTestPage(testPageUrl, page);
@@ -281,11 +262,11 @@ export async function testMultipleSuggestionsAndUndo(
const editor = getEditor(page);
if (setup) {
- await setup(editor);
+ await setup(page, editor);
}
await replaceEditorContent(editor, 'The first tset.\nThe second tset.\nThe third tset.');
- await page.waitForTimeout(3000);
+ await page.waitForTimeout(6000);
const highlights = getHarperHighlights(page);
await expect(highlights).toHaveCount(3);
@@ -303,14 +284,14 @@ export async function testMultipleSuggestionsAndUndo(
await page.waitForTimeout(500);
// Verify only second "tset" was corrected
- await expect(editor).toContainText('first tset');
- await expect(editor).toContainText('second test');
- await expect(editor).toContainText('third tset');
+ await assertEditorContains(editor, 'first tset');
+ await assertEditorContains(editor, 'second test');
+ await assertEditorContains(editor, 'third tset');
// Undo
await editor.press('Control+z');
await page.waitForTimeout(300);
- await expect(editor).toContainText('The second tset');
+ await assertEditorContains(editor, 'The second tset');
});
}
diff --git a/packages/lint-framework/src/lint/computeLintBoxes.ts b/packages/lint-framework/src/lint/computeLintBoxes.ts
index fbde63540..0d9dad987 100644
--- a/packages/lint-framework/src/lint/computeLintBoxes.ts
+++ b/packages/lint-framework/src/lint/computeLintBoxes.ts
@@ -89,8 +89,8 @@ function replaceValue(
span?: { start: number; end: number },
replacementText?: string,
) {
- if (isFormEl(el)) {
- replaceFormElementValue(el as HTMLTextAreaElement | HTMLInputElement, value);
+ if (isFormEl(el) && span && replacementText !== undefined) {
+ replaceFormElementValue(el as HTMLTextAreaElement | HTMLInputElement, span, replacementText);
} else if (getLexicalRoot(el) != null && span && replacementText !== undefined) {
replaceLexicalValue(el, span, replacementText);
} else if (getDraftRoot(el) != null && span && replacementText !== undefined) {
@@ -108,10 +108,14 @@ function replaceValue(
el.dispatchEvent(new Event('change', { bubbles: true }));
}
-function replaceFormElementValue(el: HTMLTextAreaElement | HTMLInputElement, value: string) {
- el.dispatchEvent(new InputEvent('beforeinput', { bubbles: true, data: value }));
- el.value = value;
- el.dispatchEvent(new InputEvent('input', { bubbles: true }));
+function replaceFormElementValue(
+ el: HTMLTextAreaElement | HTMLInputElement,
+ span: { start: number; end: number },
+ replacementText: string,
+) {
+ el.focus();
+ el.setSelectionRange(span.start, span.end);
+ document.execCommand('insertText', false, replacementText);
}
function replaceLexicalValue(