Esempio n. 1
0
        private RuleSet ParseRuleSet(string css)
        {
            RuleSet ruleSet = new RuleSet();
            IRule   current = ruleSet;
            string  buffer  = string.Empty;

            foreach (char ch in css)
            {
                switch (ch)
                {
                case ';':
                    var declaration = ParseDeclaration(buffer.Trim());
                    if (declaration != null)
                    {
                        current.Declarations.Add(declaration);
                    }
                    buffer = string.Empty;
                    break;

                case '{':
                    IRule nestedClass;
                    if (buffer.IndexOf("@media") == -1)
                    {
                        nestedClass          = new PredefinedRuleSet();
                        nestedClass.Selector = buffer.Trim();
                    }
                    else
                    {
                        nestedClass          = new MediaQuery();
                        nestedClass.Selector = buffer.Trim() + "{&";
                    }
                    ruleSet.NestedRules.Add(nestedClass);
                    buffer  = string.Empty;
                    current = nestedClass;
                    break;

                case '}':
                    break;

                default:
                    buffer += ch;
                    break;
                }
            }
            ruleSet.SetClassName();
            return(ruleSet);
        }
Esempio n. 2
0
        private RuleSet ParseRuleSet(string css)
        {
            RuleSet ruleSet = new RuleSet
            {
                Label = _id != DEFAULT ? _id : null
            };
            IRule  current           = ruleSet;
            string buffer            = string.Empty;
            bool   nestedClassClosed = true; //Start from true becuase the parent doesnt need to be closed

            foreach (char ch in css)
            {
                switch (ch)
                {
                case ';':
                    Declaration declaration = ParseDeclaration(buffer.Trim());
                    if (declaration != null)
                    {
                        if (declaration.Property == "label")
                        {
                            current.Label = current.Label == null?declaration.Value.Trim() : $"{current.Label}-{declaration.Value.Trim()}";
                        }
                        else
                        {
                            current.Declarations.Add(declaration);
                        }
                    }
                    buffer = string.Empty;
                    break;

                case '{':
                    IRule nestedClass;
                    if (buffer.IndexOf("@media") == -1)
                    {
                        nestedClass = new PredefinedRuleSet
                        {
                            Selector = buffer.Trim()
                        };
                    }
                    else
                    {
                        nestedClass = new MediaQuery
                        {
                            Selector = buffer.Trim() + "{&"
                        };
                    }
                    ruleSet.NestedRules.Add(nestedClass);
                    buffer            = string.Empty;
                    current           = nestedClass;
                    nestedClassClosed = false;
                    break;

                case '}':
                    nestedClassClosed = true;
                    break;

                default:
                    buffer += ch;
                    break;
                }
            }
            if (!nestedClassClosed)
            {
                throw StyledException.GetException(css, "A nested class is missing a '}' character", null);
            }
            if (buffer.Trim() != string.Empty)
            {
                throw StyledException.GetException(buffer, "This is usually caused by a missing ';' character at the end of a declaration", null);
            }
            ruleSet.SetClassName();
            return(ruleSet);
        }