Skip to content

Commit c17f8ad

Browse files
committed
Improve support of table alignment #187
1 parent 2c4d0e5 commit c17f8ad

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

src/Html2OpenXml/Expressions/BlockElementExpression.cs

+38-12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class BlockElementExpression: PhrasingElementExpression
2727
{
2828
private readonly OpenXmlLeafElement[]? defaultStyleProperties;
2929
protected readonly ParagraphProperties paraProperties = new();
30+
protected TableProperties? tableProperties;
3031
// some style attributes, such as borders or bgcolor, will convert this node to a framed container
3132
protected bool renderAsFramed;
3233
private HtmlBorder styleBorder;
@@ -115,22 +116,44 @@ protected override IEnumerable<OpenXmlElement> Interpret (
115116
public override void CascadeStyles(OpenXmlElement element)
116117
{
117118
base.CascadeStyles(element);
118-
if (!paraProperties.HasChildren || element is not Paragraph paragraph)
119+
if (!paraProperties.HasChildren)
119120
return;
120121

121-
paragraph.ParagraphProperties ??= new ParagraphProperties();
122-
123-
var knownTags = new HashSet<string>();
124-
foreach (var prop in paragraph.ParagraphProperties)
122+
if (element is Paragraph paragraph)
125123
{
126-
if (!knownTags.Contains(prop.LocalName))
127-
knownTags.Add(prop.LocalName);
128-
}
124+
paragraph.ParagraphProperties ??= new ParagraphProperties();
129125

130-
foreach (var prop in paraProperties)
126+
var knownTags = new HashSet<string>();
127+
foreach (var prop in paragraph.ParagraphProperties)
128+
{
129+
if (!knownTags.Contains(prop.LocalName))
130+
knownTags.Add(prop.LocalName);
131+
}
132+
133+
foreach (var prop in paraProperties)
134+
{
135+
if (!knownTags.Contains(prop.LocalName))
136+
paragraph.ParagraphProperties.AddChild(prop.CloneNode(true));
137+
}
138+
}
139+
else if (tableProperties != null && element is Table table)
131140
{
132-
if (!knownTags.Contains(prop.LocalName))
133-
paragraph.ParagraphProperties.AddChild(prop.CloneNode(true));
141+
var props = table.GetFirstChild<TableProperties>();
142+
if (props is null)
143+
return;
144+
145+
var knownTags = new HashSet<string>();
146+
foreach (var prop in props)
147+
{
148+
if (!knownTags.Contains(prop.LocalName))
149+
knownTags.Add(prop.LocalName);
150+
}
151+
152+
foreach (var prop in tableProperties)
153+
{
154+
if (!knownTags.Contains(prop.LocalName))
155+
props.AddChild(prop.CloneNode(true));
156+
}
134157
}
135158
}
136159

@@ -170,9 +193,12 @@ protected override void ComposeStyles (ParsingContext context)
170193

171194
JustificationValues? align = Converter.ToParagraphAlign(styleAttributes!["text-align"]);
172195
if (!align.HasValue) align = Converter.ToParagraphAlign(node.GetAttribute("align"));
196+
if (!align.HasValue) align = Converter.ToParagraphAlign(styleAttributes["justify-content"]);
173197
if (align.HasValue)
174198
{
175199
paraProperties.Justification = new() { Val = align };
200+
tableProperties ??= new();
201+
tableProperties.TableJustification = new() { Val = align.Value.ToTableRowAlignment() };
176202
}
177203

178204

@@ -194,7 +220,7 @@ protected override void ComposeStyles (ParsingContext context)
194220
}
195221

196222
var margin = styleAttributes.GetMargin("margin");
197-
Indentation? indentation = null;
223+
Indentation? indentation = null;
198224
if (!margin.IsEmpty)
199225
{
200226
if (margin.Top.IsFixed || margin.Bottom.IsFixed)

src/Html2OpenXml/Expressions/Table/TableExpression.cs

+2
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ protected override void ComposeStyles (ParsingContext context)
199199
}
200200

201201
var align = Converter.ToParagraphAlign(tableNode.GetAttribute("align"));
202+
if (!align.HasValue)
203+
align = Converter.ToParagraphAlign(styleAttributes["justify-self"]);
202204
if (align.HasValue)
203205
tableProperties.TableJustification = new() { Val = align.Value.ToTableRowAlignment() };
204206

test/HtmlToOpenXml.Tests/TableTests.cs

+30-4
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,9 @@ public void TableCaption_ReturnsPositionedParagraph(string position, int caption
330330
});
331331
}
332332

333-
[TestCase("right", "right")]
334-
[TestCase("", "center")]
335-
public void TableCaptionAlign_ReturnsPositionedParagraph_AlignedWithTable(string alignment, string expectedAlign)
333+
[TestCase("right", ExpectedResult = "right")]
334+
[TestCase("", ExpectedResult = "center")]
335+
public string? TableCaptionAlign_ReturnsPositionedParagraph_AlignedWithTable(string alignment)
336336
{
337337
var elements = converter.Parse(@$"<table align=""center"">
338338
<caption align=""{alignment}"">Some table caption</caption>
@@ -341,7 +341,33 @@ public void TableCaptionAlign_ReturnsPositionedParagraph_AlignedWithTable(string
341341

342342
Assert.That(elements, Has.Count.EqualTo(2));
343343
var caption = (Paragraph) elements[1];
344-
Assert.That(caption.ParagraphProperties?.Justification?.Val?.ToString(), Is.EqualTo(expectedAlign));
344+
return caption.ParagraphProperties?.Justification?.Val?.ToString();
345+
}
346+
347+
[TestCase("align='right'", ExpectedResult = "right")]
348+
[TestCase("style='justify-self:center'", ExpectedResult = "center")]
349+
public string? TableAlign_ReturnsTableJustification(string style)
350+
{
351+
var elements = converter.Parse(@$"<table {style}>
352+
<tr><td>Cell 1.1</td></tr>
353+
</table>");
354+
355+
Assert.That(elements, Has.Count.EqualTo(1));
356+
return elements[0].GetFirstChild<TableProperties>()?.TableJustification?.Val?.ToString();
357+
}
358+
359+
[TestCase("", ExpectedResult = "right")]
360+
[TestCase("justify-self:center", ExpectedResult = "center")]
361+
public string? NestedTableAlign_ReturnsTableOrParentJustification(string tableStyle)
362+
{
363+
var elements = converter.Parse(@$"<div style='justify-content: right'>
364+
<table style='{tableStyle}'>
365+
<tr><td>Cell 1.1</td></tr>
366+
</table>
367+
</div>");
368+
369+
Assert.That(elements, Has.Count.EqualTo(1));
370+
return elements[0].GetFirstChild<TableProperties>()?.TableJustification?.Val?.ToString();
345371
}
346372

347373
[Test]

0 commit comments

Comments
 (0)