Skip to content

[lexical-selection] Bug Fix: preserve adjacent mixed-size selection fragments - #9187

Closed
yftx293 wants to merge 1 commit into
meta:mainfrom
yftx293:fix/selection-mixed-font-rects
Closed

yftx293 wants to merge 1 commit into
meta:mainfrom
yftx293:fix/selection-mixed-font-rects

Conversation

@yftx293

@yftx293 yftx293 commented Sep 22, 2026

Copy link
Copy Markdown

Description

createRectsFromDOMRange may sort differently-sized inline fragments out of their text order. Its overlap filter previously checked only the preceding rect's right edge, so it could remove an adjacent fragment that did not overlap horizontally.

Require horizontal overlap in both directions before filtering a rect.

Closes #9184

Test plan

Before

Edge DOM Range reproduction:
{"sourceFragments":2,"before":1}

After

Edge DOM Range reproduction:
{"sourceFragments":2,"after":2}

pnpm vitest --project unit --run ... -> 66 passed
pnpm exec prettier --check ... -> passed
pnpm exec eslint ... -> passed

The repository Chromium browser test could not run locally because the Playwright Chromium executable was unavailable; the new browser regression test is included for CI.

…ragments

## Description
`createRectsFromDOMRange` may sort differently-sized inline fragments out of their text order. Its overlap filter previously checked only the preceding rect's right edge, so it could remove an adjacent fragment that did not overlap horizontally.

Require horizontal overlap in both directions before filtering a rect.

Closes meta#9184

## Test plan

### Before
Edge DOM Range reproduction:
{"sourceFragments":2,"before":1}

### After
Edge DOM Range reproduction:
{"sourceFragments":2,"after":2}

pnpm vitest --project unit --run ... -> 66 passed
pnpm exec prettier --check ... -> passed
pnpm exec eslint ... -> passed

The repository Chromium browser test could not run locally because the Playwright Chromium executable was unavailable; the new browser regression test is included for CI.
@meta-cla

meta-cla Bot commented Sep 22, 2026

Copy link
Copy Markdown

Hi @yftx293!

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 22, 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 22, 2026 3:11am UTC
lexical-playground Ready Ready Preview Sep 22, 2026 3:11am UTC

Request Review

@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 22, 2026
@meta-cla

meta-cla Bot commented Sep 22, 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!

prevRect.top <= selectionRect.top &&
prevRect.top + prevRect.height > selectionRect.top &&
prevRect.left + prevRect.width > selectionRect.left;
prevRect.left < selectionRect.right &&

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If I understand correctly, this checks if the block is actually to the left. However, I think we should fix the root cause instead - the array is expected to already be sorted by this point

prevRect.top + prevRect.height > selectionRect.top &&
prevRect.left + prevRect.width > selectionRect.left;
prevRect.left < selectionRect.right &&
prevRect.right > selectionRect.left;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'm not sure where the issue originated - whether it's an error in the math when adding prev.rect + prev.width or an issue with how clientRects is calculated. Because of that, I think we should add a 1px threshold here.

That said, I'm not a maintainer, so they might see it differently

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.

I don't think a 1px threshold is sufficient to fix this issue, there can be arbitrary partial overlaps between these rectangles.

This branch was successfully deployed

2 active deployments
Preview – lexical — cbd952ed Deployed Sep 22, 2026 by vercel[bot]
Preview – lexical-playground — cbd952ed Deployed Sep 22, 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.

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