Пример #1
0
        public void FontFaceRule_ParseTest()
        {
            string            text   = "@font-face {  font-family: Headline; src: local(Futura-Medium), url(fonts.svg#MyGeometricModern) format(\"svg\", 'opentype'); unicode-range: U+3000-9FFF, U+ff??; }";
            ITextProvider     tp     = new StringTextProvider(text);
            TokenStream       tokens = Helpers.MakeTokenStream(tp);
            FontFaceDirective ff     = new FontFaceDirective();

            Assert.IsTrue(ff.Parse(new ItemFactory(tp, null), tp, tokens));

            Assert.IsTrue(tp.CompareTo(ff.Keyword.Start, "font-face", ignoreCase: false));
            Assert.IsNotNull(ff.Block.OpenCurlyBrace);
            Assert.IsNotNull(ff.Block.CloseCurlyBrace);

            Assert.AreEqual(3, ff.RuleBlock.Declarations.Count);

            Assert.IsTrue(tp.CompareTo(ff.RuleBlock.Declarations[0].PropertyName.Start, "font-family", ignoreCase: false));

            Assert.IsTrue(tp.CompareTo(ff.RuleBlock.Declarations[1].PropertyName.Start, "src", ignoreCase: false));
            Assert.IsTrue(ff.RuleBlock.Declarations[1].Values[0] is FunctionLocal);
            Assert.IsTrue(ff.RuleBlock.Declarations[1].Values[2] is UrlItem);
            Assert.IsTrue(ff.RuleBlock.Declarations[1].Values[3] is FunctionFormat);
            FunctionFormat func = ff.RuleBlock.Declarations[1].Values[3] as FunctionFormat;

            FunctionArgument arg = func.Arguments[0] as FunctionArgument;

            Assert.AreEqual("\"svg\",", tp.GetText(arg.Start, arg.Length));

            arg = func.Arguments[1] as FunctionArgument;
            Assert.AreEqual("'opentype'", tp.GetText(arg.Start, arg.Length));

            Assert.IsTrue(tp.CompareTo(ff.RuleBlock.Declarations[2].PropertyName.Start, "unicode-range", ignoreCase: false));
        }
Пример #2
0
        public void FunctionVar_ParseTest_WithDeclarationValues()
        {
            ITextProvider tp     = new StringTextProvider("var(--test, 20px 10px solid red )");
            TokenStream   tokens = Helpers.MakeTokenStream(tp);
            Function      f      = Function.ParseFunction(null, new ItemFactory(tp, null), tp, tokens);

            Assert.IsInstanceOfType(f, typeof(FunctionVar));

            FunctionVar fv = f as FunctionVar;

            Assert.AreEqual(2, fv.Arguments.Count);
            Assert.IsNotNull(fv.CustomPropertyName);
            Assert.AreEqual("--test", tp.GetText(fv.CustomPropertyName.Start, fv.CustomPropertyName.Length));
            Assert.AreEqual("20px 10px solid red", tp.GetText(fv.Arguments[1].Start, fv.Arguments[1].Length));
        }
