Skip to content

[lexical][lexical-text][lexical-y] Feature: Record text entity grows and remakes as moves and retypes for @lexical/y - #9483

Merged
zurfyx merged 1 commit into
mainfrom
zurfyx/text-entity-edit-intent
Oct 29, 2026
Merged

zurfyx merged 1 commit into
mainfrom
zurfyx/text-entity-edit-intent

Conversation

@zurfyx

@zurfyx zurfyx commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Description

Builds on #9400 (@lexical/y), now in main.

Current behavior. While @lexical/y is attached, core records every text edit of an update in EditorState#_textEdits, and the binding replays that record to keep each character's identity. registerLexicalTextEntity (hashtags, keywords and other entity plugins) makes two kinds of edits whose record does not say which characters persisted:

  • An entity grown over the start of the next text node (the space between #foo and bar deleted) sets both texts whole: setTextContent on the entity, then setTextContent or remove() on the next node. The record has two set entries (or a set and a detach).
  • An entity made plain text again, or plain text made an entity, copies the text into a new node and calls replace(). The record has the old node's detach and the new node's attach.

The binding then aligns the old and the new text of the line to guess which characters stayed. Next to equal text made or removed in the same update (a paste, a node that app code removes), the guess can take other characters, and a collaborator's concurrent edit of them is lost or lands on a neighbour. #9400 lists this in its Known open issues ("Text changed without a record of the edit") and names this change as the planned follow-up.

New behavior. The helper records what it already knows, around the calls it made before. The calls themselves do not change.

  • The grow is recorded as one move entry: the node the characters left, the entity, how many, and both texts before. Core builds it from the two set entries those calls record, and only when they are exactly that move (the entity's text is its old text plus the first characters of the next node, which keeps the rest or is removed); otherwise they stay as recorded, for example under a subclass whose setTextContent writes other text.
  • A remake is recorded as a retype entry right after the replace(): the old node, the new node and their text. It is recorded only when the last two entries are that replace's detach and attach and both nodes hold the same text (a createNode that changes the text records nothing).
  • @lexical/y replays both. A move keeps the moved characters' identities in the entity. A retype gives the new node what the old node held when it was removed, when all of the new node's text was made in this update and equals it. Where an entry does not match the texts it covers, the binding guesses as before.
  • One exception for the move. Text that left a node earlier in the update (a text set, typed text deleted again) is aligned with what arrived in that node when the node is set whole again. Where the move splits such text between the two nodes, or joins it with the entity's own, the binding marks it (the mark follows the text through splits, merges and remakes). Where a later text set in the update reaches marked text, or a node removed by the end of the update still holds some, it replays the update's record again with that move as the two text sets it stands for, as before.

Internal only. The two helpers are exported from lexical as INTERNAL_$recordTextMove and INTERNAL_$recordTextRetype, marked @internal like INTERNAL_$normalizeNodeStructure; TextEditRecord gains the two kinds, also in the Flow type of EditorState#_textEdits. No public API changes.

Without collaboration (_recordTextEdits unset) the helper makes the same calls and records nothing: one flag check per grow and per remake. With collaboration the editor also ends the same; only the record differs.

Not covered: app code that moves text between nodes with setTextContent and remove(), or remakes nodes itself, is still placed by the line's diff (#9400's Known open issues).

Changeset: .changeset/lexical-y-text-entities.md, a [fix] for lexical, @lexical/text and @lexical/y (patch).

Test plan

New tests:

  • @lexical/text registerLexicalTextEntityRecord.test.ts: the record of a grow over part of the next node, over all of it, a shrink (a split, unchanged), an entity made plain text, plain text made an entity, and a createNode that changes the text; each with and without recording, from the same node keys, with the same JSON, nodes and selection both ways.
  • @lexical/text registerLexicalTextEntityEquivalence.test.ts: a copy of the helper as it was before this change against the new one, in two editors with a DOM root, over random caret edits and app-code sets and removals next to two kinds of entities. After every edit: the same JSON, selection, mutation-listener events and node keys. With recording too, where the new record, with each move expanded back into its two sets and the retypes dropped, equals the old one. Also the cases where changing the calls would have differed (a stale caret inside the entity, a setter that refuses empty text, a NodeSelection) and a shortened entity with the caret past its end.
  • lexical LexicalTextEditRecord.test.ts: both helpers called directly, including what they leave as recorded (other text, a node that is not the previous sibling, a split or a move inside, a retype that does not follow the replace) and an error thrown inside, which reaches onError with the update discarded.
  • @lexical/y regressions/TextIdentity.test.ts: R19A-5383 (app code removes a node next to two stale entities that the transforms remake) and R19A-4590 (a paste and an app-code grow next to two stale entities that the transforms remake), from [lexical][lexical-devtools][lexical-extension][lexical-list][lexical-website][lexical-y][lexical-yjs] Feature: @lexical/y, an experimental Yjs 14 collaboration binding #9400's randomized lanes, with controls; a move out of a node set and edited earlier in the update, then either node set again (directly and through the hashtag plugin), a merge, a segmented split, a remake (also of a link around it), a second move of all of the first one's destination, or the node it moved to set with the rest put before it; the hashtag grow above with a concurrent delete of a moved character.

Before

The new regression cases on main:

$ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/regressions/TextIdentity.test.ts -t "Text entity edits"
   ❯ Text entity edits: grows and remakes keep their characters (36)
     ❯ R19A-5383: app code removes a node next to entities the transforms remake (6)
       × R19A-5383 the peer deletes the "l" of the remade tag (ids 1,2)
       × R19A-5383 the peer deletes the "l" of the remade tag (ids 2,1)
       × R19A-5383 the peer deletes the "a" of the remade tag (ids 1,2)
       × R19A-5383 the peer deletes the "a" of the remade tag (ids 2,1)
       ✓ R19A-5383 the peer deletes control: the "a" of the other tag (ids 1,2)
       ✓ R19A-5383 the peer deletes control: the "a" of the other tag (ids 2,1)
     ❯ R19A-4590: a paste and an app-code grow next to entities the transforms remake (10)
       ✓ R19A-4590 the peer deletes control: the "l" of the first tag (ids 1,2)
       ✓ R19A-4590 the peer deletes control: the "l" of the first tag (ids 2,1)
       × R19A-4590 the peer deletes the "l" of the second tag (ids 1,2)
       × R19A-4590 the peer deletes the "l" of the second tag (ids 2,1)
       × R19A-4590 the peer deletes the "a" of the second tag (ids 1,2)
       × R19A-4590 the peer deletes the "a" of the second tag (ids 2,1)
       ✓ R19A-4590 the peer deletes the "l" app code moved (ids 1,2)
       ✓ R19A-4590 the peer deletes the "l" app code moved (ids 2,1)
       ✓ R19A-4590 the peer deletes the "a" app code moved (ids 1,2)
       ✓ R19A-4590 the peer deletes the "a" app code moved (ids 2,1)
     ❯ a move out of a node set and edited earlier in the update (4)
       × then the node it moved to set (ids 1,2): the peer deletes a character the author deleted
       ...
     × a move, a move of all of its destination, then that node set again (ids 2,1)
     ✓ a hashtag grown over text set and edited earlier in the update, then that text set again (ids 1,2)
     ✓ a hashtag grown over text set and edited earlier in the update, then that text set again (ids 2,1)
     ✓ a hashtag grown over the next word (2)

AssertionError: expected [ 'la  ab ', 'la  ab ', 'la  ab ' ] to deeply equal [ 'a  ab ', 'a  ab ', 'a  ab ' ]
AssertionError: expected [ 'lla lalaa a', 'lla lalaa a', …(1) ] to deeply equal [ 'lla alaa a', 'lla alaa a', …(1) ]
TypeError: (0 , INTERNAL_$recordTextMove) is not a function

      Tests  24 failed | 12 passed | 95 skipped (131)

The peer's delete of the remade tag's characters is lost (5383), or lands on the pasted text (4590). The 16 cases that call INTERNAL_$recordTextMove directly cannot run on main; the hashtag cases pass there, as they do here.

The randomized lanes of #9400's review on main (fixtures not in the repository; the same seeds and harness as #9400's test plan):

R19A {'sessions': 5000, 'ran': 4998, 'anew': 169, 'anewSessions': 51, ...} author-wrong [4706, 4895, 5192] agent-wrong [3114, 5383]
OWN 4000 {'wrong-kept': 10, 'written-anew': 9, 'concurrent-text': 1}
STRETCH 2000 {'wrong-kept': 998, 'written-anew': 1015, 'concurrent-text': 206} concurrent-text target-only vs r11 [317, 521, 577, 645, 762, 1025, 1109, 1353, 1477, 1582]
R20A {'sessions': 4000, ...} concurrency-wrong 254 target-only vs r11 [2787]
fuzz {"swap4000": "10/3850", "entity3000": "0/2738", "compound6000": "0/5271"}

OwnLaneV2 flagged 10 of 4,000 sessions. "target-only vs r11": sessions that an earlier line-diff revision of #9400 (round 11) handles and the measured code does not.

After

$ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/regressions/TextIdentity.test.ts
      Tests  131 passed (131)
$ npx vitest --project unit --no-watch packages/lexical-text packages/lexical/src/__tests__/unit/LexicalTextEditRecord.test.ts
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalTextEditRecord.test.ts (16 tests)
 ✓ |unit| packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityRecord.test.ts (6 tests)
 ✓ |unit| packages/lexical-text/src/__tests__/unit/canShowPlaceholder.test.ts (6 tests)
 ✓ |unit| packages/lexical-text/src/__tests__/unit/registerLexicalTextEntity.test.ts (2 tests)
 ✓ |unit| packages/lexical-text/src/__tests__/unit/Issue5466Repro.test.ts (5 tests)
 ✓ |unit| packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityEquivalence.test.ts (10 tests)
      Tests  45 passed (45)

$ TEXT_ENTITY_EQUIVALENCE_SESSIONS=3000 npx vitest --project unit --no-watch packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityEquivalence.test.ts
   ✓ registerLexicalTextEntity against the helper before it recorded its edits (2)
     ✓ 3000 random sessions of 8 edits next to entities, not recording text edits: the same JSON, selection, mutations and keys 13963ms
     ✓ 3000 random sessions of 8 edits next to entities, recording text edits: the same JSON, selection, mutations and keys 37824ms
      Tests  10 passed (10)

24,000 edits in each mode; with recording they made 671 moves and 4,998 retypes. The committed default is 300 sessions.

$ pnpm run test-unit
 Test Files  561 passed | 3 skipped (564)
      Tests  15333 passed | 16 expected fail | 4 skipped (15353)

Against main run the same way (559 files; 15,273 passed, 16 expected fail): no test missing or with another status, 60 added, all passing. DisposeMemory.test.ts, main's heap check, fails in some full runs once test files are added: main with two placeholder test files added failed it in 2 of 4 runs, so it is not this change.

tsc, tsc -p tsconfig.test.json, pnpm run flow (0 errors), eslint and prettier on the changed files, pnpm run check-changesets and pnpm run build: all pass.

The lanes:

R19A {'sessions': 5000, 'ran': 4998, 'anew': 35, 'anewSessions': 11, ...} author-wrong [5192] agent-wrong []
OWN 4000 {}
STRETCH 2000 {'wrong-kept': 954, 'written-anew': 970, 'concurrent-text': 192} concurrent-text target-only vs r11 [317, 521, 577, 645, 762, 1025, 1109, 1353, 1477, 1582]
R20A {'sessions': 4000, ...} concurrency-wrong 232 target-only vs r11 []
fuzz {"swap4000": "10/3850", "entity3000": "0/2738", "compound6000": "0/5271"}
  • R19-A: characters kept for another 3 to 1 (author) and 2 to 0 (agent, 5383 included); characters written anew 169 to 35 (51 to 11 sessions).
  • R20-A: sessions worse than round 11, 1 to 0; concurrent edits ending wrong, 254 to 232.
  • OwnLaneV2: flagged sessions, 10 to 0.
  • Unchanged: TextEntityEdits 0 of 2,738, CompoundTextEdits 0 of 5,271; NodeSwapEdits 10 of 3,850 and StretchLane's 10 sessions are the lanes' own app-code setTextContent grows and sets, as is R19-A's 5192.

Not exercised: browsers (the change has no DOM or input path; the equivalence test runs in jsdom).

@vercel

vercel Bot commented Oct 28, 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 Oct 29, 2026 3:41pm UTC
lexical-playground Ready Ready Preview Oct 29, 2026 3:41pm UTC

Request Review

@zurfyx
zurfyx requested a review from etrepum as a code owner October 28, 2026 01:50
@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 Oct 28, 2026
@pkg-pr-new

pkg-pr-new Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9483

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9483

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9483

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9483

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9483

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9483

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9483

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9483

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9483

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9483

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9483

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9483

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9483

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9483

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9483

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9483

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9483

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9483

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9483

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9483

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9483

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9483

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9483

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9483

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9483

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9483

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9483

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9483

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9483

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9483

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9483

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9483

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9483

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9483

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9483

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9483

commit: 02b3c84

…and remakes as moves and retypes for @lexical/y

## Description

Builds on #9400 (`@lexical/y`), now in main.

**Current behavior.** While `@lexical/y` is attached, core records every text edit of an update in `EditorState#_textEdits`, and the binding replays that record to keep each character's identity. `registerLexicalTextEntity` (hashtags, keywords and other entity plugins) makes two kinds of edits whose record does not say which characters persisted:

- An entity grown over the start of the next text node (the space between `#foo` and `bar` deleted) sets both texts whole: `setTextContent` on the entity, then `setTextContent` or `remove()` on the next node. The record has two `set` entries (or a `set` and a `detach`).
- An entity made plain text again, or plain text made an entity, copies the text into a new node and calls `replace()`. The record has the old node's `detach` and the new node's `attach`.

The binding then aligns the old and the new text of the line to guess which characters stayed. Next to equal text made or removed in the same update (a paste, a node that app code removes), the guess can take other characters, and a collaborator's concurrent edit of them is lost or lands on a neighbour. #9400 lists this in its Known open issues ("Text changed without a record of the edit") and names this change as the planned follow-up.

**New behavior.** The helper records what it already knows, around the calls it made before. The calls themselves do not change.

- The grow is recorded as one `move` entry: the node the characters left, the entity, how many, and both texts before. Core builds it from the two `set` entries those calls record, and only when they are exactly that move (the entity's text is its old text plus the first characters of the next node, which keeps the rest or is removed); otherwise they stay as recorded, for example under a subclass whose `setTextContent` writes other text.
- A remake is recorded as a `retype` entry right after the `replace()`: the old node, the new node and their text. It is recorded only when the last two entries are that replace's `detach` and `attach` and both nodes hold the same text (a `createNode` that changes the text records nothing).
- `@lexical/y` replays both. A move keeps the moved characters' identities in the entity. A retype gives the new node what the old node held when it was removed, when all of the new node's text was made in this update and equals it. Where an entry does not match the texts it covers, the binding guesses as before.
- One exception for the move. Text that left a node earlier in the update (a text set, typed text deleted again) is aligned with what arrived in that node when the node is set whole again. Where the move splits such text between the two nodes, or joins it with the entity's own, the binding marks it (the mark follows the text through splits, merges and remakes). Where a later text set in the update reaches marked text, or a node removed by the end of the update still holds some, it replays the update's record again with that move as the two text sets it stands for, as before.

Internal only. The two helpers are exported from `lexical` as `INTERNAL_$recordTextMove` and `INTERNAL_$recordTextRetype`, marked `@internal` like `INTERNAL_$normalizeNodeStructure`; `TextEditRecord` gains the two kinds, also in the Flow type of `EditorState#_textEdits`. No public API changes.

Without collaboration (`_recordTextEdits` unset) the helper makes the same calls and records nothing: one flag check per grow and per remake. With collaboration the editor also ends the same; only the record differs.

Not covered: app code that moves text between nodes with `setTextContent` and `remove()`, or remakes nodes itself, is still placed by the line's diff (#9400's Known open issues).

Changeset: `.changeset/lexical-y-text-entities.md`, a `[fix]` for `lexical`, `@lexical/text` and `@lexical/y` (patch).

## Test plan

New tests:
- `@lexical/text` `registerLexicalTextEntityRecord.test.ts`: the record of a grow over part of the next node, over all of it, a shrink (a split, unchanged), an entity made plain text, plain text made an entity, and a `createNode` that changes the text; each with and without recording, from the same node keys, with the same JSON, nodes and selection both ways.
- `@lexical/text` `registerLexicalTextEntityEquivalence.test.ts`: a copy of the helper as it was before this change against the new one, in two editors with a DOM root, over random caret edits and app-code sets and removals next to two kinds of entities. After every edit: the same JSON, selection, mutation-listener events and node keys. With recording too, where the new record, with each move expanded back into its two sets and the retypes dropped, equals the old one. Also the cases where changing the calls would have differed (a stale caret inside the entity, a setter that refuses empty text, a NodeSelection) and a shortened entity with the caret past its end.
- `lexical` `LexicalTextEditRecord.test.ts`: both helpers called directly, including what they leave as recorded (other text, a node that is not the previous sibling, a split or a move inside, a retype that does not follow the replace) and an error thrown inside, which reaches `onError` with the update discarded.
- `@lexical/y` `regressions/TextIdentity.test.ts`: R19A-5383 (app code removes a node next to two stale entities that the transforms remake) and R19A-4590 (a paste and an app-code grow next to two stale entities that the transforms remake), from #9400's randomized lanes, with controls; a move out of a node set and edited earlier in the update, then either node set again (directly and through the hashtag plugin), a merge, a segmented split, a remake (also of a link around it), a second move of all of the first one's destination, or the node it moved to set with the rest put before it; the hashtag grow above with a concurrent delete of a moved character.

### Before

The new regression cases on main:

```
$ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/regressions/TextIdentity.test.ts -t "Text entity edits"
   ❯ Text entity edits: grows and remakes keep their characters (36)
     ❯ R19A-5383: app code removes a node next to entities the transforms remake (6)
       × R19A-5383 the peer deletes the "l" of the remade tag (ids 1,2)
       × R19A-5383 the peer deletes the "l" of the remade tag (ids 2,1)
       × R19A-5383 the peer deletes the "a" of the remade tag (ids 1,2)
       × R19A-5383 the peer deletes the "a" of the remade tag (ids 2,1)
       ✓ R19A-5383 the peer deletes control: the "a" of the other tag (ids 1,2)
       ✓ R19A-5383 the peer deletes control: the "a" of the other tag (ids 2,1)
     ❯ R19A-4590: a paste and an app-code grow next to entities the transforms remake (10)
       ✓ R19A-4590 the peer deletes control: the "l" of the first tag (ids 1,2)
       ✓ R19A-4590 the peer deletes control: the "l" of the first tag (ids 2,1)
       × R19A-4590 the peer deletes the "l" of the second tag (ids 1,2)
       × R19A-4590 the peer deletes the "l" of the second tag (ids 2,1)
       × R19A-4590 the peer deletes the "a" of the second tag (ids 1,2)
       × R19A-4590 the peer deletes the "a" of the second tag (ids 2,1)
       ✓ R19A-4590 the peer deletes the "l" app code moved (ids 1,2)
       ✓ R19A-4590 the peer deletes the "l" app code moved (ids 2,1)
       ✓ R19A-4590 the peer deletes the "a" app code moved (ids 1,2)
       ✓ R19A-4590 the peer deletes the "a" app code moved (ids 2,1)
     ❯ a move out of a node set and edited earlier in the update (4)
       × then the node it moved to set (ids 1,2): the peer deletes a character the author deleted
       ...
     × a move, a move of all of its destination, then that node set again (ids 2,1)
     ✓ a hashtag grown over text set and edited earlier in the update, then that text set again (ids 1,2)
     ✓ a hashtag grown over text set and edited earlier in the update, then that text set again (ids 2,1)
     ✓ a hashtag grown over the next word (2)

AssertionError: expected [ 'la  ab ', 'la  ab ', 'la  ab ' ] to deeply equal [ 'a  ab ', 'a  ab ', 'a  ab ' ]
AssertionError: expected [ 'lla lalaa a', 'lla lalaa a', …(1) ] to deeply equal [ 'lla alaa a', 'lla alaa a', …(1) ]
TypeError: (0 , INTERNAL_$recordTextMove) is not a function

      Tests  24 failed | 12 passed | 95 skipped (131)
```

The peer's delete of the remade tag's characters is lost (5383), or lands on the pasted text (4590). The 16 cases that call `INTERNAL_$recordTextMove` directly cannot run on main; the hashtag cases pass there, as they do here.

The randomized lanes of #9400's review on main (fixtures not in the repository; the same seeds and harness as #9400's test plan):

```
R19A {'sessions': 5000, 'ran': 4998, 'anew': 169, 'anewSessions': 51, ...} author-wrong [4706, 4895, 5192] agent-wrong [3114, 5383]
OWN 4000 {'wrong-kept': 10, 'written-anew': 9, 'concurrent-text': 1}
STRETCH 2000 {'wrong-kept': 998, 'written-anew': 1015, 'concurrent-text': 206} concurrent-text target-only vs r11 [317, 521, 577, 645, 762, 1025, 1109, 1353, 1477, 1582]
R20A {'sessions': 4000, ...} concurrency-wrong 254 target-only vs r11 [2787]
fuzz {"swap4000": "10/3850", "entity3000": "0/2738", "compound6000": "0/5271"}
```

OwnLaneV2 flagged 10 of 4,000 sessions. "target-only vs r11": sessions that an earlier line-diff revision of #9400 (round 11) handles and the measured code does not.

### After

```
$ npx vitest --project unit --no-watch packages/lexical-y/src/__tests__/unit/regressions/TextIdentity.test.ts
      Tests  131 passed (131)
```

```
$ npx vitest --project unit --no-watch packages/lexical-text packages/lexical/src/__tests__/unit/LexicalTextEditRecord.test.ts
 ✓ |unit| packages/lexical/src/__tests__/unit/LexicalTextEditRecord.test.ts (16 tests)
 ✓ |unit| packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityRecord.test.ts (6 tests)
 ✓ |unit| packages/lexical-text/src/__tests__/unit/canShowPlaceholder.test.ts (6 tests)
 ✓ |unit| packages/lexical-text/src/__tests__/unit/registerLexicalTextEntity.test.ts (2 tests)
 ✓ |unit| packages/lexical-text/src/__tests__/unit/Issue5466Repro.test.ts (5 tests)
 ✓ |unit| packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityEquivalence.test.ts (10 tests)
      Tests  45 passed (45)

$ TEXT_ENTITY_EQUIVALENCE_SESSIONS=3000 npx vitest --project unit --no-watch packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityEquivalence.test.ts
   ✓ registerLexicalTextEntity against the helper before it recorded its edits (2)
     ✓ 3000 random sessions of 8 edits next to entities, not recording text edits: the same JSON, selection, mutations and keys 13963ms
     ✓ 3000 random sessions of 8 edits next to entities, recording text edits: the same JSON, selection, mutations and keys 37824ms
      Tests  10 passed (10)
```

24,000 edits in each mode; with recording they made 671 moves and 4,998 retypes. The committed default is 300 sessions.

```
$ pnpm run test-unit
 Test Files  561 passed | 3 skipped (564)
      Tests  15333 passed | 16 expected fail | 4 skipped (15353)
```

Against main run the same way (559 files; 15,273 passed, 16 expected fail): no test missing or with another status, 60 added, all passing. `DisposeMemory.test.ts`, main's heap check, fails in some full runs once test files are added: main with two placeholder test files added failed it in 2 of 4 runs, so it is not this change.

`tsc`, `tsc -p tsconfig.test.json`, `pnpm run flow` (0 errors), `eslint` and `prettier` on the changed files, `pnpm run check-changesets` and `pnpm run build`: all pass.

The lanes:

```
R19A {'sessions': 5000, 'ran': 4998, 'anew': 35, 'anewSessions': 11, ...} author-wrong [5192] agent-wrong []
OWN 4000 {}
STRETCH 2000 {'wrong-kept': 954, 'written-anew': 970, 'concurrent-text': 192} concurrent-text target-only vs r11 [317, 521, 577, 645, 762, 1025, 1109, 1353, 1477, 1582]
R20A {'sessions': 4000, ...} concurrency-wrong 232 target-only vs r11 []
fuzz {"swap4000": "10/3850", "entity3000": "0/2738", "compound6000": "0/5271"}
```

- R19-A: characters kept for another 3 to 1 (author) and 2 to 0 (agent, 5383 included); characters written anew 169 to 35 (51 to 11 sessions).
- R20-A: sessions worse than round 11, 1 to 0; concurrent edits ending wrong, 254 to 232.
- OwnLaneV2: flagged sessions, 10 to 0.
- Unchanged: TextEntityEdits 0 of 2,738, CompoundTextEdits 0 of 5,271; NodeSwapEdits 10 of 3,850 and StretchLane's 10 sessions are the lanes' own app-code `setTextContent` grows and sets, as is R19-A's 5192.

Not exercised: browsers (the change has no DOM or input path; the equivalence test runs in jsdom).
Merged via the queue into main with commit f77b0b0 Oct 29, 2026
53 checks passed
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). Any match right after it stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call, made only for a match at the start of a node after a simple text node.

