From c095261cd6557e9f16193ae85d4da0f5edcdc0fe Mon Sep 17 00:00:00 2001 From: Steven Cohn Date: Wed, 1 Nov 2023 10:41:26 -0400 Subject: [PATCH] consider mathML comments as text content #1150 --- OneMore/Commands/Clean/RemoveEmptyCommand.cs | 3 ++- OneMore/Helpers/Extensions/XElementExtensions.cs | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/OneMore/Commands/Clean/RemoveEmptyCommand.cs b/OneMore/Commands/Clean/RemoveEmptyCommand.cs index ff82be4f9a..1c6594f3b5 100644 --- a/OneMore/Commands/Clean/RemoveEmptyCommand.cs +++ b/OneMore/Commands/Clean/RemoveEmptyCommand.cs @@ -9,6 +9,7 @@ namespace River.OneMoreAddIn.Commands using System.Collections.Generic; using System.Globalization; using System.Linq; + using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; @@ -122,7 +123,7 @@ public bool CollapseEmptyLines() var elements = range .Select(e => e.Parent) .Distinct() - .Where(e => e.TextValue().Trim().Length == 0) + .Where(e => e.TextValue().Trim().Length == 0 && !e.IsMathML()) .ToList(); if (elements?.Any() != true) diff --git a/OneMore/Helpers/Extensions/XElementExtensions.cs b/OneMore/Helpers/Extensions/XElementExtensions.cs index 0fba1bad41..a19c4f880f 100644 --- a/OneMore/Helpers/Extensions/XElementExtensions.cs +++ b/OneMore/Helpers/Extensions/XElementExtensions.cs @@ -427,6 +427,20 @@ public static XElement FirstAncestor(this XElement element, XName name, XName br } + /// + /// Determines if the content of the element consists only of a mathML statement + /// which is identified when its CDATA specifies an XML comment. + /// + /// + /// + public static bool IsMathML(this XElement element) + { + var cdata = element.GetCData(); + return cdata != null && + Regex.IsMatch(cdata.Value, @"", RegexOptions.Singleline); + } + + /// /// OneMore Extension >> Extract the sanitized text value of the given element ///