Пример #3
0
        public void CssTextRange_Simple()
        {
            ITextProvider textProvider = new StringTextProvider("01234567890123456789");

            Assert.AreEqual("01234", textProvider.GetText(10, 5));

            Assert.IsFalse(TextRange.Touches(10, 5, -1));
            Assert.IsFalse(TextRange.Touches(10, 5, 9));
            Assert.IsTrue(TextRange.Touches(10, 5, 10));
            Assert.IsTrue(TextRange.Touches(10, 5, 14));
            Assert.IsTrue(TextRange.Touches(10, 5, 15));
            Assert.IsFalse(TextRange.Touches(10, 5, 16));
            Assert.IsFalse(TextRange.Touches(10, 5, 100));

            Assert.IsFalse(TextRange.ContainsChar(10, 5, -1));
            Assert.IsFalse(TextRange.ContainsChar(10, 5, 9));
            Assert.IsTrue(TextRange.ContainsChar(10, 5, 10));
            Assert.IsTrue(TextRange.ContainsChar(10, 5, 14));
            Assert.IsFalse(TextRange.ContainsChar(10, 5, 15));
            Assert.IsFalse(TextRange.ContainsChar(10, 5, 16));
            Assert.IsFalse(TextRange.ContainsChar(10, 5, 100));

            Assert.IsFalse(TextRange.Intersects(10, 5, 0, 10));
            Assert.IsTrue(TextRange.Intersects(10, 5, 0, 11));
            Assert.IsFalse(TextRange.Intersects(10, 5, 10, 0));
            Assert.IsTrue(TextRange.Intersects(10, 5, 10, 1));
            Assert.IsTrue(TextRange.Intersects(10, 5, 11, 10));
            Assert.IsTrue(TextRange.Intersects(10, 5, 14, 1));
            Assert.IsTrue(TextRange.Intersects(10, 5, 14, 10));
            Assert.IsFalse(TextRange.Intersects(10, 5, 15, 0));
            Assert.IsFalse(TextRange.Intersects(10, 5, 15, 1));
            Assert.IsFalse(TextRange.Intersects(10, 5, 20, 10));
        }
        public void IncrementalParse_SimpleChangeTest()
        {
            CssTree       doc   = new CssTree(new DefaultParserFactory());
            ITextProvider text1 = new StringTextProvider(".foo { color: red } .bar { /* comment */ color: rgb(100 }");

            doc.TextProvider = text1;

            bool fullTreeUpdateFired = false;
            CssItemsChangedEventArgs changeEventArgs = null;

            doc.TreeUpdated += (object sender, CssTreeUpdateEventArgs eventArgs) =>
            {
                fullTreeUpdateFired = true;
            };

            doc.ItemsChanged += (object sender, CssItemsChangedEventArgs eventArgs) =>
            {
                changeEventArgs = eventArgs;
            };

            ITextProvider text2 = new StringTextProvider(".foo { color: BLUE; } .bar { /* comment */ color: rgb(100 }");

            doc.OnTextChange(text2, 14, 3, 5);

            Assert.IsFalse(fullTreeUpdateFired);
            Assert.IsNotNull(changeEventArgs);
            Assert.AreEqual(1, changeEventArgs.DeletedItems.Count);
            Assert.AreEqual(1, changeEventArgs.InsertedItems.Count);

            Declaration oldDecl = changeEventArgs.DeletedItems[0] as Declaration;
            Declaration newDecl = changeEventArgs.InsertedItems[0] as Declaration;

            Assert.AreEqual("color: red", text1.GetText(oldDecl.Start, oldDecl.Length));
            Assert.AreEqual("color: BLUE;", text2.GetText(newDecl.Start, newDecl.Length));
        }
Пример #5
0
        public void Function_ParseTest5()
        {
            ITextProvider tp     = new StringTextProvider("foo(a,}");
            TokenStream   tokens = Helpers.MakeTokenStream(tp);
            Function      f      = Function.ParseFunction(null, new ItemFactory(tp, null), tp, tokens);

            Assert.AreEqual(typeof(Function), f.GetType());
            Assert.AreEqual("foo(", tp.GetText(f.FunctionName.Start, f.FunctionName.Length));
        }
Пример #6
0
        public void CssToken_Simple()
        {
            ITextProvider tp    = new StringTextProvider("foo*+bar");
            CssToken      token = new CssToken(CssTokenType.Asterisk, 3, 0);

            Assert.AreEqual(3, token.Start);
            Assert.AreEqual(0, token.Length);
            Assert.AreEqual(3, token.AfterEnd);
            Assert.AreEqual(string.Empty, tp.GetText(token.Start, token.Length));
            Assert.AreEqual(CssTokenType.Asterisk, token.TokenType);
        }
Пример #7
0
        public void Function_ParseTest7()
        {
            ITextProvider tp     = new StringTextProvider("var(a,}");
            TokenStream   tokens = Helpers.MakeTokenStream(tp);

            Function f = Function.ParseFunction(null, new ItemFactory(tp, null), tp, tokens);

            Assert.IsTrue(f.IsUnclosed);
            Assert.AreEqual(6, f.Length);
            Assert.IsInstanceOfType(f, typeof(FunctionVar));
            Assert.AreEqual("var(", tp.GetText(f.FunctionName.Start, f.FunctionName.Length));
        }