That check was also skipped when the text node's transform returned early because its text had no match after the text node before it. Its own loop checks the entity after it, but never ran, so typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. The transform now checks that entity as its loop would: after the matches it would leave as text at its start (at most 8), the entity is turned back into text if the rest of its text in front of it is no match, or if nothing is left in front of it. An entity whose own transform is still splitting it or turning it back into text (its text is not exactly one match) is left to that transform.

No API is added or changed. Nothing changes for a text node that follows an entity or no text node, or for an entity's own transform. Entities can differ from before only next to the boundary between a simple text node and the text node before it (adjacent simple text nodes merge, so the two differ in format or style), where the two transforms disagreed.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 26 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; `createNode` factories that lower-case, upper-case or add text; and one `getMatch` call per keystroke in a long node before an entity. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- any edit (typing or deleting anywhere, bolding a node) settles, and so does transforming every node again.

`Hashtags.spec.mjs`: typing `#foo #bar` after a bold `a`, and a space inside `#foobar #baz` between bold `a` and bold `!`, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  10 failed | 16 passed (26)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  26 passed (26)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  26 passed (26)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). Any match right after it stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call, made only for a match at the start of a node after a simple text node.

That check was also skipped when the text node's transform returned early because its text had no match after the text node before it: its own loop checks the entity after it, but never ran. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. The transform now checks that entity as its loop would, with the same weak checks: the entity is turned back into text if it is no match after the node's text (the check above), if a match of the node's text ends where the entity starts (the entity touches a match left as text, like a match right after an entity), or if it is no match after what is left of the node's text past the matches at its start (at most 8), which the loop leaves as text. An entity whose own transform is still splitting it or turning it back into text (its text is not exactly one match) is left to that transform. This costs at most a few `getMatch` calls, only in that early return.

A zero-length match is left to the code as before.

No API is added or changed. Nothing changes for a text node that follows an entity or no text node, or for an entity's own transform. Entities can differ from before only next to the boundary between a simple text node and the text node before it (adjacent simple text nodes merge, so the two differ in format or style), where the two transforms disagreed.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 26 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; `createNode` factories that lower-case, upper-case or add text; and one `getMatch` call per keystroke in a long node before an entity. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- any edit (typing or deleting anywhere, bolding a node) settles, and so does transforming every node again.

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  10 failed | 16 passed (26)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  26 passed (26)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  26 passed (26)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). Any match right after it stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call, made only for a match at the start of a node after a simple text node.

That transform also returned early, before its loop, when its text had no match after the text node before it, or the first one started in that node; so it never checked the text node after it. An entity there was kept even when the text in front of it no longer let it match: typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. And a match left as text at the start of the next node was not reconsidered when typing made it a match again. In that early return the transform now checks the node after it, with the text after that node as context: the first match from its own text on, past the matches of its own text (at most 8), as its loop passes over them, must start where an entity there starts, or the entity is turned back into text; a simple text node there that such a match now starts is marked dirty, so its transform makes the entity. The loop's own check of the next node marks it dirty in that case too. An entity whose own transform is still splitting it or turning it back into text (its text is not exactly one match) is left to that transform, and past 8 matches nothing is changed. This costs at most 10 `getMatch` calls, only in that early return.

A zero-length match is handled as before.

No API is added or changed, and an entity's own transform is unchanged. Entities can differ from before only at the start of the node right after a simple text node, where one of the checks above was skipped or disagreed with the other.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 28 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; a matcher that needs the text after a match; a match at the start of the next node that typing makes valid; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- any edit (typing or deleting anywhere, bolding a node) settles, and so does transforming every node again.

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × typing that makes a match at the start of the next node one makes it an entity
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  11 failed | 17 passed (28)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  28 passed (28)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  28 passed (28)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

That transform also returned early, before its loop, when its text had no match after the text node before it, or the first one started in that node; so it never checked the text node after it. An entity there was kept even when the text in front of it no longer let it match: typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. And a match left as text at the start of the next node was not reconsidered when typing made it a match again. In that early return the transform now checks the node after it, with the text after that node as context: the first match from its own text on, past the matches of its own text (at most 8), as its loop passes over them, must start where an entity there starts, or the entity is turned back into text; a simple text node there that such a match now starts is marked dirty, so its transform makes the entity. The loop's own check of the next node marks it dirty in that case too. An entity whose own transform is still splitting it or turning it back into text (its text is not exactly one match) is left to that transform, and past 8 matches nothing is changed. This costs at most 10 `getMatch` calls, only in that early return.

