diff --git a/src/ReverseMarkdown.Test/ConverterTests.WhenTableRowWithDuplicateStyleKeysAfterTrimming_ThenConvertWithoutException.verified.md b/src/ReverseMarkdown.Test/ConverterTests.WhenTableRowWithDuplicateStyleKeysAfterTrimming_ThenConvertWithoutException.verified.md new file mode 100644 index 0000000..6651f7c --- /dev/null +++ b/src/ReverseMarkdown.Test/ConverterTests.WhenTableRowWithDuplicateStyleKeysAfterTrimming_ThenConvertWithoutException.verified.md @@ -0,0 +1,3 @@ +| Header | +| --- | +| Data | diff --git a/src/ReverseMarkdown.Test/ConverterTests.cs b/src/ReverseMarkdown.Test/ConverterTests.cs index 5692ada..ee0d594 100644 --- a/src/ReverseMarkdown.Test/ConverterTests.cs +++ b/src/ReverseMarkdown.Test/ConverterTests.cs @@ -1971,5 +1971,12 @@ public Task WhenTable_WithCaptionContainingNewlines_ThenHandleNewlines() var html = $"
Multi{Environment.NewLine}Line{Environment.NewLine}Caption
Col
Data
"; return CheckConversion(html); } + + [Fact] + public Task WhenTableRowWithDuplicateStyleKeysAfterTrimming_ThenConvertWithoutException() + { + var html = "
Header
Data
"; + return CheckConversion(html); + } } } diff --git a/src/ReverseMarkdown/Helpers/StringUtils.cs b/src/ReverseMarkdown/Helpers/StringUtils.cs index 365a8df..8927e4b 100644 --- a/src/ReverseMarkdown/Helpers/StringUtils.cs +++ b/src/ReverseMarkdown/Helpers/StringUtils.cs @@ -64,7 +64,8 @@ public static Dictionary ParseStyle(string? style) var styles = style!.Split(';'); return styles.Select(styleItem => styleItem.Split(':')) .Where(styleParts => styleParts.Length == 2) - .DistinctBy(styleParts => styleParts[0]) - .ToDictionary(styleParts => styleParts[0].Trim(), styleParts => styleParts[1].Trim(), StringComparer.OrdinalIgnoreCase); + .Select(styleParts => new[] { styleParts[0].Trim(), styleParts[1].Trim() }) + .DistinctBy(styleParts => styleParts[0], StringComparer.OrdinalIgnoreCase) + .ToDictionary(styleParts => styleParts[0], styleParts => styleParts[1], StringComparer.OrdinalIgnoreCase); } }