Пример #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
        private static SortedList <string, StyleClass> Join(IEnumerable <string> cssBlocks)
        {
            var parser = new CssParser();

            foreach (var block in cssBlocks)
            {
                parser.AddStyleSheet(block);
            }

            return(parser.Styles);
        }
Пример #3
0
        private static CssParser Join(IEnumerable <string> cssBlocks)
        {
            var parser = new CssParser();

            foreach (var block in cssBlocks)
            {
                parser.AddStyleSheet(block);
            }

            return(parser);
        }
Пример #4
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);
        }
Пример #5
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, true);

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

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

			return doc.DocumentNode.OuterHtml;
		}
Пример #6
0
		private static SortedList<string, StyleClass> Join(IEnumerable<string> cssBlocks)
		{
			var parser = new CssParser();

			foreach (var block in cssBlocks)
			{
				parser.AddStyleSheet(block);
			}

			return parser.Styles;
		}