Ejemplo n.º 1
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        private void TestSelectorSpecificity(string selector, int specificity)
        {
            var parser = new ExCSS.Parser();
            var sheet  = parser.Parse(selector + " {color:black}");

            Assert.AreEqual(specificity, CssQuery.GetSpecificity(sheet.StyleRules[0].Selector));
        }
Ejemplo n.º 2
0
        public void TestStyle_BuildFrom()
        {
            Style style;
            var   parser     = new ExCSS.Parser();
            var   stylesheet = parser.Parse(
                @"#Header
{
	padding-left: 20px;
	padding-top: 15px;
    padding-bottom: 15px;
}");
            var result = Style.BuildFrom(out style, stylesheet.Rules[0] as StyleRule);

            Assert.IsTrue(result, "Style.BuildFrom Failed!");
        }
Ejemplo n.º 3
0
            public SvgNodeInfo(XElement elem)
            {
                this.elem = elem;

                var elemStyles = new Dictionary <string, string>();

                string style = (string)elem.Attribute("style");

                if (!string.IsNullOrWhiteSpace(style))
                {
                    var cssParser = new ExCSS.Parser();

                    var inlineSheet = cssParser.Parse("#a{" + style + "}");

                    foreach (var rule in inlineSheet.StyleRules)
                    {
                        foreach (var decl in rule.Declarations)
                        {
                            elemStyles[decl.Name] = decl.Term.ToString();
                        }
                    }
                }

                // Look for explicit attribute and override style
                foreach (var attributeName in new[] { "stroke", "fill" })
                {
                    if ((string)elem.Attribute(attributeName) is string attributeValue)
                    {
                        elemStyles[attributeName] = attributeValue;
                    }
                }

                // Process style rule into value
                if (elemStyles.TryGetValue("stroke", out string strokeText))
                {
                    this.Stroke = ColorExtensions.GetColorServer(strokeText);
                }

                string strokeWidth = (string)elem.Attribute("stroke-width");

                this.StrokeWidth = string.IsNullOrWhiteSpace(strokeWidth) ? new SvgUnit(1.0f) : (SvgUnit)svgUnitConverter.ConvertFrom(strokeWidth);

                // Process style rule into value
                if (elemStyles.TryGetValue("fill", out string fillText))
                {
                    this.Fill = ColorExtensions.GetColorServer(fillText);
                }
            }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var k = new HtmlAgilityPack.HtmlDocument();

            k.LoadHtml(File.ReadAllText(@"c:\temp\google.html"));;
            var styles = k.DocumentNode.Descendants().Where(x => x.Name == "style");

            foreach (var style in styles.Take(1))
            {
                var p = new ExCSS.Parser();
                var d = p.Parse(style.InnerText).ToString();
                if (d.Contains(":px") || d.Contains(": px"))
                {
                    Debugger.Break();
                }
            }
        }
Ejemplo n.º 5
0
        public void TestStyleDeclarations(string style, string expected)
        {
            var actual = new StringBuilder();

            var cssParser   = new ExCSS.Parser();
            var inlineSheet = cssParser.Parse("#a{" + style + "}");

            foreach (var rule in inlineSheet.StyleRules)
            {
                foreach (var decl in rule.Declarations)
                {
                    actual.Append(decl.Name).Append(":").Append(decl.Term.ToString()).Append(";");
                }
            }

            Assert.AreEqual(expected, actual.ToString());
        }
