Ejemplo n.º 1
0
        private Dictionary <IElement, List <StyleClass> > SortBySpecificity(
            Dictionary <IElement, List <StyleClass> > styles)
        {
            var result = new Dictionary <IElement, List <StyleClass> >();

            foreach (var style in styles)
            {
                if (style.Key.Attributes != null)
                {
                    var sortedStyles = style.Value.OrderBy(x => _cssSelectorParser.GetSelectorSpecificity(x.Name)).ThenBy(x => x.Position).ToList();
                    var styleAttr    = style.Key.Attributes["style"];

                    if (styleAttr == null || String.IsNullOrWhiteSpace(styleAttr.Value))
                    {
                        style.Key.SetAttribute("style", String.Empty);
                    }
                    else                     // Ensure that existing inline styles always win.
                    {
                        sortedStyles.Add(_cssParser.ParseStyleClass("inline", styleAttr.Value));
                    }

                    result[style.Key] = sortedStyles;
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        private int GetSelectorSpecificity(Dictionary <string, int> cache, string selector)
        {
            selector = selector ?? "";
            int specificity;

            if (!cache.TryGetValue(selector, out specificity))
            {
                specificity     = _cssSelectorParser.GetSelectorSpecificity(selector);
                cache[selector] = specificity;
            }

            return(specificity);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Static method to quickly find the specificity of a single CSS selector.<para/>
        /// Don't use this when parsing a lot of selectors, create an instance of <see cref="CssSelectorParser"/> and use that instead.
        /// </summary>
        /// <param name="selector">CSS Selector</param>
        /// <returns>Specificity score of the given selector.</returns>
        public static int SelectorSpecificity(string selector)
        {
            var instance = new CssSelectorParser();

            return(instance.GetSelectorSpecificity(selector));
        }
Ejemplo n.º 4
0
		/// <summary>
		/// Static method to quickly find the specificity of a single CSS selector.<para/>
		/// Don't use this when parsing a lot of selectors, create an instance of <see cref="CssSelectorParser"/> and use that instead.
		/// </summary>
		/// <param name="selector">CSS Selector</param>
		/// <returns>Specificity score of the given selector.</returns>
		public static int SelectorSpecificity(string selector)
		{
			var instance = new CssSelectorParser();
			return instance.GetSelectorSpecificity(selector);
		}