Ejemplo n.º 1
0
		/// <summary>
		/// Fills the style class.
		/// </summary>
		/// <param name="s">The style block.</param>
		private void FillStyleClassFromBlock(string s)
		{
			string[] parts = s.Split('{');
			var cleaned = parts[0].Trim();
			var styleNames = cleaned.Split(',').Select(x => x.Trim());

			foreach (var styleName in styleNames)
			{
				StyleClass sc;
				if (_scc.ContainsKey(styleName))
				{
					sc = _scc[styleName];
					_scc.Remove(styleName);
				}
				else
				{
					sc = new StyleClass();
				}

				sc.Position = ++styleCount;

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

				_scc.Add(sc.Name, sc);
			}
		}
Ejemplo n.º 2
0
        public StyleClass ParseStyleClass(string className, string style) {
            var sc = new StyleClass { Name = className };

            FillStyleClass(sc, className, style);

            return sc;
        }
Ejemplo n.º 3
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 (!Attributes.ContainsKey(item.Key))
             Attributes.Add(item.Key, item.Value);
         else if (canOverwrite)
             Attributes[item.Key] = item.Value;
     }
 }
        public static IList<AttributeToCss> FindEquivalent(IDomObject domobject, StyleClass styles)
        {

            return (from attributeRuleMatch in _linkedAttributes
                    where domobject.HasAttribute(attributeRuleMatch.Key) && styles.Attributes.ContainsKey(attributeRuleMatch.Value)
                    select new AttributeToCss
                        {
                            AttributeName = attributeRuleMatch.Key, CssValue = styles.Attributes[attributeRuleMatch.Value].Value
                        }).ToList();
        }
Ejemplo n.º 5
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) {
         CssAttribute existing;
         
         if (!Attributes.TryGetValue(item.Key, out existing) ||
             canOverwrite && (!existing.Important || item.Value.Important))
         {
             Attributes[item.Key] = item.Value;
         }
     }
 }
Ejemplo n.º 6
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 = style.Split(';');
			string[] atrs = CleanUp(style).Split(';');

            foreach (string a in atrs) {
				var attribute = CssAttribute.FromRule(a);

				if (attribute != null) sc.Attributes[attribute.Style] = attribute;
            }
        }
	   public static IEnumerable<AttributeToCss> GetAllStyles(IDomObject domElement, StyleClass styleClass)
        {
	        var attributeCssList = new List<AttributeToCss>();

	        AddSpecialPremailerAttributes(attributeCssList, styleClass);

		   if (styleClass.Attributes.Count > 0)
			   attributeCssList.Add(new AttributeToCss { AttributeName = "style", CssValue = styleClass.ToString() });

		   attributeCssList.AddRange(CssStyleEquivalence.FindEquivalent(domElement, styleClass));

            return attributeCssList;
        }
Ejemplo n.º 8
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>
		public void Merge(StyleClass styleClass)
		{
			foreach (var item in styleClass.Attributes)
			{
				if (!this.Attributes.ContainsKey(item.Key))
				{
					this.Attributes.Add(item.Key, item.Value);
				}
				else
				{
					this.Attributes[item.Key] = item.Value;
				}
			}
		}
        private static IDomObject ApplyStyles(IDomObject domElement, StyleClass clazz)
        {
            var styles = CssElementStyleResolver.GetAllStyles(domElement, clazz);

            foreach (var attributeToCss in styles)
            {
                PrepareAttribute(domElement, attributeToCss);
                //domElement.SetAttribute(attributeToCss.AttributeName, attributeToCss.CssValue);
            }

	        if (string.IsNullOrEmpty(domElement.Attributes["style"]))
		        domElement.RemoveAttribute("style");

            return domElement;
        }
Ejemplo n.º 10
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 (_scc.ContainsKey(styleName)) {
                sc = _scc[styleName];
                _scc.Remove(styleName);
            } else {
                sc = new StyleClass();
            }

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

            _scc.Add(sc.Name, sc);
        }
Ejemplo n.º 11
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(":"))
                    continue;

                string key = a.Split(':')[0].Trim();

                if (sc.Attributes.ContainsKey(key))
                    sc.Attributes.Remove(key);

                sc.Attributes.Add(key, a.Split(':')[1].Trim().ToLower());
            }
        }
