Update how newlines are handled

This commit is contained in:
2024-10-17 19:39:14 -04:00
parent 69a0d3a062
commit 494a47aba1

View File

@@ -21,13 +21,10 @@ namespace ChrisKaczor.Wpf.Controls
// Add a root tag so the parser is happy // Add a root tag so the parser is happy
text = string.Format(CultureInfo.InvariantCulture, "<body>{0}</body>", text); text = string.Format(CultureInfo.InvariantCulture, "<body>{0}</body>", text);
// Normalize line endings
text = text.Replace("\r\n", "\n");
// Create an XML document and load it with the text // Create an XML document and load it with the text
var xmlDocument = new XmlDocument var xmlDocument = new XmlDocument
{ {
PreserveWhitespace = true PreserveWhitespace = false
}; };
xmlDocument.LoadXml(text); xmlDocument.LoadXml(text);
@@ -55,15 +52,10 @@ namespace ChrisKaczor.Wpf.Controls
{ {
case "#WHITESPACE": case "#WHITESPACE":
case "#TEXT": 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 // Create a new fragment and fill the style information
var textFragment = new TextFragment(_parentControl) var textFragment = new TextFragment(_parentControl)
@@ -79,13 +71,19 @@ namespace ChrisKaczor.Wpf.Controls
// Add the fragment to the list // Add the fragment to the list
textLine.FragmentList.Add(textFragment); textLine.FragmentList.Add(textFragment);
if (!firstLine) break;
textLines.Add(textLine);
firstLine = false;
} }
case "BR":
{
var textLine = new TextLine();
textLine.FragmentList.Add(new TextFragment(_parentControl) { Text = Environment.NewLine });
textLines.Add(textLine);
break; break;
}
case "EM": case "EM":
case "B": case "B":