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

Bug fix/document assembler paragraph properties not copied from template #91

Merged
83 changes: 83 additions & 0 deletions Clippit.Tests/Word/DocumentAssemblerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,89 @@ out var returnedTemplateError
Assert.Equal(err, returnedTemplateError);
}

[Theory]
[InlineData("DA289-xhtml-formatting.docx", "DA289-no-inline-styles.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-bold.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-strong.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-italic.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-emphasis.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-underline.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-bold-underline.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-bold-italic.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-italic-underline.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-subscript.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-superscript.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-strikethrough.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-hyperlink.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-hyperlink-bold.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-hyperlink-italic.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-hyperlink-underline.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-hyperlink-no-protocol.xml", 1, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-multi-paragraph.xml", 3, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-invalid.xml", 0, true)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-not-well-formed.xml", 0, true)]
public void DA289(string name, string data, int parasInContent, bool err)
{
var templateDocx = new FileInfo(Path.Combine(_sourceDir.FullName, name));
var dataFile = new FileInfo(Path.Combine(_sourceDir.FullName, data));

var wmlTemplate = new WmlDocument(templateDocx.FullName);
var xmlData = XElement.Load(dataFile.FullName);

var wmlResult = DocumentAssembler.AssembleDocument(wmlTemplate, xmlData, out var returnedTemplateError);
var assembledDocx = new FileInfo(
Path.Combine(TempDir, data.Replace(".xml", "-processed-by-DocumentAssembler.docx"))
);
wmlResult.SaveAs(assembledDocx.FullName);

Validate(assembledDocx);
Assert.Equal(err, returnedTemplateError);

// if we are not expecting an error then verify that we have the same number of paragraphs and that
// the paragraph properties from source and target are the same
if (!err)
{
IList<XElement> sourceParas = wmlTemplate.MainDocumentPart.Element(W.body).Descendants(W.p).ToList();
IList<XElement> targetParas = wmlResult.MainDocumentPart.Element(W.body).Descendants(W.p).ToList();

// Check we have the expected number of paragraphs
// Expected document structure is:
// Heading paragraph (1 line)
// Empty paragraph (1 line)
// Escaped HTML paragraph (potential multi-line)
// CDATA paragraph (potential multi-line)

int expectedParas = sourceParas.Count + (2 * parasInContent) - 2;
Assert.Equal(expectedParas, targetParas.Count);

var equalityComparer = new XNodeEqualityComparer();
int paraOffset = 0;

for (var i = 0; i < sourceParas.Count(); i++)
{
var parasToCompare = i <= 1 ? 1 : parasInContent;
var sourceProps = sourceParas[i].Element(W.pPr);

for (var j = i + paraOffset; j < i + paraOffset + parasToCompare; j++)
{
var targetProps = targetParas[j].Element(W.pPr);
if (sourceProps == null && targetProps == null)
{
continue;
}

Assert.True(equalityComparer.Equals(sourceProps, targetProps));
}

// update paragraph offset versus source when we have processed multi-line content
if (parasToCompare > 1)
{
paraOffset += parasToCompare - 1;
}
}
}
}

[Theory]
[InlineData("DA259-MultiLineContents.docx", "DA-Data.xml", false)]
public void DA259(string name, string data, bool err)
Expand Down
2 changes: 1 addition & 1 deletion Clippit/PtOpenXmlUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5870,7 +5870,7 @@ public static class XhtmlNoNamespace
public static XName ol = "ol";
public static XName ul = "ul";
public static XName li = "li";
public static XName strong = "Bold";
public static XName strong = "strong";
public static XName em = "em";
public static XName tbody = "tbody";
public static XName valign = "valign";
Expand Down
22 changes: 18 additions & 4 deletions Clippit/Word/DocumentAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,8 @@ OpenXmlPart part
return element.CreateContextErrorMessage($"Content: {ex.Message}", templateError);
}

// get XElements and ensure all but the first element is in a
// get XElements and ensure all but the first element is in a paragraph

List<XElement> elements = new List<XElement>();
for (int i = 0; i < content.Count; i++)
{
Expand All @@ -1308,12 +1309,25 @@ OpenXmlPart part
var objEl = obj as XElement;
if (i > 0 && objEl.Name == W.r || objEl.Name == W.hyperlink)
{
elements.Add(new XElement(W.p, currentParaProps, content[i]));
objEl = new XElement(W.p, currentParaProps, content[i]);
}
else
else if (objEl.Name == W.p)
{
elements.Add(objEl);
// get the processed paragraph properties
XElement pProps = objEl.Descendants(W.pPr).FirstOrDefault();
if (pProps != null)
{
pProps.Remove();
}

// add the current paragraph properties
if (currentParaProps != null)
{
objEl.AddFirst(currentParaProps);
}
}

elements.Add(objEl);
}
}

Expand Down
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-bold-italic.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Bold and Italic</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;b&gt;&lt;i&gt;HTML&lt;/i&gt;&lt;/b&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <b><i>CDATA</i></b></p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-bold-underline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Bold and Underline</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;b&gt;&lt;u&gt;HTML&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <b><u>CDATA</u></b></p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-bold.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Bold</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;b&gt;HTML&lt;/b&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <b>CDATA</b></p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-emphasis.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Emphasis</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;em&gt;HTML&lt;/em&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <em>CDATA</em></p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-hyperlink-bold.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Hyperlink and Bold</TestCase>
<EscapedHTML>&lt;b&gt;&lt;a href="https://www.bbc.co.uk"&gt;BBC&lt;/a&gt;&lt;/b&gt;</EscapedHTML>
<CDATA><![CDATA[<b><a href="https://www.bbc.co.uk">BBC</a></b>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-hyperlink-italic.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Hyperlink and Italic</TestCase>
<EscapedHTML>&lt;i&gt;&lt;a href="https://www.bbc.co.uk"&gt;BBC&lt;/a&gt;&lt;/i&gt;</EscapedHTML>
<CDATA><![CDATA[<i><a href="https://www.bbc.co.uk">BBC</a></i>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-hyperlink-no-protocol.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Hyperlink with No Protocol</TestCase>
<EscapedHTML>&lt;a href="www.bbc.co.uk"&gt;BBC&lt;/a&gt;</EscapedHTML>
<CDATA><![CDATA[<a href="www.bbc.co.uk">BBC</a>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-hyperlink-underline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Hyperlink and Underline</TestCase>
<EscapedHTML>&lt;u&gt;&lt;a href="https://www.bbc.co.uk"&gt;BBC&lt;/a&gt;&lt;/u&gt;</EscapedHTML>
<CDATA><![CDATA[<u><a href="https://www.bbc.co.uk">BBC</a></u>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-hyperlink.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Hyperlink</TestCase>
<EscapedHTML>&lt;a href="https://www.bbc.co.uk"&gt;BBC&lt;/a&gt;</EscapedHTML>
<CDATA><![CDATA[<a href="https://www.bbc.co.uk">BBC</a>]]></CDATA>
</Doc>
6 changes: 6 additions & 0 deletions TestFiles/DA/DA289-invalid.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Doc>
<TestCase>Invalid XHTML</TestCase>
<EscapedHTML>This is NOT valid XHTML.&lt;br&gt;But is Valid HTML.</EscapedHTML>
<!-- Invalid XHTML -->
<CDATA><![CDATA[This is NOT valid XHTML.<br>But is Valid HTML.]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-italic-underline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Italic and Underline</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;i&gt;&lt;u&gt;HTML&lt;/u&gt;&lt;/i&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <i><u>CDATA</u></i></p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-italic.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Italic</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;i&gt;HTML&lt;/i&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <i>CDATA</i></p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-multi-paragraph.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Multiple Paragraphs</TestCase>
<EscapedHTML>&lt;p&gt;Boggarts lavender robes, Hermione Granger &lt;i&gt;Fantastic Beasts&lt;/i&gt; and Where to Find Them.&lt;/p&gt;&lt;p&gt;Bee in your bonnet Hand of Glory elder wand, spectacles &lt;b&gt;House Cup&lt;/b&gt; Bertie Bott’s Every Flavor Beans Impedimenta.&lt;/p&gt;&lt;p&gt;Stunning spells tap-dancing spider Slytherin’s Heir &lt;u&gt;mewing kittens&lt;/u&gt; &lt;s&gt;Remus Lupin&lt;/s&gt;.&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>Boggarts lavender robes, Hermione Granger <i>Fantastic Beasts</i> and Where to Find Them.</p><p>Bee in your bonnet Hand of Glory elder wand, spectacles <b>House Cup</b> Bertie Bott’s Every Flavor Beans Impedimenta.</p><p>Stunning spells tap-dancing spider Slytherin’s Heir <u>mewing kittens</u> <s>Remus Lupin</s>.</p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-no-inline-styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>No Inline Styles</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped HTML&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from CDATA</p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-not-well-formed.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Not Well Formed XHTML</TestCase>
<EscapedHTML>&lt;b&gt;&lt;i&gt;This is NOT well formed XHTML.&lt;/b&gt;&lt;/i&gt;</EscapedHTML>
<CDATA><![CDATA[<b><i>This is NOT well formed XHTML.</b></i>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-strikethrough.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Strikethrough</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;s&gt;HTML&lt;/s&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <s>CDATA</s></p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-strong.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Strong</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;strong&gt;HTML&lt;/strong&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <strong>CDATA</strong></p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-subscript.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Subscript</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;sub&gt;HTML&lt;/sub&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <sub>CDATA</sub></p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-superscript.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Superscript</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;sup&gt;HTML&lt;/sup&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <sup>CDATA</sup></p>]]></CDATA>
</Doc>
5 changes: 5 additions & 0 deletions TestFiles/DA/DA289-underline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Doc>
<TestCase>Underline</TestCase>
<EscapedHTML>&lt;p&gt;This content is escaped &lt;u&gt;HTML&lt;/u&gt;&lt;/p&gt;</EscapedHTML>
<CDATA><![CDATA[<p>This content comes from <u>CDATA</u></p>]]></CDATA>
</Doc>
Binary file added TestFiles/DA/DA289-xhtml-formatting.docx
Binary file not shown.
Loading