diff --git a/OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs b/OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs index cdbdb3bb..c775eadc 100644 --- a/OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs +++ b/OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs @@ -107,6 +107,7 @@ public class DaTests [InlineData("DA264-InvalidRunLevelRepeat.docx", "DA-Data.xml", true)] [InlineData("DA265-RunLevelRepeatWithWhiteSpaceBefore.docx", "DA-Data.xml", false)] [InlineData("DA266-RunLevelRepeat-NoData.docx", "DA-Data.xml", true)] + [InlineData("DA268-Block-Conditional-In-Table-Cell.docx", "DA268-data.xml", false)] public void DA101(string name, string data, bool err) { var sourceDir = new DirectoryInfo("../../../../TestFiles/"); diff --git a/OpenXmlPowerTools/DocumentAssembler/DocumentAssembler.cs b/OpenXmlPowerTools/DocumentAssembler/DocumentAssembler.cs index a639ce5d..478aad91 100644 --- a/OpenXmlPowerTools/DocumentAssembler/DocumentAssembler.cs +++ b/OpenXmlPowerTools/DocumentAssembler/DocumentAssembler.cs @@ -873,9 +873,24 @@ private class RunReplacementInfo } return null; } + var transformedNodes = element.Nodes().Select(n => ContentReplacementTransform(n, data, templateError, owningPart)); + if (element.Name == W.tc) + { + // Check if the table cell contains any block-level elements + // Valid block-level elements in a table cell: p (paragraph), tbl (table), sdt (structured document tag), customXml + var nodesList = transformedNodes.ToList(); + var hasBlockLevelContent = nodesList.Any(n => n is XElement xe && + (xe.Name == W.p || xe.Name == W.tbl || xe.Name == W.sdt || xe.Name == W.customXml)); + if (!hasBlockLevelContent) + { + // Table cells must contain at least one block-level element -- add an empty paragraph + nodesList.Add(new XElement(W.p)); + } + transformedNodes = nodesList; + } return new XElement(element.Name, element.Attributes(), - element.Nodes().Select(n => ContentReplacementTransform(n, data, templateError, owningPart))); + transformedNodes); } return node; } diff --git a/TestFiles/DA268-Block-Conditional-In-Table-Cell.docx b/TestFiles/DA268-Block-Conditional-In-Table-Cell.docx new file mode 100644 index 00000000..7796dabb Binary files /dev/null and b/TestFiles/DA268-Block-Conditional-In-Table-Cell.docx differ diff --git a/TestFiles/DA268-data.xml b/TestFiles/DA268-data.xml new file mode 100644 index 00000000..10750a67 --- /dev/null +++ b/TestFiles/DA268-data.xml @@ -0,0 +1,4 @@ + + + False + \ No newline at end of file