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
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ export const directionFormatHandler: FormatHandler<DirectionFormat> = {
format.direction = dir == 'rtl' ? 'rtl' : 'ltr';
}
},
apply: (format, element) => {
apply: (format, element, context) => {
if (format.direction) {
element.style.direction = format.direction;
}

if (format.direction == 'rtl' && isElementOfType(element, 'table')) {
if (
format.direction == 'rtl' &&
isElementOfType(element, 'table') &&
context.implicitFormat.direction != 'rtl'
) {
element.style.justifySelf = 'flex-end';
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,29 @@ export const handleFormatContainer: ContentModelBlockHandler<ContentModelFormatC
applyFormat(containerNode, context.formatAppliers.container, container.format, context);
});

if (container.tagName == 'pre') {
stackFormat(context, PreChildFormat, () => {
context.modelHandlers.blockGroupChildren(doc, containerNode, container, context);
});
} else {
context.modelHandlers.blockGroupChildren(doc, containerNode, container, context);
}
stackFormat(
context,
container.format.direction ? { direction: container.format.direction } : null,
() => {
if (container.tagName == 'pre') {
stackFormat(context, PreChildFormat, () => {
context.modelHandlers.blockGroupChildren(
doc,
containerNode,
container,
context
);
});
} else {
context.modelHandlers.blockGroupChildren(
doc,
containerNode,
container,
context
);
}
}
);

element = containerNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ export const handleListItem: ContentModelBlockHandler<ContentModelListItem> = (
applyFormat(li, context.formatAppliers.listItemElement, listItem.format, context);

stackFormat(context, listItem.formatHolder.format, () => {
context.modelHandlers.blockGroupChildren(doc, li, listItem, context);
stackFormat(
context,
listItem.format.direction ? { direction: listItem.format.direction } : null,
() => {
context.modelHandlers.blockGroupChildren(doc, li, listItem, context);
}
);
});
} else {
// There is no level for this list item, that means it should be moved out of the list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ export const handleTable: ContentModelBlockHandler<ContentModelTable> = (
applyFormat(td, context.formatAppliers.dataset, cell.dataset, context);
}

context.modelHandlers.blockGroupChildren(doc, td, cell, context);
stackFormat(
context,
cell.format.direction ? { direction: cell.format.direction } : null,
() => {
context.modelHandlers.blockGroupChildren(doc, td, cell, context);
}
);
});

context.onNodeCreated?.(cell, td);
Expand Down
243 changes: 243 additions & 0 deletions packages/roosterjs-content-model-dom/test/endToEndTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,249 @@ describe('End to end test for DOM => Model => DOM/TEXT', () => {
);
});

it('LTR table under RTL table', () => {
runTest(
'<table dir="rtl"><tr><td><table dir="ltr"><tr><td>bb</td></tr></table></td></tr></table>',
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Table',
rows: [
{
format: {},
height: 0,
cells: [
{
blockGroupType: 'TableCell',
blocks: [
{
blockType: 'Table',
rows: [
{
format: {},
height: 0,
cells: [
{
blockGroupType: 'TableCell',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'bb',
format: {},
},
],
format: {
direction: 'ltr',
},
isImplicit: true,
},
],
format: {
direction: 'ltr',
},
spanLeft: false,
spanAbove: false,
isHeader: false,
dataset: {},
},
],
},
],
format: {
direction: 'ltr',
},
widths: [],
dataset: {},
},
],
format: {
direction: 'rtl',
},
spanLeft: false,
spanAbove: false,
isHeader: false,
dataset: {},
},
],
},
],
format: { direction: 'rtl' },
widths: [],
dataset: {},
},
],
},
'bb',
'<table style="direction: rtl; justify-self: flex-end;"><tbody><tr><td style="direction: rtl;"><table style="direction: ltr;"><tbody><tr><td style="direction: ltr;"><div style="direction: ltr;">bb</div></td></tr></tbody></table></td></tr></tbody></table>'
);
});

