Skip to content

Commit bc542f2

Browse files
matt2eclaude
andauthored
fix(composer): prevent WebKit overpainting mention selections (#490)
Signed-off-by: Matt Toohey <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
1 parent 5d5094e commit bc542f2

3 files changed

Lines changed: 507 additions & 3 deletions

File tree

‎src/features/messages/EditableInput.module.css‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ html[data-keyboard-navigation] .input:focus-visible {
4343
background-color: transparent;
4444
color: inherit;
4545
}
46+
/* A token paints its own selected state above. Its native selection colour is
47+
transparent because WebKit fills a line's selection gap with the colour of the
48+
first selected box's parent, and a range that starts exactly where prose meets
49+
a token has no selected prose before it: WebKit then treats the line as the
50+
end of a longer selection and fills from the block edge to the token, which
51+
highlights prose that is not selected. */
52+
.input .token::selection,
53+
.input .token *::selection {
54+
background-color: transparent;
55+
}
4656
.input .token :is(div, p) {
4757
display: inline;
4858
margin: 0;

‎src/features/messages/EditableInput.test.tsx‎

Lines changed: 242 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import { mentionDraft, type MentionDraft } from "./mention-draft";
1919
composerDOMFixture();
2020
afterEach(cleanup);
2121

22-
function mount(text = "") {
22+
function mount(initial: string | MentionDraft = "") {
2323
const ref = createRef<ComposerInputElement>();
24-
let draft: MentionDraft = mentionDraft(text);
24+
let draft: MentionDraft = mentionDraft(initial);
25+
const text = draft.text;
2526
function Editor() {
2627
const [value, setValue] = useState(draft);
2728
return (
@@ -32,7 +33,14 @@ function mount(text = "") {
3233
disabled={false}
3334
placeholder="Draft"
3435
maxLength={16000}
35-
decorationsFor={() => []}
36+
// Explicit recipients render as mention tokens, as in RichComposerInput.
37+
decorationsFor={(current) =>
38+
current.recipients.map(({ start, end, name }) => ({
39+
start,
40+
end,
41+
content: <span data-mention>@{name}</span>,
42+
}))
43+
}
3644
onFormatsChange={() => {}}
3745
onDraftChange={(next) => {
3846
draft = next;
@@ -256,6 +264,237 @@ it.each([true, false])(
256264
},
257265
);
258266

267+
const imp = { pubkey: "a".repeat(64), name: "Imp" };
268+
const jitter = { pubkey: "b".repeat(64), name: "Jitter" };
269+
// Display names with a space are one token too; the label never splits.
270+
const maryJane = { pubkey: "c".repeat(64), name: "Mary Jane" };
271+
const mattToohey = { pubkey: "d".repeat(64), name: "Matt Toohey" };
272+
/** Every `@Name` of these people in the text is an explicit, resolved recipient. */
273+
function mentioned(text: string): MentionDraft {
274+
return {
275+
text,
276+
recipients: [imp, jitter, maryJane, mattToohey].flatMap((recipient) => {
277+
const start = text.indexOf(`@${recipient.name}`);
278+
return start < 0
279+
? []
280+
: [{ ...recipient, start, end: start + recipient.name.length + 1 }];
281+
}),
282+
};
283+
}
284+
const selection = (input: ComposerInputElement) =>
285+
[input.selectionStart, input.selectionEnd, input.selectionDirection] as const;
286+
/** Arrow keys are deliberately low-level: user-event cannot extend a selection. */
287+
const shiftArrow = (
288+
input: ComposerInputElement,
289+
key: "ArrowLeft" | "ArrowRight",
290+
) =>
291+
act(() => {
292+
fireEvent.keyDown(input, { key: "Shift", keyCode: 16, shiftKey: true });
293+
fireEvent.keyDown(input, {
294+
key,
295+
code: key,
296+
keyCode: key === "ArrowLeft" ? 37 : 39,
297+
shiftKey: true,
298+
});
299+
});
300+
301+
it.each([jitter.name, maryJane.name])(
302+
"extends a backward selection over @%s before it and keeps the anchor",
303+
(name) => {
304+
const text = `@Imp say hello to @${name} `;
305+
const mention = text.lastIndexOf("@");
306+
const h = mount(mentioned(text));
307+
expect(h.input.querySelectorAll("[data-mention]")).toHaveLength(2);
308+
act(() => h.input.setSelectionRange(text.length, text.length));
309+
// The browser itself answers the first Shift+Left by selecting the trailing
310+
// space. Model that native result, then let the editor handle the second.
311+
act(() => {
312+
const space = h.input.querySelector("p")?.lastChild;
313+
if (!(space instanceof Text)) throw new Error("Missing trailing text");
314+
document.getSelection()?.setBaseAndExtent(space, 1, space, 0);
315+
});
316+
shiftArrow(h.input, "ArrowLeft");
317+
expect(selection(h.input)).toEqual([mention, text.length, "backward"]);
318+
expect(text.slice(mention)).toBe(`@${name} `);
319+
expect(h.input.selectionEnd - h.input.selectionStart).toBeLessThanOrEqual(
320+
h.input.value.length,
321+
);
322+
shiftArrow(h.input, "ArrowRight");
323+
expect(selection(h.input)).toEqual([
324+
text.length - 1,
325+
text.length,
326+
"backward",
327+
]);
328+
},
329+
);
330+
331+
it.each([
332+
[
333+
"after a mention with no trailing space",
334+
"@Imp say hello to @Jitter",
335+
25,
336+
"ArrowLeft",
337+
[18, 25, "backward"],
338+
],
339+
[
340+
"before a mention",
341+
"@Imp say hello to @Jitter ",
342+
18,
343+
"ArrowRight",
344+
[18, 25, "forward"],
345+
],
346+
[
347+
"after the mention at the start",
348+
"@Imp say hello to @Jitter ",
349+
4,
350+
"ArrowLeft",
351+
[0, 4, "backward"],
352+
],
353+
[
354+
"the document start",
355+
"@Imp say hello to @Jitter ",
356+
0,
357+
"ArrowRight",
358+
[0, 4, "forward"],
359+
],
360+
["the document end", "hello @Jitter", 13, "ArrowLeft", [6, 13, "backward"]],
361+
[
362+
"after a two-word mention with no trailing space",
363+
"@Imp say hello to @Mary Jane",
364+
28,
365+
"ArrowLeft",
366+
[18, 28, "backward"],
367+
],
368+
[
369+
"before a two-word mention",
370+
"@Imp say hello to @Mary Jane ",
371+
18,
372+
"ArrowRight",
373+
[18, 28, "forward"],
374+
],
375+
[
376+
"after the two-word mention at the start",
377+
"@Mary Jane says hello",
378+
10,
379+
"ArrowLeft",
380+
[0, 10, "backward"],
381+
],
382+
[
383+
"the document start before a two-word mention",
384+
"@Mary Jane says hello",
385+
0,
386+
"ArrowRight",
387+
[0, 10, "forward"],
388+
],
389+
[
390+
"the document end after a two-word mention",
391+
"hello @Mary Jane",
392+
16,
393+
"ArrowLeft",
394+
[6, 16, "backward"],
395+
],
396+
] as const)(
397+
"Shift+Arrow from %s selects the whole mention",
398+
(_, text, caret, key, expected) => {
399+
const h = mount(mentioned(text));
400+
act(() => h.input.setSelectionRange(caret, caret));
401+
shiftArrow(h.input, key);
402+
expect(selection(h.input)).toEqual(expected);
403+
},
404+
);
405+
406+
it.each([
407+
[
408+
"extends a forward selection from prose over a mention",
409+
"@Imp say hello to @Jitter ",
410+
16,
411+
18,
412+
"forward",
413+
"ArrowRight",
414+
[16, 25, "forward"],
415+
],
416+
[
417+
"extends a backward selection from prose over a mention",
418+
"@Imp say hello to @Jitter ",
419+
4,
420+
8,
421+
"backward",
422+
"ArrowLeft",
423+
[0, 8, "backward"],
424+
],
425+
[
426+
"shrinks a forward selection off a mention",
427+
"@Imp say hello to @Jitter ",
428+
18,
429+
25,
430+
"forward",
431+
"ArrowLeft",
432+
[18, 18, "forward"],
433+
],
434+
[
435+
"shrinks a backward selection off a mention",
436+
"@Imp say hello to @Jitter ",
437+
18,
438+
26,
439+
"backward",
440+
"ArrowRight",
441+
[25, 26, "backward"],
442+
],
443+
[
444+
"extends a forward selection from prose over a two-word mention",
445+
"@Imp say hello to @Mary Jane ",
446+
16,
447+
18,
448+
"forward",
449+
"ArrowRight",
450+
[16, 28, "forward"],
451+
],
452+
[
453+
"shrinks a forward selection off a two-word mention",
454+
"@Imp say hello to @Mary Jane ",
455+
18,
456+
28,
457+
"forward",
458+
"ArrowLeft",
459+
[18, 18, "forward"],
460+
],
461+
[
462+
"shrinks a backward selection off a two-word mention",
463+
"@Imp say hello to @Mary Jane ",
464+
18,
465+
29,
466+
"backward",
467+
"ArrowRight",
468+
[28, 29, "backward"],
469+
],
470+
// Two two-word mentions one space apart: the browser selects that space,
471+
// then the editor extends over the first mention in the same direction.
472+
[
473+
"extends a backward selection over the first of two two-word mentions",
474+
"@Mary Jane @Matt Toohey ",
475+
10,
476+
24,
477+
"backward",
478+
"ArrowLeft",
479+
[0, 24, "backward"],
480+
],
481+
[
482+
"extends a forward selection over the second of two two-word mentions",
483+
"@Mary Jane @Matt Toohey ",
484+
0,
485+
11,
486+
"forward",
487+
"ArrowRight",
488+
[0, 23, "forward"],
489+
],
490+
] as const)("%s", (_, text, start, end, direction, key, expected) => {
491+
const h = mount(mentioned(text));
492+
act(() => h.input.setSelectionRange(start, end, direction));
493+
expect(selection(h.input)).toEqual([start, end, direction]);
494+
shiftArrow(h.input, key);
495+
expect(selection(h.input)).toEqual(expected);
496+
});
497+
259498
it("retains the native caret after replacing text with an unchanged suffix", async () => {
260499
const h = mount(":unknown:");
261500
act(() => {

0 commit comments

Comments
 (0)