facebook / facebook/lexical

Bug: Inconsistent getTextContent works differently in $getRoot().getTextContent()

Open
#5,336 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
23.9k
Forks
2.2k
Avg merge
1d 16h
Merged PRs (30d)
61

Description

Lexical version: latest

## Steps To Reproduce

1. Create and add a custom paragraph node with a custom getTextContent, example:
```
getTextContent(): string {
const textContent = this.getChildren()
.map((child: LexicalNode) => child.getTextContent())
.join('');
return `~"${textContent}"`;
}
```
2. Add a OnchangePlugin, then getTextContent from $getRoot and from $getChildAtIndex

```
{
const textContent = editorState.read(() => $getRoot().getTextContent());

const textContent2 = editorState.read(() =>
$getRoot().getChildAtIndex(0).getTextContent(),
);
console.log('>>>>', textContent);
console.log('>>>>2', textContent2);

```

Link to code example:

## The current behavior
$getRoot().getTextContent() completely ignores the custom node getTextContent, while the $getRoot().getChildAtIndex(0).getTextContent() successfully returns the correct custom getTextContent.

$getRoot().getTextContent():
```
Hello, {firstName}, how are you?
```

$getRoot().getChildAtIndex(0).getTextContent():
```
~"Hello, {firstName}, how are you?"
```

## The expected behavior
In my opinion, the result should be the same.

Contributor guide

Open the contributing guide

Research direction

Reproduce the discrepancy with the custom paragraph node and OnChangePlugin, comparing $getRoot().getTextContent() with $getRoot().getChildAtIndex(0).getTextContent(). Trace the TypeScript implementations of getTextContent from the root and child entry points; done means both calls honor the custom node output and regression coverage verifies the expected string.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.