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
23 changes: 12 additions & 11 deletions src/Html2OpenXml/Utilities/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ static class Converter
{
Span<char> loweredValue = span.Length <= 128 ? stackalloc char[span.Length] : new char[span.Length];
span.ToLowerInvariant(loweredValue);
return loweredValue switch
return loweredValue.Trim() switch
{
"left" => JustificationValues.Left,
"left" => JustificationValues.Left,
"right" => JustificationValues.Right,
"center" => JustificationValues.Center,
"justify" => JustificationValues.Both,
Expand All @@ -43,7 +43,7 @@ static class Converter
{
Span<char> loweredValue = span.Length <= 128 ? stackalloc char[span.Length] : new char[span.Length];
span.ToLowerInvariant(loweredValue);
return loweredValue switch
return loweredValue.Trim() switch
{
"top" => TableVerticalAlignmentValues.Top,
"middle" => TableVerticalAlignmentValues.Center,
Expand All @@ -61,7 +61,7 @@ public static Unit ToFontSize(ReadOnlySpan<char> span)

Span<char> loweredValue = span.Length <= 128 ? stackalloc char[span.Length] : new char[span.Length];
span.ToLowerInvariant(loweredValue);
var unit = loweredValue switch
var unit = loweredValue.Trim() switch
{
"1" or "xx-small" => new Unit(UnitMetric.Point, 10),
"2" or "x-small" => new Unit(UnitMetric.Point, 15),
Expand Down Expand Up @@ -92,7 +92,7 @@ public static Unit ToFontSize(ReadOnlySpan<char> span)

Span<char> loweredValue = span.Length <= 128 ? stackalloc char[span.Length] : new char[span.Length];
span.ToLowerInvariant(loweredValue);
return loweredValue switch
return loweredValue.Trim() switch
{
"small-caps" => FontVariant.SmallCaps,
"normal" => FontVariant.Normal,
Expand All @@ -106,7 +106,7 @@ public static Unit ToFontSize(ReadOnlySpan<char> span)

Span<char> loweredValue = span.Length <= 128 ? stackalloc char[span.Length] : new char[span.Length];
span.ToLowerInvariant(loweredValue);
return loweredValue switch
return loweredValue.Trim() switch
{
"italic" or "oblique" => FontStyle.Italic,
"normal" => FontStyle.Normal,
Expand All @@ -120,7 +120,7 @@ public static Unit ToFontSize(ReadOnlySpan<char> span)

Span<char> loweredValue = span.Length <= 128 ? stackalloc char[span.Length] : new char[span.Length];
span.ToLowerInvariant(loweredValue);
return loweredValue switch
return loweredValue.Trim() switch
{
"700" or "bold" => FontWeight.Bold,
"bolder" => FontWeight.Bolder,
Expand All @@ -135,8 +135,9 @@ public static Unit ToFontSize(ReadOnlySpan<char> span)

// return the first font name
Span<Range> tokens = stackalloc Range[1];
return span.SplitCompositeAttribute(tokens, ',') switch {
1 => span.Slice(tokens[0]).ToString(),
return span.SplitCompositeAttribute(tokens, ',') switch
{
1 => span.Slice(tokens[0]).Trim().ToString(),
_ => null
};
}
Expand All @@ -148,7 +149,7 @@ public static BorderValues ToBorderStyle(ReadOnlySpan<char> span)

Span<char> loweredValue = span.Length <= 128 ? stackalloc char[span.Length] : new char[span.Length];
span.ToLowerInvariant(loweredValue);
return loweredValue switch
return loweredValue.Trim() switch
{
"dotted" => BorderValues.Dotted,
"dashed" => BorderValues.Dashed,
Expand Down Expand Up @@ -177,7 +178,7 @@ public static ICollection<TextDecoration> ToTextDecoration(ReadOnlySpan<char> va
var tokenCount = span.Split(tokens, ' ', StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < tokenCount; i++)
{
switch (span.Slice(tokens[i]))
switch (span.Slice(tokens[i]).Trim())
{
case "underline": decorations.Add(TextDecoration.Underline); break;
case "line-through": decorations.Add(TextDecoration.LineThrough); break;
Expand Down
16 changes: 8 additions & 8 deletions test/HtmlToOpenXml.Tests/ElementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public void PhrasingTag_ReturnsRunWithDefaultStyle<T> (string html) where T : Op
public void MultipleStyle_ShouldBeAllApplied ()
{
var elements = converter.Parse(@"<b style=""
font-style:italic;
font-size:12px;
font-family:Verdana;
font-variant:small-caps;
color:white;
text-decoration:wavy line-through double;
background:red;
font-style: italic;
font-size: 12px;
font-family: Verdana;
font-variant: small-caps;
color: white;
text-decoration: wavy line-through double;
background: red;
"">bold with italic style</b>");
Assert.That(elements, Has.Count.EqualTo(1));

Expand All @@ -67,7 +67,7 @@ public void MultipleStyle_ShouldBeAllApplied ()
}
}

[TestCase("<span style='font-style:normal'><span style='font-style:italic'>Italic!</span></span>")]
[TestCase("<span style='font-style: normal'><span style='font-style: italic'>Italic!</span></span>")]
[TestCase("<div style='font-style:italic'><span style='font-style:normal'><span style='font-style:italic'>Italic!</span></span></div>")]
[TestCase("<div id='outer' style='font-style:italic'><div id='inner'>Italic</div></div>")]
public void NestedTagWithStyle_ShouldCascadeParentStyle (string html)
Expand Down