Пример #8
0
        public void TokenizerTest_EncodedUrl()
        {
            CssTokenizer tokenizer = new CssTokenizer();

            // Base case: No encoding:
            ITextProvider text   = new StringTextProvider(@"url('foo.jpg')");
            TokenList     actual = Helpers.MakeTokens(text);

            TokenizeFilesTest.CompareTokenArrays(
                new CssToken[]
            {
                new CssToken(CssTokenType.Url, 0, 4),
                new CssToken(CssTokenType.String, 4, 9),
                new CssToken(CssTokenType.CloseFunctionBrace, 13, 1),
                CssToken.EndOfFileToken(text)
            },
                actual);

#if SUPPORT_ENCODED_CSS
            // Escape characters:
            text   = new StringTextProvider(@"\u\r\l('foo.jpg')");
            actual = Helpers.MakeTokens(text.GetText(0, text.Length));

            TokenizeFilesTest.CompareTokenArrays(
                new CssToken[]
            {
                new CssToken(CssTokenType.Url, 0, 7),
                new CssToken(CssTokenType.String, 7, 9),
                new CssToken(CssTokenType.CloseFunctionBrace, 16, 1),
                CssToken.EndOfFileToken(text)
            },
                actual);

            // Unicode encode and escape characters:
            text   = new StringTextProvider(@"u\52 \l(foo)");
            actual = Helpers.MakeTokens(text.GetText(0, text.Length));

            TokenizeFilesTest.CompareTokenArrays(
                new CssToken[]
            {
                new CssToken(CssTokenType.Url, 0, 8),
                new CssToken(CssTokenType.UnquotedUrlString, 8, 3),
                new CssToken(CssTokenType.CloseFunctionBrace, 11, 1),
                CssToken.EndOfFileToken(text)
            },
                actual);
#endif
        }
        public void StringTextProvider_Simple()
        {
            string        text = "abcd";
            ITextProvider ts   = new StringTextProvider(text);

            Assert.AreEqual(text[0], ts[0]);
            Assert.AreEqual(text[3], ts[3]);
            Assert.AreEqual(4, ts.Length);
            Assert.AreEqual("bc", ts.GetText(1, 2));
            Assert.IsTrue(ts.CompareTo(1, "bcd", ignoreCase: false));
            Assert.IsFalse(ts.CompareTo(1, "abcd", ignoreCase: true));

            Assert.IsTrue(ts.CompareTo(1, new StringTextProvider("BCd"), 0, 3, ignoreCase: true));
            Assert.IsFalse(ts.CompareTo(1, new StringTextProvider("aBCDe"), 1, 3, ignoreCase: false));
            Assert.IsTrue(ts.CompareTo(1, new StringTextProvider("aBCDe"), 1, 3, ignoreCase: true));
        }
Пример #10
0
        public void TokenItem_ParseTest()
        {
            ITextProvider tp     = new StringTextProvider("foo .a { } *");
            TokenList     tokens = Helpers.MakeTokens(tp);
            TokenStream   iter   = new TokenStream(tokens);

            TokenItem ti = new TokenItem(null, null);

            Assert.IsTrue(ti.Parse(new ItemFactory(tp, null), tp, iter));
            Assert.AreEqual(CssTokenType.Identifier, ti.TokenType);
            Assert.AreEqual(0, ti.Token.Start);
            Assert.AreEqual(3, ti.Token.AfterEnd);
            Assert.AreEqual("foo", tp.GetText(ti.Start, ti.Length));

            ti = new TokenItem(null, null);
            Assert.IsTrue(ti.Parse(new ItemFactory(tp, null), tp, iter));
            Assert.AreEqual(CssTokenType.Dot, ti.TokenType);
            Assert.AreEqual(4, ti.Token.Start);
            Assert.AreEqual(1, ti.Token.Length);
        }
