Skip to content

Commit 56941ca

Browse files
committed
Table inside a list must be aligned #192
1 parent 44747db commit 56941ca

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Html2OpenXml/Expressions/Numbering/ListExpression.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ public override IEnumerable<OpenXmlElement> Interpret(ParsingContext context)
9191
var childElements = expression.Interpret(context);
9292
if (!childElements.Any()) continue;
9393

94+
// table must be aligned to the list item
95+
var tables = childElements.OfType<Table>();
96+
foreach (var table in tables)
97+
{
98+
var tableProperties = table.GetFirstChild<TableProperties>();
99+
if (tableProperties == null)
100+
table.PrependChild(tableProperties = new());
101+
102+
tableProperties.TableIndentation ??= new() { Width = level * Indentation * 2 };
103+
}
104+
94105
// ensure to filter out any non-paragraph like any nested table
95106
var paragraphs = childElements.OfType<Paragraph>();
96107
var listItemStyleId = GetStyleIdForListItem(context.DocumentStyle, liNode);

test/HtmlToOpenXml.Tests/NumberingTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,5 +611,32 @@ public void NestedParagraph_ReturnsIndentedItems()
611611
"Last standalone paragraph is aligned with the level 1");
612612
});
613613
}
614+
615+
[Test]
616+
public void NestedTable_ReturnsAlignedTable()
617+
{
618+
var elements = converter.Parse(@"<ol>
619+
<li>Item 1
620+
<table><tr><td>Cell1</td></tr></table>
621+
<ol>
622+
<li>Item 1.1
623+
<table><tr><td>Cell1.1</td></tr></table>
624+
</li>
625+
</ol>
626+
</li>
627+
</ol>");
628+
629+
Assert.That(elements, Is.Not.Empty);
630+
var odds = elements.Where((item, index) => index % 2 != 0);
631+
Assert.That(odds, Has.All.TypeOf<Table>());
632+
for (int i = 0; i < 2; i++)
633+
{
634+
var table = (Table) odds.ElementAt(i);
635+
var tableProperties = table.GetFirstChild<TableProperties>();
636+
Assert.That(tableProperties, Is.Not.Null);
637+
Assert.That(tableProperties.TableIndentation, Is.Not.Null);
638+
Assert.That(tableProperties.TableIndentation.Width?.Value, Is.EqualTo(720 * (i+1)));
639+
}
640+
}
614641
}
615642
}

0 commit comments

Comments
 (0)