Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 20 additions & 0 deletions packages/lexical-selection/src/lexical-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,26 @@ export function $patchStyleText(
$forEachSelectedTextNode((textNode) => {
$patchStyle(textNode, patch);
});

const nodes = selection.getNodes();
if (nodes.length > 0) {
const patchedElementKeys = new Set<NodeKey>();
for (const node of nodes) {
if (
!$isElementNode(node) ||
!node.canBeEmpty() ||
node.getChildrenSize() !== 0
) {
continue;
}
const key = node.getKey();
if (patchedElementKeys.has(key)) {
continue;
}
patchedElementKeys.add(key);
$patchStyle(node, patch);
}
}
}

export function $forEachSelectedTextNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
$createTextNode,
$getRoot,
$getSelection,
$isParagraphNode,
$isTextNode,
$setSelection,
} from 'lexical';
import {initializeUnitTest} from 'lexical/src/__tests__/utils';
Expand Down Expand Up @@ -87,6 +89,39 @@ describe('table selection', () => {
{discrete: true},
);
});

test('$patchStyleText applies styles to empty table cells', () => {
testEnv.editor.update(
() => {
const selection = $getSelection();
expect($isTableSelection(selection)).toBe(true);

const emptyCell = tableMap.at(0)!.at(0)!.cell;
const emptyParagraph = emptyCell.getFirstChild();
if (!$isParagraphNode(emptyParagraph)) {
throw new Error('Expected paragraph node in empty cell');
}
emptyParagraph.clear();
expect(emptyParagraph.getChildrenSize()).toBe(0);

$patchStyleText(selection!, {color: 'blue'});

const filledCell = tableMap.at(0)!.at(1)!.cell;
const filledParagraph = filledCell.getFirstChild();
if (!$isParagraphNode(filledParagraph)) {
throw new Error('Expected paragraph node in filled cell');
}
const textNode = filledParagraph.getFirstChild();
if (!$isTextNode(textNode)) {
throw new Error('Expected text node inside filled cell');
}

expect(textNode.getStyle()).toBe('color: blue;');
expect(emptyParagraph.getTextStyle()).toBe('color: blue;');
},
{discrete: true},
);
});
});

describe('regression #7140', () => {
Expand Down
Loading