Skip to content

[lexical-list] Bug Fix: list operations and DOM import/export keep the node's own state - #9050

Merged
etrepum merged 3 commits into
meta:mainfrom
LeSingh1:consol/list-node-state
Aug 29, 2026
Merged

etrepum merged 3 commits into
meta:mainfrom
LeSingh1:consol/list-node-state

Conversation

@LeSingh1

Copy link
Copy Markdown
Contributor

Description

@lexical/list rebuilds a ListNode or a ListItemNode in a lot of places —
converting a list to another type, removing a list, splitting one around an
inserted block, merging two adjacent lists, splicing a block in, importing and
exporting HTML. In each of these the replacement is supposed to stand in for the
node it replaces, but several of them build it from scratch and drop what the
original carried: its element state, its listType, its numbering, its block
formatting, or its dir. One import path also hand-rolls a ListItemNode
instead of asking the ListNode subclass for one.

Each fix keeps the replacement equivalent to the node it stands in for:

  • $insertList (formatList.ts) converted an existing list by building a
    default-constructed $createListNode(listType) and moving the items across,
    so the replacement lost the list's direction, format, style, indent
    and start — toggling an RTL list from bullets to numbers re-rendered it
    left-to-right, and <ol start="4"> restarted at 1. A $newListFrom helper
    now $copyNodes the list being replaced and sets the new type, at both sites
    in $insertList that replace a ListNode. This is what every other
    ListNode-for-ListNode swap in the package ($handleIndent,
    $handleOutdent, ListItemNode.replace, …) already does.

  • $removeList (formatList.ts) is the inverse of $createListOrMerge, which
    copies a block's format and indent onto the list item it creates. It built
    each replacement paragraph from the selection's text style alone, so the
    item's own format, indent, direction and style were dropped. It now
    carries all four onto the paragraph; indent in particular is the only thing
    that reproduces a nested item's rendering once the list is gone.

  • mergeLists (formatList.ts) and ListItemNode.remove
    (LexicalListItemNode.ts) merged the sublists at the join point
    unconditionally. mergeNextSiblingListIfSameType compares listType at the
    top level, but neither of these did, so a nested <ol> adjacent to a nested
    <ul> lost its type. Both now compare listType before joining.

  • ListItemNode.insertAfter (LexicalListItemNode.ts) splits its list when the
    inserted node is not a ListItemNode — the page-break / horizontal-rule /
    image case. The trailing items move into $copyNode(listNode), which carries
    the original list's start, so the ListNode $transform renumbered them
    from there and the list visibly restarted at 1. It now derives the new start
    from the first item that moves, via the existing $getNewListStart helper
    that $handleListInsertParagraph already uses for the same split. Scoped to
    listType === 'number', so bullet and check lists keep the start they copy
    today. Refs Bug: Numbered List Resets After Page Break in Lexical Editor #7032.

  • ListNode.splice (LexicalListNode.ts) wraps a non-ListItemNode in a list
    item, and for a block-level node it replaced the block with
    $createTextNode(node.getTextContent()) — a string, so every text format and
    style was discarded and inline nodes were flattened (a paragraph holding bold
    text and a link became one unformatted TextNode with the link gone). It now
    unwraps the block into its own children, the same conversion
    $createListOrMerge and ListItemNode.append already perform. Non-block and
    inline nodes take the same path as before and a ListNode child is still left
    alone.

  • ListNode.exportDOM (LexicalListNode.ts) builds its element with
    createDOM and returns it without going through super.exportDOM, which is
    where ElementNode emits element.dir from getDirection(). Both importers
    read dir back off the <ol>/<ul> ($convertListNode and ListRule), and
    ListItemNode.exportDOM right next to it does emit dir, so an RTL list
    exported as <ul><li dir="rtl">…</li></ul> — the item kept its direction and
    the list that owns it silently lost it. It now emits dir when the node has
    an explicit direction; a node with no explicit direction is unaffected, so
    existing output is unchanged.

  • $normalizeListChildren (ListImportExtension.ts) is the DOMImportExtension
    port of the legacy $normalizeChildren. The legacy one synthesizes items with
    listNode.createListItemNode.bind(listNode) — the subclass hook added in
    [lexical-list] Feature: Add the createListItemNode method to the ListNode and use it for children normalization #8427 — and so does ListNode.splice, but this one called the module-level
    $createListItemNode(), so a subclassed list imported through the new
    pipeline got plain ListItemNode wrappers for both of its synthesized-item
    cases. The ListNode being built is now threaded through and
    listNode.createListItemNode() is used. The default implementation returns
    $createListItemNode(), so nothing changes for unsubclassed lists.

