Skip to content

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

fix(chrome-ext): highlights are mis-positioned in fixed elements (#2477)

Elijah Potter58c74369eb4f
diff --git a/index.html b/index.html
new file mode 100644
index 000000000..9be31820b
--- /dev/null
+++ b/index.html
@@ -0,0 +1,115 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8" />
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
+ <title>Textarea Ancestor Positioning Hierarchy Test</title>
+ <style>
+ html,
+ body {
+ height: 100%;
+ margin: 0;
+ }
+
+ #hgl-container {
+ position: fixed;
+ top: 50%;
+ left: 50%;
+ transform: translate(-50%, -50%);
+ width: 50vw;
+ height: 50vh;
+ z-index: 2147483647;
+ background: rgb(255, 255, 255);
+ color: rgb(0, 0, 0);
+ border: 1px solid #ccc;
+ padding: 0;
+ box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
+ box-sizing: border-box;
+ display: flex;
+ flex-direction: column;
+ font-family: system-ui, -apple-system, sans-serif;
+ }
+
+ #hgl-titlebar {
+ background-color: #f0f0f0;
+ color: rgb(0, 0, 0);
+ padding: 8px 12px;
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ cursor: move;
+ user-select: none;
+ border-bottom: 1px solid #ccc;
+ font-size: 13px;
+ font-weight: 500;
+ min-height: 32px;
+ box-sizing: border-box;
+ }
+
+ #hgl-close {
+ background: none;
+ border: none;
+ cursor: pointer;
+ font-size: 20px;
+ padding: 0 4px;
+ color: rgb(0, 0, 0);
+ min-width: 32px;
+ min-height: 32px;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ flex: 0 0 auto;
+ }
+
+ #hgl-wrapper {
+ flex: 1;
+ overflow: hidden;
+ padding: 4px;
+ box-sizing: border-box;
+ position: relative;
+ }
+
+ #hgl-textarea {
+ width: 100%;
+ height: 100%;
+ box-sizing: border-box;
+ border: none;
+ font-family: monospace;
+ font-size: 13px;
+ resize: none;
+ padding: 8px;
+ color: rgb(0, 0, 0);
+ background-color: rgb(255, 255, 255);
+ }
+
+ #hgl-resize {
+ position: absolute;
+ bottom: 0;
+ right: 0;
+ width: 16px;
+ height: 16px;
+ cursor: nwse-resize;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ font-size: 14px;
+ color: #999;
+ pointer-events: none;
+ user-select: none;
+ }
+ </style>
+ </head>
+ <body>
+ <div id="hgl-container">
+ <div id="hgl-titlebar">
+ <span>🧐 Harper Glasses • Layout test</span>
+ <button id="hgl-close" type="button" aria-label="Close">✕</button>
+ </div>
+
+ <div id="hgl-wrapper">
+ <textarea id="hgl-textarea" spellcheck="false">Textarea content…</textarea>
+ <div id="hgl-resize">⋱</div>
+ </div>
+ </div>
+ </body>
+</html>
diff --git a/packages/chrome-plugin/tests/nested_elements.spec.ts b/packages/chrome-plugin/tests/nested_elements.spec.ts
new file mode 100644
index 000000000..3916ce3f5
--- /dev/null
+++ b/packages/chrome-plugin/tests/nested_elements.spec.ts
@@ -0,0 +1,22 @@
+import { test } from './fixtures';
+import { assertHarperHighlightBoxes, getTextarea, replaceEditorContent } from './testUtils';
+
+const TEST_PAGE_URL = 'http://localhost:8081/nested_elements.html';
+
+test('Positions properly in oddly nested page.', async ({ page }, testInfo) => {
+ await page.goto(TEST_PAGE_URL);
+
+ const editor = getTextarea(page);
+ await replaceEditorContent(
+ editor,
+ 'This is an test of the Harper grammar checker, specifically if \n the highlights are positionasd properly.',
+ );
+
+ await page.waitForTimeout(6000);
+
+ await assertHarperHighlightBoxes(page, [
+ { x: 396.390625, y: 243, width: 15.625, height: 19 },
+ { x: 794.1875, y: 243, width: 23.421875, height: 19 },
+ { x: 490, y: 260, width: 85.828125, height: 19 },
+ ]);
+});
diff --git a/packages/chrome-plugin/tests/pages/nested_elements.html b/packages/chrome-plugin/tests/pages/nested_elements.html
new file mode 100644
index 000000000..0f2970281
--- /dev/null
+++ b/packages/chrome-plugin/tests/pages/nested_elements.html
@@ -0,0 +1,115 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8" />
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
+ <title>Textarea Ancestor Positioning Hierarchy Test</title>
+ <style>
+ html,
+ body {
+ height: 100%;
+ margin: 0;
+ }
+
+ #hgl-container {
+ position: fixed;
+ top: 50%;
+ left: 50%;
+ transform: translate(-50%, -50%);
+ width: 50vw;
+ height: 50vh;
+ z-index: 2147483647;
+ background: rgb(255, 255, 255);
+ color: rgb(0, 0, 0);
+ border: 1px solid #ccc;
+ padding: 0;
+ box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
+ box-sizing: border-box;
+ display: flex;
+ flex-direction: column;
+ font-family: system-ui, -apple-system, sans-serif;
+ }
+
+ #hgl-titlebar {
+ background-color: #f0f0f0;
+ color: rgb(0, 0, 0);
+ padding: 8px 12px;
+ display: flex;
+ justify-content: space-between;
+ align-items: center;
+ cursor: move;
+ user-select: none;
+ border-bottom: 1px solid #ccc;
+ font-size: 13px;
+ font-weight: 500;
+ min-height: 32px;
+ box-sizing: border-box;
+ }
+
+ #hgl-close {
+ background: none;
+ border: none;
+ cursor: pointer;
+ font-size: 20px;
+ padding: 0 4px;
+ color: rgb(0, 0, 0);
+ min-width: 32px;
+ min-height: 32px;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ flex: 0 0 auto;
+ }
+
+ #hgl-wrapper {
+ flex: 1;
+ overflow: hidden;
+ padding: 4px;
+ box-sizing: border-box;
+ position: relative;
+ }
+
+ #hgl-textarea {
+ width: 100%;
+ height: 100%;
+ box-sizing: border-box;
+ border: none;
+ font-family: monospace;
+ font-size: 13px;
+ resize: none;
+ padding: 8px;
+ color: rgb(0, 0, 0);
+ background-color: rgb(255, 255, 255);
+ }
+
+ #hgl-resize {
+ position: absolute;
+ bottom: 0;
+ right: 0;
+ width: 16px;
+ height: 16px;
+ cursor: nwse-resize;
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ font-size: 14px;
+ color: #999;
+ pointer-events: none;
+ user-select: none;
+ }
+ </style>
+ </head>
+ <body>
+ <div id="hgl-container">
+ <div id="hgl-titlebar">
+ <span>Layout test</span>
+ <button id="hgl-close" type="button" aria-label="Close">✕</button>
+ </div>
+
+ <div id="hgl-wrapper">
+ <textarea id="hgl-textarea" spellcheck="false">Textarea content…</textarea>
+ <div id="hgl-resize">⋱</div>
+ </div>
+ </div>
+ </body>
+</html>
diff --git a/packages/lint-framework/src/lint/Highlights.ts b/packages/lint-framework/src/lint/Highlights.ts
index e7373d8b5..11d908bda 100644
--- a/packages/lint-framework/src/lint/Highlights.ts
+++ b/packages/lint-framework/src/lint/Highlights.ts
@@ -117,20 +117,30 @@ export default class Highlights {
if (cpa != null) {
const hostStyle = host.style;
- hostStyle.contain = 'layout';
hostStyle.position = 'absolute';
hostStyle.top = '0px';
hostStyle.left = '0px';
- hostStyle.transform = `translate(${-cpa.x}px, ${-cpa.y}px)`;
hostStyle.inset = '0';
hostStyle.pointerEvents = 'none';
hostStyle.width = '0px';
hostStyle.height = '0px';
+ hostStyle.contain = 'none';
+ hostStyle.transform = 'none';
} else if (host.hasAttribute('style')) {
host.removeAttribute('style');
}
- renderBox.render(this.renderTree(boxes));
+ renderBox.render(
+ this.renderTree(
+ boxes,
+ cpa
+ ? {
+ x: cpa.x,
+ y: cpa.y,
+ }
+ : null,
+ ),
+ );
updated.add(source);
}
@@ -153,8 +163,10 @@ export default class Highlights {
}
}
- private renderTree(boxes: LintBox[]): VNode {
+ private renderTree(boxes: LintBox[], offset: { x: number; y: number } | null): VNode {
const elements = [];
+ const offsetX = offset?.x ?? 0;
+ const offsetY = offset?.y ?? 0;
for (const box of boxes) {
const boxEl = h(
@@ -164,7 +176,7 @@ export default class Highlights {
position: 'fixed',
left: '0px',
top: '0px',
- transform: `translate(${box.x}px, ${box.y}px)`,
+ transform: `translate(${box.x - offsetX}px, ${box.y - offsetY}px)`,
width: `${box.width}px`,
height: `${box.height}px`,
pointerEvents: 'none',