Ejemplo n.º 12
0
		private static IElement ApplyStyles(IElement domElement, StyleClass clazz)
		{
			var styles = CssElementStyleResolver.GetAllStyles(domElement, clazz);

			foreach (var attributeToCss in styles)
			{
				PrepareAttribute(domElement, attributeToCss);
				//domElement.SetAttribute(attributeToCss.AttributeName, attributeToCss.CssValue);
			}

			var styleAttr = domElement.Attributes["style"];
			if (styleAttr == null || string.IsNullOrEmpty(styleAttr.Value))
			{
				domElement.RemoveAttribute("style");
			}

			return domElement;
		}
	   private static void AddSpecialPremailerAttributes(List<AttributeToCss> attributeCssList, StyleClass styleClass)
	   {
		   while (true)
		   {
			   var premailerRuleMatch = styleClass.Attributes.FirstOrDefault(a => a.Key.StartsWith(premailerAttributePrefix));

			   var key = premailerRuleMatch.Key;
			   var cssAttribute = premailerRuleMatch.Value;

			   if (key == null)
				   break;

			   attributeCssList.Add(new AttributeToCss
			   {
				   AttributeName = key.Replace(premailerAttributePrefix, ""),
				   CssValue = cssAttribute.Value
			   });

			   styleClass.Attributes.Remove(key);
		   }
	   }
Ejemplo n.º 14
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(":"))
                {
                    continue;
                }

                string key = a.Split(':')[0].Trim();

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

                sc.Attributes.Add(key, a.Split(':')[1].Trim().ToLower());
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Fills the style class.
        /// </summary>
        /// <param name="s">The style block.</param>
        private void FillStyleClassFromBlock(string s)
        {
            string[] parts      = s.Split('{');
            var      cleaned    = parts[0].Trim();
            var      styleNames = cleaned.Split(',').Select(x => x.Trim());

            foreach (var styleName in styleNames)
            {
                StyleClass sc;
                if (_scc.ContainsKey(styleName))
                {
                    sc = _scc[styleName];
                    _scc.Remove(styleName);
                }
                else
                {
                    sc = new StyleClass();
                }

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

                _scc.Add(sc.Name, sc);
            }
        }
Ejemplo n.º 16
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;
		}
Ejemplo n.º 17
0
        private static void AddSpecialPremailerAttributes(List <AttributeToCss> attributeCssList, StyleClass styleClass)
        {
            while (true)
            {
                var premailerRuleMatch = styleClass.Attributes.FirstOrDefault(a => a.Key.StartsWith(premailerAttributePrefix));

                var key          = premailerRuleMatch.Key;
                var cssAttribute = premailerRuleMatch.Value;

                if (key == null)
                {
                    break;
                }

                attributeCssList.Add(new AttributeToCss
                {
                    AttributeName = key.Replace(premailerAttributePrefix, ""),
                    CssValue      = cssAttribute.Value
                });

                styleClass.Attributes.Remove(key);
            }
        }
Ejemplo n.º 18
0
        public static IEnumerable <AttributeToCss> GetAllStyles(IDomObject domElement, StyleClass styleClass)
        {
            var attributeCssList = new List <AttributeToCss>();

            AddSpecialPremailerAttributes(attributeCssList, styleClass);

            if (styleClass.Attributes.Count > 0)
            {
                attributeCssList.Add(new AttributeToCss {
                    AttributeName = "style", CssValue = styleClass.ToString()
                });
            }

            attributeCssList.AddRange(CssStyleEquivalence.FindEquivalent(domElement, styleClass));

            return(attributeCssList);
        }
Ejemplo n.º 19
0
        public static IEnumerable <AttributeToCss> GetAllStyles(IDomObject domElement, StyleClass styleClass)
        {
            var attributeCssList = new List <AttributeToCss>
            {
                new AttributeToCss {
                    AttributeName = "style", CssValue = styleClass.ToString()
                }
            };

            attributeCssList.AddRange(CssStyleEquivalence.FindEquivalent(domElement, styleClass));

            return(attributeCssList);
        }