Skip to content

[lexical-selection][lexical-utils] Bug Fix: Preserve selection coverage across mixed typography - #9196

Merged
etrepum merged 8 commits into
meta:mainfrom
minwookshin:codex/fix-mixed-typography-selection
Sep 26, 2026
Merged

etrepum merged 8 commits into
meta:mainfrom
minwookshin:codex/fix-mixed-typography-selection

Conversation

@minwookshin

@minwookshin minwookshin commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Persistent selections can omit smaller text preceding a larger run on the same line, including while the Playground's link input has focus. A larger rectangle sorts before the smaller one, and the overlap check discards the latter even when it covers unique selected text.

Discard only fully contained rectangles in createRectsFromDOMRange. Preserve the existing sort, full-width block exclusion and downstream Safari deduplication behavior. Add native Range regressions for mixed typography, partial overlap, wrapped LTR/RTL text and duplicate rectangles; update the utility characterization for the corrected containment contract.

Closes #9184
Closes #9187

Test plan

Before

The new mixed-size regression fails against baseline implementation 83e8b49: Selection should cover "Small before " at (0.1, 0.1). The actual Playground leaves the first selected text run unpainted after focus leaves the editor.

Before: the smaller first text run loses its selection paint

After

After: all selected text retains its paint

These screenshots were captured on 83e8b49. The three candidate files remained byte-identical after synchronization to the tested PR parent, bc4dcc814f66e35b00f8e3b8b235e559f07454f2.

  • 341 focused unit tests pass on the synchronized candidate.
  • 24 native browser cases pass across Chromium, Firefox and WebKit, including the existing underpaint characterization:
VITEST_BROWSER=chromium,firefox,webkit pnpm exec vitest run --project browser packages/lexical-selection/src/__tests__/browser --maxWorkers=1

The subsequent upstream commit 2fd17f0 touches other files, and this patch applies cleanly to it. Local results above use the recorded parent. The full remote CI matrix, native mobile and screen-reader behavior remain unverified. This does not change the separate downstream keep-smaller policy for overlapping decorators.

Source and visual evidence. AI-assisted implementation and validation with OpenAI Codex.

@meta-cla

meta-cla Bot commented Sep 23, 2026

Copy link
Copy Markdown

Hi @minwookshin!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@vercel

vercel Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 26, 2026 4:16pm UTC
lexical-playground Ready Ready Preview Sep 26, 2026 4:16pm UTC

Request Review

@mayrang mayrang left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like the right direction — isContainedRect seems like the correct predicate here, and the new tests do a good job of capturing the bug.

The CLA check is failing — PRs can't be merged without a signed CLA. Please sign at https://code.facebook.com/cla.

One thing on the new browser test — see the inline comment.

root.style.cssText =
'position:absolute;left:20px;top:20px;width:600px;padding:0;font:16px/1.5 Arial';
root.innerHTML = html;
document.body.append(root);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing dedupeSelectionRectsUnderpaint.test.ts adds void root.offsetHeight after setting innerHTML to flush layout before measuring. Probably better to follow the same pattern here.

```suggestion
root.innerHTML = html;
void root.offsetHeight;
```

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 25, 2026
@meta-cla

meta-cla Bot commented Sep 25, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex found two regressions here:

Reviewed 21304dcd15cf1dc75edc206f94a40c7012f9ad23. I’d request changes for two confirmed regressions:

  1. [P2] Retaining the smaller rectangle moves Yjs’s remote caret to the wrong endpoint. Select Small before at 16px followed by LARGE at 40px. The larger run sorts first; the newly retained smaller run sorts last. Yjs’s default overlay path attaches the caret to the last rectangle (SyncCursors.ts:624–626), so the caret and name move to the end of Small before . In Chromium, the caret’s x-coordinate changes from approximately 240.75 on the parent to 106.11 on this PR, while the selection endpoint remains 240.73. Position the caret from the range endpoint independently of the highlight rectangles, and add a collaboration regression test.

  2. [P2] Strict containment upstream introduces additional underpainting downstream. In the existing dedupeSelectionRectsUnderpaint.test.ts fixture, change vertical-align:0.5px to vertical-align:-1px. The parent preserves the wide text highlight. This PR retains an additional narrow rectangle because its bottom extends 1px below the wide rectangle. positionNodeOnRange then calls dedupeSelectionRects, whose 1px containment tolerance discards the wide rectangle and keeps the narrow one. My Chromium reproduction reduces approximately 154px of text highlight to 9.6px. Coordinate the two filters so newly retained partial overlaps cannot erase previously covered text, and test this downward-shifted variant.

Both originate in the changed containment predicate at packages/lexical-selection/src/utils.ts:164–169.

@minwookshin

Copy link
Copy Markdown
Contributor Author

Updated selection rect handling to preserve coverage across mixed typography and positioned collaborator carets at the actual focus point. Unit, browser, type, and lint checks pass locally.

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex noted one additional edge case:

[P2] Handle zero-size rectangles when positioning element-point carets — packages/lexical-yjs/src/SyncCursors.ts:498–508

A collapsed DOM range at an element boundary can return an all-zero rectangle. positionCaretAtFocus accepts that rectangle, places the caret at the origin, and returns true, preventing fallback positioning.

Confirmed in Chromium with collaboration v2:

  • Paragraph containing "ab cd".
  • Anchor: (text, 0, text).
  • Focus: (paragraph, 1, element).
  • Previous cursor implementation: caret at (124, 138), correctly following the text.
  • This revision: caret at (0, 0) with the fallback 16px height.

Resolve element points to a measurable text/DOM boundary, or fall back to valid selection geometry when the collapsed range has no usable bounds. Add a browser regression covering v2 element-point selections; the new tests only exercise text-point focus positions.

@minwookshin

minwookshin commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Handled zero-size DOM ranges at v2 element-point carets and flushed layout after mounting the selection fixture. Browser regressions (8), unit tests, typecheck, formatting, and lint pass locally.

@etrepum
etrepum added this pull request to the merge queue Sep 26, 2026
Merged via the queue into meta:main with commit 54e6bd3 Sep 26, 2026
42 checks passed
@etrepum etrepum mentioned this pull request Sep 28, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 3a7210b8 Deployed Sep 26, 2026 by vercel[bot]
Preview – lexical-playground — 3a7210b8 Deployed Sep 26, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Selection highlight is partially lost when text has multiple font sizes or families

3 participants