Ejemplo n.º 6
0
        protected override bool TryCreate(string value, out object x)
        {
            //var rc = new ExCSS.StylesheetParser().Parse("#a{" + value + "}");
            var cssParser = new ExCSS.Parser();
            var sheet     = cssParser.Parse("#dummyxx{" + value + "}");

            var list = new List <KeyValuePair <string, string> >();

            foreach (var rule in sheet.StyleRules)
            {
                foreach (var decl in rule.Declarations)
                {
                    list.Add(new KeyValuePair <string, string>(decl.Name, decl.Term.ToString()));
                }
            }
            x = list;
            return(sheet != null);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            { // FONTAWESOME :-)
                Console.WriteLine("MAKE FontAwesome");
                var          data = new Data();
                ExCSS.Parser p    = new ExCSS.Parser();
                var          css  = p.Parse(File.ReadAllText("font-awesome.css"));
                foreach (var item in css.StyleRules)
                {
                    ExCSS.StyleRule srule = item as ExCSS.StyleRule;
                    //.fa-glass:before
                    string cleanName = srule.Value.Replace(".fa-", "").Replace(":before", "").Replace("-", "_");

                    foreach (var name in cleanName.Split(','))
                    {
                        FontSymbol d = new FontSymbol();
                        d.Name = MakeCamelText(name);

                        var decl = srule.Declarations.FirstOrDefault(A => A.Name.ToLower() == "content");
                        if (decl != null)
                        {
                            var term = decl.Term as ExCSS.PrimitiveTerm;
                            if (term != null)
                            {
                                d.Hex = string.Format("{0:X}", (int)(term.Value as string)[0]);
                                data.Symbols.Add(d);
                            }
                        }
                    }
                }
                MakeJson(data, "fontawesome.json", false);
                Console.WriteLine("MAKE FontAwesome DONE.");
            }


            Console.Read();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            { // FONTAWESOME :-)
                Console.WriteLine("MAKE FontAwesome");
                var data = new Data();
                ExCSS.Parser p = new ExCSS.Parser();
                var css = p.Parse(File.ReadAllText("font-awesome.css"));
                foreach (var item in css.StyleRules)
                {
                    ExCSS.StyleRule srule = item as ExCSS.StyleRule;
                    //.fa-glass:before
                    string cleanName = srule.Value.Replace(".fa-", "").Replace(":before", "").Replace("-", "_");

                    foreach (var name in cleanName.Split(','))
                    {
                        FontSymbol d = new FontSymbol();
                        d.Name = MakeCamelText(name);

                        var decl = srule.Declarations.FirstOrDefault(A => A.Name.ToLower() == "content");
                        if (decl != null)
                        {
                            var term = decl.Term as ExCSS.PrimitiveTerm;
                            if (term != null)
                            {
                                d.Hex = string.Format("{0:X}", (int)(term.Value as string)[0]);
                                data.Symbols.Add(d);
                            }
                        }
                    }
                }
                MakeJson(data, "fontawesome.json", false);
                Console.WriteLine("MAKE FontAwesome DONE.");
            }

            Console.Read();
        }
Ejemplo n.º 9
-1
        /// <summary>
        /// Determines whether the specified text for the style declaration
        /// (either as an inline attribute or a &lt;style&quot; element)
        /// is valid.  
        /// 
        /// The presence of <c>@import</c> directives or
        /// CSS expressions invalidates the style declaration.
        /// Similarly, any parsing errors invalidate the style declaration.
        /// </summary>
        /// <param name="styleText">The text of the style declaration.</param>
        /// <returns><c>true</c> if the specified style declaration is valid;
        /// otherwise, <c>false</c>.</returns>
        private static bool IsValidStyle(string styleText)
        {
            // Remove remote-linked stylesheets
              if (string.IsNullOrWhiteSpace(styleText))
              {
            return false;
              }

              // HACK: Look for CSS expressions, because
              // ExCSS doesn't successfully parse them out.
              if (_cssExpressionRegex.IsMatch(styleText))
              {
            return false;
              }

              // Next, try to parse out the CSS using ExCSS.
              // If it fails, we'll remove the node too.
              try
              {
            var stylesheetParser = new ExCSS.Parser();
            var stylesheet = stylesheetParser.Parse(styleText);

            // Make sure we have no import directives.
            if (stylesheet.ImportDirectives.Count > 0)
            {
              return false;
            }

            // Note: this is disabled because ExCSS doesn't even recognize
            // CSS expressions at the moment.
            //// Make sure the value of no property declaration starts with
            //// expression(
            //foreach (ExCSS.StyleRule ruleset in stylesheet.Rulesets)
            //{
            //  foreach (ExCSS.Property propDecl in ruleset.Declarations)
            //  {
            //    if (propDecl.Term.ToString().Trim().ToLower().StartsWith("expression("))
            //    {
            //      node.Remove();
            //      return;
            //    }
            //  }
            //}
            return true;

              }
              catch
              {
            // Some sort of parsing error, which means this style
            // declaration is invalid.
            return false;
              }
        }