Пример #1
0
        public static SVGStyleSheet Parse(string cssText)
        {
            var result = new SVGStyleSheet();
            var tokens = Tokenize(cssText);

            var sheet = new SVGStyleSheet();

            while (ParseSelector(tokens, sheet))
            {
                foreach (var sel in sheet.selectors)
                {
                    if (result.selectors.Contains(sel))
                    {
                        CombineProperties(result[sel], sheet[sel]);
                    }
                    else
                    {
                        result[sel] = sheet[sel];
                    }
                }
                sheet.Clear();
            }

            return(result);
        }
Пример #2
0
        private static bool ParseSelector(List <string> tokens, SVGStyleSheet sheet)
        {
            if (tokens.Count == 0)
            {
                return(false);
            }

            var newSheet = new SVGStyleSheet();

            while (true)
            {
                var selectorName = PopToken(tokens);
                newSheet[selectorName] = new SVGPropertySheet();

                while (PeekToken(tokens) == ",")
                {
                    PopToken(tokens);
                }

                if (PeekToken(tokens) == "" || PeekToken(tokens) == "{")
                {
                    break;
                }
            }

            var sep = PopToken(tokens);

            if (sep != "{")
            {
                Debug.LogError("Invalid CSS selector opening bracket: \"" + sep + "\"");
                return(false);
            }

            var props = new SVGPropertySheet();

            ParseProperties(tokens, props);

            // Transfer properties to the new selectors
            foreach (var key in newSheet.selectors)
            {
                sheet[key] = CopyProperties(props);
            }

            sep = PopToken(tokens);
            if (sep != "}")
            {
                Debug.LogError("Invalid CSS selector closing bracket: \"" + sep + "\"");
                return(false);
            }

            return(true);
        }