From 0421f8cedbf8250bb08f94b89fc35f1707ca3b73 Mon Sep 17 00:00:00 2001 From: Springcomp Date: Wed, 9 Apr 2025 23:10:16 +0200 Subject: [PATCH 1/2] Records inner text from XML comments --- Core.Tests/Parser/CommentTestFixture.cs | 57 +++++++++++++++++++++++++ Core/Dom/XComment.cs | 2 + Core/Parser/XmlCommentState.cs | 6 ++- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 Core.Tests/Parser/CommentTestFixture.cs diff --git a/Core.Tests/Parser/CommentTestFixture.cs b/Core.Tests/Parser/CommentTestFixture.cs new file mode 100644 index 00000000..67dded82 --- /dev/null +++ b/Core.Tests/Parser/CommentTestFixture.cs @@ -0,0 +1,57 @@ +using MonoDevelop.Xml.Analysis; +using MonoDevelop.Xml.Dom; +using MonoDevelop.Xml.Parser; +using NUnit.Framework; + +namespace MonoDevelop.Xml.Tests.Parser +{ + [TestFixture] + public class CommentTestFixture + { + public virtual XmlRootState CreateRootState () + { + return new XmlRootState (); + } + + [TestCase ("", " text ")] + [TestCase ("", " < ")] + [TestCase ("", " ", " { + parser.AssertStateIs (); + }); + + var comment = result.doc.RootElement?.FirstChild as XComment; + + Assert.IsNotNull (comment); + Assert.AreEqual (innerText, comment.InnerText); + } + + [TestCase (""); + + parser.AssertDiagnostics ( + (XmlCoreDiagnostics.IncompleteEndComment, 10, 0) + ); + } + } +} diff --git a/Core/Dom/XComment.cs b/Core/Dom/XComment.cs index 44ccc52d..885c4c29 100644 --- a/Core/Dom/XComment.cs +++ b/Core/Dom/XComment.cs @@ -36,6 +36,8 @@ public XComment (TextSpan span) : base (span) {} protected XComment () {} protected override XObject NewInstance () { return new XComment (); } + public string? InnerText { get; internal set; } + public override string FriendlyPathRepresentation { get { return ""; } } diff --git a/Core/Parser/XmlCommentState.cs b/Core/Parser/XmlCommentState.cs index 528bb2f7..1cc7fdc9 100644 --- a/Core/Parser/XmlCommentState.cs +++ b/Core/Parser/XmlCommentState.cs @@ -68,7 +68,11 @@ public class XmlCommentState : XmlParserState } } else { // not any part of a '-->', so make sure matching is reset + if (context.StateTag == SINGLE_DASH) + context.KeywordBuilder.Append ('-'); + context.StateTag = NOMATCH; + context.KeywordBuilder.Append (c); } return null; @@ -76,7 +80,7 @@ public class XmlCommentState : XmlParserState XmlParserState? EndAndPop () { var comment = (XComment)context.Nodes.Pop (); - + comment.InnerText = context.KeywordBuilder.ToString (); comment.End (context.PositionAfterCurrentChar); if (context.BuildTree) { ((XContainer)context.Nodes.Peek ()).AddChildNode (comment); From 969fcd3be69c2b24f80e6ab01a5d2b327251b907 Mon Sep 17 00:00:00 2001 From: springcomp Date: Fri, 9 May 2025 14:39:55 +0000 Subject: [PATCH 2/2] Supports creating a DOM comment object --- Core/Dom/XComment.cs | 11 ++++++++++- Core/Parser/XmlCommentState.cs | 3 +-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Core/Dom/XComment.cs b/Core/Dom/XComment.cs index 885c4c29..83fe3327 100644 --- a/Core/Dom/XComment.cs +++ b/Core/Dom/XComment.cs @@ -36,7 +36,16 @@ public XComment (TextSpan span) : base (span) {} protected XComment () {} protected override XObject NewInstance () { return new XComment (); } - public string? InnerText { get; internal set; } + public string InnerText { get; private set; } = ""; + + public void End (string text) + { + int startLen = "".Length; + + InnerText = text; + Span = new TextSpan (Span.Start, startLen + text.Length + endLen); + } public override string FriendlyPathRepresentation { get { return ""; } diff --git a/Core/Parser/XmlCommentState.cs b/Core/Parser/XmlCommentState.cs index 1cc7fdc9..554fb114 100644 --- a/Core/Parser/XmlCommentState.cs +++ b/Core/Parser/XmlCommentState.cs @@ -80,8 +80,7 @@ public class XmlCommentState : XmlParserState XmlParserState? EndAndPop () { var comment = (XComment)context.Nodes.Pop (); - comment.InnerText = context.KeywordBuilder.ToString (); - comment.End (context.PositionAfterCurrentChar); + comment.End (context.KeywordBuilder.ToString()); if (context.BuildTree) { ((XContainer)context.Nodes.Peek ()).AddChildNode (comment); }