Skip to content

Commit

Permalink
Merge branch 'master' into feature/document-and-document-templates
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-tihon committed Jan 26, 2025
2 parents e19d7b1 + 2aa5126 commit f43d5ce
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [2.4.2] - January 15, 2025

- fix: Font Sizing Font Styling and Font Coloring is not working #94
- hk: dependencies update

## [2.4.1] - January 4, 2025

- fix: Bug fix/document assembler paragraph properties not copied from template #91
Expand Down
6 changes: 3 additions & 3 deletions Clippit.Tests/Clippit.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit.v3" Version="1.0.0" />
<PackageReference Include="xunit.v3.runner.console" Version="1.0.0">
<PackageReference Include="xunit.v3" Version="1.0.1" />
<PackageReference Include="xunit.v3.runner.console" Version="1.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
5 changes: 2 additions & 3 deletions Clippit.Tests/Word/DocumentAssemblerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using Xunit;
using static System.Runtime.InteropServices.JavaScript.JSType;

#if !ELIDE_XUNIT_TESTS

Expand Down Expand Up @@ -146,9 +147,6 @@ public class DocumentAssemblerTests(ITestOutputHelper log) : TestsBase(log)
[InlineData("DA285-ImageSelectNoParagraphFollowedAfterMetadata.docx", "DA-Data-WithImages.xml", true)]
[InlineData("DA285A-ImageSelectNoParagraphFollowedAfterMetadata.docx", "DA-Data-WithImages.xml", true)]
[InlineData("DA-I0038-TemplateWithMultipleXPathResults.docx", "DA-I0038-Data.xml", false)]
[InlineData("DA289A-xhtml-formatting.docx", "DA-html-input.xml", false)]
[InlineData("DA289B-html-not-supported.docx", "DA-html-input.xml", true)]
[InlineData("DA289C-not-well-formed-xhtml.docx", "DA-html-input.xml", true)]
public void DA101(string name, string data, bool err)
{
var templateDocx = new FileInfo(Path.Combine(_sourceDir.FullName, name));
Expand Down Expand Up @@ -192,6 +190,7 @@ out var returnedTemplateError
[InlineData("DA289-xhtml-formatting.docx", "DA289-multi-paragraph.xml", 3, false)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-invalid.xml", 0, true)]
[InlineData("DA289-xhtml-formatting.docx", "DA289-not-well-formed.xml", 0, true)]
[InlineData("DA289-xhtml-merge-run-formatting.docx", "DA289-xhtml-merge-run-formatting.xml", 1, false)]
public void DA289(string name, string data, int parasInContent, bool err)
{
var templateDocx = new FileInfo(Path.Combine(_sourceDir.FullName, name));
Expand Down
73 changes: 73 additions & 0 deletions Clippit/Word/Assembler/XElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace Clippit.Word.Assembler
{
internal static class XElementExtensions
{
internal static void MergeRunProperties(
this XElement element,
XElement paraRunProperties,
XElement runRunProperties
)
{
// merge run properties of paragraph properties
if (element.Name == W.p && paraRunProperties != null)
{
XElement paraProps = element.Elements(W.pPr).FirstOrDefault();
if (paraProps != null)
{
XElement paraRunProps = paraProps.Elements(W.rPr).FirstOrDefault();
if (paraRunProps == null)
{
paraProps.Add(paraRunProperties);
}
else
{
paraRunProps.MergeOriginalRunProperties(paraRunProperties);
}
}
}

// merge run properties of runs
if (runRunProperties != null)
{
foreach (var run in element.DescendantsAndSelf(W.r))
{
XElement runProps = run.Elements(W.rPr).FirstOrDefault();
if (runProps == null)
{
run.AddFirst(runRunProperties);
}
else
{
runProps.MergeOriginalRunProperties(runRunProperties);
}
}
}
}

private static void MergeOriginalRunProperties(this XElement runProps, XElement originalRunProps)
{
foreach (var prop in originalRunProps.Elements())
{
if (runProps.Element(prop.Name) == null)
{
if (prop.Name == W.rStyle)
{
runProps.AddFirst(prop);
}
else
{
runProps.Add(prop);
}
}
}
}
}
}
8 changes: 8 additions & 0 deletions Clippit/Word/DocumentAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,12 @@ OpenXmlPart part

XElement currentPara = embeddedPara ?? parentPara;
XElement currentParaProps = currentPara.Descendants(W.pPr).FirstOrDefault();
XElement currentParaRunProps = currentParaProps?.Elements(W.rPr).FirstOrDefault();
XElement currentRunRunProps = element
.Descendants(W.r)
.FirstOrDefault()
?.Elements(W.rPr)
.FirstOrDefault();

// get the list of created elements, could be all paragraphs or a run followed by paragraphs
List<object> content;
Expand Down Expand Up @@ -1424,6 +1430,8 @@ OpenXmlPart part
}
}

objEl.MergeRunProperties(currentParaRunProps, currentRunRunProps);

elements.Add(objEl);
}
}
Expand Down
Binary file not shown.
6 changes: 6 additions & 0 deletions TestFiles/DA/DA289-xhtml-merge-run-formatting.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Doc>
<TestCase>1000101</TestCase>
<Name>TestCustomer</Name>
<CDATA><![CDATA[Testing Data]]></CDATA>
<EscapedHTML>TestURL</EscapedHTML>
</Doc>
Binary file removed TestFiles/DA/DA289A-xhtml-formatting.docx
Binary file not shown.
Binary file removed TestFiles/DA/DA289B-html-not-supported.docx
Binary file not shown.
Binary file removed TestFiles/DA/DA289C-not-well-formed-xhtml.docx
Binary file not shown.

0 comments on commit f43d5ce

Please sign in to comment.