Skip to content
Closed
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
8 changes: 6 additions & 2 deletions packages/lexical-table/src/LexicalTableCellNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,12 @@ export function $convertTableCellNodeElement(
);

tableCellNode.__rowSpan = domNode_.rowSpan;
const backgroundColor = domNode_.style.backgroundColor;
if (backgroundColor !== '') {
const backgroundColor =
domNode_.style.backgroundColor ||
domNode_.style.background ||
domNode_.getAttribute('bgcolor') ||
null;
if (backgroundColor !== null) {
tableCellNode.__backgroundColor = backgroundColor;
}
const verticalAlign = domNode_.style.verticalAlign;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,5 +368,65 @@ describe('LexicalTableCellNode tests', () => {
expect(node.getHeaderStyles()).toBe(TableCellHeaderStates.ROW);
});
});

test('DOM Conversion: <td> with style.backgroundColor reads inline background-color', async () => {
const {editor} = testEnv;

await editor.update(() => {
const td = document.createElement('td');
td.style.backgroundColor = '#F4B084';

const result = convertHTMLTag(td);
const node = expectTableCellNode(result);

// Browsers normalize hex to rgb when set via .style
expect(node.getBackgroundColor()).toBe(td.style.backgroundColor);
});
});

test('DOM Conversion: <td> with style.background shorthand (Excel/Outlook) reads background color', async () => {
const {editor} = testEnv;

await editor.update(() => {
const td = document.createElement('td');
td.style.background = '#F4B084';

const result = convertHTMLTag(td);
const node = expectTableCellNode(result);

// Browsers normalize hex to rgb; background may expand to backgroundColor
const expected =
td.style.backgroundColor || td.style.background || null;
expect(node.getBackgroundColor()).toBe(expected);
expect(node.getBackgroundColor()).not.toBeNull();
});
});

test('DOM Conversion: <td> with bgcolor attribute (legacy HTML) reads background color', async () => {
const {editor} = testEnv;

await editor.update(() => {
const td = document.createElement('td');
td.setAttribute('bgcolor', '#F4B084');

const result = convertHTMLTag(td);
const node = expectTableCellNode(result);

expect(node.getBackgroundColor()).toBe('#F4B084');
});
});

test('DOM Conversion: <td> with no background color sets backgroundColor to null', async () => {
const {editor} = testEnv;

await editor.update(() => {
const td = document.createElement('td');

const result = convertHTMLTag(td);
const node = expectTableCellNode(result);

expect(node.getBackgroundColor()).toBeNull();
});
});
});
});
Loading