Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect font weight in TH element #2939

Merged
merged 4 commits into from
Feb 7, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ export const defaultContentModelFormatMap: DefaultImplicitFormatMap = {
marginTop: '1em',
marginBottom: '1em',
},
th: {
fontWeight: 'bold',
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export const defaultHTMLStyleMap: DefaultStyleMap = {
},
th: {
display: 'table-cell',
fontWeight: 'bold',
},
u: {
textDecoration: 'underline',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ export function setFirstColumnFormatBorders(
switch (rowIndex) {
case 0:
cell.isHeader = !!format.hasHeaderRow;

if (cell.isHeader) {
cell.format.fontWeight = 'bold';
}
break;
case rows.length - 1:
setBorderColor(cell.format, 'borderTop');
Expand Down Expand Up @@ -295,6 +299,7 @@ function setHeaderRowFormat(
const cell = mutateBlock(readonlyCell);

cell.isHeader = true;
cell.format.fontWeight = 'bold';

if (format.headerRowColor) {
if (!metaOverrides.bgColorOverrides[rowIndex][cellIndex]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { hasMetadata } from '../../modelApi/metadata/updateMetadata';
import { isBlockEmpty } from '../../modelApi/common/isEmpty';
import { moveChildNodes } from '../../domUtils/moveChildNodes';
import { reuseCachedElement } from '../../domUtils/reuseCachedElement';
import { stackFormat } from '../utils/stackFormat';
import type {
ContentModelBlockHandler,
ContentModelTable,
Expand Down Expand Up @@ -96,9 +97,9 @@ export const handleTable: ContentModelBlockHandler<ContentModelTable> = (
}

if (!cell.spanAbove && !cell.spanLeft) {
const tag = cell.isHeader ? 'th' : 'td';
const td =
(context.allowCacheElement && cell.cachedElement) ||
doc.createElement(cell.isHeader ? 'th' : 'td');
(context.allowCacheElement && cell.cachedElement) || doc.createElement(tag);

tr.appendChild(td);

Expand Down Expand Up @@ -132,18 +133,25 @@ export const handleTable: ContentModelBlockHandler<ContentModelTable> = (
}
}

if (!cell.cachedElement) {
if (context.allowCacheElement) {
cell.cachedElement = td;
stackFormat(context, tag, () => {
if (!cell.cachedElement) {
if (context.allowCacheElement) {
cell.cachedElement = td;
}

applyFormat(td, context.formatAppliers.block, cell.format, context);
applyFormat(td, context.formatAppliers.tableCell, cell.format, context);
applyFormat(
td,
context.formatAppliers.tableCellBorder,
cell.format,
context
);
applyFormat(td, context.formatAppliers.dataset, cell.dataset, context);
}

applyFormat(td, context.formatAppliers.block, cell.format, context);
applyFormat(td, context.formatAppliers.tableCell, cell.format, context);
applyFormat(td, context.formatAppliers.tableCellBorder, cell.format, context);
applyFormat(td, context.formatAppliers.dataset, cell.dataset, context);
}

context.modelHandlers.blockGroupChildren(doc, td, cell, context);
context.modelHandlers.blockGroupChildren(doc, td, cell, context);
});

context.onNodeCreated?.(cell, td);
}
Expand Down
153 changes: 153 additions & 0 deletions packages/roosterjs-content-model-dom/test/endToEndTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2226,4 +2226,157 @@ describe('End to end test for DOM => Model => DOM/TEXT', () => {
'<div style="text-align: center;">test</div>'
);
});

it('TH without font-weight', () => {
runTest(
'<table><tr><th>test</th></tr></table>',
{
blockGroupType: 'Document',
blocks: [
{
widths: [],
rows: [
{
height: 0,
cells: [
{
spanAbove: false,
spanLeft: false,
isHeader: true,
blockGroupType: 'TableCell',
blocks: [
{
isImplicit: true,
segments: [
{
text: 'test',
segmentType: 'Text',
format: {
fontWeight: 'bold',
},
},
],
blockType: 'Paragraph',
format: {},
},
],
format: {},
dataset: {},
},
],
format: {},
},
],
blockType: 'Table',
format: {},
dataset: {},
},
],
},
'test',
'<table><tbody><tr><th>test</th></tr></tbody></table>'
);
});

it('TH with font-weight: 400', () => {
runTest(
'<table><tr><th style="font-weight:400">test</th></tr></table>',
{
blockGroupType: 'Document',
blocks: [
{
widths: [],
rows: [
{
height: 0,
cells: [
{
spanAbove: false,
spanLeft: false,
isHeader: true,
blockGroupType: 'TableCell',
blocks: [
{
isImplicit: true,
segments: [
{
text: 'test',
segmentType: 'Text',
format: {
fontWeight: '400',
},
},
],
blockType: 'Paragraph',
format: {},
},
],
format: {},
dataset: {},
},
],
format: {},
},
],
blockType: 'Table',
format: {},
dataset: {},
},
],
},
'test',
'<table><tbody><tr><th><span style="font-weight: 400;">test</span></th></tr></tbody></table>'
);
});

it('TH with font-weight: bold', () => {
runTest(
'<table><tr><th style="font-weight: bold">test</th></tr></table>',
{
blockGroupType: 'Document',
blocks: [
{
widths: [],
rows: [
{
height: 0,
cells: [
{
spanAbove: false,
spanLeft: false,
isHeader: true,
blockGroupType: 'TableCell',
blocks: [
{
isImplicit: true,
segments: [
{
text: 'test',
segmentType: 'Text',
format: {
fontWeight: 'bold',
},
},
],
blockType: 'Paragraph',
format: {},
},
],
format: {},
dataset: {},
},
],
format: {},
},
],
blockType: 'Table',
format: {},
dataset: {},
},
],
},
'test',
'<table><tbody><tr><th>test</th></tr></tbody></table>'
);
});
});
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { BoldFormat } from './formatParts/BoldFormat';
import type { BorderBoxFormat } from './formatParts/BorderBoxFormat';
import type { ContentModelBlockFormat } from './ContentModelBlockFormat';
import type { SizeFormat } from './formatParts/SizeFormat';
Expand All @@ -13,4 +14,5 @@ export type ContentModelTableCellFormat = ContentModelBlockFormat &
VerticalAlignFormat &
WordBreakFormat &
TextColorFormat &
SizeFormat;
SizeFormat &
BoldFormat;
Loading