Skip to content

Commit

Permalink
Fix indentation of numbering list #166
Browse files Browse the repository at this point in the history
  • Loading branch information
onizet committed Oct 7, 2024
1 parent 05d11a5 commit 3bdee30
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/Html2OpenXml/Expressions/Numbering/ListExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public override IEnumerable<OpenXmlElement> Interpret(ParsingContext context)

p.ParagraphProperties ??= new();
p.ParagraphProperties.ParagraphStyleId = GetStyleIdForListItem(context.DocumentStyle, liNode);
p.ParagraphProperties.Indentation = level < 2? null : new() { Left = (level * Indentation).ToString() };
p.ParagraphProperties.NumberingProperties = new NumberingProperties {
NumberingLevelReference = new() { Val = level - 1 },
NumberingId = new() { Val = listContext.InstanceId }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ private static Dictionary<string, AbstractNum> InitKnownLists()
LevelText = new() { Val = string.Format(text, lvlIndex+1) },
LevelJustification = new() { Val = LevelJustificationValues.Left },
PreviousParagraphProperties = new() {
Indentation = new() { Left = Indentation.ToString(), Hanging = Indentation.ToString() }
Indentation = new() {
Left = ((lvlIndex + 1) * Indentation * 2).ToString(),
Hanging = Indentation.ToString()
}
},
NumberingSymbolRunProperties = useSymbol? new () {
RunFonts = new() { Ascii = "Symbol", Hint = FontTypeHintValues.Default }
Expand Down Expand Up @@ -280,7 +283,10 @@ private static Dictionary<string, AbstractNum> InitKnownLists()
StartNumberingValue = new() { Val = 1 },
NumberingFormat = new() { Val = NumberFormatValues.Decimal },
LevelIndex = lvlIndex,
LevelText = new() { Val = lvlText.ToString() }
LevelText = new() { Val = lvlText.ToString() },
PreviousParagraphProperties = new() {
Indentation = new() { Left = "0", Hanging = Indentation.ToString() }
}
});
}
knownAbstractNums.Add(listName, abstractNum);
Expand Down
14 changes: 11 additions & 3 deletions src/Html2OpenXml/PredefinedStyles.resx
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,20 @@
<data name="ListParagraph" xml:space="preserve">
<value><![CDATA[<w:style w:type="paragraph" w:styleId="ListParagraph" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:name w:val="List Paragraph" />
<w:basedOn w:val="Normal" />
<w:qFormat />
<w:next w:val="List Paragraph"/>
<w:pPr>
<w:ind w:left="720" />
<w:keepNext w:val="0"/>
<w:keepLines w:val="0"/>
<w:spacing w:before="0" w:after="0" w:line="240" w:lineRule="auto"/>
<w:ind w:left="720" w:right="0" w:firstLine="0"/>
<w:contextualSpacing />
</w:pPr>
</w:style>]]></value>
</data>
<data name="Normal" xml:space="preserve">
<value><![CDATA[<w:style w:type="paragraph" w:styleId="Normal" w:default="1" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:name w:val="Normal"/>
<w:qFormat/>
</w:style>]]></value>
</data>
<data name="Quote" xml:space="preserve">
Expand Down
44 changes: 41 additions & 3 deletions test/HtmlToOpenXml.Tests/NumberingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ namespace HtmlToOpenXml.Tests
[TestFixture]
public class NumberingTests : HtmlConverterTestBase
{
const int maxLevel = 8;


[Test(Description = "Skip any elements that is not a `li` tag")]
public void NonLiElement_ShouldBeIgnored()
{
Expand Down Expand Up @@ -181,7 +184,6 @@ public void WithExistingNumbering_ReturnsUniqueInstanceId()
[Test(Description = "Word doesn't display more than 8 deep levels.")]
public void MaxNumberingLevel_ShouldBeIgnored()
{
const int maxLevel = 8;
var sb = new System.Text.StringBuilder();
for (int i = 0; i <= maxLevel; i++)
sb.AppendFormat("<ol><li>Item {0}", i+1);
Expand Down Expand Up @@ -333,16 +335,17 @@ public async Task DisableContinueNumbering_ReturnsSecondList_RestartingOrder()
/// Tiered numbering such as: 1, 1.1, 1.1.1
/// </summary>
[Test(Description = "Nested numbering (issue #81)")]
public void DecimalTieredStyle_ReturnsListWithTieredNumbering()
public async Task DecimalTieredStyle_ReturnsListWithTieredNumbering()
{
var elements = converter.Parse(
await converter.ParseBody(
@"<ol style='list-style-type:decimal-tiered'>
<li>Item 1
<ol><li>Item 1.1</li></ol>
</li>
<li>Item 2</li>
</ol>");

var elements = mainPart.Document.Body!.ChildElements;
var absNum = mainPart.NumberingDefinitionsPart?.Numbering
.Elements<AbstractNum>()
.SingleOrDefault();
Expand All @@ -351,10 +354,15 @@ public void DecimalTieredStyle_ReturnsListWithTieredNumbering()
var instances = mainPart.NumberingDefinitionsPart?.Numbering
.Elements<NumberingInstance>().Where(i => i.AbstractNumId!.Val == absNum.AbstractNumberId);
Assert.That(instances, Is.Not.Null);
var levels = absNum.Elements<Level>();
Assert.Multiple(() =>
{
Assert.That(instances.Count(), Is.EqualTo(1));
Assert.That(instances.Select(i => i.NumberID?.HasValue), Has.All.True);
Assert.That(levels.Count(), Is.EqualTo(maxLevel + 1));
Assert.That(levels.Select(l => l.NumberingFormat?.Val?.Value), Has.All.EqualTo(NumberFormatValues.Decimal));
Assert.That(levels.Select(l => l.PreviousParagraphProperties?.Indentation?.Left?.Value), Has.All.EqualTo("0"),
"Decimal Tiered style must all be aligned on left with no indent");
});

Assert.That(elements, Is.Not.Empty);
Expand All @@ -363,6 +371,7 @@ public void DecimalTieredStyle_ReturnsListWithTieredNumbering()
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 = "Allow to specify another start value for the first item of a `ol` list")]
Expand Down Expand Up @@ -503,5 +512,34 @@ public void WithRtl_ReturnsBidi(string dir, bool? expectedValue)
var bidi = elements.Last().GetFirstChild<ParagraphProperties>()?.BiDi;
return bidi?.Val?.Value;
}

[Test]
public void NestedNumberList_ReturnsIncrementalIdentation()
{
const int maxLevel = 8;
var sb = new System.Text.StringBuilder();
for (int i = 0; i < maxLevel; i++)
sb.AppendFormat("<ol><li>Item {0}", i+1);
for (int i = 0; i < maxLevel; i++)
sb.Append("</li></ol>");

converter.Parse(sb.ToString());

var absNum = mainPart.NumberingDefinitionsPart?.Numbering
.Elements<AbstractNum>()
.SingleOrDefault();
Assert.That(absNum, Is.Not.Null);

var levels = absNum.Elements<Level>();
Assert.That(levels.Count(), Is.EqualTo(maxLevel + 1));
for (int i = 0; i <= maxLevel; i++)
{
var level = levels.ElementAt(i);
var ident = level.PreviousParagraphProperties?.Indentation;
Assert.That(ident?.Hanging?.Value, Is.EqualTo("360"));
Assert.That(Convert.ToInt32(ident?.Left?.Value), Is.EqualTo((i + 1) * 2 * 360));
TestContext.Out.WriteLine($"{i}. {ident?.Left?.Value}");
}
}
}
}

0 comments on commit 3bdee30

Please sign in to comment.