Skip to content

Commit

Permalink
Fix detecting reserved __GoBack bookmark and ensure it remains after …
Browse files Browse the repository at this point in the history
…the appended output
  • Loading branch information
onizet committed Sep 16, 2024
1 parent de8b612 commit d31d51b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Html2OpenXml/HtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ public async Task ParseHtml(string html, CancellationToken cancellationToken = d
body.Append(para);

// move the paragraph with BookmarkStart `_GoBack` as the last child
// That bookmark is continuously moved after the last edit
var p = body.GetFirstChild<Paragraph>();
if (p != null && p.GetFirstChild<BookmarkStart>()?.Id == "_GoBack")
if (p != null && p.GetFirstChild<BookmarkStart>()?.Name == "_GoBack")
{
p.Remove();
body.Append(p);
Expand Down
24 changes: 24 additions & 0 deletions test/HtmlToOpenXml.Tests/BodyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,29 @@ public bool PageOrientation_OverrideExistingLayout_ReturnsLandscapeDimension(str
var bidi = mainPart.Document.Body!.GetFirstChild<SectionProperties>()?.GetFirstChild<BiDi>();
return bidi?.Val?.Value;
}

[Test(Description = "Bookmark _GoBack is reserved and must stand after the last edit change")]
public async Task WithGoBackBookmark_ShouldBeAfterAppendedOutput()
{
using var generatedDocument = new MemoryStream();
using (var buffer = ResourceHelper.GetStream("Resources.DocWithCustomStyle.docx"))
buffer.CopyTo(generatedDocument);

generatedDocument.Position = 0L;
using WordprocessingDocument package = WordprocessingDocument.Open(generatedDocument, true);
MainDocumentPart mainPart = package.MainDocumentPart!;

var goBackBookmark = mainPart.Document.Body!.Elements<Paragraph>()
.FirstOrDefault(p => p.GetFirstChild<BookmarkStart>()?.Name?.Value == "_GoBack");
Assert.That(goBackBookmark, Is.Not.Null);

HtmlConverter converter = new HtmlConverter(mainPart);
await converter.ParseHtml("<p>Placeholder</p>");

Assert.That(mainPart.Document.Body!.LastChild, Is.TypeOf<SectionProperties>());
var paragrahs = mainPart.Document.Body!.Elements<Paragraph>();
Assert.That(paragrahs.Count(), Is.EqualTo(2));
Assert.That(paragrahs.Last().GetFirstChild<BookmarkStart>()?.Name?.Value, Is.EqualTo("_GoBack"));
}
}
}

0 comments on commit d31d51b

Please sign in to comment.