diff --git a/TextParser.cs b/TextParser.cs index ef7bac3..9f35cb2 100644 --- a/TextParser.cs +++ b/TextParser.cs @@ -21,13 +21,10 @@ namespace ChrisKaczor.Wpf.Controls // Add a root tag so the parser is happy text = string.Format(CultureInfo.InvariantCulture, "{0}", text); - // Normalize line endings - text = text.Replace("\r\n", "\n"); - // Create an XML document and load it with the text var xmlDocument = new XmlDocument { - PreserveWhitespace = true + PreserveWhitespace = false }; xmlDocument.LoadXml(text); @@ -55,15 +52,10 @@ namespace ChrisKaczor.Wpf.Controls { case "#WHITESPACE": case "#TEXT": - - // Split the fragment and the line endings - var lines = xmlNode.Value!.Split('\n'); - - var firstLine = true; - - foreach (var line in lines) { - var textLine = (firstLine ? textLines[^1] : new TextLine()); + var line = xmlNode.Value!; + + var textLine = textLines[^1]; // Create a new fragment and fill the style information var textFragment = new TextFragment(_parentControl) @@ -79,13 +71,19 @@ namespace ChrisKaczor.Wpf.Controls // Add the fragment to the list textLine.FragmentList.Add(textFragment); - if (!firstLine) - textLines.Add(textLine); - - firstLine = false; + break; } - break; + case "BR": + { + var textLine = new TextLine(); + + textLine.FragmentList.Add(new TextFragment(_parentControl) { Text = Environment.NewLine }); + + textLines.Add(textLine); + + break; + } case "EM": case "B": @@ -124,7 +122,7 @@ namespace ChrisKaczor.Wpf.Controls break; case "COLOR": - style.Color = (Brush?) new BrushConverter().ConvertFromString(attribute.Value); + style.Color = (Brush?)new BrushConverter().ConvertFromString(attribute.Value); break; } }