public static SideBorder Parse(String str) { if (str == null) { return(SideBorder.Empty); } // The properties of a border that can be set, are (in order): border-width, border-style, and border-color. // It does not matter if one of the values above are missing, e.g. border:solid #ff0000; is allowed. // The main problem for parsing this attribute is that the browsers allow any permutation of the values... meaning more coding :( // http://www.w3schools.com/cssref/pr_border.asp List <String> borderParts = new List <String>(str.Split(HttpUtility.WhiteSpaces, StringSplitOptions.RemoveEmptyEntries)); if (borderParts.Count == 0) { return(SideBorder.Empty); } // Initialize default values Unit borderWidth = Unit.Empty; HtmlColor borderColor = HtmlColor.Empty; w.BorderValues borderStyle = w.BorderValues.Nil; // Now try to guess the values with their permutation // handle border style for (int i = 0; i < borderParts.Count; i++) { borderStyle = Converter.ToBorderStyle(borderParts[i]); if (borderStyle != w.BorderValues.Nil) { borderParts.RemoveAt(i); // no need to process this part anymore break; } } for (int i = 0; i < borderParts.Count; i++) { borderWidth = ParseWidth(borderParts[i]); if (borderWidth.IsValid) { borderParts.RemoveAt(i); // no need to process this part anymore break; } } // find width if (borderParts.Count > 0) { borderColor = HtmlColor.Parse(borderParts[0]); } // returns the instance with default value if needed. // These value are the ones used by the browser, i.e: solid 3px black return(new SideBorder( borderStyle == w.BorderValues.Nil? w.BorderValues.Single : borderStyle, borderColor.IsEmpty? HtmlColor.Black : borderColor, borderWidth.IsFixed? borderWidth : new Unit(UnitMetric.Pixel, 4))); }
/// <summary> /// Gets an attribute representing a color (named color, hexadecimal or hexadecimal /// without the preceding # character). /// </summary> public HtmlColor GetAsColor(String name) { return(HtmlColor.Parse(this[name])); }