Пример #11
0
        public void PageDirective_ParseTest()
        {
            string        text = "@page { }";
            ITextProvider tp   = new StringTextProvider(text);
            TokenStream   ts   = Helpers.MakeTokenStream(tp);
            PageDirective pd   = new PageDirective();

            Assert.IsTrue(pd.Parse(new ItemFactory(tp, null), tp, ts));

            text = "@page:left { }";
            tp   = new StringTextProvider(text);
            ts   = Helpers.MakeTokenStream(tp);
            pd   = new PageDirective();
            Assert.IsTrue(pd.Parse(new ItemFactory(tp, null), tp, ts));
            Assert.IsTrue(tp.CompareTo(pd.Keyword.Start, "page", ignoreCase: false));
            Assert.AreEqual("left", tp.GetText(pd.PseudoPage.Start, pd.PseudoPage.Length));
            Assert.IsNotNull(pd.Block.OpenCurlyBrace);
            Assert.IsNotNull(pd.Block.CloseCurlyBrace);

            text = "@page :right{ }";
            tp   = new StringTextProvider(text);
            ts   = Helpers.MakeTokenStream(tp);
            pd   = new PageDirective();
            Assert.IsTrue(pd.Parse(new ItemFactory(tp, null), tp, ts));
            Assert.IsTrue(tp.CompareTo(pd.Keyword.Start, "page", ignoreCase: false));
            Assert.AreEqual("right", tp.GetText(pd.PseudoPage.Start, pd.PseudoPage.Length));
            Assert.IsNotNull(pd.Block.OpenCurlyBrace);
            Assert.IsNotNull(pd.Block.CloseCurlyBrace);

            text = "@page foo:first{ }";
            tp   = new StringTextProvider(text);
            ts   = Helpers.MakeTokenStream(tp);
            pd   = new PageDirective();
            Assert.IsTrue(pd.Parse(new ItemFactory(tp, null), tp, ts));
            Assert.IsTrue(tp.CompareTo(pd.Keyword.Start, "page", ignoreCase: false));
            Assert.IsTrue(tp.CompareTo(pd.Identifier.Start, "foo", ignoreCase: false));
            Assert.AreEqual("first", tp.GetText(pd.PseudoPage.Start, pd.PseudoPage.Length));
            Assert.IsNotNull(pd.Block.OpenCurlyBrace);
            Assert.IsNotNull(pd.Block.CloseCurlyBrace);

            text = "@page :foo{ @top-left { } }";
            tp   = new StringTextProvider(text);
            ts   = Helpers.MakeTokenStream(tp);
            pd   = new PageDirective();
            Assert.IsTrue(pd.Parse(new ItemFactory(tp, null), tp, ts));
            Assert.AreEqual(1, pd.PageDirectiveBlock.Margins.Count);
            Assert.AreEqual(MarginDirectiveType.TopLeft, pd.PageDirectiveBlock.Margins[0].DirectiveType);
            Assert.IsTrue(tp.CompareTo(pd.PageDirectiveBlock.Margins[0].Keyword.Start, "top-left", ignoreCase: false));

            text = "@page";
            tp   = new StringTextProvider(text);
            ts   = Helpers.MakeTokenStream(tp);
            pd   = new PageDirective();
            Assert.IsTrue(pd.Parse(new ItemFactory(tp, null), tp, ts));
            Assert.IsNull(pd.Block);

            text = "@page :right{ @top-left { }";
            tp   = new StringTextProvider(text);
            ts   = Helpers.MakeTokenStream(tp);
            pd   = new PageDirective();
            Assert.IsTrue(pd.Parse(new ItemFactory(tp, null), tp, ts));
            Assert.AreEqual(1, pd.PageDirectiveBlock.Margins.Count);
            Assert.IsTrue(tp.CompareTo(pd.PageDirectiveBlock.Margins[0].Keyword.Start, "top-left", ignoreCase: false));
            Assert.IsNotNull(pd.PageDirectiveBlock.Margins[0].RuleBlock.OpenCurlyBrace);
            Assert.IsNotNull(pd.PageDirectiveBlock.Margins[0].RuleBlock.CloseCurlyBrace);
            Assert.IsNotNull(pd.Block.OpenCurlyBrace);
            Assert.IsNull(pd.Block.CloseCurlyBrace);
        }