Test plan

13 new unit test cases in packages/lexical-list/src/__tests__/unit/ — added to
formatList.test.ts, LexicalListItemNode.test.ts, LexicalListNode.test.ts
and ListImportExtension.test.ts, plus four focused files
(InsertListKeepsListState.test.ts, RemoveListKeepsItemState.test.ts,
Issue7032Repro.test.ts, ListSpliceKeepsFormatting.test.ts). 12 of them fail
on main; the 13th is a guard that a bullet list still keeps its start when
it is split. No existing test expectation was changed.

Before

(source changes reverted, new tests kept)

$ npx vitest run --project unit packages/lexical-list
     × wrapper items come from ListNode.createListItemNode() 53ms
     × does not merge nested sublists of a different listType 108ms
     × ListNode.exportDOM() round-trips the dir attribute 56ms
       × both siblings are nested with a different listType 68ms
     × the paragraphs keep the items format, indent, direction and style 247ms
     × a formatted paragraph round-trips through a list 3ms
     × keeps the text formats of the block it unwraps 129ms
     × keeps an inline node of the block it unwraps 22ms
     × converting a populated list keeps its direction, format and start 22ms
     × converting from an empty list item keeps the list state 2ms
     × inserting a block between list items continues the numbering 30ms
     × a list that does not start at 1 continues from the split point 5ms

 Test Files  8 failed | 6 passed (14)
      Tests  12 failed | 126 passed (138)

After

$ npx vitest run --project unit packages/lexical-list

 Test Files  14 passed (14)
      Tests  138 passed (138)

$ npx vitest run --project unit packages/lexical-markdown packages/lexical-mdast packages/lexical-clipboard packages/lexical-html

 Test Files  22 passed (22)
      Tests  710 passed (710)

$ npx vitest run --project unit packages/lexical-react

 Test Files  30 passed (30)
      Tests  181 passed (181)

$ npx tsc --noEmit -p .
(clean, no output)

Supersedes #8925, #9001, #9002, #9021, #9027, #9031, #9034, consolidated per the
review feedback on #9027 and #9035.

…e node's own state

## Description

`@lexical/list` rebuilds a `ListNode` or a `ListItemNode` in a lot of places —
converting a list to another type, removing a list, splitting one around an
inserted block, merging two adjacent lists, splicing a block in, importing and
exporting HTML. In each of these the replacement is supposed to stand in for the
node it replaces, but several of them build it from scratch and drop what the
original carried: its element state, its `listType`, its numbering, its block
formatting, or its `dir`. One import path also hand-rolls a `ListItemNode`
instead of asking the `ListNode` subclass for one.

Each fix keeps the replacement equivalent to the node it stands in for:

- `$insertList` (`formatList.ts`) converted an existing list by building a
  default-constructed `$createListNode(listType)` and moving the items across,
  so the replacement lost the list's `direction`, `format`, `style`, `indent`
  and `start` — toggling an RTL list from bullets to numbers re-rendered it
  left-to-right, and `<ol start="4">` restarted at 1. A `$newListFrom` helper
  now `$copyNode`s the list being replaced and sets the new type, at both sites
  in `$insertList` that replace a `ListNode`. This is what every other
  `ListNode`-for-`ListNode` swap in the package (`$handleIndent`,
  `$handleOutdent`, `ListItemNode.replace`, …) already does.

- `$removeList` (`formatList.ts`) is the inverse of `$createListOrMerge`, which
  copies a block's `format` and `indent` onto the list item it creates. It built
  each replacement paragraph from the selection's text style alone, so the
  item's own `format`, `indent`, `direction` and `style` were dropped. It now
  carries all four onto the paragraph; `indent` in particular is the only thing
  that reproduces a nested item's rendering once the list is gone.

- `mergeLists` (`formatList.ts`) and `ListItemNode.remove`
  (`LexicalListItemNode.ts`) merged the sublists at the join point
  unconditionally. `mergeNextSiblingListIfSameType` compares `listType` at the
  top level, but neither of these did, so a nested `<ol>` adjacent to a nested
  `<ul>` lost its type. Both now compare `listType` before joining.

