Skip to content

Commit 78f7506

Browse files
authored
Merge pull request #222 Improve parsing to correctly handle spaces
Improve parsing to correctly handle spaces
2 parents c917867 + aa3df3e commit 78f7506

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/Html2OpenXml/Utilities/Converter.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ static class Converter
2626
{
2727
Span<char> loweredValue = span.Length <= 128 ? stackalloc char[span.Length] : new char[span.Length];
2828
span.ToLowerInvariant(loweredValue);
29-
return loweredValue switch
29+
return loweredValue.Trim() switch
3030
{
31-
"left" => JustificationValues.Left,
31+
"left" => JustificationValues.Left,
3232
"right" => JustificationValues.Right,
3333
"center" => JustificationValues.Center,
3434
"justify" => JustificationValues.Both,
@@ -43,7 +43,7 @@ static class Converter
4343
{
4444
Span<char> loweredValue = span.Length <= 128 ? stackalloc char[span.Length] : new char[span.Length];
4545
span.ToLowerInvariant(loweredValue);
46-
return loweredValue switch
46+
return loweredValue.Trim() switch
4747
{
4848
"top" => TableVerticalAlignmentValues.Top,
4949
"middle" => TableVerticalAlignmentValues.Center,
@@ -61,7 +61,7 @@ public static Unit ToFontSize(ReadOnlySpan<char> span)
6161

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

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

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

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

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

149150
Span<char> loweredValue = span.Length <= 128 ? stackalloc char[span.Length] : new char[span.Length];
150151
span.ToLowerInvariant(loweredValue);
151-
return loweredValue switch
152+
return loweredValue.Trim() switch
152153
{
153154
"dotted" => BorderValues.Dotted,
154155
"dashed" => BorderValues.Dashed,
@@ -177,7 +178,7 @@ public static ICollection<TextDecoration> ToTextDecoration(ReadOnlySpan<char> va
177178
var tokenCount = span.Split(tokens, ' ', StringSplitOptions.RemoveEmptyEntries);
178179
for (int i = 0; i < tokenCount; i++)
179180
{
180-
switch (span.Slice(tokens[i]))
181+
switch (span.Slice(tokens[i]).Trim())
181182
{
182183
case "underline": decorations.Add(TextDecoration.Underline); break;
183184
case "line-through": decorations.Add(TextDecoration.LineThrough); break;

test/HtmlToOpenXml.Tests/ElementTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ public void PhrasingTag_ReturnsRunWithDefaultStyle<T> (string html) where T : Op
3737
public void MultipleStyle_ShouldBeAllApplied ()
3838
{
3939
var elements = converter.Parse(@"<b style=""
40-
font-style:italic;
41-
font-size:12px;
42-
font-family:Verdana;
43-
font-variant:small-caps;
44-
color:white;
45-
text-decoration:wavy line-through double;
46-
background:red;
40+
font-style: italic;
41+
font-size: 12px;
42+
font-family: Verdana;
43+
font-variant: small-caps;
44+
color: white;
45+
text-decoration: wavy line-through double;
46+
background: red;
4747
"">bold with italic style</b>");
4848
Assert.That(elements, Has.Count.EqualTo(1));
4949

@@ -67,7 +67,7 @@ public void MultipleStyle_ShouldBeAllApplied ()
6767
}
6868
}
6969

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

0 commit comments

Comments
 (0)