/// <summary>
        /// Find the item that will contain all of the incremental parsing changes.
        /// This never returns null.
        /// </summary>
        private IIncrementalParseItem GetIncrementalParseParent(int start, int length)
        {
            for (ComplexItem item = StyleSheet.ComplexItemFromRange(start, length); item != null; item = item.Parent)
            {
                if (item is IIncrementalParseItem)
                {
                    return((IIncrementalParseItem)item);
                }
            }

            // Could be typing in whitespace outside all items, so try using the root StyleSheet
            return(StyleSheet);
        }
Exemple #2
0
        public void HtmlComment_ParseTest()
        {
            string        text1  = "<!-- foo#bar -->";
            ITextProvider tp     = new StringTextProvider(text1);
            TokenStream   tokens = Helpers.MakeTokenStream(tp);

            // Parse "<!--"
            {
                HtmlComment hc = new HtmlComment();
                Assert.IsTrue(hc.Parse(new ItemFactory(tp, null), tp, tokens));
                Assert.AreEqual(1, hc.Children.Count);
                Assert.AreEqual(CssTokenType.OpenHtmlComment, ((TokenItem)hc.Children[0]).TokenType);
            }

            // Parse "foo#bar"
            {
                Assert.AreEqual(CssTokenType.Identifier, tokens.Advance(1).TokenType);
                Assert.AreEqual(CssTokenType.HashName, tokens.Advance(1).TokenType);
            }

            // Parse "-->"
            {
                HtmlComment hc = new HtmlComment();
                Assert.IsTrue(hc.Parse(new ItemFactory(tp, null), tp, tokens));
                Assert.AreEqual(1, hc.Children.Count);
                Assert.AreEqual(CssTokenType.CloseHtmlComment, ((TokenItem)hc.Children[0]).TokenType);
            }

            string     text2  = "<!-- @namespace foo \"www.foo.com\" -->";
            CssParser  parser = new CssParser();
            StyleSheet sheet  = parser.Parse(text2, true);

            Assert.IsTrue(sheet.ComplexItemFromRange(14, 0) is NamespaceDirective);
            Assert.IsTrue(sheet.ComplexItemFromRange(2, 0) is HtmlComment);
            Assert.IsTrue(sheet.ComplexItemFromRange(35, 0) is HtmlComment);
        }
        /// <summary>
        /// Inserts comments into the StyleSheet that were in the range of changed text
        /// </summary>
        private void InsertComments(IEnumerable <Comment> comments, IList <ParseItem> newChildren)
        {
            if (comments != null)
            {
                foreach (ParseItem comment in comments)
                {
                    ComplexItem parent = StyleSheet.ComplexItemFromRange(comment.Start, comment.Length) ?? StyleSheet;

                    if (!(parent is Comment))
                    {
                        parent.InsertChildIntoSubtree(comment);

                        // Don't save the comment if its parent or itself is already in the list
                        bool alreadyAdded = false;

                        for (ParseItem commentParent = comment; commentParent != null; commentParent = commentParent.Parent)
                        {
                            if (newChildren.Contains(commentParent))
                            {
                                alreadyAdded = true;
                                break;
                            }
                        }

                        if (!alreadyAdded)
                        {
                            newChildren.Add(comment);
                        }
                    }
                    else // the comment is already in the tree, so we passed the reparsed text
                    {
                        Debug.Assert(parent.Start == comment.Start && parent.Length == comment.Length);
                        break;
                    }
                }
            }
        }
Exemple #4
0
        public void ComplexItemFromRangeTest()
        {
            CssParser  p = new CssParser();
            StyleSheet s = p.Parse("@charset \"foo\"; .a { color: red; } moo boo", true);

            Assert.IsNull(s.ComplexItemFromRange(0, 0));
            Assert.IsTrue(s.ComplexItemFromRange(0, 2) is CharsetDirective);
            Assert.AreSame(s, s.ComplexItemFromRange(16, 0));
            Assert.IsTrue(s.ComplexItemFromRange(16, 4) is RuleSet);
            Assert.IsTrue(s.ComplexItemFromRange(20, 0) is RuleBlock);
            Assert.IsTrue(s.ComplexItemFromRange(20, 5) is RuleBlock);
            Assert.IsTrue(s.ComplexItemFromRange(33, 1) is RuleBlock);
            Assert.AreSame(s, s.ComplexItemFromRange(34, 0));
            Assert.IsTrue(s.ComplexItemFromRange(s.Start, s.Length) is StyleSheet);
            Assert.IsTrue(s.ComplexItemFromRange(s.AfterEnd - 1, 0) is ItemName);
            Assert.IsTrue(s.ComplexItemFromRange(s.AfterEnd - 2, 1) is ItemName);
        }