Skip to content

[lexical-list] Bug Fix: removing a list keeps each item's own element state - #9031

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:list-removelist-keep-item-state
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:list-removelist-keep-item-state

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

$createListOrMerge copies a block's element state onto the list item it
creates when a list is inserted:

listItem.setFormat(node.getFormatType());
listItem.setIndent(node.getIndent());

$removeList is the inverse operation and does not do the reverse. It builds
each replacement paragraph from the selection's text style and format only,
so the item's own format (alignment), indent, direction and style are
dropped. Turning a centred, indented paragraph into a list and back loses the
centring and the indent; a nested item comes back flush left instead of one
level in; an RTL item comes back LTR even though both importers read dir off
the <li> and ListItemNode.exportDOM writes it.

This carries those four onto the paragraph that stands in for the item.
indent in particular is derived from the nesting for an attached list item,
so it is the only thing that can reproduce a nested item's rendering once the
list is gone.

Test plan

npx vitest run packages/lexical-list/src/__tests__/unit/RemoveListKeepsItemState.test.ts

(The natural test file, formatList.test.ts, is already being edited by an
open PR, so the repro lives in its own file.)

Before

 ❯ |unit| packages/lexical-list/src/__tests__/unit/RemoveListKeepsItemState.test.ts (2 tests | 2 failed) 17ms
     × the paragraphs keep the items format, indent, direction and style 14ms
     × a formatted paragraph round-trips through a list 2ms

 FAIL  ... > the paragraphs keep the items format, indent, direction and style
AssertionError: expected [ { direction: null, …(4) }, …(1) ] to deeply equal [ { direction: 'rtl', …(4) }, …(1) ]

- Expected
+ Received

  [
    {
-     "direction": "rtl",
-     "format": "center",
+     "direction": null,
+     "format": "",
      "indent": 0,
-     "style": "color: red;",
+     "style": "",
      "text": "a",
    },
    {
      "direction": null,
      "format": "",
-     "indent": 1,
+     "indent": 0,
      "style": "",
      "text": "b",
    },
  ]

After

 Test Files  1 passed (1)
      Tests  2 passed (2)

Also green:

  • npx vitest run packages/lexical-list packages/lexical-markdown packages/lexical-mdast packages/lexical-react — 49 files, 902 tests
  • E2E_BROWSER=chromium npx playwright test --project=chromium List.spec.mjs — 36 passed

Only chromium was exercised locally for the e2e run.

… state

## Description

`$createListOrMerge` copies a block's element state onto the list item it
creates when a list is inserted:

```ts
listItem.setFormat(node.getFormatType());
listItem.setIndent(node.getIndent());
```

`$removeList` is the inverse operation and does not do the reverse. It builds
each replacement paragraph from the *selection's* text style and format only,
so the item's own `format` (alignment), `indent`, `direction` and `style` are
dropped. Turning a centred, indented paragraph into a list and back loses the
centring and the indent; a nested item comes back flush left instead of one
level in; an RTL item comes back LTR even though both importers read `dir` off
the `<li>` and `ListItemNode.exportDOM` writes it.

This carries those four onto the paragraph that stands in for the item.
`indent` in particular is derived from the nesting for an attached list item,
so it is the only thing that can reproduce a nested item's rendering once the
list is gone.

## Test plan

`npx vitest run packages/lexical-list/src/__tests__/unit/RemoveListKeepsItemState.test.ts`

(The natural test file, `formatList.test.ts`, is already being edited by an
open PR, so the repro lives in its own file.)

### Before

```
 ❯ |unit| packages/lexical-list/src/__tests__/unit/RemoveListKeepsItemState.test.ts (2 tests | 2 failed) 17ms
     × the paragraphs keep the items format, indent, direction and style 14ms
     × a formatted paragraph round-trips through a list 2ms

 FAIL  ... > the paragraphs keep the items format, indent, direction and style
AssertionError: expected [ { direction: null, …(4) }, …(1) ] to deeply equal [ { direction: 'rtl', …(4) }, …(1) ]

- Expected
+ Received

  [
    {
-     "direction": "rtl",
-     "format": "center",
+     "direction": null,
+     "format": "",
      "indent": 0,
-     "style": "color: red;",
+     "style": "",
      "text": "a",
    },
    {
      "direction": null,
      "format": "",
-     "indent": 1,
+     "indent": 0,
      "style": "",
      "text": "b",
    },
  ]
```

### After

```
 Test Files  1 passed (1)
      Tests  2 passed (2)
```

Also green:

- `npx vitest run packages/lexical-list packages/lexical-markdown packages/lexical-mdast packages/lexical-react` — 49 files, 902 tests
- `E2E_BROWSER=chromium npx playwright test --project=chromium List.spec.mjs` — 36 passed

Only chromium was exercised locally for the e2e run.
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 9, 2026
@LeSingh1

Copy link
Copy Markdown
Contributor Author

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

@LeSingh1 LeSingh1 closed this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant