Skip to content

Commit 9f4cdbf

Browse files
committed
Support margin auto for table alignment #194
1 parent 462e507 commit 9f4cdbf

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/Html2OpenXml/Expressions/Table/TableExpression.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,6 @@ protected override void ComposeStyles (ParsingContext context)
201201
}
202202
}
203203

204-
var align = Converter.ToParagraphAlign(tableNode.GetAttribute("align"));
205-
if (!align.HasValue)
206-
align = Converter.ToParagraphAlign(styleAttributes["justify-self"]);
207-
if (align.HasValue)
208-
tableProperties.TableJustification = new() { Val = align.Value.ToTableRowAlignment() };
209-
210204
var dir = tableNode.GetTextDirection();
211205
if (dir.HasValue)
212206
tableProperties.BiDiVisual = new() {
@@ -286,5 +280,22 @@ protected override void ComposeStyles (ParsingContext context)
286280
};
287281
}
288282
}
283+
284+
var align = Converter.ToParagraphAlign(tableNode.GetAttribute("align"))
285+
?? Converter.ToParagraphAlign(styleAttributes["justify-self"]);
286+
if (!align.HasValue)
287+
{
288+
var margin = styleAttributes.GetMargin("margin");
289+
if (margin.Left.Type == UnitMetric.Auto)
290+
{
291+
if (margin.Right.Type == UnitMetric.Auto)
292+
align = JustificationValues.Center;
293+
else
294+
align = JustificationValues.Right;
295+
}
296+
}
297+
298+
if (align.HasValue)
299+
tableProperties.TableJustification = new() { Val = align.Value.ToTableRowAlignment() };
289300
}
290301
}

test/HtmlToOpenXml.Tests/TableTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ public void TableCaption_ReturnsPositionedParagraph(string position, int caption
352352

353353
[TestCase("align='right'", ExpectedResult = "right")]
354354
[TestCase("style='justify-self:center'", ExpectedResult = "center")]
355+
[TestCase("style='margin-left:auto'", ExpectedResult = "right")]
356+
[TestCase("style='margin-left:auto;margin-right:auto'", ExpectedResult = "center")]
355357
public string? TableAlign_ReturnsTableJustification(string style)
356358
{
357359
var elements = converter.Parse(@$"<table {style}>

0 commit comments

Comments
 (0)