Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/lexical/src/__tests__/unit/LexicalSelection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,44 @@ function mapLatest<T extends LexicalNode>(nodes: T[]): T[] {

describe('LexicalSelection tests', () => {
initializeUnitTest(testEnv => {
test('programmatic selection movement clears stale formatting', async () => {
const {editor} = testEnv;

await editor.update(() => {
const root = $getRoot();
const paragraph = $createParagraphNode();
const text = $createTextNode('Hello World');
paragraph.append(text);
root.append(paragraph);

// 1. Bold "Hello" (first 5 chars)
text.select(0, 5);
const selection = $getSelection();
if ($isRangeSelection(selection)) {
selection.formatText('bold');
}
});

await editor.update(() => {
const selection = $getSelection();
// Verify we are indeed bold
if ($isRangeSelection(selection)) {
expect(selection.hasFormat('bold')).toBe(true);
}

// 2. Jump to the end (the unformatted " World" text)
$getRoot().selectEnd();
});

await editor.update(() => {
const selection = $getSelection();
// 3. Verification: format cache must be flushed
if ($isRangeSelection(selection)) {
expect(selection.hasFormat('bold')).toBe(false);
}
});
});

describe('Inserting text either side of inline elements', () => {
const setup = async (
mode: 'start-of-paragraph' | 'mid-paragraph' | 'end-of-paragraph',
Expand Down
8 changes: 8 additions & 0 deletions packages/lexical/src/nodes/LexicalTextNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,9 @@ export class TextNode extends LexicalNode implements InlineFormattableNode {
'text',
);
} else {
const isDifferentNode =
selection.anchor.key !== key || selection.focus.key !== key;

const compositionKey = $getCompositionKey();
if (
compositionKey === selection.anchor.key ||
Expand All @@ -905,6 +908,11 @@ export class TextNode extends LexicalNode implements InlineFormattableNode {
$setCompositionKey(key);
}
selection.setTextNodeRange(this, anchorOffset, this, focusOffset);

if (isDifferentNode) {
selection.format = this.getFormat();
selection.style = this.getStyle();
}
}
return selection;
}
Expand Down
Loading