Пример #1
0
        private static IEnumerable <StyleRule> GetStyleRules(string css)
        {
            string cssWithoutComments = RemoveCommentsFromCss(css);

            MatchCollection matches = Regex.Matches(cssWithoutComments, @"\s*(?<selector>[^{]+){(?<attributes>[^}]+)}");

            var cssSelectors = new List <StyleRule>();

            foreach (Match match in matches)
            {
                string selector = match.Groups["selector"].Value;

                if (!selector.Contains(":"))
                {
                    string[] selectors = selector.Split(',');

                    foreach (string specificSelector in selectors)
                    {
                        var rule = new StyleRule
                        {
                            Selector     = specificSelector,
                            Declarations = ConvertStyleToDictionary(match.Groups["attributes"].Value),
                            Index        = match.Index
                        };

                        cssSelectors.Add(rule);
                    }
                }
            }

            cssSelectors.Sort(CompareRule);

            return(cssSelectors);
        }
Пример #2
0
        private static int CompareRule(StyleRule first, StyleRule second)
        {
            int firstPrecendence  = GetCSSSelectorPrecedence(first.Selector);
            int secondPrecendence = GetCSSSelectorPrecedence(second.Selector);

            if (firstPrecendence == secondPrecendence)
            {
                return((first.Index < second.Index) ? -1 : 1);
            }

            return((firstPrecendence < secondPrecendence) ? -1 : 1);
        }
Пример #3
0
        private static IEnumerable<StyleRule> GetStyleRules(string css)
        {
            string cssWithoutComments = RemoveCommentsFromCss(css);

            MatchCollection matches = Regex.Matches(cssWithoutComments, @"\s*(?<selector>[^{]+){(?<attributes>[^}]+)}");

            var cssSelectors = new List<StyleRule>();

            foreach (Match match in matches)
            {
                string selector = match.Groups["selector"].Value;

                if (!selector.Contains(":"))
                {
                    string[] selectors = selector.Split(',');

                    foreach (string specificSelector in selectors)
                    {
                        var rule = new StyleRule
                        {
                            Selector = specificSelector,
                            Declarations = ConvertStyleToDictionary(match.Groups["attributes"].Value),
                            Index = match.Index
                        };

                        cssSelectors.Add(rule);
                    }
                }
            }

            cssSelectors.Sort(CompareRule);

            return cssSelectors;
        }
Пример #4
0
        private static int CompareRule(StyleRule first, StyleRule second)
        {
            int firstPrecendence = GetCSSSelectorPrecedence(first.Selector);
            int secondPrecendence = GetCSSSelectorPrecedence(second.Selector);

            if (firstPrecendence == secondPrecendence)
            {
                return (first.Index < second.Index) ? -1 : 1;
            }

            return (firstPrecendence < secondPrecendence) ? -1 : 1;
        }