/// <summary> /// Sets a named style, validating its format. /// </summary> /// /// <exception cref="ArgumentException"> /// Thrown if the style name and value are not valid CSS /// </exception> /// /// <param name="name"> /// The style name. /// </param> /// <param name="value"> /// The style value. /// </param> /// <param name="strict"> /// When true, the styles will be validated and an error thrown if any are not valid. /// </param> public void SetStyle(string name, string value, bool strict) { name = Utility.Support.FromCamelCase(name); if (value == null) { Remove(name); return; } value = value.Trim().Replace(";", String.Empty); name = name.Trim(); CssStyle style = null; if (!HtmlStyles.StyleDefs.TryGetValue(name, out style)) { if (strict) { throw new ArgumentException("The style '" + name + "' is not valid (strict mode)"); } } else { switch (style.Type) { case CSSStyleType.UnitOption: if (!style.Options.Contains(value)) { try { value = ValidateUnitString(name, value); } catch { throw new ArgumentException("No valid unit data or option provided for attribue '" + name + "'. Valid options are: " + OptionList(style)); } } break; case CSSStyleType.Option: if (!style.Options.Contains(value)) { throw new ArgumentException("The value '" + value + "' is not allowed for attribute '" + name + "'. Valid options are: " + OptionList(style)); } break; case CSSStyleType.Unit: value = ValidateUnitString(name, value); break; default: // TODO: other formatting verification break; } } SetRaw(name, value); }
/// <summary> /// Returns the options for this style as a comma-separated list /// </summary> /// /// <param name="style"> /// The style. /// </param> /// /// <returns> /// A comma-separated string /// </returns> protected string OptionList(CssStyle style) { string list = ""; foreach (string item in style.Options) { list += (list == String.Empty ? String.Empty : ",") + "'" + item + "'"; } return(list); }
static HtmlStyles() { XmlDocument xDoc = new XmlDocument(); Stream dataStream = Support.GetResourceStream("CsQuery.Resources." + CssDefs); xDoc.Load(dataStream); XmlNamespaceManager nsMan = new XmlNamespaceManager(xDoc.NameTable); nsMan.AddNamespace("cssmd", "http://schemas.microsoft.com/Visual-Studio-Intellisense/css"); var nodes = xDoc.DocumentElement.SelectNodes("cssmd:property-set/cssmd:property-def", nsMan); string type; foreach (XmlNode el in nodes) { CssStyle st = new CssStyle(); st.Name = el.Attributes["_locID"].Value; type = el.Attributes["type"].Value; switch (type) { case "length": st.Type = CSSStyleType.Unit; break; case "color": st.Type = CSSStyleType.Color; break; case "composite": st.Type = CSSStyleType.Composite; st.Format = el.Attributes["syntax"].Value; break; case "enum": case "enum-length": if (type == "enum-length") { st.Type = CSSStyleType.UnitOption; } else { st.Type = CSSStyleType.Option; } st.Options = new HashSet<string>(el.Attributes["enum"].Value .Split(StringSep, StringSplitOptions.RemoveEmptyEntries)); break; case "font": st.Type = CSSStyleType.Font; break; case "string": st.Type = CSSStyleType.String; break; case "url": st.Type = CSSStyleType.Url; break; default: throw new NotImplementedException("Error parsing css xml: unknown type '" + type + "'"); } st.Description = el.Attributes["description"].Value; StyleDefs[st.Name] = st; } }