Skip to content

Commit a5ac68a

Browse files
committed
Fix detecting reserved __GoBack bookmark and ensure it remains as last bookmark of the body
1 parent de8b612 commit a5ac68a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/Html2OpenXml/HtmlConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,9 @@ public async Task ParseHtml(string html, CancellationToken cancellationToken = d
125125
body.Append(para);
126126

127127
// move the paragraph with BookmarkStart `_GoBack` as the last child
128+
// That bookmark is continuously moved after the last edit
128129
var p = body.GetFirstChild<Paragraph>();
129-
if (p != null && p.GetFirstChild<BookmarkStart>()?.Id == "_GoBack")
130+
if (p != null && p.GetFirstChild<BookmarkStart>()?.Name == "_GoBack")
130131
{
131132
p.Remove();
132133
body.Append(p);

test/HtmlToOpenXml.Tests/BodyTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,29 @@ public bool PageOrientation_OverrideExistingLayout_ReturnsLandscapeDimension(str
5555
var bidi = mainPart.Document.Body!.GetFirstChild<SectionProperties>()?.GetFirstChild<BiDi>();
5656
return bidi?.Val?.Value;
5757
}
58+
59+
[Test(Description = "Bookmark _GoBack is reserved and must stand after the last edit change")]
60+
public async Task WithGoBackBookmark_ShouldBeAfterAppendedOutput()
61+
{
62+
using var generatedDocument = new MemoryStream();
63+
using (var buffer = ResourceHelper.GetStream("Resources.DocWithCustomStyle.docx"))
64+
buffer.CopyTo(generatedDocument);
65+
66+
generatedDocument.Position = 0L;
67+
using WordprocessingDocument package = WordprocessingDocument.Open(generatedDocument, true);
68+
MainDocumentPart mainPart = package.MainDocumentPart!;
69+
70+
var goBackBookmark = mainPart.Document.Body!.Elements<Paragraph>()
71+
.FirstOrDefault(p => p.GetFirstChild<BookmarkStart>()?.Name?.Value == "_GoBack");
72+
Assert.That(goBackBookmark, Is.Not.Null);
73+
74+
HtmlConverter converter = new HtmlConverter(mainPart);
75+
await converter.ParseHtml("<p>Placeholder</p>");
76+
77+
Assert.That(mainPart.Document.Body!.LastChild, Is.TypeOf<SectionProperties>());
78+
var paragrahs = mainPart.Document.Body!.Elements<Paragraph>();
79+
Assert.That(paragrahs.Count(), Is.EqualTo(2));
80+
Assert.That(paragrahs.Last().GetFirstChild<BookmarkStart>()?.Name?.Value, Is.EqualTo("_GoBack"));
81+
}
5882
}
5983
}

0 commit comments

Comments
 (0)