예제 #1
0
        Stylesheet TestFile(string file, bool testSimilarities = true)
        {
            var input  = GetTestFile(file);
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            var ir = p.Rules.Where(r => !r.IsValid).ToArray();

            Assert.AreEqual(0, parser.Errors.Count);
            Assert.IsTrue(p.IsValid, "invalid css");
            var output = p.ToString();

            if (testSimilarities)
            {
                Assert.AreEqual(input.Count(c => c == '{'), output.Count(c => c == '{'));
                Assert.AreEqual(input.Count(c => c == '}'), output.Count(c => c == '}'));
                foreach (var c in new[] { "{", ";\r" })
                {
                    string toReparse;
                    if (c == "{")
                    {
                        var reg = new Regex(@"(?<=\}[^\{\}\'\" + "\"" + @"+]+)\{", RegexOptions.Multiline | RegexOptions.Compiled);
                        toReparse = reg.Replace(output, "/* some comment */{");
                    }
                    else
                    {
                        var reg = new Regex(@"(?<!url\(data:[a-zA-Z\+/]+)\;", RegexOptions.Multiline | RegexOptions.Compiled);
                        toReparse = reg.Replace(output, "/* some comment */;");
                    }
                    parser.SetContext(toReparse);
                    var p2 = parser.DoParse();
                    Assert.IsTrue(parser.End);
                    Assert.AreEqual(0, parser.Errors.Count);
                    Assert.IsTrue(p2.IsValid, "invalid css on reparse");
                    var output2 = p2.ToString();
                    Assert.AreEqual(output, output2);
                }
            }

            var clone = p.Clone();

            Assert.AreEqual(output, clone.ToString());

            return(p);
        }
예제 #2
0
        public Stylesheet ParseSheet(string cssBody)
        {
            var parser = new StylesheetParser();

            parser.SetContext(cssBody);
            var ret = parser.DoParse();

            Errors = parser.Errors;
            return(ret);
        }
예제 #3
0
        public void InvalidOscaro()
        {
            var input  = ".lvml{behavior:url(#default#VML);display:inline-block;position:absolute;}";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.AreEqual(0, parser.Errors.Count);
            Assert.IsTrue(p.IsValid, "invalid css");
        }
예제 #4
0
        public void GumbyComment()
        {
            var input  = "@charset \"UTF-8\";\n/*  */\n@import url(url);";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.AreEqual(0, parser.Errors.Count);
            Assert.IsTrue(p.IsValid, "invalid css");
        }
예제 #5
0
        public void SpaceIssue2()
        {
            var input  = ".class{-webkit-box-shadow:inset 0 -1px 0 rgba(0,0,0,0.15);}";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.IsTrue(p.IsValid, "must be valid css");
            Assert.AreEqual(".class{-webkit-box-shadow:inset 0 -1px 0 rgba(0,0,0,0.15);}", p.ToString());
        }
예제 #6
0
        public void SpaceIssue1()
        {
            var input  = ".class{outline:5px auto -webkit-focus-ring-color;}";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.IsTrue(p.IsValid, "must be valid css");
            Assert.AreEqual(".class{outline:5px auto -webkit-focus-ring-color;}", p.ToString());
        }
예제 #7
0
        public void SeemsToBeAnOperation()
        {
            var input  = ".class{background-position: 9px -20px;}";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.IsTrue(p.IsValid, "must be valid css");
            Assert.AreEqual(".class{background-position:9px -20px;}", p.ToString());
        }
예제 #8
0
        public void TwoBracesAtOnce()
        {
            var input  = "@media print{.table-bordered th{border:1px solid #ddd !important;}}@font-face{font-family:'Glyphicons Halflings';}";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.AreEqual(0, parser.Errors.Count);
            Assert.IsTrue(p.IsValid, "invalid css");
            Assert.AreEqual(input, p.ToString());
        }
예제 #9
0
        public void StringsInProperty()
        {
            // from materialize.css
            var input  = "a[href]:after {content: \" (\" attr(href) \")\"}";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.AreEqual(0, parser.Errors.Count);
            Assert.IsTrue(p.IsValid, "invalid css");
        }
예제 #10
0
        public void AllSelectorBlock()
        {
            var input  = "*{color:blue;}";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.AreEqual(0, parser.Errors.Count);
            Assert.AreEqual(0, parser.Errors.Count);
            Assert.IsTrue(p.IsValid, "invalid css");
            Assert.AreEqual(input, p.ToString());
        }
예제 #11
0
        public void Rem()
        {
            // from materialize.css
            var input  = "textarea{width:calc(100% - 3rem);}";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.AreEqual(0, parser.Errors.Count);
            Assert.IsTrue(p.IsValid, "invalid css");
            Assert.AreEqual(input.Replace(" - ", "-"), p.ToString());
        }
예제 #12
0
        public void LinearGradient()
        {
            // from facebook
            var input  = ".x{background-image:-webkit-linear-gradient(135deg, red 0%, orange 15%, yellow 30%, green 45%, blue 60%,indigo 75%, violet 80%, red 100%);}";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.AreEqual(0, parser.Errors.Count);
            Assert.IsTrue(p.IsValid, "invalid css");
            Assert.AreEqual(input.Replace(", ", ","), p.ToString());
        }
예제 #13
0
        static Stylesheet Test(string input, string expected = null, int count = 1)
        {
            expected = expected ?? input;
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.AreEqual(0, parser.Errors.Count);
            Assert.IsTrue(p.IsValid, "invlid css");
            Assert.AreEqual(count, p.Rules.Count);
            Assert.AreEqual(expected, p.ToString());
            return(p);
        }
예제 #14
0
        public void OrphanBlock()
        {
            var input  = "{color:blue;}.class{color:red;}";
            var parser = new StylesheetParser();

            parser.SetContext(input);
            var p = parser.DoParse();

            Assert.IsTrue(parser.End);
            Assert.AreEqual(1, parser.Errors.Count);
            Assert.IsFalse(p.IsValid, "must be invalid css");
            Assert.AreEqual(".class{color:red;}", p.ToString());
            Assert.AreEqual(input, p.ToString(CssRestitution.OriginalWhenErrorOrInvalid));
            Assert.AreEqual(2, p.Rules.Count);
            Assert.IsInstanceOfType(p.Rules[0], typeof(OrphanBlockRule));
            Assert.IsInstanceOfType(p.Rules[1], typeof(ClassRule));
            Assert.IsTrue(p.Rules[1].IsValid);
            Assert.AreEqual(".class{color:red;}", p.Rules[1].ToString());
        }