Skip to content

Commit

Permalink
Merge pull request #73 from Ceus/master
Browse files Browse the repository at this point in the history
Resolved crash on XmlException due to deleted event or cleared log.
  • Loading branch information
kmaki565 authored Aug 26, 2024
2 parents 5821fdc + 2eeed3b commit ca3838a
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion EventLook/View/TextBoxBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml;
using System.Xml.Linq;

namespace EventLook.View;
Expand Down Expand Up @@ -63,6 +64,22 @@ public static bool GetEscapeClearsText(DependencyObject dependencyObject)
new FrameworkPropertyMetadata(string.Empty, OnHighlightedXmlChanged)
);

/// <summary>
/// Validate string is proper XML format
/// </summary>
private static bool IsValidXml(string xml)
{
try
{
XElement.Parse(xml);
return true;
}
catch (XmlException)
{
return false;
}
}

/// <summary>
/// Sets the HighlightedXml property on a DependencyObject.
/// </summary>
Expand All @@ -86,7 +103,15 @@ private static void OnHighlightedXmlChanged(DependencyObject d, DependencyProper
{
if (d is RichTextBox richTextBox && e.NewValue is string xml)
{
// Highlight XML content in RichTextBox
// If invalid XML, skip highlight and display string.
if (!IsValidXml(xml))
{
richTextBox.Document.Blocks.Clear();
richTextBox.AppendText(xml);
return;
}

// If valid XML, proceed to highlight it
HighlightXml(richTextBox, xml);
}
}
Expand Down

0 comments on commit ca3838a

Please sign in to comment.