diff --git a/src/Azos/Text/TagParsingUtils.cs b/src/Azos/Text/TagParsingUtils.cs index c90168fb7..a430d7cf0 100644 --- a/src/Azos/Text/TagParsingUtils.cs +++ b/src/Azos/Text/TagParsingUtils.cs @@ -40,8 +40,7 @@ internal Segment(bool isTag, int idxs, int idxe, string content) public readonly bool IsTag; public readonly int IdxStart; public readonly int IdxEnd; - public int Length => IdxEnd - IdxStart; - + public int Length => 1 + IdxEnd - IdxStart; public readonly string Content; } @@ -83,7 +82,7 @@ public static IEnumerable ParseSegments(this IEnumerable source, { if (buf.Length > 0) { - yield return new Segment(false, idxs, idx, buf.ToString()); + yield return new Segment(false, idxs, idx - 1, buf.ToString()); buf.Clear(); } isTag = true; @@ -161,7 +160,7 @@ public static IEnumerable ParseTags(this IEnumerable source, strin /// /// Expands HTML content by invoking a function to build tag content into a string builder /// - public static StringBuilder ExpandHtmlTags(this IEnumerable source, Action fTagExpander, string tagPragma = "@") + public static StringBuilder ExpandHtmlTags(this IEnumerable source, Action fTagExpander, string tagPragma = "@") { fTagExpander.NonNull(nameof(fTagExpander)); var spans = source.ParseSegments('<', '>'); @@ -170,8 +169,8 @@ public static StringBuilder ExpandHtmlTags(this IEnumerable source, Action var result = new StringBuilder(1024); foreach (var tag in tags) { - if (tag.Segment.IsTag) - fTagExpander(tag, result); + if (tag.Segment.IsTag && tag.Data != null) + fTagExpander(result, tag); else result.Append($"<{tag.Segment.Content}>"); } diff --git a/src/testing/Azos.Tests.Nub/Parsing/TagParsingTests.cs b/src/testing/Azos.Tests.Nub/Parsing/TagParsingTests.cs new file mode 100644 index 000000000..bd7a26400 --- /dev/null +++ b/src/testing/Azos.Tests.Nub/Parsing/TagParsingTests.cs @@ -0,0 +1,65 @@ +/* + * Azos (A to Z Application Operating System) Framework + * The A to Z Foundation (a.k.a. Azist) licenses this file to you under the MIT license. + * See the LICENSE file in the project root for more information. +*/ + +using System.Linq; +using Azos.Scripting; +using Azos.Text; + +namespace Azos.Tests.Nub.Parsing +{ + [Runnable] + public class TagParsingTests + { + + [Run] + public void ParseSegments_00() + { + var got = "".ParseSegments().ToArray(); + Aver.AreEqual(0, got.Length); + + got = ((string)null).ParseSegments().ToArray(); + Aver.AreEqual(0, got.Length); + + got = " ".ParseSegments().ToArray(); + Aver.AreEqual(1, got.Length); + Aver.IsFalse(got[0].IsTag); + Aver.AreEqual(" ", got[0].Content); + Aver.AreEqual(0, got[0].IdxStart); + Aver.AreEqual(2, got[0].IdxEnd); + Aver.AreEqual(3, got[0].Length); + } + + + [Run] + public void ParseSegments_01() + { + var got = "How is doing?".ParseSegments().ToArray(); + + got.See(); + + Aver.AreEqual(3, got.Length); + + Aver.IsFalse(got[0].IsTag); + Aver.AreEqual("How is ", got[0].Content); + Aver.AreEqual(0, got[0].IdxStart); + Aver.AreEqual(6, got[0].IdxEnd); + Aver.AreEqual(7, got[0].Length); + + Aver.IsTrue(got[1].IsTag); + Aver.AreEqual("monster", got[1].Content); + Aver.AreEqual(7, got[1].IdxStart); + Aver.AreEqual(15, got[1].IdxEnd); + Aver.AreEqual(9, got[1].Length);//with < > + + Aver.IsFalse(got[2].IsTag); + Aver.AreEqual(" doing?", got[2].Content); + Aver.AreEqual(16, got[2].IdxStart); + Aver.AreEqual(22, got[2].IdxEnd); + Aver.AreEqual(7, got[2].Length); + } + + } +}