- `ListItemNode.insertAfter` (`LexicalListItemNode.ts`) splits its list when the
  inserted node is not a `ListItemNode` — the page-break / horizontal-rule /
  image case. The trailing items move into `$copyNode(listNode)`, which carries
  the *original* list's `start`, so the `ListNode` `$transform` renumbered them
  from there and the list visibly restarted at 1. It now derives the new start
  from the first item that moves, via the existing `$getNewListStart` helper
  that `$handleListInsertParagraph` already uses for the same split. Scoped to
  `listType === 'number'`, so bullet and check lists keep the `start` they copy
  today. Refs meta#7032.

- `ListNode.splice` (`LexicalListNode.ts`) wraps a non-`ListItemNode` in a list
  item, and for a block-level node it replaced the block with
  `$createTextNode(node.getTextContent())` — a string, so every text format and
  style was discarded and inline nodes were flattened (a paragraph holding bold
  text and a link became one unformatted `TextNode` with the link gone). It now
  unwraps the block into its own children, the same conversion
  `$createListOrMerge` and `ListItemNode.append` already perform. Non-block and
  inline nodes take the same path as before and a `ListNode` child is still left
  alone.

- `ListNode.exportDOM` (`LexicalListNode.ts`) builds its element with
  `createDOM` and returns it without going through `super.exportDOM`, which is
  where `ElementNode` emits `element.dir` from `getDirection()`. Both importers
  read `dir` back off the `<ol>`/`<ul>` (`$convertListNode` and `ListRule`), and
  `ListItemNode.exportDOM` right next to it does emit `dir`, so an RTL list
  exported as `<ul><li dir="rtl">…</li></ul>` — the item kept its direction and
  the list that owns it silently lost it. It now emits `dir` when the node has
  an explicit direction; a node with no explicit direction is unaffected, so
  existing output is unchanged.

- `$normalizeListChildren` (`ListImportExtension.ts`) is the `DOMImportExtension`
  port of the legacy `$normalizeChildren`. The legacy one synthesizes items with
  `listNode.createListItemNode.bind(listNode)` — the subclass hook added in
  meta#8427 — and so does `ListNode.splice`, but this one called the module-level
  `$createListItemNode()`, so a subclassed list imported through the new
  pipeline got plain `ListItemNode` wrappers for both of its synthesized-item
  cases. The `ListNode` being built is now threaded through and
  `listNode.createListItemNode()` is used. The default implementation returns
  `$createListItemNode()`, so nothing changes for unsubclassed lists.

## Test plan

13 new unit test cases in `packages/lexical-list/src/__tests__/unit/` — added to
`formatList.test.ts`, `LexicalListItemNode.test.ts`, `LexicalListNode.test.ts`
and `ListImportExtension.test.ts`, plus four focused files
(`InsertListKeepsListState.test.ts`, `RemoveListKeepsItemState.test.ts`,
`Issue7032Repro.test.ts`, `ListSpliceKeepsFormatting.test.ts`). 12 of them fail
on `main`; the 13th is a guard that a bullet list still keeps its `start` when
it is split. No existing test expectation was changed.

### Before

(source changes reverted, new tests kept)

```
$ npx vitest run --project unit packages/lexical-list
     × wrapper items come from ListNode.createListItemNode() 53ms
     × does not merge nested sublists of a different listType 108ms
     × ListNode.exportDOM() round-trips the dir attribute 56ms
       × both siblings are nested with a different listType 68ms
     × the paragraphs keep the items format, indent, direction and style 247ms
     × a formatted paragraph round-trips through a list 3ms
     × keeps the text formats of the block it unwraps 129ms
     × keeps an inline node of the block it unwraps 22ms
     × converting a populated list keeps its direction, format and start 22ms
     × converting from an empty list item keeps the list state 2ms
     × inserting a block between list items continues the numbering 30ms
     × a list that does not start at 1 continues from the split point 5ms

 Test Files  8 failed | 6 passed (14)
      Tests  12 failed | 126 passed (138)
```

### After

```
$ npx vitest run --project unit packages/lexical-list

 Test Files  14 passed (14)
      Tests  138 passed (138)

$ npx vitest run --project unit packages/lexical-markdown packages/lexical-mdast packages/lexical-clipboard packages/lexical-html

 Test Files  22 passed (22)
      Tests  710 passed (710)

$ npx vitest run --project unit packages/lexical-react

 Test Files  30 passed (30)
      Tests  181 passed (181)

$ npx tsc --noEmit -p .
(clean, no output)
```

Supersedes meta#8925, meta#9001, meta#9002, meta#9021, meta#9027, meta#9031, meta#9034, consolidated per the
review feedback on meta#9027 and meta#9035.
@vercel

