Example #1
0
		public StyleClass ParseStyleClass(string className, string style)
		{
			StyleClass sc = new StyleClass();
			sc.Name = className;

			this.FillStyleClass(sc, className, style);

			return sc;
		}
Example #2
0
		/// <summary>
		/// Merges the specified style class, with this instance. Styles on this instance are not overriden by duplicates in the specified styleClass.
		/// </summary>
		/// <param name="styleClass">The style class.</param>
		/// <param name="canOverwrite">if set to <c>true</c> [can overwrite].</param>
		public void Merge(StyleClass styleClass, bool canOverwrite)
		{
			foreach (var item in styleClass.Attributes)
			{
				if (!this.Attributes.ContainsKey(item.Key))
				{
					this.Attributes.Add(item.Key, item.Value);
				}
				else if (canOverwrite)
				{
					this.Attributes[item.Key] = item.Value;
				}
			}
		}
Example #3
0
		/// <summary>
		/// Fills the style class.
		/// </summary>
		/// <param name="s">The style block.</param>
		private void FillStyleClass(string s)
		{
			StyleClass sc = null;
			string[] parts = s.Split('{');
			string styleName = CleanUp(parts[0]).Trim();

			if (this._scc.ContainsKey(styleName))
			{
				sc = this._scc[styleName];
				this._scc.Remove(styleName);
			}
			else
			{
				sc = new StyleClass();
			}

			this.FillStyleClass(sc, styleName, parts[1]);

			this._scc.Add(sc.Name, sc);
		}
Example #4
0
		/// <summary>
		/// Fills the style class.
		/// </summary>
		/// <param name="sc">The style class.</param>
		/// <param name="styleName">Name of the style.</param>
		/// <param name="style">The styles.</param>
		private void FillStyleClass(StyleClass sc, string styleName, string style)
		{
			sc.Name = styleName;

			string[] atrs = CleanUp(style).Split(';');

			foreach (string a in atrs)
			{
				if (a.Contains(":"))
				{
					string _key = a.Split(':')[0].Trim();

					if (sc.Attributes.ContainsKey(_key))
					{
						sc.Attributes.Remove(_key);
					}

					sc.Attributes.Add(_key, a.Split(':')[1].Trim().ToLower());
				}
			}
		}
Example #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 static 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)
                {
                    //RWM: Just because one style fails to merge doesn't mean they all should.
                    try
                    {
                        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();
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Trace.Fail(ex.Message);
                    }
                }

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

            return(doc.DocumentNode.OuterHtml);
        }