Пример #12
0
        public void Declaration_ParseTest_CustomPropertyValue_WithMultipleBracedBlocks()
        {
            string        text   = "--jsCode: if(condition) { trueAction(); } else { falseAction() };";
            ITextProvider tp     = new StringTextProvider(text);
            TokenStream   tokens = Helpers.MakeTokenStream(tp);
            Declaration   d      = new Declaration();

            Assert.IsTrue(d.Parse(new ItemFactory(tp, null), tp, tokens));
            Assert.IsTrue(d.IsCustomProperty);

            Assert.IsFalse(d.HasParseErrors);

            Assert.AreEqual(0, d.Start);
            Assert.AreEqual(text.Length, d.AfterEnd);

            Assert.AreEqual(7, d.Children.Count);
            Assert.AreEqual(typeof(TokenItem), d.Semicolon.GetType());
            Assert.AreEqual(64, d.Semicolon.Start); // make sure we have the correct semicolon

            Assert.AreEqual(typeof(TokenItem), d.Children[0].GetType());
            Assert.AreEqual(CssTokenType.Identifier, ((TokenItem)d.Children[0]).Token.TokenType);

            Assert.AreEqual(typeof(TokenItem), d.Children[1].GetType());
            Assert.AreEqual(CssTokenType.Colon, ((TokenItem)d.Children[1]).Token.TokenType);

            Assert.IsInstanceOfType(d.Children[2], typeof(Function));
            Function ifFunction = (Function)d.Children[2];

            // asserts for: if(condition)
            {
                Assert.AreEqual(1, ifFunction.Arguments.Count);
                Assert.AreEqual("condition", tp.GetText(ifFunction.Arguments[0].Start, ifFunction.Arguments[0].Length));
            }

            Assert.IsInstanceOfType(d.Children[3], typeof(PropertyValueBlock));
            PropertyValueBlock pvb1 = (PropertyValueBlock)d.Children[3];

            // asserts for: { trueAction(); }
            {
                Assert.AreEqual(4, pvb1.Children.Count);

                Assert.IsInstanceOfType(pvb1.Children[0], typeof(TokenItem));
                Assert.AreEqual(CssTokenType.OpenCurlyBrace, ((TokenItem)pvb1.Children[0]).TokenType);

                Assert.IsInstanceOfType(pvb1.Children[1], typeof(Function));
                Assert.AreEqual(0, ((Function)pvb1.Children[1]).Arguments.Count);

                Assert.IsInstanceOfType(pvb1.Children[2], typeof(TokenItem));
                Assert.AreEqual(CssTokenType.Semicolon, ((TokenItem)pvb1.Children[2]).TokenType);

                Assert.IsInstanceOfType(pvb1.Children[3], typeof(TokenItem));
                Assert.AreEqual(CssTokenType.CloseCurlyBrace, ((TokenItem)pvb1.Children[3]).TokenType);
            }

            Assert.IsInstanceOfType(d.Children[4], typeof(TokenItem));
            Assert.AreEqual(CssTokenType.Identifier, ((TokenItem)d.Children[4]).TokenType);

            Assert.IsInstanceOfType(d.Children[5], typeof(PropertyValueBlock));
            PropertyValueBlock pvb2 = (PropertyValueBlock)d.Children[5];

            // asserts for: { falseAction() }
            // note: to differentiate between the block above, this one doesn't have a semicolon
            // after the method call
            {
                Assert.AreEqual(3, pvb2.Children.Count);

                Assert.IsInstanceOfType(pvb2.Children[0], typeof(TokenItem));
                Assert.AreEqual(CssTokenType.OpenCurlyBrace, ((TokenItem)pvb2.Children[0]).TokenType);

                Assert.IsInstanceOfType(pvb2.Children[1], typeof(Function));
                Assert.AreEqual(0, ((Function)pvb2.Children[1]).Arguments.Count);

                Assert.IsInstanceOfType(pvb2.Children[2], typeof(TokenItem));
                Assert.AreEqual(CssTokenType.CloseCurlyBrace, ((TokenItem)pvb2.Children[2]).TokenType);
            }

            Assert.AreEqual(typeof(TokenItem), d.Children[d.Children.Count - 1].GetType());
            Assert.AreEqual(CssTokenType.Semicolon, ((TokenItem)d.Children[d.Children.Count - 1]).Token.TokenType);

            Assert.AreEqual(4, d.Values.Count);
            Assert.AreEqual(d.Children[2], d.Values[0]);
            Assert.AreEqual(d.Children[3], d.Values[1]);
            Assert.AreEqual(d.Children[4], d.Values[2]);
            Assert.AreEqual(d.Children[5], d.Values[3]);
        }