Skip to content

[lexical-text] Bug Fix: replace a text entity match that ends a TextNode - #8982

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/5466-text-entity-match-at-node-end
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/5466-text-entity-match-at-node-end

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Description

registerLexicalTextEntity silently skips a match when the match ends at the end of its TextNode and does not start at offset 0. In #5466 the reporter's ${Tenant.Zip} and ${Facility.Zip} never became VariableNodes, and they found the tell themselves: excluding the trailing } from the selection made the transform work, and the problem only ever hit the last match in a node when that match also ended the node.

The transform walks a node replacing matches one at a time. Once a match is consumed, the loop keeps a text cursor holding only the text still to be processed. When the remaining text runs out, the node's match might be continued by the next sibling — a ${Tenant. in one node and Zip} in the next — so the code re-runs getMatch over the combined text and requires the result to line up before replacing.

Two things were wrong with that check:

  • it re-ran getMatch over currentNode.getTextContent() + nextSibling.getTextContent(), the whole node text, rather than the text still to be processed, and
  • it then required nextMatch.start === 0.

Those only agree when the match starts at offset 0 of the node. For any later match, nextMatch.start is the offset within the full node text, never 0, so the function returned early and left the match untransformed. The combined text is now built from the remaining text, and the check compares against match.start, which is the same coordinate space.

Closes #5466

Test plan

packages/lexical-text/src/__tests__/unit/Issue5466Repro.test.ts registers a ${...} entity, matching the reporter's plugin, and covers the three broken shapes plus two controls that must keep working: a match that does not end the node, and a match that only completes inside the sibling (which must still be left alone).

Before

$ npx vitest run packages/lexical-text
 × replaces a match that ends the node and does not start at offset 0
 × replaces the last of several matches in the same node
 × replaces every match when the first one starts at offset 0
 Test Files  1 failed (1)
      Tests  3 failed | 2 passed (5)

After

$ npx vitest run packages/lexical-text
 Test Files  1 passed (1)
      Tests  5 passed (5)

$ npx vitest run packages/lexical-text packages/lexical-react packages/lexical/src/__tests__
 Test Files  88 passed (88)
      Tests  1120 passed | 1 skipped (1121)

Unit tests only — this is a text-transform path with no e2e coverage of its own.

…ode with a text sibling

When a match ended exactly where the TextNode ended and the node still had a
TextNode sibling, registerLexicalTextEntity re-ran getMatch over the combined
text and bailed out unless that match started at offset 0 of the whole node.
Matches that had already been replaced or skipped were part of that combined
text, so any match that did not start the node - typically the last match of
several in the same node - was never replaced.

Compare against the text that is still to be processed and against the current
match's own start offset instead of 0.
@vercel

vercel Bot commented Aug 8, 2026

Copy link
Copy Markdown

@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@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 Aug 8, 2026
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9060 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

@LeSingh1 LeSingh1 closed this Aug 10, 2026
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: Inconsistent behaviour from useLexicalTextEntity when there are several matches within the same node

1 participant