-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Amberg/workitems/bug_fix
Bookmark bug fix
- Loading branch information
Showing
11 changed files
with
322 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
DocxTemplater.Test/BookmarkInDifferentTableRowDoesNotcauseCrash.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using DocumentFormat.OpenXml; | ||
using DocumentFormat.OpenXml.Packaging; | ||
using DocumentFormat.OpenXml.Wordprocessing; | ||
|
||
namespace DocxTemplater.Test | ||
{ | ||
internal class BookmarkInDifferentTableRowDoesNotcauseCrash | ||
{ | ||
[Test] | ||
public void Test() | ||
{ | ||
string content = @"<w:p xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""> | ||
<w:r> | ||
<w:t xml:space=""preserve"">This is sentence one.</w:t> | ||
</w:r> | ||
<w:bookmarkStart w:id=""0"" w:name=""testing123""/> | ||
<w:r> | ||
<w:t>This is sentence two. {{.}}</w:t> | ||
</w:r> | ||
</w:p> | ||
<w:p xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main""> | ||
<w:r> | ||
<w:t xml:space=""preserve"">This </w:t> | ||
</w:r> | ||
<w:bookmarkEnd w:id=""0""/> | ||
<w:r> | ||
<w:t>is sentence three.{{.}}</w:t> | ||
</w:r> | ||
</w:p>"; | ||
|
||
using var memStream = new MemoryStream(); | ||
using var wpDocument = WordprocessingDocument.Create(memStream, WordprocessingDocumentType.Document); | ||
var mainPart = wpDocument.AddMainDocumentPart(); | ||
mainPart.Document = new Document | ||
{ | ||
Body = new Body | ||
{ | ||
InnerXml = content | ||
} | ||
}; | ||
wpDocument.Save(); | ||
memStream.Position = 0; | ||
var docTemplate = new DocxTemplate(memStream); | ||
docTemplate.BindModel("ds", "hi there"); | ||
var result = docTemplate.Process(); | ||
docTemplate.Validate(); | ||
|
||
// get body | ||
var document = WordprocessingDocument.Open(result, false); | ||
var body = document.MainDocumentPart.Document.Body; | ||
Assert.That(body.Descendants<BookmarkEnd>().Count(), Is.EqualTo(0)); | ||
Assert.That(body.Descendants<BookmarkStart>().Count(), Is.EqualTo(0)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using DocumentFormat.OpenXml.Packaging; | ||
using DocumentFormat.OpenXml.Wordprocessing; | ||
using DocumentFormat.OpenXml; | ||
|
||
namespace DocxTemplater.Test | ||
{ | ||
internal class SpecialCollectionVariableTest | ||
{ | ||
[Test] | ||
public void TestIndexVariableInLoop() | ||
{ | ||
var model = new[] { "Item1", "Item2", "Item3", "Item4" }; | ||
var template = "Items:{{#Items}}{{Items._Idx}}{{.}} {{/Items}}"; | ||
|
||
using var memStream = new MemoryStream(); | ||
using var wpDocument = WordprocessingDocument.Create(memStream, WordprocessingDocumentType.Document); | ||
MainDocumentPart mainPart = wpDocument.AddMainDocumentPart(); | ||
mainPart.Document = new Document(new Body(new Paragraph(new Run(new Text(template))))); | ||
wpDocument.Save(); | ||
memStream.Position = 0; | ||
var docTemplate = new DocxTemplate(memStream); | ||
docTemplate.BindModel("Items", model); | ||
var result = docTemplate.Process(); | ||
result.Position = 0; | ||
// compare body | ||
result.Position = 0; | ||
var document = WordprocessingDocument.Open(result, false); | ||
var body = document.MainDocumentPart.Document.Body; | ||
Assert.That(body.InnerXml, Is.EqualTo("<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" + | ||
"<w:r>" + | ||
"<w:t xml:space=\"preserve\">Items:</w:t><w:t xml:space=\"preserve\">1</w:t><w:t xml:space=\"preserve\">Item1</w:t>" + | ||
"<w:t xml:space=\"preserve\"> </w:t><w:t xml:space=\"preserve\">2</w:t><w:t xml:space=\"preserve\">Item2</w:t>" + | ||
"<w:t xml:space=\"preserve\"> </w:t><w:t xml:space=\"preserve\">3</w:t><w:t xml:space=\"preserve\">Item3</w:t>" + | ||
"<w:t xml:space=\"preserve\"> </w:t><w:t xml:space=\"preserve\">4</w:t><w:t xml:space=\"preserve\">Item4</w:t>" + | ||
"<w:t xml:space=\"preserve\"> </w:t></w:r></w:p>")); | ||
} | ||
|
||
[Test] | ||
public void TestConditionWithIndexVariableInLoop() | ||
{ | ||
var model = new[] { "Item1", "Item2", "Item3", "Item4" }; | ||
var template = "Items:{{#Items}}{?{Items._Idx % 2 == 0}}{{.}}{{/}}{{/Items}}"; | ||
|
||
using var memStream = new MemoryStream(); | ||
using var wpDocument = WordprocessingDocument.Create(memStream, WordprocessingDocumentType.Document); | ||
MainDocumentPart mainPart = wpDocument.AddMainDocumentPart(); | ||
mainPart.Document = new Document(new Body(new Paragraph(new Run(new Text(template))))); | ||
wpDocument.Save(); | ||
memStream.Position = 0; | ||
var docTemplate = new DocxTemplate(memStream); | ||
docTemplate.BindModel("Items", model); | ||
var result = docTemplate.Process(); | ||
result.Position = 0; | ||
// compare body | ||
result.Position = 0; | ||
var document = WordprocessingDocument.Open(result, false); | ||
var body = document.MainDocumentPart.Document.Body; | ||
Assert.That(body.InnerXml, Is.EqualTo("<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\"><w:r><w:t xml:space=\"preserve\">Items:" + | ||
"</w:t><w:t xml:space=\"preserve\">Item2</w:t>" + | ||
"<w:t xml:space=\"preserve\">Item4</w:t></w:r></w:p>")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.