Ejemplo n.º 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);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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;
        }
Ejemplo n.º 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;
        }