Skip to content

[lexical-text] Bug Fix: $canShowPlaceholder scans every child of the top block - #9036

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:text-placeholder-loop-index
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:text-placeholder-loop-index

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

$canShowPlaceholder documents itself as "If anything is in the root the
placeholder should not be shown". Its innermost loop is meant to reject a top
block that holds any non-text child, but it indexes with the outer loop's
variable:

for (let s = 0; s < topBlockChildrenLength; s++) {
  const child = topBlockChildren[i];   // `i`, not `s`
  if (!$isTextNode(child)) {
    return false;
  }
}

s is declared and incremented but never read, which is what gives the typo
away. i indexes the loop over the root's children, and the earlier
if (childrenLength > 1) { return false; } guard means it is always 0. So
the loop tests topBlockChildren[0] N times and never looks at index 1 or
beyond — the one thing it exists to do.

A paragraph whose children are [TextNode(""), <inline DecoratorNode>]
therefore reports that the placeholder may be shown, and the placeholder
renders on top of real content. The root's text content is genuinely "" (a
decorator contributes no text), so the $isRootTextContentEmpty(isComposing, false) gate legitimately passes and the child scan is the only thing standing
between that document and a wrong placeholder.

Reaching it needs the TextNode at index 0 to survive reconciliation: a simple
empty TextNode is removed, which would shuffle the decorator to index 0 and
mask the bug. token and segmented nodes are not simple text and do survive,
which is the shape the test uses.

Test plan

New unit test packages/lexical-text/src/__tests__/unit/canShowPlaceholder.test.ts.
It asserts the premise (the text node survived, root text content is "")
before asserting the result, so the test cannot silently stop exercising the
scan. The last three cases are controls — decorator first, empty paragraph, and
a paragraph of only empty text nodes — and pass before and after.

Before

$ npx vitest run packages/lexical-text

     × is false when a decorator follows an empty token text node 32ms
     × is false when a decorator follows an empty segmented text node 4ms
     × is false when the decorator is several positions along 3ms

AssertionError: expected true to be false // Object.is equality

      Tests  3 failed | 3 passed (6)

After

$ npx vitest run packages/lexical-text

 Test Files  1 passed (1)
      Tests  6 passed (6)

@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
@mayrang

mayrang commented Aug 9, 2026 •

Copy link
Copy Markdown
Collaborator

Simple one-character typo (i → s in the inner loop) — nothing much to review here.

@etrepum

etrepum commented Aug 9, 2026

Copy link
Copy Markdown
Collaborator

The tests fail tsc, needs an $assertNodeType or equivalent

…top block

## Description

`$canShowPlaceholder` documents itself as "If anything is in the root the
placeholder should not be shown". Its innermost loop is meant to reject a top
block that holds any non-text child, but it indexes with the *outer* loop's
variable:

```js
for (let s = 0; s < topBlockChildrenLength; s++) {
  const child = topBlockChildren[i];   // `i`, not `s`
  if (!$isTextNode(child)) {
    return false;
  }
}
```

`s` is declared and incremented but never read, which is what gives the typo
away. `i` indexes the loop over the *root's* children, and the earlier
`if (childrenLength > 1) { return false; }` guard means it is always `0`. So
the loop tests `topBlockChildren[0]` N times and never looks at index 1 or
beyond — the one thing it exists to do.

A paragraph whose children are `[TextNode(""), <inline DecoratorNode>]`
therefore reports that the placeholder may be shown, and the placeholder
renders on top of real content. The root's text content is genuinely `""` (a
decorator contributes no text), so the `$isRootTextContentEmpty(isComposing,
false)` gate legitimately passes and the child scan is the only thing standing
between that document and a wrong placeholder.

Reaching it needs the TextNode at index 0 to survive reconciliation: a *simple*
empty TextNode is removed, which would shuffle the decorator to index 0 and
mask the bug. `token` and `segmented` nodes are not simple text and do survive,
which is the shape the test uses.

## Test plan

New unit test `packages/lexical-text/src/__tests__/unit/canShowPlaceholder.test.ts`.
It asserts the premise (the text node survived, root text content is `""`)
before asserting the result, so the test cannot silently stop exercising the
scan. The last three cases are controls — decorator first, empty paragraph, and
a paragraph of only empty text nodes — and pass before and after.

### Before

```
$ npx vitest run packages/lexical-text

     × is false when a decorator follows an empty token text node 32ms
     × is false when a decorator follows an empty segmented text node 4ms
     × is false when the decorator is several positions along 3ms

AssertionError: expected true to be false // Object.is equality

      Tests  3 failed | 3 passed (6)
```

### After

```
$ npx vitest run packages/lexical-text

 Test Files  1 passed (1)
      Tests  6 passed (6)
```
@LeSingh1
LeSingh1 force-pushed the text-placeholder-loop-index branch from 2b0216c to 32d86db Compare August 9, 2026 23:15
@LeSingh1

LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 32d86db — $assertNodeType($getRoot().getFirstChild(), $isParagraphNode) instead of getFirstChildOrThrow(), which returns LexicalNode and so has no getChildrenSize. tsc --noEmit is clean now and the 6 tests still pass.

Sorry for the churn, and thanks for catching it — I had run the test suite but not the typecheck on this branch.

@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9060 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.

3 participants