Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions OpenXmlPowerTools.Tests/DocumentAssemblerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public void DA102_Throws(string name, string data)

[Theory]
[InlineData("DA025-TemplateDocument.docx", "DA-Data.xml", false)]
[InlineData("DA-lastRenderedPageBreak.docx", "DA-lastRenderedPageBreak.xml", false)]
public void DA103_UseXmlDocument(string name, string data, bool err)
{
var sourceDir = new DirectoryInfo("../../../../TestFiles/");
Expand Down
40 changes: 40 additions & 0 deletions OpenXmlPowerTools.Tests/OpenXmlRegexTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,21 @@ public class OpenXmlRegexTests
</w:body>
</w:document>";

private const string LastRenderedPageBreakXmlString =
@"<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:body>
<w:p>
<w:r>
<w:t>ThisIsAParagraphContainingNoNaturalLi</w:t>
</w:r>
<w:r>
<w:lastRenderedPageBreak/>
<w:t>neBreaksSoTheLineBreakIsForced.</w:t>
</w:r>
</w:p>
</w:body>
</w:document>";

private static string InnerText(XContainer e)
{
return e.Descendants(W.r)
Expand Down Expand Up @@ -365,5 +380,30 @@ public void CanReplaceTextWithFields()
Assert.Equal(1, count);
Assert.Equal("As stated in Article {__1} and this Section {__1.1}, this is described in Exhibit 4.", innerText);
}

[Fact]
public void CanMatchDespiteInvisibleLayoutMarkers()
{
XDocument partDocument = XDocument.Parse(LastRenderedPageBreakXmlString);
XElement p = partDocument.Descendants(W.p).Last();

using (var stream = new MemoryStream())
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(stream, DocumentType))
{
MainDocumentPart part = wordDocument.AddMainDocumentPart();
part.PutXDocument(partDocument);

var content = partDocument.Descendants(W.p);
var regex = new Regex(@"LineBreak");
int count = OpenXmlRegex.Replace(content, regex, "LB", null);

p = partDocument.Descendants(W.p).Last();
string innerText = InnerText(p);

Assert.Equal(2, count);
Assert.Equal("ThisIsAParagraphContainingNoNaturalLBsSoTheLBIsForced.", innerText);
}
}

}
}
29 changes: 29 additions & 0 deletions OpenXmlPowerTools.Tests/UnicodeMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,34 @@ public void CanStringifySymbols()
Assert.Equal(symFromChar1.ToString(SaveOptions.None), symFromChar2.ToString(SaveOptions.None));
Assert.Equal(symFromChar1.ToString(SaveOptions.None), symFromChar3.ToString(SaveOptions.None));
}

private const string LastRenderedPageBreakXmlString =
@"<w:document xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main"">
<w:body>
<w:p>
<w:r>
<w:t>ThisIsAParagraphContainingNoNaturalLi</w:t>
</w:r>
<w:r>
<w:lastRenderedPageBreak/>
<w:t>neBreaksSoTheLineBreakIsForced.</w:t>
</w:r>
</w:p>
</w:body>
</w:document>";

[Fact]
public void IgnoresTemporaryLayoutMarkers()
{
XDocument partDocument = XDocument.Parse(LastRenderedPageBreakXmlString);
XElement p = partDocument.Descendants(W.p).Last();
string actual = p.Descendants(W.r)
.Select(UnicodeMapper.RunToString)
.StringConcatenate();
// p.Value is "the concatenated text content of this element", which
// (in THIS test case, which does not feature any symbols or special
// characters) should exactly match the output of UnicodeMapper:
Assert.Equal(p.Value, actual);
}
}
}
2 changes: 1 addition & 1 deletion OpenXmlPowerTools/OpenXmlRegex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ private static object WmlSearchAndReplaceTransform(XNode node, Regex regex, stri
if (element.Name == W.r)
{
return element.Elements()
.Where(e => e.Name != W.rPr)
.Where(e => e.Name != W.rPr && e.Name != W.lastRenderedPageBreak)
.Select(e => e.Name == W.t
? ((string)e).Select(c =>
new XElement(W.r,
Expand Down
5 changes: 5 additions & 0 deletions OpenXmlPowerTools/UnicodeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public static string RunToString(XElement element)
{
return HorizontalTabulation.ToString();
}
// Ignore temporary layout markers that are not actual document content
if (element.Name == W.lastRenderedPageBreak)
{
return string.Empty;
}

if (element.Name == W.fldChar)
{
Expand Down
Binary file added TestFiles/DA-lastRenderedPageBreak.docx
Binary file not shown.
4 changes: 4 additions & 0 deletions TestFiles/DA-lastRenderedPageBreak.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<TestCase>
<noun1>test</noun1>
<noun2>emergency</noun2>
</TestCase>
Loading