Skip to content

Commit 879a122

Browse files
committed
fix(web): reveal HTML citations before reporting coordinates
1 parent 936799a commit 879a122

2 files changed

Lines changed: 84 additions & 7 deletions

File tree

‎apps/web/src/lib/htmlRenderSelection.test.ts‎

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ describe("HTML render selection bridge", () => {
105105

106106
it("reveals a quote through nested scroll containers before returning its target", () => {
107107
const p = page(
108-
'<div id="outer" style="overflow-x: auto; overflow-y: auto"><div id="inner" style="overflow-x: auto; overflow-y: auto"><p>quoted text</p></div></div>',
108+
'<div id="outer" style="overflow-x: auto; overflow-y: auto; scroll-behavior: smooth"><div id="inner" style="overflow-x: auto; overflow-y: auto; scroll-behavior: smooth"><p>quoted text</p></div></div>',
109109
);
110110
const outer = p.view.document.querySelector<HTMLElement>("#outer")!;
111111
const inner = p.view.document.querySelector<HTMLElement>("#inner")!;
@@ -121,6 +121,20 @@ describe("HTML render selection bridge", () => {
121121
scrollHeight: { value: 1000 },
122122
scrollWidth: { value: 1000 },
123123
});
124+
for (const element of [inner, outer]) {
125+
let top = 0;
126+
let left = 0;
127+
Object.defineProperties(element, {
128+
// Auto scrolling follows the page's smooth behavior and does not finish synchronously.
129+
scrollTop: { get: () => top, set: () => {} },
130+
scrollLeft: { get: () => left, set: () => {} },
131+
});
132+
element.scrollBy = (options?: ScrollToOptions | number) => {
133+
if (typeof options !== "object" || options.behavior !== "instant") return;
134+
top += options.top ?? 0;
135+
left += options.left ?? 0;
136+
};
137+
}
124138
const bounds = (left: number, top: number, width: number, height: number) =>
125139
new p.view.DOMRect(left, top, width, height);
126140
inner.getBoundingClientRect = () =>
@@ -147,8 +161,16 @@ describe("HTML render selection bridge", () => {
147161
expect(target).toEqual({ left: 80, top: 145, width: 80, height: 15 });
148162
expect(scroll).not.toHaveBeenCalled();
149163

150-
inner.scrollTop = inner.scrollLeft = 500;
151-
outer.scrollTop = outer.scrollLeft = 300;
164+
inner.scrollBy({
165+
top: 500 - inner.scrollTop,
166+
left: 500 - inner.scrollLeft,
167+
behavior: "instant",
168+
});
169+
outer.scrollBy({
170+
top: 300 - outer.scrollTop,
171+
left: 300 - outer.scrollLeft,
172+
behavior: "instant",
173+
});
152174
p.command("target", { text: "quoted text", start: 0, end: 11, prefix: "", suffix: "" });
153175
expect(htmlSelectionParams(p.messages().at(-1))?.target).toEqual({
154176
left: 10,
@@ -158,6 +180,49 @@ describe("HTML render selection bridge", () => {
158180
});
159181
});
160182

183+
it.each([
184+
{ left: 700, top: 20, expectedLeft: 160, expectedTop: 20 },
185+
{ left: -200, top: 20, expectedLeft: 80, expectedTop: 20 },
186+
{ left: 10, top: 700, expectedLeft: 10, expectedTop: 80 },
187+
{ left: 10, top: -200, expectedLeft: 10, expectedTop: 80 },
188+
{ left: 700, top: 700, expectedLeft: 160, expectedTop: 80 },
189+
])(
190+
"reveals a quote at ($left, $top) before replying on a smooth-scrolling page",
191+
({ left, top, expectedLeft, expectedTop }) => {
192+
const p = page("<style>html { scroll-behavior: smooth }</style><p>quoted text</p>");
193+
Object.defineProperties(p.view, {
194+
innerWidth: { value: 320 },
195+
innerHeight: { value: 240 },
196+
});
197+
let dx = 0;
198+
let dy = 0;
199+
const bounds = () => new p.view.DOMRect(left - dx, top - dy, 80, 15);
200+
Object.assign(p.view.Range.prototype, {
201+
getBoundingClientRect: bounds,
202+
getClientRects: () => [bounds()],
203+
});
204+
const scroll = vi
205+
.spyOn(p.view, "scrollBy")
206+
.mockImplementation((options?: ScrollToOptions | number) => {
207+
// The reply must use coordinates after scrolling, without waiting for an animation.
208+
if (typeof options !== "object" || options.behavior !== "instant") return;
209+
dx += options.left ?? 0;
210+
dy += options.top ?? 0;
211+
});
212+
const quote = { text: "quoted text", start: 0, end: 11, prefix: "", suffix: "" };
213+
p.command("mark", quote);
214+
expect(scroll).not.toHaveBeenCalled();
215+
p.command("target", quote);
216+
expect(htmlSelectionParams(p.messages().at(-1))?.target).toEqual({
217+
left: expectedLeft,
218+
top: expectedTop,
219+
width: 80,
220+
height: 15,
221+
});
222+
expect(scroll).toHaveBeenCalledTimes(1);
223+
},
224+
);
225+
161226
it("keeps native Tab navigation for oversized selections", async () => {
162227
const p = page(`<p>${"x".repeat(8001)}</p><button>Next</button>`);
163228
const paragraph = p.view.document.querySelector("p")!;

‎packages/shared/src/htmlRenderSelectionScript.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,26 +250,38 @@ export const HTML_RENDER_SELECTION_SCRIPT = String.raw`// Loaded as raw source i
250250
const bounds = rect(marked);
251251
const top = box.top + element.clientTop;
252252
const left = box.left + element.clientLeft;
253+
let dy = 0;
254+
let dx = 0;
253255
if (
254256
/^(auto|scroll|hidden)$/.test(style.overflowY) &&
255257
element.scrollHeight > element.clientHeight
256258
)
257-
element.scrollTop +=
259+
dy =
258260
bounds.top < top
259261
? bounds.top - top
260262
: Math.max(0, bounds.top + bounds.height - top - element.clientHeight);
261263
if (
262264
/^(auto|scroll|hidden)$/.test(style.overflowX) &&
263265
element.scrollWidth > element.clientWidth
264266
)
265-
element.scrollLeft +=
267+
dx =
266268
bounds.left < left
267269
? bounds.left - left
268270
: Math.max(0, bounds.left + bounds.width - left - element.clientWidth);
271+
if (dx || dy) element.scrollBy({ left: dx, top: dy, behavior: "instant" });
269272
}
270273
const bounds = marked.getBoundingClientRect();
271-
if (bounds.top < 0 || bounds.bottom > window.innerHeight)
272-
window.scrollBy(0, bounds.top - Math.min(80, window.innerHeight / 3));
274+
const marginX = Math.min(80, window.innerWidth / 3);
275+
const dx = bounds.left < 0
276+
? bounds.left - marginX
277+
: bounds.right > window.innerWidth
278+
? bounds.right - window.innerWidth + marginX
279+
: 0;
280+
const dy = bounds.top < 0 || bounds.bottom > window.innerHeight
281+
? bounds.top - Math.min(80, window.innerHeight / 3)
282+
: 0;
283+
// Page CSS can request smooth scrolling; return geometry only after positioning.
284+
if (dx || dy) window.scrollBy({ left: dx, top: dy, behavior: "instant" });
273285
}
274286
if (window.CSS?.highlights && window.Highlight)
275287
CSS.highlights.set("t3-html-citation", new Highlight(marked));

0 commit comments

Comments
 (0)