Skip to content

Commit

Permalink
Fixed mess I made of merge, tests running and passing, csharpier re-run.
Browse files Browse the repository at this point in the history
  • Loading branch information
MalcolmJohnston committed Jan 10, 2025
1 parent bf821fe commit e19d7b1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 50 deletions.
47 changes: 21 additions & 26 deletions Clippit.Tests/Word/DocumentAssemblerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ public void DA289(string name, string data, int parasInContent, bool err)
var wmlTemplate = new WmlDocument(templateDocx.FullName);
var xmlData = XElement.Load(dataFile.FullName);

var wmlResult = DocumentAssembler.AssembleDocument(
wmlTemplate,
xmlData,
out var returnedTemplateError
);
var wmlResult = DocumentAssembler.AssembleDocument(wmlTemplate, xmlData, out var returnedTemplateError);
var assembledDocx = new FileInfo(
Path.Combine(TempDir, data.Replace(".xml", "-processed-by-DocumentAssembler.docx"))
);
Expand Down Expand Up @@ -405,27 +401,26 @@ private void Validate(FileInfo fi)
Validate(wDoc, s_expectedErrors);
}

private static readonly List<string> s_expectedErrors =
new()
{
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenVBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRow' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRowFirstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRowLastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRow' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRowFirstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRowLastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:noHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:noVBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:oddHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:oddVBand' attribute is not declared.",
"The 'http://schemas.microsoft.com/office/word/2012/wordml:restartNumberingAfterBreak' attribute is not declared.",
"The 'http://schemas.microsoft.com/office/word/2016/wordml/cid:durableId' attribute is not declared.",
"Attribute 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:val' should have unique value. Its current value",
};
private static readonly List<string> s_expectedErrors = new()
{
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:evenVBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRow' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRowFirstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:firstRowLastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRow' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRowFirstColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:lastRowLastColumn' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:noHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:noVBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:oddHBand' attribute is not declared.",
"The 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:oddVBand' attribute is not declared.",
"The 'http://schemas.microsoft.com/office/word/2012/wordml:restartNumberingAfterBreak' attribute is not declared.",
"The 'http://schemas.microsoft.com/office/word/2016/wordml/cid:durableId' attribute is not declared.",
"Attribute 'http://schemas.openxmlformats.org/wordprocessingml/2006/main:val' should have unique value. Its current value",
};
}
}

Expand Down
2 changes: 1 addition & 1 deletion Clippit/Html/HtmlToWmlConverterCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3043,7 +3043,7 @@ internal static XElement GetRunProperties(XText textNode, HtmlToWmlConverterSett
{
return GetRunProperties(parent, settings);
}

return new XElement(W.rPr);
}

Expand Down
42 changes: 26 additions & 16 deletions Clippit/Word/Assembler/HtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

using Clippit.Html;
using Clippit.Internal;
using DocumentFormat.OpenXml.Packaging;

using NextExpected = Clippit.Html.HtmlToWmlConverterCore.NextExpected;

namespace Clippit.Word.Assembler
{
internal static class HtmlConverter
{
private static readonly HtmlToWmlConverterSettings htmlConverterSettings = HtmlToWmlConverter.GetDefaultSettings();
private static readonly HtmlToWmlConverterSettings htmlConverterSettings =
HtmlToWmlConverter.GetDefaultSettings();

private static readonly Regex detectEntityRegEx = new Regex("^&(?:#([0-9]+)|#x([0-9a-fA-F]+)|([0-9a-zA-Z]+));");

Expand Down Expand Up @@ -48,17 +47,25 @@ ref OpenXmlPart part
}

// otherwise split the values if there are new line characters
values = values.SelectMany(x => x.Replace("\r\n", "\n", StringComparison.OrdinalIgnoreCase)
.Split('\n'))
.ToArray();
values = values
.SelectMany(x => x.Replace("\r\n", "\n", StringComparison.OrdinalIgnoreCase).Split('\n'))
.ToArray();

List<object> results = new List<object>();
for(int i = 0; i < values.Length; i++)
for (int i = 0; i < values.Length; i++)
{
// try processing as XML
XElement parsedElement = XElement.Parse($"<xhtml>{EscapeAmpersands(values[i])}</xhtml>");

results.Add(Transform(parsedElement, htmlConverterSettings, part, i == 0 ? NextExpected.Run : NextExpected.Paragraph, true));
results.Add(
Transform(
parsedElement,
htmlConverterSettings,
part,
i == 0 ? NextExpected.Run : NextExpected.Paragraph,
true
)
);
}

results = FlattenResults(results);
Expand Down Expand Up @@ -168,7 +175,7 @@ bool preserveWhiteSpace
{
return new XElement(W.p, hyperlink);
}

return new[] { hyperlink };
}
}
Expand All @@ -185,10 +192,7 @@ bool preserveWhiteSpace
if (
element
.Descendants()
.Any(d =>
d.Name == XhtmlNoNamespace.li ||
d.Name == XhtmlNoNamespace.p
)
.Any(d => d.Name == XhtmlNoNamespace.li || d.Name == XhtmlNoNamespace.p)
)
{
return element
Expand Down Expand Up @@ -302,7 +306,11 @@ bool preserveWhiteSpace
new XElement(
W.r,
HtmlToWmlConverterCore.GetRunProperties(xTextNode, settings),
new XElement(W.t, HtmlToWmlConverterCore.GetXmlSpaceAttribute(textNodeString), textNodeString)
new XElement(
W.t,
HtmlToWmlConverterCore.GetXmlSpaceAttribute(textNodeString),
textNodeString
)
)
);
return p;
Expand All @@ -315,7 +323,10 @@ bool preserveWhiteSpace
var element = node as XElement;
if (element != null)
{
return element.Nodes().Select(n => Transform(n, settings, part, nextExpected, preserveWhiteSpace)).AsEnumerable();
return element
.Nodes()
.Select(n => Transform(n, settings, part, nextExpected, preserveWhiteSpace))
.AsEnumerable();
}
else
{
Expand All @@ -331,7 +342,6 @@ bool preserveWhiteSpace
}
}


private static string EscapeAmpersands(string value)
{
// check whether we have any processing to do
Expand Down
8 changes: 1 addition & 7 deletions Clippit/Word/DocumentAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -944,8 +944,6 @@ private static string ValidatePerSchema(XElement element)
},
};



/// <summary>
/// Calculates the maximum docPr id. The identifier is
/// unique throughout the document. This method
Expand Down Expand Up @@ -1486,11 +1484,7 @@ OpenXmlPart part
return documentBytes.GetBase64EncodedDocumentElement();
}

// add all but the first element after the current paragraph
for (int i = elements.Count - 1; i > 0; i--)
{
return element.CreateContextErrorMessage($"Template not found at '{documentPath}'", templateError);
}
return element.CreateContextErrorMessage($"Template not found at '{documentPath}'", templateError);
}
if (element.Name == PA.DocumentTemplate)
{
Expand Down

0 comments on commit e19d7b1

Please sign in to comment.