Skip to content

[lexical-compiler][lexical-mark][lexical-website] Feature: mark flavors, one MarkStateExtension per kind of annotation - #9496

Merged
zurfyx merged 1 commit into
mainfrom
zurfyx/mark-flavors
Oct 29, 2026
Merged

zurfyx merged 1 commit into
mainfrom
zurfyx/mark-flavors

Conversation

@zurfyx

@zurfyx zurfyx commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Description

Stacked on #9489 (isMarkIDShown), which should land first.

MarkStateExtension maps ids to the text they cover, but nothing says what
an id is: comments, suggestions and highlights all share the markIds key,
the mark theme class and one markIDNodeKeys output, and every app diffs
that output by hand to keep its own thread store in step.

This adds mark flavors to @lexical/mark:

  • defineMarkExtension({name, stateKey, attribute?, className?, overlapClassName?, config?})
    makes one extension per kind of annotation. Each flavor works like
    MarkStateExtension on its own set-valued NodeState key, with its own
    attribute (default data-lexical-<kebab key>), classes, config
    (isMarkIDShown, disabled, markableNodes, exportIDsToHTML), output
    and listeners. Two flavors on the same text never see each other's ids.
  • $addMarkID, $removeMarkID, $getMarkIDsAtSelection, $getNodeMarkIDs
    and $migrateMarkNodesToMarkState take a flavor as an optional first
    argument: $addMarkID(CommentMarks, 'c1', selection). Without one they
    act on MarkStateExtension, as before. The flavor-first form is the first
    overload, so the original signature stays the last one and
    Parameters<typeof $addMarkID> and the like do not change (a type test
    checks this).
  • registerMarkIDsListener(editor, flavor, listener) is called after each
    commit that changes which ids of a flavor are in the document, with
    added, removed, ids and the update's tags, and once right away
    when the document already has ids. When all of a comment's text is
    deleted its id is reported removed (orphan the thread); undo, redo or a
    paste reports it added again. A disabled flavor reports nothing, and
    reports the net change when enabled again.
  • MarkBaseExtension is the shared base every flavor depends on (the name
    MarkExtension is taken by the MarkNode extension). It keeps the editor's
    flavors by key (flavors output; $getMarkIDsByFlavor(node) reads all of
    them), throws at build when two flavors share a key or an attribute, and
    lets the inserted-text rules of the flavors tell a TextNode piece that
    another flavor's rule split off in this update from inserted content.
    Without that, text typed at the edge of a mark of one flavor joined a mark
    of the other (see the Before output).
  • Two flavors that share a class, or a flavor that uses a theme class of
    MarkStateExtension, do not remove it from each other.
  • MarkStateExtension is now the default flavor, on markIds, with the
    same name, config, output, attribute, theme classes and JSON. Its type
    gains stateKey and attribute. MarkStateExtension.test.ts is
    unchanged and passes.

The rules are types where they can be: CommentMarks.stateKey has the type
'commentIds', and a stateKey that is not a literal, is empty, contains
whitespace, or is 'markIds' or '__proto__' is a type error whose message
says how to fix it (the runtime checks the same). Duplicate keys or
attributes in one editor can only be caught at build; that error names both
flavors and the fix.

defineMarkExtension is declared @__NO_SIDE_EFFECTS__ and added to
PURE_FACTORY_FUNCTIONS, so module-scope flavor definitions tree-shake.
Changesets cover @lexical/mark and @lexical/compiler.
Flow declarations and the Flow smoke test cover the new API. The Comments
and Annotations docs get a "Mark flavors" section with a Y.Map thread
store kept in sync through the listener.

Collaboration needs no binding change: a flavor is one more NodeState key
with the same set schema as markIds. @lexical/yjs syncs it as any
NodeState. In @lexical/y (#9400) each flavor is its own s:<stateKey>
format key, and with #9439 its own family stored one key per id, since
#9439 recognizes a set by its schema, not its name. That check is not in
this PR because @lexical/y is not on this branch; see the test plan.

Test plan

Before

The new tests against #9489's branch:

$ npx vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkFlavors.test.ts packages/lexical-mark/src/__tests__/unit/MarkStateProperty.test.ts
 FAIL  |unit| packages/lexical-mark/src/__tests__/unit/MarkFlavors.test.ts
TypeError: defineMarkExtension is not a function
 FAIL  |unit| packages/lexical-mark/src/__tests__/unit/MarkStateProperty.test.ts
TypeError: defineMarkExtension is not a function
 Test Files  2 failed (2)

The two-flavor property run with the shared split records turned off (each
flavor's rule on its own), showing the cross-flavor interference they fix:

$ npx vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkStateProperty.test.ts
   ✓ MarkStateExtension property test (1)
     × 40 random edit sequences of 30 steps keep the ids of every character
+ seed 1: document
+   got:  ("axqb") / ("d" "cmro😃q"[c2,c5] "A"[c2]) / ("alau"[c2]) / ("f"[c2] "ubq")
+   want: ("axqb") / ("d" "cmro😃q"[c2,c5] "A") / ("alau"[c2]) / ("f"[c2] "ubq")

After

$ npx vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkFlavors.test.ts --reporter=verbose
 ✓ two flavors on the same text > each flavor sees only its own ids
 ✓ two flavors on the same text > each flavor renders its own classes and attribute
 ✓ two flavors on the same text > flavors that share a class do not remove it from each other
 ✓ two flavors on the same text > typing at the end of both marks gives the text the ids of both flavors by the same rule
 ✓ two flavors on the same text > typing inside both marks gives the text the ids of both flavors by the same rule
 ✓ two flavors on the same text > typing at the start of both marks gives the text the ids of both flavors by the same rule
 ✓ two flavors on the same text > text typed at the edge of one flavor joins the other where it is inside
 ✓ two flavors on the same text > text replacing a selection gets the ids all of it had, per flavor
 ✓ two flavors on the same text > pasted text keeps the ids of every flavor
 ✓ registerMarkIDsListener > reports added, removed (orphaned) and restored ids across undo and paste
 ✓ registerMarkIDsListener > a collaborator update is tagged
 ✓ registerMarkIDsListener > the first call lists the ids already in the document, and unregistering stops it
 ✓ registerMarkIDsListener > disabling reports nothing, enabling reports the net change
 ✓ registerMarkIDsListener > a listener registered while disabled gets every id when enabled
 ✓ registerMarkIDsListener > a listener that throws on its first call is not registered
 ✓ registerMarkIDsListener > a flavor disabled by another flavor listener in the same commit reports nothing
 ✓ registerMarkIDsListener > an editor without the flavor throws, naming the fix
 ✓ isMarkIDShown per flavor > a flavor filter hides only that flavor
 ✓ the default flavor > MarkStateExtension is the flavor on markIds
 ✓ the default flavor > the functions without a flavor act on MarkStateExtension, beside other flavors
 ✓ the default flavor > $migrateMarkNodesToMarkState can target a flavor
 ✓ serialization > JSON round trips the ids of every flavor
 ✓ serialization > ids of a flavor the editor lacks are kept as unknown state
 ✓ serialization > HTML round trips each flavor through its own attribute
 ✓ errors > two flavors with the same stateKey fail when the editor is built
 ✓ errors > two flavors with the same attribute fail when the editor is built
 ✓ errors > attributes that differ only in case clash
 ✓ errors > stateKey "" is rejected with the fix in the message
 ✓ errors > stateKey "a b" is rejected with the fix in the message
 ✓ errors > stateKey "markIds" is rejected with the fix in the message
 ✓ errors > stateKey "__proto__" is rejected with the fix in the message
 ✓ types > the definition types the flavor
 ✓ types > the original signatures stay the last overloads
 ✓ types > the type rules reject what the runtime rejects

$ MARK_STATE_PROPERTY_SEEDS=3000 MARK_STATE_PROPERTY_STEPS=40 npx vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkStateProperty.test.ts
   ✓ MarkStateExtension property test (1)
     ✓ 3000 random edit sequences of 40 steps keep the ids of every character 54224ms
   ✓ MarkStateExtension property test with two mark flavors (1)
     ✓ 3000 random edit sequences of 40 steps keep the ids of every character 86535ms
      Tests  2 passed (2)

$ npx vitest run --project unit packages/lexical-mark
 Test Files  13 passed (13)
      Tests  852 passed (852)

$ npx vitest run scripts/__tests__/unit/treeShakingSource.test.ts packages/lexical-compiler
 Test Files  7 passed (7)
      Tests  570 passed (570)

$ pnpm run test-unit
 Test Files  413 passed | 1 skipped (414)
      Tests  10208 passed | 4 expected fail | 2 skipped (10214)

$ pnpm run build && pnpm run build-types
(both exit 0)

$ pnpm run ci-check
Found 0 errors

@lexical/y check, not part of this PR: on a copy of the #9400 tree with
this MarkStateExtension.ts, a two-peer test that adds a comment on one
peer and a suggestion plus a default-flavor id on the other, on the same
words, converges with all three, and removing the comment on one peer
leaves the suggestion on both. The Yjs text holds s:markIds,
s:commentIds and s:suggestionIds as separate format keys. The
@lexical/y Slots, EmptyMarks, TypeAtMarkStart, Races, TypeOverSelection
and FormatOnlyChanges suites pass there too (751 tests).

Browser-mode tests and E2E were not run; the DOM checks run 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 5:39pm UTC
lexical-playground Ready Ready Preview Oct 29, 2026 5:39pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label 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@9496

@lexical/a11y

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

@lexical/clipboard

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

@lexical/code

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

@lexical/code-core

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

@lexical/code-prism

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

@lexical/code-shiki

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

@lexical/compiler

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

@lexical/devtools-core

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

@lexical/dragon

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

@lexical/eslint-plugin

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

@lexical/extension

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

@lexical/fast-check

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

@lexical/file

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

@lexical/hashtag

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

@lexical/headless

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

@lexical/history

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

@lexical/html

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

@lexical/internal

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

@lexical/link

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

@lexical/list

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

@lexical/mark

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

@lexical/markdown

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

@lexical/mdast

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

@lexical/offset

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

@lexical/overflow

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

@lexical/plain-text

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

@lexical/react

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

@lexical/rich-text

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

@lexical/selection

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

@lexical/table

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

@lexical/tailwind

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

@lexical/text

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

@lexical/utils

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

@lexical/y

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

@lexical/yjs

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

commit: 7db2405

@emilyhorsman
emilyhorsman self-requested a review October 28, 2026 22:12
@zurfyx
zurfyx force-pushed the zurfyx/mark-state-filter branch from d3b22e8 to 7644510 Compare October 29, 2026 02:12
@zurfyx
zurfyx force-pushed the zurfyx/mark-flavors branch from 305e960 to 1ffb39c Compare October 29, 2026 02:12
@zurfyx
zurfyx force-pushed the zurfyx/mark-state-filter branch from 7644510 to 52c6c99 Compare October 29, 2026 17:18
zurfyx added a commit that referenced this pull request Oct 29, 2026
…e][lexical-y] Bug Fix: Keep comment ids and NodeState when a block change moves or re-creates text

## Description

`MarkStateExtension` gives inserted text its comment ids with a root transform
that compares the blocks around the caret with the previous editor state. That
comparison only covered the sibling blocks of the caret's block and treated a
nested block (the items of a list) as one opaque unit. Text that a block
change moved in from outside it had no counterpart, so it was taken for text
typed into an existing node: it lost its own ids and got the ids shared by
its neighbours. The core keeps the NodeState in these conversions; with the
transform disabled, the ids survive.

- Turning a lone list item into a paragraph, quote or heading
  (`$setBlocksType`) removed every comment id from its text, with or without
  `@lexical/y`.
- Turning a paragraph into a list item that joins the list above it
  (`$insertList`) removed the comment from a link when the item above ended
  with the same text, or gave the moved text the comment of the item above
  (#9490).
- Converting a list item to a highlighted code block, which re-creates the
  text in new nodes, lost its ids too.
- Turning a highlighted code block into a paragraph lost them whenever the
  inserted-text rule could not run (with the code selected, or for pasted
  code): Prism and Shiki replace each code highlight node that leaves a code
  block with `$createTextNode(text)`, which drops its NodeState.

The comparison now:

- keeps the ids of a leaf that existed before the update and kept its text,
  wherever the update moved it, and of the text a moved TextNode kept at each
  end when the same update also typed into it (each run of inserted units
  between kept ones is an insertion, with the ids shared by its neighbours);
- compares a nested block that the update changed by its text, like the
  blocks around it (an unchanged one stays a single unit, so an edit next to
  a large nested list does not read it);
- also compares the following block when it lost its first children, as a
  list does when its first item is converted.

`@lexical/code-prism` and `@lexical/code-shiki` now copy the NodeState of a
code highlight node (flat states included) into the TextNode that replaces
it, keeping any state that a TextNode replacement sets, so any NodeState
survives leaving a code block, not only comment ids.

A node that code moves (rather than creates) into the middle of a marked run
now keeps exactly its own ids instead of taking the run's; `comments.md` says
so, and that block changes keep every id.

Independent of #9489 and #9496. It merges with #9489 without conflicts of its
own (that PR's `comments.md` conflict with main is unrelated). With #9496 it
has two one-line conflicts in `MarkStateExtension.ts`, at the two
`$collectSegments` calls: #9496 adds a `flavor` first argument and this adds
a `$isUnchangedBlock` last one, so both become
`$collectSegments(flavor, windows, points, $isUnchangedBlock)`.

Closes #9490

## Test plan

`MarkStateBlockConversion.test.ts` states the invariant (converting blocks
keeps the ids of every character) over every source and target (paragraph,
heading, quote, code with and without highlighting, bullet, number and check
items, nested items as sources, no list as a target), with links, comments
on part, inside a link, overlapping or on all of the text, neighbouring
lists that the conversion merges with (including text that lines up and
shared ids), the caret or a range at several places, text typed in the same
update, and pasted blocks: 27,072 cases. Each also checks that the
conversion happened. It includes both reports and a comment on code text
turned into a paragraph; the lexical-y test round-trips the list item case
through a Yjs document, and the code-prism and code-shiki tests check that
NodeState (nested and flat) survives leaving a code block.

### Before

New tests against `main`'s sources:

```
$ pnpm exec vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkStateBlockConversion.test.ts packages/lexical-y/src/__tests__/unit/MarkStateBlockType.test.ts packages/lexical-code-prism/src/__tests__/unit/CodePrismLeaveCodeNodeState.test.ts packages/lexical-code-shiki/src/__tests__/unit/CodeShikiLeaveCodeNodeState.test.ts
 FAIL ... > reported cases > a paragraph turned into a list item keeps the comment on a link
-   "a:c0",
-   "b:c0",
-   "c:c0",
-   "d:c0",
+   "a:",
+   "b:",
+   "c:",
+   "d:",
 FAIL ... > MarkStateExtension with @lexical/y > a list item with a comment turned into a paragraph keeps the comment
Expected: "paragraph("alpha "[] "beta"["c1"])"
Received: "paragraph("alpha beta"[])"
 FAIL ... CodePrismLeaveCodeNodeState.test.ts > code highlight nodes turned back into text when they leave a code block keep their NodeState
AssertionError: expected [ [ true, 'note', '' ] ] to deeply equal [ [ true, 'note', 'kept' ] ]
...
 Test Files  4 failed (4)
      Tests  89 failed | 107 passed (196)
```

3,804 of the 27,072 matrix cases fail, for example:

```
"bullet -> paragraph, plain, marks part, alone, selection 17:
  before a: l: p: h: a:  : b:c1 e:c1 t:c1 a:c1  : g: a: m: m: a:  :
  after  a: l: p: h: a:  : b: e: t: a:  : g: a: m: m: a:  :"
```

### After

```
$ pnpm exec vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkStateBlockConversion.test.ts packages/lexical-y/src/__tests__/unit/MarkStateBlockType.test.ts packages/lexical-code-prism/src/__tests__/unit/CodePrismLeaveCodeNodeState.test.ts packages/lexical-code-shiki/src/__tests__/unit/CodeShikiLeaveCodeNodeState.test.ts
 Test Files  4 passed (4)
      Tests  196 passed (196)

$ pnpm exec vitest run --project unit packages/lexical-y packages/lexical-mark/ packages/lexical-list packages/lexical-selection packages/lexical-code packages/lexical-table packages/lexical-rich-text packages/lexical-markdown
 Test Files  250 passed | 2 skipped (252)
      Tests  7251 passed | 16 expected fail | 3 skipped (7270)

$ MARK_STATE_PROPERTY_SEEDS=400 MARK_STATE_PROPERTY_STEPS=120 pnpm exec vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkStateProperty.test.ts
      Tests  1 passed (1)

$ pnpm run ci-check
Found 0 errors
```

Unit tests only (Node/jsdom); no browser or E2E run. Not covered (the
matrix skips them, with the reason): comment ids on part of a code
highlighting token, and marked text that a range conversion merges into a
highlighted code block, which re-tokenizing drops (#9491); and typing inside
a link in the same update that turns it into highlighted code, where the
highlighter puts the caret back at a wrong offset, so the typed text is not
before the caret as the inserted-text rule needs.
@zurfyx
zurfyx force-pushed the zurfyx/mark-flavors branch from 1ffb39c to 02bb4cc Compare October 29, 2026 17:21
…rs, one MarkStateExtension per kind of annotation

Stacked on #9489 (`isMarkIDShown`), which should land first.

`MarkStateExtension` maps ids to the text they cover, but nothing says what
an id is: comments, suggestions and highlights all share the `markIds` key,
the `mark` theme class and one `markIDNodeKeys` output, and every app diffs
that output by hand to keep its own thread store in step.

This adds mark flavors to `@lexical/mark`:

- `defineMarkExtension({name, stateKey, attribute?, className?, overlapClassName?, config?})`
  makes one extension per kind of annotation. Each flavor works like
  `MarkStateExtension` on its own set-valued NodeState key, with its own
  attribute (default `data-lexical-<kebab key>`), classes, config
  (`isMarkIDShown`, `disabled`, `markableNodes`, `exportIDsToHTML`), output
  and listeners. Two flavors on the same text never see each other's ids.
- `$addMarkID`, `$removeMarkID`, `$getMarkIDsAtSelection`, `$getNodeMarkIDs`
  and `$migrateMarkNodesToMarkState` take a flavor as an optional first
  argument: `$addMarkID(CommentMarks, 'c1', selection)`. Without one they
  act on `MarkStateExtension`, as before. The flavor-first form is the first
  overload, so the original signature stays the last one and
  `Parameters<typeof $addMarkID>` and the like do not change (a type test
  checks this).
- `registerMarkIDsListener(editor, flavor, listener)` is called after each
  commit that changes which ids of a flavor are in the document, with
  `added`, `removed`, `ids` and the update's `tags`, and once right away
  when the document already has ids. When all of a comment's text is
  deleted its id is reported removed (orphan the thread); undo, redo or a
  paste reports it added again. A disabled flavor reports nothing, and
  reports the net change when enabled again.
- `MarkBaseExtension` is the shared base every flavor depends on (the name
  `MarkExtension` is taken by the MarkNode extension). It keeps the editor's
  flavors by key (`flavors` output; `$getMarkIDsByFlavor(node)` reads all of
  them), throws at build when two flavors share a key or an attribute, and
  lets the inserted-text rules of the flavors tell a TextNode piece that
  another flavor's rule split off in this update from inserted content.
  Without that, text typed at the edge of a mark of one flavor joined a mark
  of the other (see the Before output).
- Two flavors that share a class, or a flavor that uses a theme class of
  `MarkStateExtension`, do not remove it from each other.
- `MarkStateExtension` is now the default flavor, on `markIds`, with the
  same name, config, output, attribute, theme classes and JSON. Its type
  gains `stateKey` and `attribute`. `MarkStateExtension.test.ts` is
  unchanged and passes.

The rules are types where they can be: `CommentMarks.stateKey` has the type
`'commentIds'`, and a `stateKey` that is not a literal, is empty, contains
whitespace, or is `'markIds'` or `'__proto__'` is a type error whose message
says how to fix it (the runtime checks the same). Duplicate keys or
attributes in one editor can only be caught at build; that error names both
flavors and the fix.

`defineMarkExtension` is declared `@__NO_SIDE_EFFECTS__` and added to
`PURE_FACTORY_FUNCTIONS`, so module-scope flavor definitions tree-shake.
Changesets cover `@lexical/mark` and `@lexical/compiler`.
Flow declarations and the Flow smoke test cover the new API. The Comments
and Annotations docs get a "Mark flavors" section with a `Y.Map` thread
store kept in sync through the listener.

Collaboration needs no binding change: a flavor is one more NodeState key
with the same set schema as `markIds`. `@lexical/yjs` syncs it as any
NodeState. In `@lexical/y` (#9400) each flavor is its own `s:<stateKey>`
format key, and with #9439 its own family stored one key per id, since
this PR because `@lexical/y` is not on this branch; see the test plan.

The new tests against #9489's branch:

```
$ npx vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkFlavors.test.ts packages/lexical-mark/src/__tests__/unit/MarkStateProperty.test.ts
 FAIL  |unit| packages/lexical-mark/src/__tests__/unit/MarkFlavors.test.ts
TypeError: defineMarkExtension is not a function
 FAIL  |unit| packages/lexical-mark/src/__tests__/unit/MarkStateProperty.test.ts
TypeError: defineMarkExtension is not a function
 Test Files  2 failed (2)
```

The two-flavor property run with the shared split records turned off (each
flavor's rule on its own), showing the cross-flavor interference they fix:

```
$ npx vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkStateProperty.test.ts
   ✓ MarkStateExtension property test (1)
     × 40 random edit sequences of 30 steps keep the ids of every character
+ seed 1: document
+   got:  ("axqb") / ("d" "cmro😃q"[c2,c5] "A"[c2]) / ("alau"[c2]) / ("f"[c2] "ubq")
+   want: ("axqb") / ("d" "cmro😃q"[c2,c5] "A") / ("alau"[c2]) / ("f"[c2] "ubq")
```

```
$ npx vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkFlavors.test.ts --reporter=verbose
 ✓ two flavors on the same text > each flavor sees only its own ids
 ✓ two flavors on the same text > each flavor renders its own classes and attribute
 ✓ two flavors on the same text > flavors that share a class do not remove it from each other
 ✓ two flavors on the same text > typing at the end of both marks gives the text the ids of both flavors by the same rule
 ✓ two flavors on the same text > typing inside both marks gives the text the ids of both flavors by the same rule
 ✓ two flavors on the same text > typing at the start of both marks gives the text the ids of both flavors by the same rule
 ✓ two flavors on the same text > text typed at the edge of one flavor joins the other where it is inside
 ✓ two flavors on the same text > text replacing a selection gets the ids all of it had, per flavor
 ✓ two flavors on the same text > pasted text keeps the ids of every flavor
 ✓ registerMarkIDsListener > reports added, removed (orphaned) and restored ids across undo and paste
 ✓ registerMarkIDsListener > a collaborator update is tagged
 ✓ registerMarkIDsListener > the first call lists the ids already in the document, and unregistering stops it
 ✓ registerMarkIDsListener > disabling reports nothing, enabling reports the net change
 ✓ registerMarkIDsListener > a listener registered while disabled gets every id when enabled
 ✓ registerMarkIDsListener > a listener that throws on its first call is not registered
 ✓ registerMarkIDsListener > a flavor disabled by another flavor listener in the same commit reports nothing
 ✓ registerMarkIDsListener > an editor without the flavor throws, naming the fix
 ✓ isMarkIDShown per flavor > a flavor filter hides only that flavor
 ✓ the default flavor > MarkStateExtension is the flavor on markIds
 ✓ the default flavor > the functions without a flavor act on MarkStateExtension, beside other flavors
 ✓ the default flavor > $migrateMarkNodesToMarkState can target a flavor
 ✓ serialization > JSON round trips the ids of every flavor
 ✓ serialization > ids of a flavor the editor lacks are kept as unknown state
 ✓ serialization > HTML round trips each flavor through its own attribute
 ✓ errors > two flavors with the same stateKey fail when the editor is built
 ✓ errors > two flavors with the same attribute fail when the editor is built
 ✓ errors > attributes that differ only in case clash
 ✓ errors > stateKey "" is rejected with the fix in the message
 ✓ errors > stateKey "a b" is rejected with the fix in the message
 ✓ errors > stateKey "markIds" is rejected with the fix in the message
 ✓ errors > stateKey "__proto__" is rejected with the fix in the message
 ✓ types > the definition types the flavor
 ✓ types > the original signatures stay the last overloads
 ✓ types > the type rules reject what the runtime rejects

$ MARK_STATE_PROPERTY_SEEDS=3000 MARK_STATE_PROPERTY_STEPS=40 npx vitest run --project unit packages/lexical-mark/src/__tests__/unit/MarkStateProperty.test.ts
   ✓ MarkStateExtension property test (1)
     ✓ 3000 random edit sequences of 40 steps keep the ids of every character 54224ms
   ✓ MarkStateExtension property test with two mark flavors (1)
     ✓ 3000 random edit sequences of 40 steps keep the ids of every character 86535ms
      Tests  2 passed (2)

$ npx vitest run --project unit packages/lexical-mark
 Test Files  13 passed (13)
      Tests  852 passed (852)

$ npx vitest run scripts/__tests__/unit/treeShakingSource.test.ts packages/lexical-compiler
 Test Files  7 passed (7)
      Tests  570 passed (570)

$ pnpm run test-unit
 Test Files  413 passed | 1 skipped (414)
      Tests  10208 passed | 4 expected fail | 2 skipped (10214)

$ pnpm run build && pnpm run build-types
(both exit 0)

$ pnpm run ci-check
Found 0 errors
```

`@lexical/y` check, not part of this PR: on a copy of the #9400 tree with
this `MarkStateExtension.ts`, a two-peer test that adds a comment on one
peer and a suggestion plus a default-flavor id on the other, on the same
words, converges with all three, and removing the comment on one peer
leaves the suggestion on both. The Yjs text holds `s:markIds`,
`s:commentIds` and `s:suggestionIds` as separate format keys. The
`@lexical/y` Slots, EmptyMarks, TypeAtMarkStart, Races, TypeOverSelection
and FormatOnlyChanges suites pass there too (751 tests).

Browser-mode tests and E2E were not run; the DOM checks run in jsdom.
@zurfyx
zurfyx force-pushed the zurfyx/mark-flavors branch from 02bb4cc to 7db2405 Compare October 29, 2026 17:37
@zurfyx
zurfyx changed the base branch from zurfyx/mark-state-filter to main October 29, 2026 17:37
@zurfyx
zurfyx added this pull request to the merge queue Oct 29, 2026
Merged via the queue into main with commit e817750 Oct 29, 2026
42 checks passed

This branch was successfully deployed

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