vercel Bot commented Aug 10, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Aug 16, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview Aug 29, 2026 4:29pm
lexical-playground Ready Ready Preview Aug 29, 2026 4:29pm

Request Review

@etrepum

etrepum commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

This PR has conflicts that need to be resolved

## Description

The branch behind meta#9050 had fallen behind `main` and could no
longer be merged. `main` picked up `WordListImportExtension` and switched
`ListImportExtension.test.ts` to type-only imports of `ListItemNode` and
`ListNode`, while this branch had started importing those two as values so the
new `CustomListNode` / `CustomListItemNode` subclasses in the test could extend
them. That left one conflict in the import block of
`packages/lexical-list/src/__tests__/unit/ListImportExtension.test.ts`.

This merge takes both sides: `ListItemNode` and `ListNode` come back as value
imports (they are subclassed), and `WordListImportExtension` from `main` is
kept so the Word-paste tests it added still resolve. Every other file merged
cleanly; the branch's net change against `main` is byte-for-byte the same list
diff as before the merge.

## Test plan

### Before

```
$ git merge upstream/main
Auto-merging packages/lexical-list/src/LexicalListNode.ts
Auto-merging packages/lexical-list/src/ListImportExtension.ts
Auto-merging packages/lexical-list/src/__tests__/unit/ListImportExtension.test.ts
CONFLICT (content): Merge conflict in packages/lexical-list/src/__tests__/unit/ListImportExtension.test.ts
Automatic merge failed; fix conflicts and then commit the result.
```

### After

```
$ npx vitest run packages/lexical-list
 Test Files  15 passed (15)
      Tests  142 passed (142)

$ pnpm run tsc
> @lexical/[email protected] tsc /home/user/lexical
> tsc

$ npx eslint packages/lexical-list
$ npx prettier --check "packages/lexical-list/**/*.{ts,tsx}"
Checking formatting...
All matched files use Prettier code style!
```

E2E and browser-mode suites were not exercised in this environment; the change
is a merge resolution confined to one test file's import block.
@etrepum
etrepum added this pull request to the merge queue Aug 29, 2026
Merged via the queue into meta:main with commit 23c11e7 Aug 29, 2026
44 checks passed
@etrepum etrepum mentioned this pull request Aug 31, 2026
etrepum pushed a commit to etrepum/lexical that referenced this pull request Sep 1, 2026
## Description

Sync with main (24 commits, through 1723cd1). Conflict resolutions and
semantic reconciliation with the checklist/semantic-nesting work:

- ListItemNode.insertAfter split: keep `$copyListForSplit` (carries the
  semantic nesting mark) and adopt main's meta#7032 numbering fix on top of it
  (`setStart($getNewListStart(...))` for numbered lists).
