/// <summary> /// Gets an attribute representing a single border side. /// If a border style/color/width has been specified individually, it will override the grouped definition. /// </summary> /// <returns>If the attribute is misformed, the <see cref="HtmlBorder.IsValid"/> property is set to false.</returns> public SideBorder GetAsSideBorder(String name) { string attrValue = this[name]; SideBorder border = SideBorder.Parse(attrValue); // handle attributes specified individually. Unit width = SideBorder.ParseWidth(this[name + "-width"]); if (width.IsValid) { border.Width = width; } var color = GetAsColor(name + "-color"); if (!color.IsEmpty) { border.Color = color; } var style = ConverterUtility.ConvertToBorderStyle(this[name + "-style"]); if (style != w.BorderValues.Nil) { border.Style = style; } return(border); }
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; Color borderColor = Color.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 = ConverterUtility.ConvertToBorderStyle(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 = ConverterUtility.ConvertToForeColor(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? Color.Black : borderColor, borderWidth.IsFixed? borderWidth : new Unit(UnitMetric.Pixel, 4))); }