Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion examples/Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static class Program
static async Task Main(string[] args)
{
const string filename = "test.docx";
string html = ResourceHelper.GetString("Resources.CompleteRunTest.html");
string html = ResourceHelper.GetString("Resources.UlStyles.html");
if (File.Exists(filename)) File.Delete(filename);

using (MemoryStream generatedDocument = new())
Expand Down
38 changes: 38 additions & 0 deletions examples/Demo/Resources/UlStyles.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
</head>
<body>
<ul style="list-style-type: '-';">
<li style="text-align: justify;">test 2</li>
<li style="text-align: justify;">test 3</li>
<li style="text-align: justify;">&nbsp;</li>
</ul>
<p style="text-align: justify;">&nbsp;</p>
<ul style="list-style-type: disc;">
<li style="text-align: justify;">test 2</li>
<li style="text-align: justify;">test 3</li>
<li style="text-align: justify;">&nbsp;</li>
</ul>
<p style="text-align: justify;">&nbsp;</p>
<ul style="list-style-type: '-';">
<li style="text-align: justify;">test 2</li>
<li style="text-align: justify;">test 3</li>
<li style="text-align: justify;">&nbsp;</li>
</ul>
<p style="text-align: justify;">&nbsp;</p>
<ul style="list-style-type: '😀';">
<li style="text-align: justify;">test 2</li>
<li style="text-align: justify;">test 3</li>
<li style="text-align: justify;">&nbsp;</li>
</ul>

<p style="text-align: justify;">&nbsp;</p>
<ul style="list-style-type: dash;">
<li style="text-align: justify;">test 2</li>
<li style="text-align: justify;">test 3</li>
<li style="text-align: justify;">&nbsp;</li>
</ul>
</body>
</html>
12 changes: 10 additions & 2 deletions src/Html2OpenXml/Expressions/Numbering/ListExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ readonly struct ListContext(string listName, int absNumId, int instanceId, int l
// https://answers.microsoft.com/en-us/msoffice/forum/all/custom-list-number-style/21a54399-4404-4c37-8843-2ccaaf827485
// Image bullet: http://officeopenxml.com/WPnumbering-imagesAsSymbol.php
private static readonly HashSet<string> supportedListTypes =
["disc", "decimal", "square", "circle",
["disc", "decimal", "square", "circle", "dash",
"lower-alpha", "upper-alpha", "lower-latin", "upper-latin",
"lower-greek", "upper-greek",
"lower-roman", "upper-roman",
Expand Down Expand Up @@ -227,7 +227,15 @@ private static string GetListName(IElement listNode, string? parentName = null)
if (parentName != null && IsCascadingStyle(parentName))
return parentName!;

type = orderedList? "decimal" : "disc";
// If a specific list-style-type is provided for an unordered list (e.g., a custom string like "'-'"),
// we strip the surrounding quotes to extract the raw symbol.
// Otherwise, we fallback to the default "decimal" or "disc" styles.
if (!orderedList && !string.IsNullOrEmpty(type))
{
return type!.Trim('\'', '\"');
}

return orderedList ? "decimal" : "disc";
}

return type!;
Expand Down
49 changes: 47 additions & 2 deletions src/Html2OpenXml/Expressions/Numbering/NumberingExpressionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,20 @@

Numbering numberingPart = context.MainPart.NumberingDefinitionsPart!.Numbering!;

AbstractNum abstractNum;

// at this stage, we have sanitized the list style so it's safe to grab them from the predefined template lists
var abstractNum = predefinedNumberingLists[listName];
if (predefinedNumberingLists.TryGetValue(listName, out var predefined))
{
// If it's a predefined style, we clone it as usual
abstractNum = (AbstractNum)predefined.CloneNode(true);
}
else
{
// If not found in predefined lists, listName contains a custom bullet character (e.g., "-")
abstractNum = CreateCustomBulletAbstractNum(listName);
}

abstractNum = (AbstractNum) abstractNum.CloneNode(true);
abstractNum.AbstractNumberId = IncrementAbstractNumId(context, numberingPart);
var level1 = abstractNum.GetFirstChild<Level>()!;
Expand Down Expand Up @@ -216,6 +228,38 @@
isInitialized = true;
}

/// <summary>
/// Generates a custom abstract numbering definition for unordered lists using a specific symbol.
/// </summary>
/// <param name="customSymbol">The custom character or string to be used as the list bullet.</param>
/// <returns>An <see cref="AbstractNum"/> instance configured with the custom bullet symbol across all levels.</returns>
private AbstractNum CreateCustomBulletAbstractNum(string customSymbol)
{
var abstractNum = new AbstractNum {
AbstractNumDefinitionName = new() { Val = customSymbol },
MultiLevelType = new() { Val = MultiLevelValues.HybridMultilevel }
};

for (var lvlIndex = 0; lvlIndex <= MaxLevel; lvlIndex++)
{
abstractNum.Append(new Level {
StartNumberingValue = new() { Val = 1 },
NumberingFormat = new() { Val = NumberFormatValues.Bullet },
LevelIndex = lvlIndex,
LevelText = new() { Val = string.Format(customSymbol, lvlIndex+1) },
LevelJustification = new() { Val = LevelJustificationValues.Left },
PreviousParagraphProperties = new() {
Indentation = new() {
Left = ((lvlIndex + 1) * Indentation * 2).ToString(),
Hanging = Indentation.ToString()
}
}
});

Check warning on line 257 in src/Html2OpenXml/Expressions/Numbering/NumberingExpressionBase.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Review this call, which partially matches an overload without 'params'. The partial match is 'void DocumentFormat.OpenXml.OpenXmlElement.Append(IEnumerable<DocumentFormat.OpenXml.OpenXmlElement> newChildren)'.

See more on https://sonarcloud.io/project/issues?id=onizet_html2openxml&issues=AZ5mf9Jmb9sO6VmIErwf&open=AZ5mf9Jmb9sO6VmIErwf&pullRequest=230
}

return abstractNum;
}

/// <summary>
/// Predefined template of lists.
/// </summary>
Expand All @@ -230,6 +274,7 @@
("disc", NumberFormatValues.Bullet, "•"),
("square", NumberFormatValues.Bullet, "▪"),
("circle", NumberFormatValues.Bullet, "o"),
("dash", NumberFormatValues.Bullet, "-"),
("upper-alpha", NumberFormatValues.UpperLetter, "%{0}."),
("lower-alpha", NumberFormatValues.LowerLetter, "%{0}."),
("upper-roman", NumberFormatValues.UpperRoman, "%{0}."),
Expand Down Expand Up @@ -299,4 +344,4 @@
return knownAbstractNums;
#endif
}
}
}
Loading