it('RTL table under LTR table', () => {
runTest(
'<table dir="ltr"><tr><td><table dir="rtl"><tr><td>bb</td></tr></table></td></tr></table>',
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Table',
rows: [
{
format: {},
height: 0,
cells: [
{
blockGroupType: 'TableCell',
blocks: [
{
blockType: 'Table',
rows: [
{
format: {},
height: 0,
cells: [
{
blockGroupType: 'TableCell',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'bb',
format: {},
},
],
format: {
direction: 'rtl',
},
isImplicit: true,
},
],
format: {
direction: 'rtl',
},
spanLeft: false,
spanAbove: false,
isHeader: false,
dataset: {},
},
],
},
],
format: {
direction: 'rtl',
},
widths: [],
dataset: {},
},
],
format: {
direction: 'ltr',
},
spanLeft: false,
spanAbove: false,
isHeader: false,
dataset: {},
},
],
},
],
format: { direction: 'ltr' },
widths: [],
dataset: {},
},
],
},
'bb',
'<table style="direction: ltr;"><tbody><tr><td style="direction: ltr;"><table style="direction: rtl; justify-self: flex-end;"><tbody><tr><td style="direction: rtl;"><div style="direction: rtl;">bb</div></td></tr></tbody></table></td></tr></tbody></table>'
);
});

it('RTL table under RTL table', () => {
runTest(
'<table dir="rtl"><tr><td><table dir="rtl"><tr><td>bb</td></tr></table></td></tr></table>',
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'Table',
rows: [
{
format: {},
height: 0,
cells: [
{
blockGroupType: 'TableCell',
blocks: [
{
blockType: 'Table',
rows: [
{
format: {},
height: 0,
cells: [
{
blockGroupType: 'TableCell',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: 'bb',
format: {},
},
],
format: {
direction: 'rtl',
},
isImplicit: true,
},
],
format: {
direction: 'rtl',
},
spanLeft: false,
spanAbove: false,
isHeader: false,
dataset: {},
},
],
},
],
format: {
direction: 'rtl',
},
widths: [],
dataset: {},
},
],
format: {
direction: 'rtl',
},
spanLeft: false,
spanAbove: false,
isHeader: false,
dataset: {},
},
],
},
],
format: { direction: 'rtl' },
widths: [],
dataset: {},
},
],
},
'bb',
'<table style="direction: rtl; justify-self: flex-end;"><tbody><tr><td style="direction: rtl;"><table style="direction: rtl;"><tbody><tr><td style="direction: rtl;"><div style="direction: rtl;">bb</div></td></tr></tbody></table></td></tr></tbody></table>'
);
});

it('Table under styled block', () => {
runTest(
'<b style="background-color:red; display: block">aa<table><tr><td>bb</td></tr></table>cc</b>',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,29 @@ describe('directionFormatHandler.apply', () => {
'<table style="direction: rtl; justify-self: flex-end;"></table>'
);
});

it('RTL on table, parent implicit direction is LTR, applies justify-self', () => {
const table = document.createElement('table');
format.direction = 'rtl';
context.implicitFormat.direction = 'ltr';
directionFormatHandler.apply(format, table, context);
expect(table.outerHTML).toBe(
'<table style="direction: rtl; justify-self: flex-end;"></table>'
);
});

it('RTL on table, parent implicit direction is RTL, skips justify-self', () => {
const table = document.createElement('table');
format.direction = 'rtl';
context.implicitFormat.direction = 'rtl';
directionFormatHandler.apply(format, table, context);
expect(table.outerHTML).toBe('<table style="direction: rtl;"></table>');
});

it('RTL on non-table element, never applies justify-self', () => {
const td = document.createElement('td');
format.direction = 'rtl';
directionFormatHandler.apply(format, td, context);
expect(td.outerHTML).toBe('<td style="direction: rtl;"></td>');
});
});
Loading
Loading