Skip to content

Commit 6e02b55

Browse files
committed
Fix nested table breaking the Word validation #190
1 parent b290bf1 commit 6e02b55

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/Html2OpenXml/Expressions/Table/TableCellExpression.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public override IEnumerable<OpenXmlElement> Interpret (ParsingContext context)
3131
{
3232
var childElements = base.Interpret (context);
3333

34-
if (!childElements.Any()) // Word requires that the cell is not empty
35-
childElements = [new Paragraph()];
36-
3734
var cell = new TableCell (cellProperties);
3835

3936
if (cellNode.ColumnSpan > 1)
@@ -46,6 +43,13 @@ public override IEnumerable<OpenXmlElement> Interpret (ParsingContext context)
4643
cellProperties.VerticalMerge = new() { Val = MergedCellValues.Restart };
4744
}
4845

46+
// Word requires at least one paragraph in a cell
47+
// OpenXmlValidator does not catch this error
48+
if (!childElements.OfType<Paragraph>().Any())
49+
{
50+
childElements = childElements.Append(new Paragraph());
51+
}
52+
4953
cell.Append(childElements);
5054
return [cell];
5155
}

test/HtmlToOpenXml.Tests/TableTests.cs

+1
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ public void NestedTable_ReturnsTableInsideTable()
493493
var cell = elements[0].GetFirstChild<TableRow>()?.GetFirstChild<TableCell>();
494494
Assert.That(cell, Is.Not.Null);
495495
Assert.That(cell.HasChild<Table>(), Is.True);
496+
Assert.That(cell.HasChild<Paragraph>(), Is.True, "Word requires at least a paragraph");
496497
}
497498

498499
[Test]

0 commit comments

Comments
 (0)