Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header and Footer support #163

Merged
merged 8 commits into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Unit test code to assert new header-footer are correctly registred
  • Loading branch information
onizet committed Sep 20, 2024
commit 2e74a8739b814b6b6eda5f5e02b1846a17272b9c
12 changes: 12 additions & 0 deletions src/Html2OpenXml/HtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public async Task ParseHeader(string html, CancellationToken cancellationToken =
if (partId != null)
{
var sectionProps = mainPart.Document.Body!.Elements<SectionProperties>();
if (!sectionProps.Any())
{
sectionProps = [new SectionProperties()];
mainPart.Document.Body!.AddChild(sectionProps.First());
}

foreach (var sectPr in sectionProps)
{
sectPr.RemoveAllChildren<HeaderReference>();
Expand Down Expand Up @@ -178,6 +184,12 @@ public async Task ParseFooter(string html, CancellationToken cancellationToken =
if (partId != null)
{
var sectionProps = mainPart.Document.Body!.Elements<SectionProperties>();
if (!sectionProps.Any())
{
sectionProps = [new SectionProperties()];
mainPart.Document.Body!.AddChild(sectionProps.First());
}

foreach (var sectPr in sectionProps)
{
sectPr.RemoveAllChildren<FooterReference>();
Expand Down
42 changes: 42 additions & 0 deletions test/HtmlToOpenXml.Tests/HeaderFooterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using NUnit.Framework;
using DocumentFormat.OpenXml.Wordprocessing;

namespace HtmlToOpenXml.Tests
{
/// <summary>
/// Tests on <c>ParseHeader</c> and <c>ParseFooter</c> methods.
/// </summary>
[TestFixture]
public class HeaderFooterTests : HtmlConverterTestBase
{
[Test]
public async Task Header_ReturnsHeaderPartLinkedToBody()
{
await converter.ParseHeader("<p>Header content</p>");

var headerPart = mainPart.HeaderParts?.FirstOrDefault();
Assert.That(headerPart, Is.Not.Null);
Assert.That(headerPart.Header, Is.Not.Null);

var sectionProperties = mainPart.Document.Body!.Elements<SectionProperties>();
Assert.That(sectionProperties, Is.Not.Empty);
Assert.That(sectionProperties.Any(s => s.HasChild<HeaderReference>()), Is.True);
AssertThatOpenXmlDocumentIsValid();
}

[Test]
public async Task Footer_ReturnsFooterPartLinkedToBody()
{
await converter.ParseFooter("<p>Footer content</p>");

var footerPart = mainPart.FooterParts?.FirstOrDefault();
Assert.That(footerPart, Is.Not.Null);
Assert.That(footerPart.Footer, Is.Not.Null);

var sectionProperties = mainPart.Document.Body!.Elements<SectionProperties>();
Assert.That(sectionProperties, Is.Not.Empty);
Assert.That(sectionProperties.Any(s => s.HasChild<FooterReference>()), Is.True);
AssertThatOpenXmlDocumentIsValid();
}
}
}
17 changes: 17 additions & 0 deletions test/HtmlToOpenXml.Tests/HtmlConverterTestBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using DocumentFormat.OpenXml.Wordprocessing;
using NUnit.Framework;

Expand Down Expand Up @@ -36,5 +37,21 @@ public void Close ()
package?.Dispose();
generatedDocument?.Dispose();
}

protected void AssertThatOpenXmlDocumentIsValid()
{
var validator = new OpenXmlValidator(FileFormatVersions.Office2021);
var errors = validator.Validate(package);

if (!errors.GetEnumerator().MoveNext())
return;

foreach (ValidationErrorInfo error in errors)
{
TestContext.Error.Write("{0}\n\t{1}\n", error.Path?.XPath, error.Description);
}

Assert.Fail("The document doesn't look 100% compatible with Office 2021");
}
}
}
1 change: 1 addition & 0 deletions test/HtmlToOpenXml.Tests/ImgTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public async Task ParseIntoDocumentPart_ReturnsImageParentedToPart (Type openXml
var p = host.ChildElements.FirstOrDefault(c => c is Paragraph);
Assert.That(p, Is.Not.Null);
AssertIsImg(container, p);
AssertThatOpenXmlDocumentIsValid();
}

private static Drawing AssertIsImg (OpenXmlPartContainer container, OpenXmlElement paragraph)
Expand Down
1 change: 1 addition & 0 deletions test/HtmlToOpenXml.Tests/LinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ public async Task ParseIntoDocumentPart_ReturnsHyperlinkParentedToPart (Type ope
}

AssertHyperlink(container, host.ChildElements);
AssertThatOpenXmlDocumentIsValid();
}

private static void AssertHyperlink(OpenXmlPartContainer container, IEnumerable<OpenXmlElement> elements)
Expand Down
2 changes: 2 additions & 0 deletions test/HtmlToOpenXml.Tests/NumberingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public async Task ContinueNumbering_ReturnsSecondList_ContinueOrder()
e.ParagraphProperties?.NumberingProperties?.NumberingId?.Val?.Value),
Has.All.EqualTo(instances.First().NumberID!.Value),
"All paragraphs are linked to the same list instance");
AssertThatOpenXmlDocumentIsValid();
}

[Test(Description = "Stop indenting from existing numbering (issue #57)")]
Expand Down Expand Up @@ -321,6 +322,7 @@ public async Task DisableContinueNumbering_ReturnsSecondList_RestartingOrder()
e.ParagraphProperties?.NumberingProperties?.NumberingId?.Val?.Value),
Is.Unique,
"All paragraphs use different list instances");
AssertThatOpenXmlDocumentIsValid();
}

/// <summary>
Expand Down