A zero-length match is handled as before.

No API is added or changed, and an entity's own transform is unchanged. Entities can differ from before only at the start of the node right after a simple text node, where one of the checks above was skipped or disagreed with the other.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 28 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match, one with a `createNode` that upper-cases; a match at the start of the next node that typing makes valid; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- any edit (typing or deleting anywhere, bolding a node) settles, and so does transforming every node again.

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × typing that makes a match at the start of the next node one makes it an entity
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  11 failed | 17 passed (28)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  28 passed (28)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  28 passed (28)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

That transform also returned early, before its loop, when its text had no match after the text node before it, or the first one started in that node; so it never checked the text node after it. An entity there was kept even when the text in front of it no longer let it match: typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. And a match left as text at the start of the next node was not reconsidered when typing made it a match again. In that early return the transform now checks the node after it, with the text after that node as context: the first match from its own text on, past the matches of its own text (at most 8), as its loop passes over them, must start where an entity there starts, or the entity is turned back into text; a simple text node there that such a match now starts is marked dirty, so its transform makes the entity. The loop's own check of the next node marks it dirty in that case too. An entity whose own transform is still splitting it or turning it back into text (its text is not exactly one match) is left to that transform, and past 8 matches nothing is changed. This costs at most 10 `getMatch` calls, only in that early return.

The loop's check of the entity after the node uses the same context: it keeps an entity that is a match with the text after it, as the check that made it an entity found it, instead of turning it back into text because the entity alone, after the node's text, is no match. And an entity's own transform, when it follows a simple text node it is no match after (with or without the text after it), marks that node dirty, so that an edit to the text after the entity (removing the space a matcher needs, say) turns it back into text in the same update rather than at the next edit of the text before it.

A zero-length match is handled as before.

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 29 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; a match at the start of the next node that typing makes valid; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- any edit (typing or deleting anywhere, bolding a node) settles, and so does transforming every node again.

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × typing that makes a match at the start of the next node one makes it an entity
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  12 failed | 17 passed (29)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  29 passed (29)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  29 passed (29)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the text node after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make; and a match left as text at the start of the next node was not reconsidered when typing made it a match again. In that early return the transform now checks the node after it:

- an entity there is turned back into text unless the transforms would keep it there (below);
- a simple text node there that a match now starts is marked dirty, so its transform makes the entity (the loop's own check of the next node does this too).

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format and style), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty once per update, so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity was turned back into text by any text node after it, and made again by the next transform, forever (any match followed by text in the same node looped). It is now turned back into text only when the text after it extends or changes its match.

These checks cost a few `getMatch` calls each (at most about 10), only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own.

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other, and for token or segmented entities with text after them.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 32 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; token and segmented entities with text after them; a match at the start of the next node that typing makes valid; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- any edit (typing or deleting anywhere, bolding a node) settles, and so does transforming every node again.

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × a token or segmented entity with text after it settles
 × removing the text between an entity and a token after it turns the entity back into text
 × typing that makes a match at the start of the next node one makes it an entity
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  14 failed | 18 passed (32)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  32 passed (32)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  32 passed (32)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the text node after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make; and a match left as text at the start of the next node was not reconsidered when typing made it a match again. In that early return the transform now checks the node after it:

- an entity there is turned back into text unless the transforms would keep it there (below);
- a simple text node there that a match now starts is marked dirty, so its transform makes the entity (the loop's own check of the next node does this too).

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format and style), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node in each update (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity was turned back into text by any text node after it, and made again by the next transform, forever (any match followed by text in the same node looped). It is now turned back into text only when the text after it extends or changes its match; that case still loops where it did on `main` (text in another format that extends the match, say).

These checks cost a few `getMatch` calls each (at most about 10), only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own.

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other, and for token or segmented entities with text after them.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 33 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; token and segmented entities with text after them; an entity checked again in a later update; a match at the start of the next node that typing makes valid; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each.

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × a token or segmented entity with text after it settles
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one makes it an entity
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  15 failed | 18 passed (33)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  33 passed (33)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  33 passed (33)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the text node after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make; and a match left as text at the start of the next node was not reconsidered when typing made it a match again. In that early return the transform now checks the node after it:

- an entity there is turned back into text unless the transforms would keep it there (below);
- a simple text node there that a match now starts is marked dirty when its own transform will make the entity (the first match of both texts starts in it), as the loop's own check of the next node does too. This is only done once `createNode` has made entities, all of them normal text nodes: a token or segmented entity with text after it in its node is turned back into text and made again forever (as on `main`), and such a node is left as it is. Not marking a node its transform leaves unchanged also keeps typing in one history entry.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node in each update (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Token and segmented entities with text after them in their node also still loop, as on `main`.

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 36 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token entity left as it was; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag (so history merges typing); a match at the start of the next node that typing makes valid; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one makes it an entity
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  15 failed | 21 passed (36)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  36 passed (36)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  36 passed (36)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the text node after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make; and a match left as text at the start of the next node was not reconsidered when typing made it a match again. In that early return the transform now checks the node after it:

- an entity there is turned back into text unless the transforms would keep it there (below);
- a simple text node there that a match now starts is marked dirty when its own transform will make the entity (the first match of both nodes' whole texts starts in it), as the loop's own check of the next node does too. This is only done once `createNode` has made entities, all of them normal text nodes: a token or segmented entity with text after it in its node is turned back into text and made again forever (as on `main`), and such a node is left as it is. Not marking a node its transform leaves unchanged also keeps typing in one history entry.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node in each update (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Token and segmented entities with text after them in their node also still loop, as on `main`.

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 37 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token entity left as it was; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one makes it an entity
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  15 failed | 22 passed (37)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  37 passed (37)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  37 passed (37)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the entity after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. In that early return the transform now turns such an entity back into text unless the transforms would keep it there (below). A match left as text at the start of the next node stays text until that node is edited, as before.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node in each update (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity with text after it in its node looped on `main`: the transform of that text turns it back into text, the text merges, and the transform makes it again. It is still turned back into text, but when the same entity (same parent, offset and text) comes back to be turned into text a third time in one update, it is left an entity. Until then the transforms run as before, so wherever they settled before they still do.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Two registrations whose entities end up touching can still loop through the rule that turns two adjacent entities into text (with a hashtag and a segmented upper-case matcher, `#xabDOG` loops on `main` from a fresh parse); with one registration, or matches that cannot touch, this does not arise. The source text of an entity is told by format, style and detail; NodeState is not compared, since the entity is `createNode`'s node and need not carry its source's state.

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 39 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token or segmented entity with text after it in its node (loops on `main`); two registrations, one making token or segmented entities, in both orders; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid, made an entity when that node next changes; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one leaves it text until that node changes
 × a token or segmented entity with text after it in its node settles
 × two registrations, one making token or segmented entities, settle
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  17 failed | 22 passed (39)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  39 passed (39)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  39 passed (39)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the entity after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. In that early return the transform now turns such an entity back into text unless the transforms would keep it there (below). A match left as text at the start of the next node stays text until that node is edited, as before.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node in each update (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity with text after it in its node looped on `main`: the transform of that text turns it back into text, the text merges, and the transform makes it again. It is still turned back into text, but when the same entity (same parent, offset and text) comes back to be turned into text a third time in one update, it is left as it is. Until then the transforms run as before, so wherever they settled before they still do. The count belongs to the update's pending editor state, so an update that fails and is discarded does not count toward the next.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Two registrations whose entities end up touching can still loop through the rule that turns two adjacent entities into text (with a hashtag and a segmented upper-case matcher, `#xabDOG` loops on `main` from a fresh parse); with one registration, or matches that cannot touch, this does not arise. The source text of an entity is told by format, style and detail; NodeState is not compared, since the entity is `createNode`'s node and need not carry its source's state.

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 41 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token or segmented entity with text after it in its node (loops on `main`), and one after a match left as text; an update that fails not counting toward the next; two registrations, one making token or segmented entities, in both orders; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid, made an entity when that node next changes; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one leaves it text until that node changes
 × a token or segmented entity with text after it in its node settles
 × two registrations, one making token or segmented entities, settle
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  17 failed | 24 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the entity after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. In that early return the transform now turns such an entity back into text unless the transforms would keep it there (below). A match left as text at the start of the next node stays text until that node is edited, as before.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node for each pending editor state (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity with text after it in its node looped on `main`: the transform of that text turns it back into text, the text merges, and the transform makes it again. It is still turned back into text, but when the same entity (same parent, offset and text) comes back to be turned into text a third time in one update (or in updates batched into one commit), it is left as it is. Until then the transforms run as before, so wherever they settled before they still do. The count belongs to the pending editor state, so an update that fails and is discarded does not count toward the next.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Two registrations whose entities end up touching can still loop through the rule that turns two adjacent entities into text (with a hashtag and a segmented upper-case matcher, `#xabDOG` loops on `main` from a fresh parse); with one registration, or matches that cannot touch, this does not arise. The source text of an entity is told by format, style and detail; NodeState is not compared, since the entity is `createNode`'s node and need not carry its source's state.

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 41 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token or segmented entity with text after it in its node (loops on `main`), and one after a match left as text, with the nodes each settles on; an update that fails not counting toward the next; two registrations, one making token or segmented entities, in both orders, with the nodes they settle on; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid, made an entity when that node next changes; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one leaves it text until that node changes
 × a token or segmented entity with text after it in its node settles
 × two registrations, one making token or segmented entities, settle
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  17 failed | 24 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 1, 2026
…ttle between text nodes of other formats

## Description

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the entity after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. In that early return the transform now turns such an entity back into text unless the transforms would keep it there (below). A match left as text at the start of the next node stays text until that node is edited, as before.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node for each pending editor state (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity with text after it in its node looped on `main`: the transform of that text turns it back into text, the text merges, and the transform makes it again. It is still turned back into text, but when the same entity (same parent, offset and text) comes back to be turned into text a third time in one update (or in updates batched into one commit), it is kept, and takes the text its match extends into, as an entity in normal mode does. Until then the transforms run as before, so wherever they settled before they still do. The count belongs to the pending editor state, so an update that fails and is discarded does not count toward the next.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Two registrations whose entities end up touching can still loop through the rule that turns two adjacent entities into text (with a hashtag and a segmented upper-case matcher, `#xabDOG` loops on `main` from a fresh parse); with one registration, or matches that cannot touch, this does not arise. The source text of an entity is told by format, style and detail; NodeState is not compared, since the entity is `createNode`'s node and need not carry its source's state.

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637

## Changeset

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

## Test plan

`registerLexicalTextEntitySettle.test.ts` (new, 41 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token or segmented entity with text after it in its node (loops on `main`), and one after a match left as text, with the nodes each settles on; an update that fails not counting toward the next; two registrations, one making token or segmented entities, in both orders, with the nodes they settle on; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid, made an entity when that node next changes; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

### Before

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one leaves it text until that node changes
 × a token or segmented entity with text after it in its node settles
 × two registrations, one making token or segmented entities, settle
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  17 failed | 24 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.
zurfyx added a commit that referenced this pull request Nov 2, 2026
…become text

## Description

`registerLexicalTextEntity` turns an entity back into plain text when it stops matching (typing right before a hashtag, deleting its `#`, joining blocks), with `$replaceWithSimpleText`. That built the new `TextNode` from the entity's text, format, style and detail, but not its NodeState, so anything an app keeps on the characters as NodeState (comment ids, suggestion marks, block ids) was dropped (#9703). The same happened in the other direction: plain text with NodeState that becomes an entity gave the entity none.

What carries, and why:

- **Demotion (entity to text): all of the entity's NodeState**, nested and flat, known and not yet parsed (read from JSON), the same value objects (ids compared by identity stay identical). NodeState is the ad-hoc data an app keeps on text; what is inherent to the entity type is its class (its fields) and its mode, which stay with the type, as before. This is what `splitText` already does for the pieces it cuts, and what the code highlighters do since #9503 when a code token becomes text. A key that the plain text type declares with a different `StateConfig` is parsed with the text type's config. Format, style and detail carry as before.
- **Promotion (text to entity): the text's NodeState too**, for every key `createNode` did not set (its state wins), with one exception: text that the entity's own transform splits off its end in the same update (text typed or pasted right after a hashtag). `splitText` gives that piece a copy of the entity's NodeState, but those characters were just inserted, so it is not theirs; an entity made from it keeps the old behaviour (MarkStateExtension relies on telling inserted text from marked text this way). An entity turned into text and back in one update gets its own NodeState back.
- **Token and segmented entities** that text follows are turned into text so that they merge with it and are parsed again. With their NodeState kept they do not merge with text that has other NodeState, and the transforms would make the entity again and turn it back forever (on `main` this already looped whenever that text had NodeState, or another format). So, as in #9686, when the same entity (same parent, node before it and text) comes back to be turned into text a third time in one update (or updates batched into one commit), it is kept, with its NodeState. It takes the text its match extends into, as an entity in normal mode does, only when that text would merge with it (the same format, style, detail and NodeState; the selection stays where it was in that text); text with other NodeState or format stays apart, so it neither takes the entity's NodeState nor makes the entity drop its own. Until the third time the transforms run as before. The count belongs to the pending editor state, so an update that fails does not count toward the next.
- The text split off the end of an entity is tracked per editor, for every registration, so an entity of another registration made from it (a keyword typed after a hashtag) does not take the copy either.

Two text nodes merge only when their NodeState is equal, so text that keeps an entity's NodeState stays its own node next to text with other NodeState, as text in another format does. That is the point (the characters keep their comment), and it is the only structural difference: text with no NodeState merges as before.

Callers in this repo, all through `registerLexicalTextEntity`: `@lexical/hashtag` (`registerLexicalHashtag`, `HashtagExtension`, `LexicalHashtagPlugin`), `@lexical/react` `useLexicalTextEntity`, and the playground's `KeywordsExtension`. Mentions (typeahead) and `@lexical/link` autolink have their own transforms and do not share this path.

Fixes #9703

## Test plan

`.changeset/lexical-text-entity-node-state.md` (`@lexical/text`, patch, `[fix]`).

`registerLexicalTextEntityNodeState.test.ts` (new, 22 tests): #9703's repro; deleting a hashtag's `#`; merging into neighbours with the same NodeState and, without NodeState, merging as before; promotion with NodeState, `createNode`'s state winning, and text inserted after a hashtag not taking the copy; a reference-typed value carried as the same object; NodeState read from JSON and not parsed yet; NodeState declared in the entity's `$config` (nested and flat) and exported nested on the text; a key the text type declares with another config; a hashtag inside a MarkNode; undo and redo; and every path that turns an entity into text: the text transform with no match, a token entity that text follows (same NodeState: it takes the text; other NodeState: kept, the text stays apart; the selection after pasted text; text with its own NodeState, which loops on `main`), the next entity that stops matching, adjacent entities, an entity followed by another, and a join across text in another format.

`registerLexicalTextEntityNodeStateProperty.test.ts` (new): random layouts of entities (hashtags, and token mentions) and text in several formats, with NodeState on both and MarkNodes around some, and random edits (typing, backspace and delete, joining blocks, removing ranges across nodes). Each entity has a letter no other text has; after every edit that turns an entity into text, every character of it that is still there has the entity's NodeState and MarkNode (or sits inside another entity that grew over it, which takes characters with its own NodeState, as before). 200 seeds per matcher (`TEXT_ENTITY_NODE_STATE_SEEDS`).

Before (main):

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeState.test.ts packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 × #9703: a hashtag that stops matching keeps its NodeState when it becomes text (typing before it)
 × deleting the # of a hashtag keeps its NodeState (reverse transform, no match)
 × a hashtag that becomes text merges into neighbors with the same NodeState
 × plain text with NodeState that becomes a hashtag keeps it
 × state that createNode gives the entity wins over the text it replaces
 × a reference-typed state value is carried as the same object
 × NodeState read from JSON (not parsed yet) is carried and exported
 × NodeState declared in the entity $config (nested and flat) is carried to the text
 × a key the plain text type declares with another config is parsed with that config
 × a hashtag inside a MarkNode stays inside it when it becomes text
 × undo and redo of a demotion restore the entity and the text with their NodeState
 × the text transform: the entity before it stops matching with its text (prevMatch null)
 × with the same NodeState as the text: it keeps it and takes the text
 × with other NodeState than the text: kept with its NodeState, the text stays apart
 × typed text after it: the selection stays after the typed text
 × with other NodeState than text that its match extends into, which keeps it apart: kept, each keeps its own
 × the text transform: the entity after it stops matching (nextMatch null)
 × the reverse transform: adjacent entities both become text
 × the reverse transform: an entity followed by an entity
 × hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 × mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
      Tests  21 failed | 3 passed (24)
```

After:

```
$ pnpm exec vitest run --project unit packages/lexical-text packages/lexical-hashtag packages/lexical-mark packages/lexical-history
      Tests  1375 passed (1375)
$ TEXT_ENTITY_NODE_STATE_SEEDS=20000 pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 ✓ hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 ✓ mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
```

The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts` (JSON, selection, mutations and keys after random sessions, against the helper before #9483, which has no NodeState in its layouts), and `MarkStateExtension`'s hashtag tests (text inserted after a marked hashtag is not marked).

A differential against `main` over random editing sessions (typing, backspace and delete, joins across paragraphs, headings, quotes, list items and table cells, ranges, formatting, paste with NodeState, undo and redo, IME composition) with hashtags, a keyword matcher, a token and a segmented matcher, autolink and MarkNodes: the NodeState of an entity's characters is kept in tens of thousands of steps where `main` drops it; no text is lost; token and segmented entities that loop on `main` settle. Where NodeState now keeps text apart, the entities can differ from `main`, as they do next to text in another format; a keyword-style matcher can then reach #9637's loop in a layout `main` merged (a few per 10,000 sessions; hashtags never do), which #9637's fix settles.
zurfyx added a commit that referenced this pull request Nov 2, 2026
…ttle between text nodes of other formats (re-land of #9686)

This re-lands #9686, reverted by #9702 because it turns more entities back into text (every edit that leaves a hashtag after text in another format), and turning an entity into text dropped its NodeState (#9703): joining a block that starts with a commented hashtag onto text in another format lost the comment (#9704). #9712 fixes #9703 (entities keep their NodeState when they become text) and brings the loop breaker for token and segmented entities; this PR is #9686 on top of it, unchanged otherwise, plus #9704's case and the long-session join as tests. Review the second commit; the first is #9712.

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the entity after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. In that early return the transform now turns such an entity back into text unless the transforms would keep it there (below). A match left as text at the start of the next node stays text until that node is edited, as before.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node for each pending editor state (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity with text after it in its node looped on `main`: the transform of that text turns it back into text, the text merges, and the transform makes it again. #9712 keeps such an entity the third time it comes back to be turned into text in one update, and this PR keeps that rule.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Two registrations whose entities end up touching can still loop through the rule that turns two adjacent entities into text (with a hashtag and a segmented upper-case matcher, `#xabDOG` loops on `main` from a fresh parse); with one registration, or matches that cannot touch, this does not arise. The source text of an entity is told by format, style and detail; NodeState is not compared (text keeps its NodeState when it becomes an entity, but `createNode` may give the entity its own).

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637
Closes #9704

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

`registerLexicalTextEntitySettle.test.ts` (new, 41 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token or segmented entity with text after it in its node (loops on `main`), and one after a match left as text, with the nodes each settles on; an update that fails not counting toward the next; two registrations, one making token or segmented entities, in both orders, with the nodes they settle on; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid, made an entity when that node next changes; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one leaves it text until that node changes
 × a token or segmented entity with text after it in its node settles
 × two registrations, one making token or segmented entities, settle
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  17 failed | 24 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.

`registerLexicalTextEntityJoin.test.ts` (new, 5 tests): #9704's repro (a list item starting with a commented hashtag joined onto italic text), the same join by backspace and by forward delete, the long-session shape (the block before ends with a tab, all with NodeState), and a hashtag with `MarkStateExtension` comment ids joined onto italic text. Without #9712 the first three lose the NodeState (as #9686 did); with #9712 alone the hashtag stays a hashtag, as on `main`; here it becomes text with its NodeState.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityJoin.test.ts
      Tests  5 passed (5)
```
zurfyx added a commit that referenced this pull request Nov 2, 2026
…hen they become text

## Description

`registerLexicalTextEntity` turns an entity back into plain text when it stops matching (typing right before a hashtag, deleting its `#`, joining blocks), with `$replaceWithSimpleText`. That built the new `TextNode` from the entity's text, format, style and detail, but not its NodeState, so anything an app keeps on the characters as NodeState (comment ids, suggestion marks, block ids) was dropped (#9703). The same happened in the other direction: plain text with NodeState that becomes an entity gave the entity none.

What carries, and why:

- **Demotion (entity to text): all of the entity's NodeState**, nested and flat, known and not yet parsed (read from JSON), the same value objects (ids compared by identity stay identical). NodeState is the ad-hoc data an app keeps on text; what is inherent to the entity type is its class (its fields) and its mode, which stay with the type, as before. This is what `splitText` already does for the pieces it cuts, and what the code highlighters do since #9503 when a code token becomes text. A key that the plain text type declares with a different `StateConfig` is parsed with the text type's config. Format, style and detail carry as before.
- **Promotion (text to entity): the text's NodeState too**, for every key `createNode` did not set (its state wins), with one exception: text that the entity's own transform splits off its end in the same update (text typed or pasted right after a hashtag). `splitText` gives that piece a copy of the entity's NodeState, but those characters were just inserted, so it is not theirs; an entity made from it keeps the old behaviour (MarkStateExtension relies on telling inserted text from marked text this way). An entity turned into text and back in one update gets its own NodeState back.
- **Token and segmented entities** that text follows are turned into text so that they merge with it and are parsed again. With their NodeState kept they do not merge with text that has other NodeState, and the transforms would make the entity again and turn it back forever (on `main` this already looped whenever that text had NodeState, or another format). So, as in #9686, when the same entity (same parent, text, and place: the first node before it that is no text entity, and the size of the entities between) comes back to be turned into text a third time in one update (or updates batched into one commit), it is kept, with its NodeState. It takes the text its match extends into, as an entity in normal mode does, only when that text would merge with it (the same format, style and detail, and NodeState equal by the rule text nodes merge by: `StateConfig.isEqual`, so two distinct value objects are two values; the selection stays where it was in that text); text with other NodeState or format stays apart, so it neither takes the entity's NodeState nor makes the entity drop its own. Until the third time the transforms run as before. The count belongs to the pending editor state, so an update that fails does not count toward the next.
- The text split off the end of an entity is tracked per editor, for every registration, so an entity of another registration made from it (a keyword typed after a hashtag) does not take the copy either.
- **Core: typing inside a segmented node** (outside composition) replaces it with a plain `TextNode` built from its text, format and style, the same way `$replaceWithSimpleText` did, and dropped its NodeState the same way. That node now keeps it (`$carryNodeState`, internal to `lexical`, with the same rules as above). Segmented entities that this change keeps or makes reach it more often, so it is fixed here.

Two text nodes merge only when their NodeState is equal, so text that keeps an entity's NodeState stays its own node next to text with other NodeState, as text in another format does. That is the point (the characters keep their comment), and it is the only structural difference: text with no NodeState merges as before.

Callers in this repo, all through `registerLexicalTextEntity`: `@lexical/hashtag` (`registerLexicalHashtag`, `HashtagExtension`, `LexicalHashtagPlugin`), `@lexical/react` `useLexicalTextEntity`, and the playground's `KeywordsExtension`. Mentions (typeahead) and `@lexical/link` autolink have their own transforms and do not share this path.

Fixes #9703

## Test plan

`.changeset/lexical-text-entity-node-state.md` (`lexical` and `@lexical/text`, patch, `[fix]`).

`LexicalSelection.test.ts`: typing inside a segmented node keeps its NodeState (fails on `main`).

`registerLexicalTextEntityNodeState.test.ts` (new, 23 tests): #9703's repro; deleting a hashtag's `#`; merging into neighbours with the same NodeState and, without NodeState, merging as before; promotion with NodeState, `createNode`'s state winning, and text inserted after a hashtag not taking the copy; a reference-typed value carried as the same object; NodeState read from JSON and not parsed yet; NodeState declared in the entity's `$config` (nested and flat) and exported nested on the text; a key the text type declares with another config; a hashtag inside a MarkNode; undo and redo; and every path that turns an entity into text: the text transform with no match, a token entity that text follows (same NodeState: it takes the text; other NodeState: kept, the text stays apart; the selection after pasted text; text with its own NodeState, which loops on `main`; a value object distinct from the text's, equal as JSON), the next entity that stops matching, adjacent entities, an entity followed by another, and a join across text in another format.

`registerLexicalTextEntityNodeStateProperty.test.ts` (new): random layouts of entities (hashtags, and token mentions) and text in several formats, with NodeState on both and MarkNodes around some, and random edits (typing, backspace and delete, joining blocks, removing ranges across nodes). Each entity has a letter no other text has; after every edit that turns an entity into text, every character of it that is still there has the entity's NodeState and MarkNode (or sits inside another entity that grew over it, which takes characters with its own NodeState, as before). 200 seeds per matcher (`TEXT_ENTITY_NODE_STATE_SEEDS`).

Before (main):

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeState.test.ts packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 × #9703: a hashtag that stops matching keeps its NodeState when it becomes text (typing before it)
 × deleting the # of a hashtag keeps its NodeState (reverse transform, no match)
 × a hashtag that becomes text merges into neighbors with the same NodeState
 × plain text with NodeState that becomes a hashtag keeps it
 × state that createNode gives the entity wins over the text it replaces
 × a reference-typed state value is carried as the same object
 × NodeState read from JSON (not parsed yet) is carried and exported
 × NodeState declared in the entity $config (nested and flat) is carried to the text
 × a key the plain text type declares with another config is parsed with that config
 × a hashtag inside a MarkNode stays inside it when it becomes text
 × undo and redo of a demotion restore the entity and the text with their NodeState
 × the text transform: the entity before it stops matching with its text (prevMatch null)
 × with the same NodeState as the text: it keeps it and takes the text
 × with other NodeState than the text: kept with its NodeState, the text stays apart
 × typed text after it: the selection stays after the typed text
 × with other NodeState than text that its match extends into, which keeps it apart: kept, each keeps its own
 × with a value object distinct from the text after it, equal as JSON: each keeps its own
 × the text transform: the entity after it stops matching (nextMatch null)
 × the reverse transform: adjacent entities both become text
 × the reverse transform: an entity followed by an entity
 × hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 × mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
      Tests  22 failed | 3 passed (25)
```

After:

```
$ pnpm exec vitest run --project unit packages/lexical-text packages/lexical-hashtag packages/lexical-mark packages/lexical-history
      Tests  1376 passed (1376)
$ pnpm exec vitest run --project unit packages/lexical/src/__tests__/unit/LexicalSelection.test.ts
      Tests  101 passed (101)
$ TEXT_ENTITY_NODE_STATE_SEEDS=20000 pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 ✓ hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 ✓ mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
```

The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts` (JSON, selection, mutations and keys after random sessions, against the helper before #9483, which has no NodeState in its layouts), and `MarkStateExtension`'s hashtag tests (text inserted after a marked hashtag is not marked).

A differential against `main` over random editing sessions (typing, backspace and delete, joins across paragraphs, headings, quotes, list items and table cells, ranges, formatting, paste with NodeState, undo and redo, IME composition) with hashtags, a keyword matcher, a token and a segmented matcher, autolink and MarkNodes: the NodeState of an entity's characters is kept in tens of thousands of steps where `main` drops it; no text is lost; token and segmented entities that loop on `main` settle. Where NodeState now keeps text apart, the entities can differ from `main`, as they do next to text in another format; a keyword-style matcher can then reach #9637's loop in a layout `main` merged (a few per 10,000 sessions; hashtags never do), which #9637's fix settles.
zurfyx added a commit that referenced this pull request Nov 2, 2026
…ttle between text nodes of other formats (re-land of #9686)

This re-lands #9686, reverted by #9702 because it turns more entities back into text (every edit that leaves a hashtag after text in another format), and turning an entity into text dropped its NodeState (#9703): joining a block that starts with a commented hashtag onto text in another format lost the comment (#9704). #9712 fixes #9703 (entities keep their NodeState when they become text) and brings the loop breaker for token and segmented entities; this PR is #9686 on top of it, unchanged otherwise, plus #9704's case and the long-session join as tests. Review the second commit; the first is #9712.

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the entity after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. In that early return the transform now turns such an entity back into text unless the transforms would keep it there (below). A match left as text at the start of the next node stays text until that node is edited, as before.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node for each pending editor state (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity with text after it in its node looped on `main`: the transform of that text turns it back into text, the text merges, and the transform makes it again. #9712 keeps such an entity the third time it comes back to be turned into text in one update, and this PR keeps that rule.

Two more changes come with the re-land, for orders of transforms that #9686 alone reached before the other checks:

- A normal-mode entity grows over the text after it (takes the characters its match extends into) only when it is an entity the transforms keep: not right after another text entity (the two are turned back into text by the rule for adjacent entities), and not after a simple text node that it is no entity after (`isEntityAfter`). Otherwise it is turned back into text first, rather than grown and then turned into text with the next node's characters, which then lost their format and NodeState (two hashtags next to each other before italic text, a `createNode` that changes the case).
- Two entities next to each other are turned back into text, as before, but when the same entity comes back to that a third time in one update it is kept (the same counter as the token breaker, counted apart): entities of two registrations that touch (a segmented `$B` next to a token `@x`) looped forever on `main`; now they settle, kept as they are.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Two registrations whose entities end up touching can still loop through the rule that turns two adjacent entities into text (with a hashtag and a segmented upper-case matcher, `#xabDOG` loops on `main` from a fresh parse); with one registration, or matches that cannot touch, this does not arise. The source text of an entity is told by format, style and detail; NodeState is not compared (text keeps its NodeState when it becomes an entity, but `createNode` may give the entity its own).

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637
Closes #9704

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

`registerLexicalTextEntitySettle.test.ts` (new, 41 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token or segmented entity with text after it in its node (loops on `main`), and one after a match left as text, with the nodes each settles on; an update that fails not counting toward the next; two registrations, one making token or segmented entities, in both orders, with the nodes they settle on; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid, made an entity when that node next changes; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one leaves it text until that node changes
 × a token or segmented entity with text after it in its node settles
 × two registrations, one making token or segmented entities, settle
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  17 failed | 24 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.

`registerLexicalTextEntityJoin.test.ts` (new, 7 tests): #9704's repro (a list item starting with a commented hashtag joined onto italic text), the same join by backspace and by forward delete, a block before that ends with a tab (all with NodeState), a hashtag with `MarkStateExtension` comment ids joined onto italic text, two hashtags turned into text before italic text with NodeState (the text keeps its format, NodeState and the selection), and typing before a hashtag that a segmented and a token entity follow (settles, with a bounded number of `getMatch` calls). With #9686 alone the first three lose the NodeState and the `MarkStateExtension` one loses its comment ids; with #9712 alone the hashtag stays a hashtag, as on `main`; here it becomes text and keeps them.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityJoin.test.ts
      Tests  7 passed (7)
```
zurfyx added a commit that referenced this pull request Nov 2, 2026
…hen they become text

## Description

`registerLexicalTextEntity` turns an entity back into plain text when it stops matching (typing right before a hashtag, deleting its `#`, joining blocks), with `$replaceWithSimpleText`. That built the new `TextNode` from the entity's text, format, style and detail, but not its NodeState, so anything an app keeps on the characters as NodeState (comment ids, suggestion marks, block ids) was dropped (#9703). The same happened in the other direction: plain text with NodeState that becomes an entity gave the entity none.

What carries, and why:

- **Demotion (entity to text): all of the entity's NodeState**, nested and flat, known and not yet parsed (read from JSON), the same value objects (ids compared by identity stay identical). NodeState is the ad-hoc data an app keeps on text; what is inherent to the entity type is its class (its fields) and its mode, which stay with the type, as before. This is what `splitText` already does for the pieces it cuts, and what the code highlighters do since #9503 when a code token becomes text. A key that the plain text type declares with a different `StateConfig` is parsed with the text type's config. Format, style and detail carry as before.
- **Promotion (text to entity): the text's NodeState too**, for every key `createNode` did not set (its state wins), with one exception: text that the entity's own transform splits off its end in the same update (text typed or pasted right after a hashtag). `splitText` gives that piece a copy of the entity's NodeState, but those characters were just inserted, so it is not theirs; an entity made from it keeps the old behaviour (MarkStateExtension relies on telling inserted text from marked text this way). An entity turned into text and back in one update gets its own NodeState back.
- **Token and segmented entities** that text follows are turned into text so that they merge with it and are parsed again. With their NodeState kept they do not merge with text that has other NodeState, and the transforms would make the entity again and turn it back forever (on `main` this already looped whenever that text had NodeState, or another format). So, as in #9686, when the same entity (same parent, text, and place: the first node before it that is no text entity, and the size of the entities between) comes back to be turned into text a third time in one update (or updates batched into one commit), it is kept, with its NodeState. It takes the text its match extends into, as an entity in normal mode does, only when that text would merge with it (the same format, style and detail, and NodeState equal by the rule text nodes merge by: `StateConfig.isEqual`, so two distinct value objects are two values; the selection stays where it was in that text); text with other NodeState or format stays apart, so it neither takes the entity's NodeState nor makes the entity drop its own. Until the third time the transforms run as before. The count belongs to the pending editor state, so an update that fails does not count toward the next.
- The text split off the end of an entity is tracked per editor, for every registration, so an entity of another registration made from it (a keyword typed after a hashtag) does not take the copy either.
- **Core: editing inside a segmented node** replaces it with a plain `TextNode` built from its text, format and style, the same way `$replaceWithSimpleText` did, and dropped its NodeState the same way: typing inside it (outside composition, `LexicalSelection`) and removing a range that starts or ends inside it (`LexicalCaretUtils`). Both now keep it (`$carryNodeState`, internal to `lexical`, with the same rules as above; keys the new node already holds, such as state a registered `TextNode` replacement sets, keep their value). Segmented entities that this change keeps or makes reach these paths more often, so they are fixed here.

Two text nodes merge only when their NodeState is equal, so text that keeps an entity's NodeState stays its own node next to text with other NodeState, as text in another format does. That is the point (the characters keep their comment), and it is the only structural difference: text with no NodeState merges as before.

Callers in this repo, all through `registerLexicalTextEntity`: `@lexical/hashtag` (`registerLexicalHashtag`, `HashtagExtension`, `LexicalHashtagPlugin`), `@lexical/react` `useLexicalTextEntity`, and the playground's `KeywordsExtension`. Mentions (typeahead) and `@lexical/link` autolink have their own transforms and do not share this path.

Fixes #9703

## Test plan

`.changeset/lexical-text-entity-node-state.md` (`lexical` and `@lexical/text`, patch, `[fix]`).

`LexicalSelection.test.ts` (3 tests, each fails on `main`): typing inside a segmented node keeps its NodeState; a registered `TextNode` replacement keeps the NodeState it sets and takes the rest; removing a range from inside a segmented node to the next block keeps the NodeState of what is left.

`registerLexicalTextEntityNodeState.test.ts` (new, 23 tests): #9703's repro; deleting a hashtag's `#`; merging into neighbours with the same NodeState and, without NodeState, merging as before; promotion with NodeState, `createNode`'s state winning, and text inserted after a hashtag not taking the copy; a reference-typed value carried as the same object; NodeState read from JSON and not parsed yet; NodeState declared in the entity's `$config` (nested and flat) and exported nested on the text; a key the text type declares with another config; a hashtag inside a MarkNode; undo and redo; and every path that turns an entity into text: the text transform with no match, a token entity that text follows (same NodeState: it takes the text; other NodeState: kept, the text stays apart; the selection after pasted text; text with its own NodeState, which loops on `main`; a value object distinct from the text's, equal as JSON), the next entity that stops matching, adjacent entities, an entity followed by another, and a join across text in another format.

`registerLexicalTextEntityNodeStateProperty.test.ts` (new): random layouts of entities (hashtags, and token mentions) and text in several formats, with NodeState on both and MarkNodes around some, and random edits (typing, backspace and delete, joining blocks, removing ranges across nodes). Each entity has a letter no other text has; after every edit that turns an entity into text, every character of it that is still there has the entity's NodeState and MarkNode (or sits inside another entity that grew over it, which takes characters with its own NodeState, as before). 200 seeds per matcher (`TEXT_ENTITY_NODE_STATE_SEEDS`).

Before (main):

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeState.test.ts packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 × #9703: a hashtag that stops matching keeps its NodeState when it becomes text (typing before it)
 × deleting the # of a hashtag keeps its NodeState (reverse transform, no match)
 × a hashtag that becomes text merges into neighbors with the same NodeState
 × plain text with NodeState that becomes a hashtag keeps it
 × state that createNode gives the entity wins over the text it replaces
 × a reference-typed state value is carried as the same object
 × NodeState read from JSON (not parsed yet) is carried and exported
 × NodeState declared in the entity $config (nested and flat) is carried to the text
 × a key the plain text type declares with another config is parsed with that config
 × a hashtag inside a MarkNode stays inside it when it becomes text
 × undo and redo of a demotion restore the entity and the text with their NodeState
 × the text transform: the entity before it stops matching with its text (prevMatch null)
 × with the same NodeState as the text: it keeps it and takes the text
 × with other NodeState than the text: kept with its NodeState, the text stays apart
 × typed text after it: the selection stays after the typed text
 × with other NodeState than text that its match extends into, which keeps it apart: kept, each keeps its own
 × with a value object distinct from the text after it, equal as JSON: each keeps its own
 × the text transform: the entity after it stops matching (nextMatch null)
 × the reverse transform: adjacent entities both become text
 × the reverse transform: an entity followed by an entity
 × hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 × mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
      Tests  22 failed | 3 passed (25)
```

After:

```
$ pnpm exec vitest run --project unit packages/lexical-text packages/lexical-hashtag packages/lexical-mark packages/lexical-history
      Tests  1376 passed (1376)
$ pnpm exec vitest run --project unit packages/lexical/src/__tests__/unit/LexicalSelection.test.ts
      Tests  103 passed (103)
$ TEXT_ENTITY_NODE_STATE_SEEDS=20000 pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 ✓ hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 ✓ mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
```

The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts` (JSON, selection, mutations and keys after random sessions, against the helper before #9483, which has no NodeState in its layouts), and `MarkStateExtension`'s hashtag tests (text inserted after a marked hashtag is not marked).

A differential against `main` over random editing sessions (typing, backspace and delete, joins across paragraphs, headings, quotes, list items and table cells, ranges, formatting, paste with NodeState, undo and redo, IME composition) with hashtags, a keyword matcher, a token and a segmented matcher, autolink and MarkNodes: the NodeState of an entity's characters is kept in tens of thousands of steps where `main` drops it; no text is lost; token and segmented entities that loop on `main` settle. Where NodeState now keeps text apart, the entities can differ from `main`, as they do next to text in another format; a keyword-style matcher can then reach #9637's loop in a layout `main` merged (a few per 10,000 sessions; hashtags never do), which #9637's fix settles.
zurfyx added a commit that referenced this pull request Nov 2, 2026
…ttle between text nodes of other formats (re-land of #9686)

This re-lands #9686, reverted by #9702 because it turns more entities back into text (every edit that leaves a hashtag after text in another format), and turning an entity into text dropped its NodeState (#9703): joining a block that starts with a commented hashtag onto text in another format lost the comment (#9704). #9712 fixes #9703 (entities keep their NodeState when they become text) and brings the loop breaker for token and segmented entities; this PR is #9686 on top of it, unchanged otherwise, plus #9704's case and the long-session join as tests. Review the second commit; the first is #9712.

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the entity after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. In that early return the transform now turns such an entity back into text unless the transforms would keep it there (below). A match left as text at the start of the next node stays text until that node is edited, as before.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node for each pending editor state (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity with text after it in its node looped on `main`: the transform of that text turns it back into text, the text merges, and the transform makes it again. #9712 keeps such an entity the third time it comes back to be turned into text in one update, and this PR keeps that rule.

Two more changes come with the re-land, for orders of transforms that #9686 alone reached before the other checks:

- A normal-mode entity grows over the text after it (takes the characters its match extends into) only when it is an entity the transforms keep: not right after another text entity (the two are turned back into text by the rule for adjacent entities), and not after a simple text node that it is no entity after (`isEntityAfter`; when that cannot tell, after too many matches in the text before, it stays as it is without growing). Otherwise it is turned back into text first, rather than grown and then turned into text with the next node's characters, which then lost their format and NodeState (two hashtags next to each other before italic text, a `createNode` that changes the case).
- Two entities next to each other are turned back into text, as before, but when the same entity comes back to that a third time in one update it is kept (the same counter as the token breaker, counted apart): entities of two registrations that touch (a segmented `$B` next to a token `@x`) looped forever on `main`; now they settle, kept as they are.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Two registrations whose entities end up touching can still loop through the rule that turns two adjacent entities into text (with a hashtag and a segmented upper-case matcher, `#xabDOG` loops on `main` from a fresh parse); with one registration, or matches that cannot touch, this does not arise. The source text of an entity is told by format, style and detail; NodeState is not compared (text keeps its NodeState when it becomes an entity, but `createNode` may give the entity its own).

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637
Closes #9704

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

`registerLexicalTextEntitySettle.test.ts` (new, 41 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token or segmented entity with text after it in its node (loops on `main`), and one after a match left as text, with the nodes each settles on; an update that fails not counting toward the next; two registrations, one making token or segmented entities, in both orders, with the nodes they settle on; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid, made an entity when that node next changes; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one leaves it text until that node changes
 × a token or segmented entity with text after it in its node settles
 × two registrations, one making token or segmented entities, settle
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  17 failed | 24 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.

`packages/lexical-y/src/__tests__/unit/EntityUndoSpread.test.ts` (new, `test.fails`, #9716): two peers; a comment removed by one peer and both peers' undos leave the comment on every block after it. A `@lexical/y` bug (undo restores a mark opener without its closer), on `main` too with this test; this change reaches it more often because entities keep their comments. Pinned so it flips when #9716 is fixed.

`registerLexicalTextEntityJoin.test.ts` (new, 9 tests): #9704's repro (a list item starting with a commented hashtag joined onto italic text), the same join by backspace and by forward delete, a block before that ends with a tab (all with NodeState), a hashtag with `MarkStateExtension` comment ids joined onto italic text, two hashtags turned into text before italic text with NodeState (the text keeps its format, NodeState and the selection), typing before a hashtag that a segmented and a token entity follow (settles, with a bounded number of `getMatch` calls), and a hashtag after text with 7 and 8 earlier matches (the most `isEntityAfter` checks) that does not grow over the commented italic text after it. With #9686 alone the first three lose the NodeState and the `MarkStateExtension` one loses its comment ids; with #9712 alone the hashtag stays a hashtag, as on `main`; here it becomes text and keeps them.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityJoin.test.ts
      Tests  9 passed (9)
```
zurfyx added a commit that referenced this pull request Nov 2, 2026
…hen they become text

## Description

`registerLexicalTextEntity` turns an entity back into plain text when it stops matching (typing right before a hashtag, deleting its `#`, joining blocks), with `$replaceWithSimpleText`. That built the new `TextNode` from the entity's text, format, style and detail, but not its NodeState, so anything an app keeps on the characters as NodeState (comment ids, suggestion marks, block ids) was dropped (#9703). The same happened in the other direction: plain text with NodeState that becomes an entity gave the entity none.

What carries, and why:

- **Demotion (entity to text): all of the entity's NodeState**, nested and flat, known and not yet parsed (read from JSON), the same value objects (ids compared by identity stay identical). NodeState is the ad-hoc data an app keeps on text; what is inherent to the entity type is its class (its fields) and its mode, which stay with the type, as before. This is what `splitText` already does for the pieces it cuts, and what the code highlighters do since #9503 when a code token becomes text. A key that the plain text type declares with a different `StateConfig` is parsed with the text type's config. Format, style and detail carry as before.
- **Promotion (text to entity): the text's NodeState too**, for every key `createNode` did not set (its state wins), with one exception: text that the entity's own transform splits off its end in the same update (text typed or pasted right after a hashtag). `splitText` gives that piece a copy of the entity's NodeState, but those characters were just inserted, so it is not theirs; an entity made from it keeps the old behaviour (MarkStateExtension relies on telling inserted text from marked text this way). An entity turned into text and back in one update gets its own NodeState back.
- **Token and segmented entities** that text follows are turned into text so that they merge with it and are parsed again. With their NodeState kept they do not merge with text that has other NodeState, and the transforms would make the entity again and turn it back forever (on `main` this already looped whenever that text had NodeState, or another format). So, as in #9686, when the same entity (same parent, text, and place: the first node before it that is no text entity, and the size of the entities between) comes back to be turned into text a third time in one update (or updates batched into one commit), it is kept, with its NodeState. It takes the text its match extends into, as an entity in normal mode does, only when that text would merge with it (the same format, style and detail, and NodeState equal by the rule text nodes merge by: `StateConfig.isEqual`, so two distinct value objects are two values; the selection stays where it was in that text); text with other NodeState or format stays apart, so it neither takes the entity's NodeState nor makes the entity drop its own. Until the third time the transforms run as before. The count belongs to the pending editor state, so an update that fails does not count toward the next.
- The text split off the end of an entity is tracked per editor, for every registration, so an entity of another registration made from it (a keyword typed after a hashtag) does not take the copy either.
- **Core: editing inside a segmented node** replaces it with a plain `TextNode` built from its text, format and style, the same way `$replaceWithSimpleText` did, and dropped its NodeState the same way (and its detail, such as `unmergeable`, which `$replaceWithSimpleText` kept): typing inside it (outside composition, `LexicalSelection`) and removing a range that starts or ends inside it (`LexicalCaretUtils`). Both now keep them (detail, and NodeState with `$carryNodeState`, internal to `lexical`, with the same rules as above; keys the new node already holds, such as state a registered `TextNode` replacement sets, keep their value). Segmented entities that this change keeps or makes reach these paths more often, so they are fixed here.

Two text nodes merge only when their NodeState is equal, so text that keeps an entity's NodeState stays its own node next to text with other NodeState, as text in another format does. That is the point (the characters keep their comment), and it is the only structural difference: text with no NodeState merges as before.

Callers in this repo, all through `registerLexicalTextEntity`: `@lexical/hashtag` (`registerLexicalHashtag`, `HashtagExtension`, `LexicalHashtagPlugin`), `@lexical/react` `useLexicalTextEntity`, and the playground's `KeywordsExtension`. Mentions (typeahead) and `@lexical/link` autolink have their own transforms and do not share this path.

Fixes #9703

## Test plan

`.changeset/lexical-text-entity-node-state.md` (`lexical` and `@lexical/text`, patch, `[fix]`).

`LexicalSelection.test.ts` (3 tests, each fails on `main`): typing inside a segmented node keeps its NodeState and detail; a registered `TextNode` replacement keeps the NodeState it sets and takes the rest; removing a range from inside a segmented node to the next block keeps the NodeState and detail of what is left.

`registerLexicalTextEntityNodeState.test.ts` (new, 23 tests): #9703's repro; deleting a hashtag's `#`; merging into neighbours with the same NodeState and, without NodeState, merging as before; promotion with NodeState, `createNode`'s state winning, and text inserted after a hashtag not taking the copy; a reference-typed value carried as the same object; NodeState read from JSON and not parsed yet; NodeState declared in the entity's `$config` (nested and flat) and exported nested on the text; a key the text type declares with another config; a hashtag inside a MarkNode; undo and redo; and every path that turns an entity into text: the text transform with no match, a token entity that text follows (same NodeState: it takes the text; other NodeState: kept, the text stays apart; the selection after pasted text; text with its own NodeState, which loops on `main`; a value object distinct from the text's, equal as JSON), the next entity that stops matching, adjacent entities, an entity followed by another, and a join across text in another format.

`registerLexicalTextEntityNodeStateProperty.test.ts` (new): random layouts of entities (hashtags, and token mentions) and text in several formats, with NodeState on both and MarkNodes around some, and random edits (typing, backspace and delete, joining blocks, removing ranges across nodes). Each entity has a letter no other text has; after every edit that turns an entity into text, every character of it that is still there has the entity's NodeState and MarkNode (or sits inside another entity that grew over it, which takes characters with its own NodeState, as before). 200 seeds per matcher (`TEXT_ENTITY_NODE_STATE_SEEDS`).

Before (main):

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeState.test.ts packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 × #9703: a hashtag that stops matching keeps its NodeState when it becomes text (typing before it)
 × deleting the # of a hashtag keeps its NodeState (reverse transform, no match)
 × a hashtag that becomes text merges into neighbors with the same NodeState
 × plain text with NodeState that becomes a hashtag keeps it
 × state that createNode gives the entity wins over the text it replaces
 × a reference-typed state value is carried as the same object
 × NodeState read from JSON (not parsed yet) is carried and exported
 × NodeState declared in the entity $config (nested and flat) is carried to the text
 × a key the plain text type declares with another config is parsed with that config
 × a hashtag inside a MarkNode stays inside it when it becomes text
 × undo and redo of a demotion restore the entity and the text with their NodeState
 × the text transform: the entity before it stops matching with its text (prevMatch null)
 × with the same NodeState as the text: it keeps it and takes the text
 × with other NodeState than the text: kept with its NodeState, the text stays apart
 × typed text after it: the selection stays after the typed text
 × with other NodeState than text that its match extends into, which keeps it apart: kept, each keeps its own
 × with a value object distinct from the text after it, equal as JSON: each keeps its own
 × the text transform: the entity after it stops matching (nextMatch null)
 × the reverse transform: adjacent entities both become text
 × the reverse transform: an entity followed by an entity
 × hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 × mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
      Tests  22 failed | 3 passed (25)
```

After:

```
$ pnpm exec vitest run --project unit packages/lexical-text packages/lexical-hashtag packages/lexical-mark packages/lexical-history
      Tests  1376 passed (1376)
$ pnpm exec vitest run --project unit packages/lexical/src/__tests__/unit/LexicalSelection.test.ts
      Tests  103 passed (103)
$ TEXT_ENTITY_NODE_STATE_SEEDS=20000 pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 ✓ hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 ✓ mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
```

The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts` (JSON, selection, mutations and keys after random sessions, against the helper before #9483, which has no NodeState in its layouts), and `MarkStateExtension`'s hashtag tests (text inserted after a marked hashtag is not marked).

A differential against `main` over random editing sessions (typing, backspace and delete, joins across paragraphs, headings, quotes, list items and table cells, ranges, formatting, paste with NodeState, undo and redo, IME composition) with hashtags, a keyword matcher, a token and a segmented matcher, autolink and MarkNodes: the NodeState of an entity's characters is kept in tens of thousands of steps where `main` drops it; no text is lost; token and segmented entities that loop on `main` settle. Where NodeState now keeps text apart, the entities can differ from `main`, as they do next to text in another format; a keyword-style matcher can then reach #9637's loop in a layout `main` merged (a few per 10,000 sessions; hashtags never do), which #9637's fix settles.
zurfyx added a commit that referenced this pull request Nov 2, 2026
…ttle between text nodes of other formats (re-land of #9686)

This re-lands #9686, reverted by #9702 because it turns more entities back into text (every edit that leaves a hashtag after text in another format), and turning an entity into text dropped its NodeState (#9703): joining a block that starts with a commented hashtag onto text in another format lost the comment (#9704). #9712 fixes #9703 (entities keep their NodeState when they become text) and brings the loop breaker for token and segmented entities; this PR is #9686 on top of it, unchanged otherwise, plus #9704's case and the long-session join as tests. Review the second commit; the first is #9712.

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the entity after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. In that early return the transform now turns such an entity back into text unless the transforms would keep it there (below). A match left as text at the start of the next node stays text until that node is edited, as before.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node for each pending editor state (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity with text after it in its node looped on `main`: the transform of that text turns it back into text, the text merges, and the transform makes it again. #9712 keeps such an entity the third time it comes back to be turned into text in one update, and this PR keeps that rule.

Two more changes come with the re-land, for orders of transforms that #9686 alone reached before the other checks:

- A normal-mode entity grows over the text after it (takes the characters its match extends into) only when it is an entity the transforms keep: not right after another text entity (the two are turned back into text by the rule for adjacent entities), and not after a simple text node that it is no entity after (`isEntityAfter`; when that cannot tell, after too many matches in the text before, it stays as it is without growing, and the text after it is parsed as usual). Otherwise it is turned back into text first, rather than grown and then turned into text with the next node's characters, which then lost their format and NodeState (two hashtags next to each other before italic text, a `createNode` that changes the case).
- Two entities next to each other are turned back into text, as before, but when the same entity comes back to that a third time in one update it is kept (the same counter as the token breaker, counted apart): entities of two registrations that touch (a segmented `$B` next to a token `@x`) looped forever on `main`; now they settle, kept as they are.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Two registrations whose entities end up touching can still loop through the rule that turns two adjacent entities into text (with a hashtag and a segmented upper-case matcher, `#xabDOG` loops on `main` from a fresh parse); with one registration, or matches that cannot touch, this does not arise. The source text of an entity is told by format, style and detail; NodeState is not compared (text keeps its NodeState when it becomes an entity, but `createNode` may give the entity its own).

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637
Closes #9704

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

`registerLexicalTextEntitySettle.test.ts` (new, 41 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token or segmented entity with text after it in its node (loops on `main`), and one after a match left as text, with the nodes each settles on; an update that fails not counting toward the next; two registrations, one making token or segmented entities, in both orders, with the nodes they settle on; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid, made an entity when that node next changes; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one leaves it text until that node changes
 × a token or segmented entity with text after it in its node settles
 × two registrations, one making token or segmented entities, settle
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  17 failed | 24 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.

`packages/lexical-y/src/__tests__/unit/EntityUndoSpread.test.ts` (new, `test.fails`, #9716): two peers; a comment removed by one peer and both peers' undos leave the comment on every block after it. A `@lexical/y` bug (undo restores a mark opener without its closer), on `main` too with this test; this change reaches it more often because entities keep their comments. Pinned so it flips when #9716 is fixed.

`registerLexicalTextEntityJoin.test.ts` (new, 11 tests): #9704's repro (a list item starting with a commented hashtag joined onto italic text), the same join by backspace and by forward delete, a block before that ends with a tab (all with NodeState), a hashtag with `MarkStateExtension` comment ids joined onto italic text, two hashtags turned into text before italic text with NodeState (the text keeps its format, NodeState and the selection), typing before a hashtag that a segmented and a token entity follow (settles, with a bounded number of `getMatch` calls), and a hashtag after text with 7 and 8 earlier matches (the most `isEntityAfter` checks) that does not grow over the commented italic text after it, while a later hashtag in that text is still made. With #9686 alone the first three lose the NodeState and the `MarkStateExtension` one loses its comment ids; with #9712 alone the hashtag stays a hashtag, as on `main`; here it becomes text and keeps them.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityJoin.test.ts
      Tests  11 passed (11)
```
zurfyx added a commit that referenced this pull request Nov 2, 2026
…ir NodeState when they become text

## Description

`registerLexicalTextEntity` turns an entity back into plain text when it stops matching (typing right before a hashtag, deleting its `#`, joining blocks), with `$replaceWithSimpleText`. That built the new `TextNode` from the entity's text, format, style and detail, but not its NodeState, so anything an app keeps on the characters as NodeState (comment ids, suggestion marks, block ids) was dropped (#9703). The same happened in the other direction: plain text with NodeState that becomes an entity gave the entity none.

What carries, and why:

- **Demotion (entity to text): all of the entity's NodeState**, nested and flat, known and not yet parsed (read from JSON), the same value objects (ids compared by identity stay identical). NodeState is the ad-hoc data an app keeps on text; what is inherent to the entity type is its class (its fields) and its mode, which stay with the type, as before. This is what `splitText` already does for the pieces it cuts, and what the code highlighters do since #9503 when a code token becomes text. A key that the plain text type declares with a different `StateConfig` is parsed with the text type's config. Format, style and detail carry as before.
- **Promotion (text to entity): the text's NodeState too**, for every key `createNode` did not set (its state wins), with one exception: text that the entity's own transform splits off its end in the same update (text typed or pasted right after a hashtag). `splitText` gives that piece a copy of the entity's NodeState, but those characters were just inserted, so it is not theirs; an entity made from it keeps the old behaviour (MarkStateExtension relies on telling inserted text from marked text this way). An entity turned into text and back in one update gets its own NodeState back.
- **Token and segmented entities** that text follows are turned into text so that they merge with it and are parsed again. With their NodeState kept they do not merge with text that has other NodeState, and the transforms would make the entity again and turn it back forever (on `main` this already looped whenever that text had NodeState, or another format). So, as in #9686, when the same entity (same parent, text, and place: the first node before it that is no text entity, and the size of the entities between) comes back to be turned into text a third time in one update (or updates batched into one commit), it is kept, with its NodeState. It takes the text its match extends into, as an entity in normal mode does, only when that text would merge with it (the same format, style and detail, and NodeState equal by the rule text nodes merge by: `StateConfig.isEqual`, so two distinct value objects are two values; the selection stays where it was in that text); text with other NodeState or format stays apart, so it neither takes the entity's NodeState nor makes the entity drop its own. Until the third time the transforms run as before. The count belongs to the pending editor state, so an update that fails does not count toward the next.
- The text split off the end of an entity is tracked per editor, for every registration, so an entity of another registration made from it (a keyword typed after a hashtag) does not take the copy either.
- **`MarkStateExtension`**: text inserted at the edge of a comment lands in the commented node, and an entity transform that makes a hashtag of it (pasting ` #tag` right after a comment) splits it off with a copy of that node's comment ids, which the hashtag now keeps. `MarkStateExtension` already treats such a copy as not the inserted text's own when the piece is plain text (`$isSplitOffEntity`); it now does the same for a text entity made in the update whose ids are the same value as the ids of a node that existed (a copy, never ids of its own: pasted or inserted content brings its own value), so the hashtag is not added to the comment, as on `main` and with `MarkNode`.
- **Core: editing inside a segmented node** replaces it with a plain `TextNode` built from its text, format and style, the same way `$replaceWithSimpleText` did, and dropped its NodeState the same way (and its detail, such as `unmergeable`, which `$replaceWithSimpleText` kept): typing inside it (outside composition, `LexicalSelection`) and removing a range that starts or ends inside it (`LexicalCaretUtils`). Both now keep them (detail, and NodeState with `$carryNodeState`, internal to `lexical`, with the same rules as above; keys the new node already holds, such as state a registered `TextNode` replacement sets, keep their value). Segmented entities that this change keeps or makes reach these paths more often, so they are fixed here.

Two text nodes merge only when their NodeState is equal, so text that keeps an entity's NodeState stays its own node next to text with other NodeState, as text in another format does. That is the point (the characters keep their comment), and it is the only structural difference: text with no NodeState merges as before.

Callers in this repo, all through `registerLexicalTextEntity`: `@lexical/hashtag` (`registerLexicalHashtag`, `HashtagExtension`, `LexicalHashtagPlugin`), `@lexical/react` `useLexicalTextEntity`, and the playground's `KeywordsExtension`. Mentions (typeahead) and `@lexical/link` autolink have their own transforms and do not share this path.

Fixes #9703

## Test plan

`.changeset/lexical-text-entity-node-state.md` (`lexical` and `@lexical/text`, patch, `[fix]`).

`MarkStateExtension.test.ts`: a hashtag pasted or inserted at the edge of a comment (after it, and before it) is not added to it, with `MarkStateExtension` and with `MarkNode`.

`LexicalSelection.test.ts` (3 tests, each fails on `main`): typing inside a segmented node keeps its NodeState and detail; a registered `TextNode` replacement keeps the NodeState it sets and takes the rest; removing a range from inside a segmented node to the next block keeps the NodeState and detail of what is left.

`registerLexicalTextEntityNodeState.test.ts` (new, 23 tests): #9703's repro; deleting a hashtag's `#`; merging into neighbours with the same NodeState and, without NodeState, merging as before; promotion with NodeState, `createNode`'s state winning, and text inserted after a hashtag not taking the copy; a reference-typed value carried as the same object; NodeState read from JSON and not parsed yet; NodeState declared in the entity's `$config` (nested and flat) and exported nested on the text; a key the text type declares with another config; a hashtag inside a MarkNode; undo and redo; and every path that turns an entity into text: the text transform with no match, a token entity that text follows (same NodeState: it takes the text; other NodeState: kept, the text stays apart; the selection after pasted text; text with its own NodeState, which loops on `main`; a value object distinct from the text's, equal as JSON), the next entity that stops matching, adjacent entities, an entity followed by another, and a join across text in another format.

`registerLexicalTextEntityNodeStateProperty.test.ts` (new): random layouts of entities (hashtags, and token mentions) and text in several formats, with NodeState on both and MarkNodes around some, and random edits (typing, backspace and delete, joining blocks, removing ranges across nodes). Each entity has a letter no other text has; after every edit that turns an entity into text, every character of it that is still there has the entity's NodeState and MarkNode (or sits inside another entity that grew over it, which takes characters with its own NodeState, as before). 200 seeds per matcher (`TEXT_ENTITY_NODE_STATE_SEEDS`).

Before (main):

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeState.test.ts packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 × #9703: a hashtag that stops matching keeps its NodeState when it becomes text (typing before it)
 × deleting the # of a hashtag keeps its NodeState (reverse transform, no match)
 × a hashtag that becomes text merges into neighbors with the same NodeState
 × plain text with NodeState that becomes a hashtag keeps it
 × state that createNode gives the entity wins over the text it replaces
 × a reference-typed state value is carried as the same object
 × NodeState read from JSON (not parsed yet) is carried and exported
 × NodeState declared in the entity $config (nested and flat) is carried to the text
 × a key the plain text type declares with another config is parsed with that config
 × a hashtag inside a MarkNode stays inside it when it becomes text
 × undo and redo of a demotion restore the entity and the text with their NodeState
 × the text transform: the entity before it stops matching with its text (prevMatch null)
 × with the same NodeState as the text: it keeps it and takes the text
 × with other NodeState than the text: kept with its NodeState, the text stays apart
 × typed text after it: the selection stays after the typed text
 × with other NodeState than text that its match extends into, which keeps it apart: kept, each keeps its own
 × with a value object distinct from the text after it, equal as JSON: each keeps its own
 × the text transform: the entity after it stops matching (nextMatch null)
 × the reverse transform: adjacent entities both become text
 × the reverse transform: an entity followed by an entity
 × hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 × mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
      Tests  22 failed | 3 passed (25)
```

After:

```
$ pnpm exec vitest run --project unit packages/lexical-text packages/lexical-hashtag packages/lexical-mark packages/lexical-history
      Tests  1376 passed (1376)
$ pnpm exec vitest run --project unit packages/lexical/src/__tests__/unit/LexicalSelection.test.ts
      Tests  103 passed (103)
$ TEXT_ENTITY_NODE_STATE_SEEDS=20000 pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 ✓ hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 ✓ mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
```

The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts` (JSON, selection, mutations and keys after random sessions, against the helper before #9483, which has no NodeState in its layouts), and `MarkStateExtension`'s hashtag tests (text inserted after a marked hashtag is not marked).

A differential against `main` over random editing sessions (typing, backspace and delete, joins across paragraphs, headings, quotes, list items and table cells, ranges, formatting, paste with NodeState, undo and redo, IME composition) with hashtags, a keyword matcher, a token and a segmented matcher, autolink and MarkNodes: the NodeState of an entity's characters is kept in tens of thousands of steps where `main` drops it; no text is lost; token and segmented entities that loop on `main` settle. Where NodeState now keeps text apart, the entities can differ from `main`, as they do next to text in another format; a keyword-style matcher can then reach #9637's loop in a layout `main` merged (a few per 10,000 sessions; hashtags never do), which #9637's fix settles.
zurfyx added a commit that referenced this pull request Nov 2, 2026
…ttle between text nodes of other formats (re-land of #9686)

This re-lands #9686, reverted by #9702 because it turns more entities back into text (every edit that leaves a hashtag after text in another format), and turning an entity into text dropped its NodeState (#9703): joining a block that starts with a commented hashtag onto text in another format lost the comment (#9704). #9712 fixes #9703 (entities keep their NodeState when they become text) and brings the loop breaker for token and segmented entities; this PR is #9686 on top of it, unchanged otherwise, plus #9704's case and the long-session join as tests. Review the second commit; the first is #9712.

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the entity after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. In that early return the transform now turns such an entity back into text unless the transforms would keep it there (below). A match left as text at the start of the next node stays text until that node is edited, as before.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node for each pending editor state (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity with text after it in its node looped on `main`: the transform of that text turns it back into text, the text merges, and the transform makes it again. #9712 keeps such an entity the third time it comes back to be turned into text in one update, and this PR keeps that rule.

Two more changes come with the re-land, for orders of transforms that #9686 alone reached before the other checks:

- A normal-mode entity grows over the text after it (takes the characters its match extends into) only when it is an entity the transforms keep: not right after another text entity (the two are turned back into text by the rule for adjacent entities), and not after a simple text node that it is no entity after (`isEntityAfter`; when that cannot tell, after too many matches in the text before, it stays as it is without growing, and the text after it is parsed as usual). Otherwise it is turned back into text first, rather than grown and then turned into text with the next node's characters, which then lost their format and NodeState (two hashtags next to each other before italic text, a `createNode` that changes the case).
- Two entities next to each other are turned back into text, as before, but when the same entity comes back to that a third time in one update it is kept (the same counter as the token breaker, counted apart): entities of two registrations that touch (a segmented `$B` next to a token `@x`) looped forever on `main`; now they settle, kept as they are.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Two registrations whose entities end up touching can still loop through the rule that turns two adjacent entities into text (with a hashtag and a segmented upper-case matcher, `#xabDOG` loops on `main` from a fresh parse); with one registration, or matches that cannot touch, this does not arise. The source text of an entity is told by format, style and detail; NodeState is not compared (text keeps its NodeState when it becomes an entity, but `createNode` may give the entity its own).

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637
Closes #9704

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

`registerLexicalTextEntitySettle.test.ts` (new, 41 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token or segmented entity with text after it in its node (loops on `main`), and one after a match left as text, with the nodes each settles on; an update that fails not counting toward the next; two registrations, one making token or segmented entities, in both orders, with the nodes they settle on; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid, made an entity when that node next changes; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one leaves it text until that node changes
 × a token or segmented entity with text after it in its node settles
 × two registrations, one making token or segmented entities, settle
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  17 failed | 24 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.

`packages/lexical-y/src/__tests__/unit/EntityUndoSpread.test.ts` (new, `test.fails`, #9716): two peers; a comment removed by one peer and both peers' undos leave the comment on every block after it. A `@lexical/y` bug (undo restores a mark opener without its closer), on `main` too with this test; this change reaches it more often because entities keep their comments. Pinned so it flips when #9716 is fixed.

`registerLexicalTextEntityJoin.test.ts` (new, 11 tests): #9704's repro (a list item starting with a commented hashtag joined onto italic text), the same join by backspace and by forward delete, a block before that ends with a tab (all with NodeState), a hashtag with `MarkStateExtension` comment ids joined onto italic text, two hashtags turned into text before italic text with NodeState (the text keeps its format, NodeState and the selection), typing before a hashtag that a segmented and a token entity follow (settles, with a bounded number of `getMatch` calls), and a hashtag after text with 7 and 8 earlier matches (the most `isEntityAfter` checks) that does not grow over the commented italic text after it, while a later hashtag in that text is still made. With #9686 alone the first three lose the NodeState and the `MarkStateExtension` one loses its comment ids; with #9712 alone the hashtag stays a hashtag, as on `main`; here it becomes text and keeps them.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityJoin.test.ts
      Tests  11 passed (11)
```
zurfyx added a commit that referenced this pull request Nov 2, 2026
…hen they become text

## Description

`registerLexicalTextEntity` turns an entity back into plain text when it stops matching (typing right before a hashtag, deleting its `#`, joining blocks), with `$replaceWithSimpleText`. That built the new `TextNode` from the entity's text, format, style and detail, but not its NodeState, so anything an app keeps on the characters as NodeState (comment ids, suggestion marks, block ids) was dropped (#9703). The same happened in the other direction: plain text with NodeState that becomes an entity gave the entity none.

What carries, and why:

- **Demotion (entity to text): all of the entity's NodeState**, nested and flat, known and not yet parsed (read from JSON), the same value objects (ids compared by identity stay identical). NodeState is the ad-hoc data an app keeps on text; what is inherent to the entity type is its class (its fields) and its mode, which stay with the type, as before. This is what `splitText` already does for the pieces it cuts, and what the code highlighters do since #9503 when a code token becomes text. A key that the plain text type declares with a different `StateConfig` is parsed with the text type's config. Format, style and detail carry as before.
- **Promotion (text to entity): the text's NodeState too**, for every key `createNode` did not set (its state wins), with two exceptions, both characters that were just inserted into a node and so only have a copy of its NodeState: text that the entity's own transform splits off its end in the same update (text typed or pasted right after a hashtag), and an entity made only of text inserted at the start or the end of a node that existed in this update, when no entity was turned into text in that block in the update (pasting ` #tag` right after commented text: the hashtag does not join the comment, as on `main` and as `MarkStateExtension`'s rule for text inserted at a comment's edge says). `splitText` gives that piece a copy of the entity's NodeState, but those characters were just inserted, so it is not theirs; an entity made from it keeps the old behaviour (MarkStateExtension relies on telling inserted text from marked text this way). An entity turned into text and back in one update gets its own NodeState back.
- **Token and segmented entities** that text follows are turned into text so that they merge with it and are parsed again. With their NodeState kept they do not merge with text that has other NodeState, and the transforms would make the entity again and turn it back forever (on `main` this already looped whenever that text had NodeState, or another format). So, as in #9686, when the same entity (same parent, text, and place: the first node before it that is no text entity, and the size of the entities between) comes back to be turned into text a third time in one update (or updates batched into one commit), it is kept, with its NodeState. It takes the text its match extends into, as an entity in normal mode does, only when that text would merge with it (the same format, style and detail, and NodeState equal by the rule text nodes merge by: `StateConfig.isEqual`, so two distinct value objects are two values; the selection stays where it was in that text); text with other NodeState or format stays apart, so it neither takes the entity's NodeState nor makes the entity drop its own. Until the third time the transforms run as before. The count belongs to the pending editor state, so an update that fails does not count toward the next.
- The text split off the end of an entity is tracked per editor, for every registration, so an entity of another registration made from it (a keyword typed after a hashtag) does not take the copy either.
- **Core: editing inside a segmented node** replaces it with a plain `TextNode` built from its text, format and style, the same way `$replaceWithSimpleText` did, and dropped its NodeState the same way (and its detail, such as `unmergeable`, which `$replaceWithSimpleText` kept): typing inside it (outside composition, `LexicalSelection`) and removing a range that starts or ends inside it (`LexicalCaretUtils`). Both now keep them (detail, and NodeState with `$carryNodeState`, internal to `lexical`, with the same rules as above; keys the new node already holds, such as state a registered `TextNode` replacement sets, keep their value). Segmented entities that this change keeps or makes reach these paths more often, so they are fixed here.

Two text nodes merge only when their NodeState is equal, so text that keeps an entity's NodeState stays its own node next to text with other NodeState, as text in another format does. That is the point (the characters keep their comment), and it is the only structural difference: text with no NodeState merges as before.

Callers in this repo, all through `registerLexicalTextEntity`: `@lexical/hashtag` (`registerLexicalHashtag`, `HashtagExtension`, `LexicalHashtagPlugin`), `@lexical/react` `useLexicalTextEntity`, and the playground's `KeywordsExtension`. Mentions (typeahead) and `@lexical/link` autolink have their own transforms and do not share this path.

Fixes #9703

## Test plan

`.changeset/lexical-text-entity-node-state.md` (`lexical` and `@lexical/text`, patch, `[fix]`).

`MarkStateExtension.test.ts`: a hashtag pasted or inserted at the edge of a comment (after it, and before it) is not added to it, with `MarkStateExtension` and with `MarkNode` (as on `main`).

`LexicalSelection.test.ts` (3 tests, each fails on `main`): typing inside a segmented node keeps its NodeState and detail; a registered `TextNode` replacement keeps the NodeState it sets and takes the rest; removing a range from inside a segmented node to the next block keeps the NodeState and detail of what is left.

`registerLexicalTextEntityNodeState.test.ts` (new, 24 tests): #9703's repro; deleting a hashtag's `#`; merging into neighbours with the same NodeState and, without NodeState, merging as before; promotion with NodeState, `createNode`'s state winning, text inserted after a hashtag not taking the copy, and a hashtag made only of inserted text not taking it; a reference-typed value carried as the same object; NodeState read from JSON and not parsed yet; NodeState declared in the entity's `$config` (nested and flat) and exported nested on the text; a key the text type declares with another config; a hashtag inside a MarkNode; undo and redo; and every path that turns an entity into text: the text transform with no match, a token entity that text follows (same NodeState: it takes the text; other NodeState: kept, the text stays apart; the selection after pasted text; text with its own NodeState, which loops on `main`; a value object distinct from the text's, equal as JSON), the next entity that stops matching, adjacent entities, an entity followed by another, and a join across text in another format.

`registerLexicalTextEntityNodeStateProperty.test.ts` (new): random layouts of entities (hashtags, and token mentions) and text in several formats, with NodeState on both and MarkNodes around some, and random edits (typing, backspace and delete, joining blocks, removing ranges across nodes). Each entity has a letter no other text has; after every edit that turns an entity into text, every character of it that is still there has the entity's NodeState and MarkNode (or sits inside another entity that grew over it, which takes characters with its own NodeState, as before). 200 seeds per matcher (`TEXT_ENTITY_NODE_STATE_SEEDS`).

Before (main):

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeState.test.ts packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 × #9703: a hashtag that stops matching keeps its NodeState when it becomes text (typing before it)
 × deleting the # of a hashtag keeps its NodeState (reverse transform, no match)
 × a hashtag that becomes text merges into neighbors with the same NodeState
 × plain text with NodeState that becomes a hashtag keeps it
 × state that createNode gives the entity wins over the text it replaces
 × a reference-typed state value is carried as the same object
 × NodeState read from JSON (not parsed yet) is carried and exported
 × NodeState declared in the entity $config (nested and flat) is carried to the text
 × a key the plain text type declares with another config is parsed with that config
 × a hashtag inside a MarkNode stays inside it when it becomes text
 × undo and redo of a demotion restore the entity and the text with their NodeState
 × the text transform: the entity before it stops matching with its text (prevMatch null)
 × with the same NodeState as the text: it keeps it and takes the text
 × with other NodeState than the text: kept with its NodeState, the text stays apart
 × typed text after it: the selection stays after the typed text
 × with other NodeState than text that its match extends into, which keeps it apart: kept, each keeps its own
 × with a value object distinct from the text after it, equal as JSON: each keeps its own
 × the text transform: the entity after it stops matching (nextMatch null)
 × the reverse transform: adjacent entities both become text
 × the reverse transform: an entity followed by an entity
 × hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 × mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
      Tests  22 failed | 3 passed (25)
```

After:

```
$ pnpm exec vitest run --project unit packages/lexical-text packages/lexical-hashtag packages/lexical-mark packages/lexical-history
      Tests  1378 passed (1378)
$ pnpm exec vitest run --project unit packages/lexical/src/__tests__/unit/LexicalSelection.test.ts
      Tests  103 passed (103)
$ TEXT_ENTITY_NODE_STATE_SEEDS=20000 pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityNodeStateProperty.test.ts
 ✓ hashtag: an edit that turns an entity into text keeps its NodeState and marks on its characters
 ✓ mention: an edit that turns an entity into text keeps its NodeState and marks on its characters
```

The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts` (JSON, selection, mutations and keys after random sessions, against the helper before #9483, which has no NodeState in its layouts), and `MarkStateExtension`'s hashtag tests (text inserted after a marked hashtag is not marked).

A differential against `main` over random editing sessions (typing, backspace and delete, joins across paragraphs, headings, quotes, list items and table cells, ranges, formatting, paste with NodeState, undo and redo, IME composition) with hashtags, a keyword matcher, a token and a segmented matcher, autolink and MarkNodes: the NodeState of an entity's characters is kept in tens of thousands of steps where `main` drops it; no text is lost; token and segmented entities that loop on `main` settle. Where NodeState now keeps text apart, the entities can differ from `main`, as they do next to text in another format; a keyword-style matcher can then reach #9637's loop in a layout `main` merged (a few per 10,000 sessions; hashtags never do), which #9637's fix settles.
zurfyx added a commit that referenced this pull request Nov 2, 2026
…ttle between text nodes of other formats (re-land of #9686)

This re-lands #9686, reverted by #9702 because it turns more entities back into text (every edit that leaves a hashtag after text in another format), and turning an entity into text dropped its NodeState (#9703): joining a block that starts with a commented hashtag onto text in another format lost the comment (#9704). #9712 fixes #9703 (entities keep their NodeState when they become text) and brings the loop breaker for token and segmented entities; this PR is #9686 on top of it, unchanged otherwise, plus #9704's case and the long-session join as tests. Review the second commit; the first is #9712.

`registerLexicalTextEntity` decides whether a match at the start of a text node is an entity in two places that disagreed:

- the node's own transform made the first match in its own text an entity, without the text node before it;
- the transform of a simple text node before it checks the entity that follows it with its own text in front, and turns it back into text when that is no match.

Take plain text between two text nodes of another format (bold `x`, `dogcat`, bold `y`) and type a space after `dog`. The first transform made `dog` an entity, the second undid it (`xdog` has no match), the nodes merged again, and so on until Lexical threw "One or more transforms are endlessly triggering additional transforms" and the edit was lost (#9637).

Now a match at the start of a node is left as text when the simple text node before it would turn it straight back into text, that is when the match's text after that node's text is no match (`getMatch(previousText + matchText) === null`, the check that node's transform makes). It is still made an entity when the first match of both nodes' text is this match, which a match that depends on the text after it can be (the entity `createNode` makes may then match on its own). Any match right after one left as text stays text too, as it does after a match next to another entity. The match is still found in the node's own text, so the entity's own transform agrees as before, and the extra check is one `getMatch` call (the first match is already computed), made only for a match at the start of a node after a simple text node.

Where that transform returned early, before its loop (its text has no match after the text node before it, or the first starts in that node), it never checked the entity after it, as its loop does. So typing a letter right before a hashtag that follows text in another format left the hashtag an entity (`b#tag`), which a fresh parse of the same text does not make. In that early return the transform now turns such an entity back into text unless the transforms would keep it there (below). A match left as text at the start of the next node stays text until that node is edited, as before.

Whether the transforms keep an entity right after a simple text node is one rule, used by both transforms and by the loop's check of the next node: the first match from that node's text on, passing the matches of its text (at most 8, as the loop passes over them; past that, nothing is changed), must start where the entity does, with the text after the entity in the node it was split from (the simple text node after it in the same format, style and detail), as the check that made it an entity found it; and that match must still hold with the text of any other text node after it, as the check that keeps a match at the end of a node from becoming an entity found it. So a match that needs the text after it (`/#[a-z]+(?= )/`) is kept where it was made, and text after it in another format, or a token node, does not keep an entity that a fresh parse would not make.

An entity's own transform, when the transforms no longer keep it after the simple text node before it (the text after it changed, say), marks that node dirty, once per node for each pending editor state (other entities' transforms may leave it dirty again), so it is turned back into text in the same update rather than at the next edit of the text before it.

A token or segmented entity with text after it in its node looped on `main`: the transform of that text turns it back into text, the text merges, and the transform makes it again. #9712 keeps such an entity the third time it comes back to be turned into text in one update, and this PR keeps that rule.

Two more changes come with the re-land, for orders of transforms that #9686 alone reached before the other checks:

- A normal-mode entity grows over the text after it (takes the characters its match extends into) only when it is an entity the transforms keep: not right after another text entity (the two are turned back into text by the rule for adjacent entities), and not after a simple text node that it is no entity after (`isEntityAfter`; when that cannot tell, after too many matches in the text before, it stays as it is without growing, and the text after it is parsed as usual). Otherwise it is turned back into text first, rather than grown and then turned into text with the next node's characters, which then lost their format and NodeState (two hashtags next to each other before italic text, a `createNode` that changes the case).
- Two entities next to each other are turned back into text, as before, but when the same entity comes back to that a third time in one update it is kept (the same counter as the token breaker, counted apart): entities of two registrations that touch (a segmented `$B` next to a token `@x`) looped forever on `main`; now they settle, kept as they are.

Each of these checks is a few `getMatch` calls, bounded by the 8 matches passed over, only on the paths above.

A matcher whose match does not hold on the entity's own text (`/dog(?=cat)/`, with `createNode` keeping the text) still loops, on plain text too, as on `main`: the entity's own transform turns it back into text every time. `registerLexicalTextEntity` needs an entity's text to be a match on its own. Two registrations whose entities end up touching can still loop through the rule that turns two adjacent entities into text (with a hashtag and a segmented upper-case matcher, `#xabDOG` loops on `main` from a fresh parse); with one registration, or matches that cannot touch, this does not arise. The source text of an entity is told by format, style and detail; NodeState is not compared (text keeps its NodeState when it becomes an entity, but `createNode` may give the entity its own).

No API is added or changed. Entities can differ from before only next to the boundary between a simple text node and the node after it, where one of the checks above was skipped or disagreed with the other.

Closes #9637
Closes #9704

`.changeset/lexical-text-entity-settle.md` (`@lexical/text`, patch, `[fix]`).

`registerLexicalTextEntitySettle.test.ts` (new, 41 tests): the issue's two cases and its controls; a later match in the node; a lookbehind-style matcher; a hashtag-like matcher after a formatted letter and a formatted space; two entities next to each other; `dogcat` between every pair of four formats and four neighbour texts, with four typed characters; IME composition; pasting formatted runs; bolding the text before an entity; typing a letter before an entity after text in another format; a matcher whose earlier match must end where the text ends; matchers that need the text after a match (with a match at the start of a node, after typing, and after removing the text after an entity), one with a `createNode` that upper-cases; text after an entity in another format; removing the text between an entity and a token after it; a token or segmented entity with text after it in its node (loops on `main`), and one after a match left as text, with the nodes each settles on; an update that fails not counting toward the next; two registrations, one making token or segmented entities, in both orders, with the nodes they settle on; text after an entity with another detail; an entity after a node with an earlier match; an entity checked again in a later update; one dirty node per keystroke in text before a formatted hashtag, with and without an earlier match in that text (so history merges typing, as on `main`); a match at the start of the next node that typing makes valid, made an entity when that node next changes; `createNode` factories that lower-case, upper-case or add text; and a bounded number of `getMatch` calls per keystroke in a long node before an entity, however long. Then, for three matchers, 2,000 random layouts each (override with `TEXT_ENTITY_SETTLE_SEEDS`):
- a fresh parse settles, and transforming every node again leaves it as it is;
- typing or deleting anywhere in text between two nodes of other formats (#9637's shape) settles on the entities a fresh parse of the result finds;
- sessions of 4 edits (typing or deleting anywhere, bolding a node) settle, and so does transforming every node again after each (a guard against new loops: these pass on `main` too).

`Hashtags.spec.mjs`: the playground's keywords, bold `x`, `congratscongrats`, bold `y`, and a space after the first `congrats` (the loop on `main`: it needs a later match that the next node turns back into text, which a hashtag cannot be, since text after a hashtag only extends it); and hashtags typed after a bold letter and between bold text, with the exact text.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
 × bold "x" | "dogcat" | bold "y", a space after "dog": settles, no entity
 × bold "x" | "dogcat" | bold "dog", an emoji after "dog": settles, no entity
 × entity at the edges of a node, every format combination around it
 × IME composition between two formatted nodes settles, and so does its end
 × typing a letter before an entity after text in another format turns it back into text
 × a match at the start of a node that needs the text after it settles after a formatted node
 × an entity after a node with an earlier match is checked too
 × removing the text between an entity and a token after it turns the entity back into text
 × an entity is checked again in each update
 × typing that makes a match at the start of the next node one leaves it text until that node changes
 × a token or segmented entity with text after it in its node settles
 × two registrations, one making token or segmented entities, settle
 × keyword: 2000 random layouts parse, and parse again the same
 × keyword: 2000 random edits between two formatted nodes settle on a fresh parse
 × tag: 2000 random edits between two formatted nodes settle on a fresh parse
 × lookbehind: 2000 random layouts parse, and parse again the same
 × lookbehind: 2000 random edits between two formatted nodes settle on a fresh parse
+   "seed 231 [\"_😀gdo\",\"*cat dog\",\"docatdog \",\"_gdo!g\"]: Error: One or more transforms are endlessly triggering additional transforms. Ma",
+   "seed 956 [\"_😀\",\"cat \",\"*_y\"] [[\"text\",\"😀\",2],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] type \"d\" at 1:0: [[\"text\",\"😀\",2],[\"text\",\"d\",0],[\"test-keyword\",\"cat\",0],[\"text\",\" \",0],[\"text\",\"y\",3]] != [[\
      Tests  17 failed | 24 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459
Error: One or more transforms are endlessly triggering additional transforms. May have encountered infinite recursion caused by transforms that have their preconditions too lose and/or conflict with each other.
```

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SETTLE_SEEDS=60000 pnpm exec vitest run --project unit --testTimeout=0 packages/lexical-text/src/__tests__/unit/registerLexicalTextEntitySettle.test.ts
      Tests  41 passed (41)
$ TEXT_ENTITY_SEED=30449 pnpm exec vitest run --project unit packages/lexical-y/src/__tests__/unit/TextEntityEdits.test.ts   # and 41459, the seeds that found #9637
      Tests  1 passed (1)
$ pnpm run ci-check
Found 0 errors
```

`TextEntityEdits.test.ts` also passes with `TEXT_ENTITY_SEEDS=25000`. The existing `@lexical/text` tests pass unchanged, including `registerLexicalTextEntityEquivalence.test.ts`, which compares the JSON, selection, mutations and keys after 300 random editing sessions with a copy of the helper before #9483.

`packages/lexical-y/src/__tests__/unit/EntityUndoSpread.test.ts` (new, `test.fails`, #9716): two peers; a comment removed by one peer and both peers' undos leave the comment on every block after it. A `@lexical/y` bug (undo restores a mark opener without its closer), on `main` too with this test; this change reaches it more often because entities keep their comments. Pinned so it flips when #9716 is fixed.

`registerLexicalTextEntityJoin.test.ts` (new, 11 tests): #9704's repro (a list item starting with a commented hashtag joined onto italic text), the same join by backspace and by forward delete, a block before that ends with a tab (all with NodeState), a hashtag with `MarkStateExtension` comment ids joined onto italic text, two hashtags turned into text before italic text with NodeState (the text keeps its format, NodeState and the selection), typing before a hashtag that a segmented and a token entity follow (settles, with a bounded number of `getMatch` calls), and a hashtag after text with 7 and 8 earlier matches (the most `isEntityAfter` checks) that does not grow over the commented italic text after it, while a later hashtag in that text is still made. With #9686 alone the first three lose the NodeState and the `MarkStateExtension` one loses its comment ids; with #9712 alone the hashtag stays a hashtag, as on `main`; here it becomes text and keeps them.

```
$ pnpm exec vitest run --project unit packages/lexical-text/src/__tests__/unit/registerLexicalTextEntityJoin.test.ts
      Tests  11 passed (11)
```

This branch was successfully deployed

2 active deployments
Preview – lexical — 02b3c84b Deployed Oct 29, 2026 by vercel[bot]
Preview – lexical-playground — 02b3c84b Deployed Oct 29, 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.

2 participants