示例#1
0
        /// <summary>
        /// Moves the CSS embedded in the specified htmlInput to inline style attributes.
        /// </summary>
        /// <param name="htmlInput">The HTML input.</param>
        /// <param name="removeStyleElements">if set to <c>true</c> the style elements are removed.</param>
        /// <returns>Returns the html input, with styles moved to inline attributes.</returns>
        public string MoveCssInline(string htmlInput, bool removeStyleElements)
        {
            var doc        = CQ.CreateDocument(htmlInput);
            var styleNodes = doc["style"];

            if (styleNodes == null || styleNodes.Length == 0)
            {
                return(htmlInput);                                              // no styles to move
            }
            foreach (var style in styleNodes)
            {
                if (style.Attributes["id"] != null && !String.IsNullOrWhiteSpace(style.Attributes["id"]) && style.Attributes["id"].Equals("mobile", StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                var    cssParser = new CssParser();
                string cssBlock  = style.InnerHTML;

                cssParser.AddStyleSheet(cssBlock);

                foreach (var rule in cssParser.Styles)
                {
                    if (rule.Key.StartsWith("@media"))
                    {
                        continue;
                    }

                    var styleClass = rule.Value;
                    var elements   = doc[styleClass.Name];

                    foreach (var element in elements)
                    {
                        if (_elementsWithoutStyle.Contains(element.NodeName.ToLower()))
                        {
                            continue;
                        }

                        var elementStyle = element.Style;
                        if (elementStyle == null)
                        {
                            continue;
                        }
                        StyleClass sc = cssParser.ParseStyleClass("dummy", elementStyle.CssText ?? String.Empty);
                        sc.Merge(styleClass, true);
                        foreach (var attr in sc.Attributes)
                        {
                            elementStyle.SetStyle(attr.Key, attr.Value, false);
                        }
                    }
                }

                if (removeStyleElements)
                {
                    style.Remove();
                }
            }

            return(doc.Render());
        }
示例#2
0
        /// <summary>
        /// Moves the CSS embedded in the specified htmlInput to inline style attributes.
        /// </summary>
        /// <param name="htmlInput">The HTML input.</param>
        /// <param name="removeStyleElements">if set to <c>true</c> the style elements are removed.</param>
        /// <returns>Returns the html input, with styles moved to inline attributes.</returns>
        public string MoveCssInline(string htmlInput, bool removeStyleElements)
        {
            HtmlDocument doc = new HtmlDocument();

            doc.LoadHtml(htmlInput);

            var styleNodes = doc.DocumentNode.SelectNodes("//style");

            if (styleNodes == null)
            {
                return(htmlInput);                                // no styles to move
            }
            foreach (var style in styleNodes)
            {
                if (style.Attributes["id"] != null && !String.IsNullOrWhiteSpace(style.Attributes["id"].Value) && style.Attributes["id"].Value.Equals("mobile", StringComparison.InvariantCultureIgnoreCase))
                {
                    continue;
                }

                CssParser cssParser = new CssParser();
                string    cssBlock  = style.InnerHtml;

                cssParser.AddStyleSheet(cssBlock);

                foreach (var item in cssParser.Styles)
                {
                    var styleClass = item.Value;
                    var elements   = doc.DocumentNode.QuerySelectorAll(styleClass.Name);

                    foreach (var element in elements)
                    {
                        HtmlAttribute styleAttribute = element.Attributes["style"];

                        if (styleAttribute == null)
                        {
                            element.Attributes.Add("style", String.Empty);
                            styleAttribute = element.Attributes["style"];
                        }

                        StyleClass sc = cssParser.ParseStyleClass("dummy", styleAttribute.Value);
                        sc.Merge(styleClass, false);

                        styleAttribute.Value = sc.ToString();
                    }
                }

                if (removeStyleElements)
                {
                    style.Remove();
                }
            }

            return(doc.DocumentNode.OuterHtml);
        }
示例#3
0
        private Dictionary <IElement, StyleClass> MergeStyleClasses(
            Dictionary <IElement, List <StyleClass> > styles)
        {
            var result = new Dictionary <IElement, StyleClass>();
            var stylesBySpecificity = SortBySpecificity(styles);

            foreach (var elemStyle in stylesBySpecificity)
            {
                // CSS Classes are assumed to be sorted by specifity now, so we can just merge these up.
                var merged = new StyleClass();
                foreach (var style in elemStyle.Value)
                {
                    merged.Merge(style, true);
                }

                result[elemStyle.Key] = merged;
            }

            return(result);
        }
示例#4
0
		private Dictionary<IDomObject, StyleClass> MergeStyleClasses(
			Dictionary<IDomObject, List<StyleClass>> styles)
		{
			var result = new Dictionary<IDomObject, StyleClass>();
			var stylesBySpecificity = SortBySpecificity(styles);

			foreach (var elemStyle in stylesBySpecificity)
			{
				// CSS Classes are assumed to be sorted by specifity now, so we can just merge these up.
				var merged = new StyleClass();
				foreach (var style in elemStyle.Value)
				{
					merged.Merge(style, true);
				}

				result[elemStyle.Key] = merged;
			}

			return result;
		}