/// <summary>
        /// Parse the rules
        /// </summary>
        private void Parse(ref string css)
        {
            bool withBrackets = false;

            css = css.Trim();

            if (css.StartsWith("{", StringComparison.OrdinalIgnoreCase))
            {
                withBrackets = true;
                css          = css.Substring(1);
            }

            while (true)
            {
                css = css.Trim();
                if (css.Length == 0)
                {
                    if (withBrackets)
                    {
                        throw new Exception("Style block missing ending bracket");
                    }
                    break;
                }
                else if (css.StartsWith("}", StringComparison.OrdinalIgnoreCase))
                {
                    // end of block;
                    css = css.Substring(1);
                    break;
                }
                else if (css.StartsWith("@", StringComparison.OrdinalIgnoreCase))
                {
                    IgnoreRule(ref css);
                }
                else
                {
                    // must be a selector or error
                    CssStyleRule rule = CssStyleRule.Parse(ref css);

                    if (rule != null)
                    {
                        InsertRule(rule);
                    }
                    else
                    {
                        IgnoreRule(ref css);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Parse and construct a CssStyleRule
        /// </summary>
        public static CssStyleRule Parse(ref string css)
        {
            Match match = StyleRuleRegex.Match(css);

            if (match.Success && match.Length > 0)
            {
                var rule = new CssStyleRule(match);
                css        = css.Substring(match.Length);
                rule.Style = new CssStyleDeclaration(ref css);

                return(rule);
            }

            return(null);
        }
 private void InsertRule(CssStyleRule rule)
 {
     cssRules.Add(rule);
 }