Skip to content

[lexical-utils] Bug Fix: don't include parent's siblings when starting $dfs at last child - #7275

Merged
etrepum merged 2 commits into
meta:mainfrom
james-atticus:dfs-only-descendants
Mar 3, 2025
Merged

etrepum merged 2 commits into
meta:mainfrom
james-atticus:dfs-only-descendants

Conversation

@james-atticus

Copy link
Copy Markdown
Contributor

Take #2 of #7274

The introduction of Carets changed $dfs to include siblings of the start node's parent, if the start node is the last child of its parent.

I've added a test case and confirmed that the test passes on 0.24.0, i.e. before the caret changes were introduced. I haven't got myself familiarised with the caret API yet though, and couldn't easily see what needed to change to fix this behaviour. On main, test fails with:

  ● LexicalNodeHelpers tests › dfs order › DFS starting at last child of element node should not include parent's siblings

    expect(received).toEqual(expected) // deep equality

    - Expected  -  0
    + Received  + 34

    @@ -32,6 +32,40 @@
            "__style": "",
            "__text": "world",
            "__type": "text",
          },
        },
    +   Object {
    +     "depth": 1,
    +     "node": ParagraphNode {
    +       "__dir": null,
    +       "__first": "17",
    +       "__format": 0,
    +       "__indent": 0,
    +       "__key": "16",
    +       "__last": "17",
    +       "__next": null,
    +       "__parent": "root",
    +       "__prev": "12",
    +       "__size": 1,
    +       "__style": "",
    +       "__textFormat": 0,
    +       "__textStyle": "",
    +       "__type": "paragraph",
    +     },
    +   },
    +   Object {
    +     "depth": 2,
    +     "node": TextNode {
    +       "__detail": 0,
    +       "__format": 0,
    +       "__key": "17",
    +       "__mode": 0,
    +       "__next": null,
    +       "__parent": "16",
    +       "__prev": null,
    +       "__style": "",
    +       "__text": "!!",
    +       "__type": "text",
    +     },
    +   },
      ]

I also came across #7174 which adds a test case for $dfs including the siblings of the startNode itself, if the start node is not an element node. It's not obvious why you'd want to DFS a leaf node, but in any case this was also a change from 0.24.0, so I've updated that test as well. Fails on main with:

  ● LexicalNodeHelpers tests › dfs order › DFS from middle leaf node should only include that node

    expect(received).toEqual(expected) // deep equality

    - Expected  -  0
    + Received  + 15

    @@ -12,6 +12,21 @@
            "__style": "",
            "__text": "world",
            "__type": "text",
          },
        },
    +   Object {
    +     "depth": 2,
    +     "node": TextNode {
    +       "__detail": 0,
    +       "__format": 0,
    +       "__key": "15",
    +       "__mode": 0,
    +       "__next": null,
    +       "__parent": "12",
    +       "__prev": "14",
    +       "__style": "",
    +       "__text": "!",
    +       "__type": "text",
    +     },
    +   },
      ]

@vercel

vercel Bot commented Mar 3, 2025 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 3, 2025 6:41am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 3, 2025 6:41am

@facebook-github-bot facebook-github-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 Mar 3, 2025

@etrepum etrepum left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the code here to fix this implicit endNode calculation and confirmed that this also passes with v0.24.0 prior to the refactoring

@etrepum
etrepum added this pull request to the merge queue Mar 3, 2025
Merged via the queue into meta:main with commit f3ae645 Mar 3, 2025
@etrepum etrepum mentioned this pull request Mar 3, 2025

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — 7714384f Deployed Mar 3, 2025 by vercel[bot]
Preview – lexical — 7714384f Deployed Mar 3, 2025 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