- ListItemNode.remove / mergeLists: main added a same-listType guard before
  merging surrounding sublists (meta#9050). `$collapseWrapperPair` now
  implements that policy for both call sites: differing-type boundary lists
  leave the two wrapper items as separate siblings (matching main's new
  tests) instead of collapsing them into one multi-list item.
- ListImportExtension: adopt main's `listNode.createListItemNode()`
  subclass hook in `$normalizeListChildren`, threading it through
  `$normalizeSemanticChildren` as the wrapper-item factory; keep the shared
  `$isDomChecklist` helper over main's local copy.
- checkList focus lifecycle (meta#9088/meta#9102): adopt main's press-tracking,
  returnFocusToRoot, CHECKBOX_KEYS keydown, and onUpdate-based arrow focus
  (replacing setTimeout) — reconciled with the native checkbox input mode:
  returnFocusToRoot resolves the focused input to its row via
  getActiveCheckListItem; the capture keydown defers ArrowRight on the
  input to KEY_ARROW_RIGHT_COMMAND (which needs the input still focused to
  fix the possibly-stale caret first); ArrowLeft on a focused native input
  stays a no-op (Right returns to the text, Escape exits) while the legacy
  li-focus mode keeps main's exit-to-root behavior; handleArrowUpOrDown
  keeps the wrapper-aware traversal, $selectCheckRowStart and plain-row
  focus-mode exit inside main's onUpdate structure.
- $insertList: keep the three-argument $isSelectingEmptyListItem; adopt
  main's `$newListFrom` (copy-based retype) with `$copySemanticNestingMark`
  preserved at both call sites.
- $removeList: keep the nested-list relocation and adopt main's
  format/indent/direction/style copy onto the replacement paragraph.
- LexicalUtils: union — main's `removeEmptyDOMAttribute` plus the readonly
  `getCachedClassNameArray` docs.

## Test plan

### After

```
pnpm run test-unit — all packages pass
pnpm vitest --project browser run packages/lexical-list
      Tests  25 passed (25)   (4 consecutive runs)
pnpm run ci-check — passes
```

Co-Authored-By: Claude Fable 5 <[email protected]>
etrepum pushed a commit to etrepum/lexical that referenced this pull request Sep 2, 2026
…round 17 — default-mode theme classes, host-row copy, removeList indent, caret in empty task rows

## Description

Fixes for the verified findings of review round 17 on the semantic list
nesting work (several of them surfaced by the main sync's meta#9050 changes).

Behavioral fixes:

- Default representation: a wrapper `<li>` inside a check list gets its
  `listitemUnchecked`/`listitemChecked` theme class back (it had been
  gated on the row rendering a checkbox, which a wrapper never does). Only
  the semantic mode leaves its wrappers class-free, where the items own the
  checkbox styling. Restores the DOM the playground e2e tests assert.
- `ListItemNode.extractWithChild` recognizes a semantic host row whose own
  inline content is covered by the selection (with or without the selection
  reaching into its nested rows), so copying/exporting the row yields a
  list item instead of bare inline text plus an orphan `<ul>`.
- `$removeList` parks a host row's nested lists in a wrapper item instead of
  linking them straight into the parent list, so the nested rows still see
  a ListItemNode grandparent and their replacement paragraphs keep their
  indent (main's meta#9050 contract).
- `ListItemNode.createDOM`/`exportDOM` take the editor from the caller
  rather than always from `$getEditor()`, so `exportDOM` of a task row
  works again inside a bare `editorState.read()`.
- The core caret guard in `onSelectionChange` also covers an element anchor
  on the element with the unmanaged prefix, so Home inside an empty task
  row (`<li><input><br></li>`) no longer leaves the visible caret before
  the checkbox.
- `$handleOutdent`'s middle split builds the trailing wrapper as a copy of
  the wrapper being split (class and element state preserved) instead of a
  fresh base-class item.
- `getCachedClassNameArray` tolerates a frozen/sealed theme object
  (tokenizes without memoizing instead of throwing from the reconciler).
- `MdastExportContext.isSelected`/`hasSelection` are optional again, with
  whole-document defaults, so contexts built before they existed still
  type-check and export.

Structural cleanups:

- `ListItemNode.isBlock()` classifies in a single child walk.
- `$mergeWrapperListItemIntoPrevious` merges boundary lists through
  `mergeLists`, the same seam policy `$collapseWrapperPair` uses.
- `$deepLastRow` lives in utils.ts and serves both the strict-indent
  transform (replacing `$findChildrenEndListItemNode`) and check-list
  arrow navigation.
- `$processNestedLists` is `$`-prefixed (rules-of-lexical warning).

One reported finding was rejected after testing: Enter on the empty inner
row of an emptied host row leaves the host as an empty row beside the new
one — the wrapper representation produces the same two empty rows, and the
existing test `Enter on the sole empty item of an emptied host row keeps the
row` pins that parity.

Regression tests added for the theme class, the host-row export, the
removeList indent, the bare-read exportDOM, the outdent wrapper copy, the
frozen theme cache, and (browser) Home in an empty task row.

## Test plan

### Before

The new unit tests fail on the previous commit: the wrapper renders
`class="nested"` (expected `unchecked nested`); the host-row export yields
`<span …>alpha</span>`; the nested row's paragraph has indent 0; the bare
read throws "Unable to find an active editor"; the trailing wrapper has no
format. The browser test measures the DOM caret at (li, 0) after Home.

### After

```
pnpm vitest --project unit run packages/lexical-list/src/__tests__/unit/semanticNesting.test.ts
      Tests  137 passed (137)
pnpm vitest --project browser run packages/lexical-list
      Tests  26 passed (26)
pnpm vitest --project unit run packages/lexical/src/__tests__/unit/GetCachedClassNameArray.test.ts packages/lexical-mdast packages/lexical-markdown
      Tests  731 passed (731)
pnpm run ci-check — passes
```

Co-Authored-By: Claude Fable 5 <[email protected]>

This branch was successfully deployed

2 active deployments
Preview – lexical — cec4ae31 Deployed Aug 29, 2026 by vercel[bot]
Preview – lexical-playground — cec4ae31 Deployed Aug 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. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants