KirillOsenkov / XmlParser

A Roslyn-inspired full-fidelity XML parser with no dependencies and a simple Visual Studio XML language service
Apache License 2.0
328 stars 49 forks source link

null reference exception in GetChangedSpans #15

Open Ttxman opened 6 years ago

Ttxman commented 6 years ago

I found a null reference exception when comparing trees. There seems to be a problem in SyntraxTriviaList enumerator Current. return _current as SyntaxTrivia; is returning null because _current is SkippedTokensTriviaSyntax

Problem is occuring when "!" is inserted after "<".

I'm adding little test with the problem... inserting [InlineData("!", "Cont")] or [InlineData("!-", "Cont")] fails with NullReferenceException on marked line..


        [Theory]
        [InlineData("X", "Cont")]
        [InlineData("!", "Cont")]
        [InlineData("!-", "Cont")]
        [InlineData("!--", "Cont")]
        [InlineData("!", "ont")]
        public void InsertToTagName(string toInsert, string insertBefore)
        {
            var Xml = @"<Root><Cont></Cont></Root>";
            var insert = toInsert;
            var offset = Xml.IndexOf(insertBefore);
            var newLength = insert.Length;
            var newXml = Xml.Insert(offset, insert);
            var changes = new TextChangeRange[] { new TextChangeRange(new TextSpan(offset, 0), newLength) };

            var root = Parser.ParseText(Xml);
            var newRoot = Parser.ParseText(newXml);
            var newRootIncremental = Parser.ParseIncremental(newXml, changes, root);

            Assert.NotSame(root, newRoot);
            Assert.Equal(newXml, newRoot.ToFullString());
            Assert.Equal(newXml, newRootIncremental.ToFullString());

            var roothanges = newRootIncremental.GetChangedSpans(root);//<< Null reference exception when inserting "!"
            Assert.Single(roothanges);
            Assert.Equal(roothanges.First(),new TextSpan(offset,newLength));
        }```