Skip to content

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

fix(lint-framework): keep render boxes out of editable content (#4508)

Branislav Klocok7c03a60ca471
diff --git a/packages/chrome-plugin/tests/editable_body.spec.ts b/packages/chrome-plugin/tests/editable_body.spec.ts
new file mode 100644
index 000000000..b2f96b0c0
--- /dev/null
+++ b/packages/chrome-plugin/tests/editable_body.spec.ts
@@ -0,0 +1,16 @@
+import { expect, test } from './fixtures';
+import { getHarperHighlights } from './testUtils';
+
+const TEST_PAGE_URL = 'http://localhost:8081/editable_body.html';
+
+test('Keeps render boxes out of an editable body.', async ({ page }) => {
+ await page.goto(TEST_PAGE_URL);
+
+ await expect(getHarperHighlights(page).first()).toBeVisible({ timeout: 24000 });
+
+ // Inside the body they would be part of the edited document (e.g. serialized into an e-mail).
+ const renderBoxesInBody = await page.evaluate(
+ () => document.body.querySelectorAll('harper-render-box').length,
+ );
+ expect(renderBoxesInBody).toBe(0);
+});
diff --git a/packages/chrome-plugin/tests/pages/editable_body.html b/packages/chrome-plugin/tests/pages/editable_body.html
new file mode 100644
index 000000000..76368432c
--- /dev/null
+++ b/packages/chrome-plugin/tests/pages/editable_body.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8" />
+ <title>Editable body test</title>
+ </head>
+ <body contenteditable="true"><p>This is an test of the Harper grammar checker.</p></body>
+</html>
diff --git a/packages/lint-framework/src/lint/Highlights.ts b/packages/lint-framework/src/lint/Highlights.ts
index 2d2aec384..dbc4102b1 100644
--- a/packages/lint-framework/src/lint/Highlights.ts
+++ b/packages/lint-framework/src/lint/Highlights.ts
@@ -1,6 +1,7 @@
import type { VNode } from 'virtual-dom';
import h from 'virtual-dom/h';
import type { LintBox } from './Box';
+import { nonEditableAncestor } from './domUtils';
import {
getCMRoot,
getDraftRoot,
@@ -258,7 +259,7 @@ export default class Highlights {
}
}
- return el.parentElement!;
+ return nonEditableAncestor(el.parentElement!);
}
}
diff --git a/packages/lint-framework/src/lint/PopupHandler.ts b/packages/lint-framework/src/lint/PopupHandler.ts
index 0d04c7af3..61573aa18 100644
--- a/packages/lint-framework/src/lint/PopupHandler.ts
+++ b/packages/lint-framework/src/lint/PopupHandler.ts
@@ -1,5 +1,6 @@
import h from 'virtual-dom/h';
import { closestBox, type IgnorableLintBox, isPointInBox } from './Box';
+import { nonEditableAncestor } from './domUtils';
import { getCaretPosition } from './editorUtils';
import type { UnpackedLint } from './unpackLint';
@@ -56,7 +57,7 @@ export default class PopupHandler {
this.currentLintBoxes = [];
this.currentHint = undefined;
this.currentHintFor = undefined;
- this.renderBox = new RenderBox(() => document.body);
+ this.renderBox = new RenderBox(() => nonEditableAncestor(document.body));
this.renderBox.getShadowHost().popover = 'manual';
this.renderBox.getShadowHost().style.pointerEvents = 'none';
this.renderBox.getShadowHost().style.border = 'none';
diff --git a/packages/lint-framework/src/lint/domUtils.ts b/packages/lint-framework/src/lint/domUtils.ts
index 9b74b39d9..76cc02c9f 100644
--- a/packages/lint-framework/src/lint/domUtils.ts
+++ b/packages/lint-framework/src/lint/domUtils.ts
@@ -54,6 +54,21 @@ export function getClosestBlockAncestor(leaf: Node, root: Element): Element | nu
return null;
}
+/**
+ * Walk up from `el` to the first element that is not editable, so render boxes are never inserted
+ * into the edited content itself (e.g. a designMode or contenteditable `<body>`), where they would
+ * be serialized with it and trigger mutation observers watching it.
+ */
+export function nonEditableAncestor(el: HTMLElement): HTMLElement {
+ let node: HTMLElement | null = el;
+
+ while (node?.isContentEditable) {
+ node = node.parentElement;
+ }
+
+ return node ?? el.ownerDocument.documentElement;
+}
+
/**
* Flatten a provided node, and its children into a single array.
* @param node