/// <summary> /// Extract all style properties of the element /// </summary> public static IDictionary <String, String> Css(this HElement element) { String style = element.Attr("style"); var result = new Dictionary <String, String>(StringComparer.OrdinalIgnoreCase); if (!String.IsNullOrWhiteSpace(style)) { // Split style String[] parts = style.Split(new Char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (var part in parts) { String name, value; int p = part.IndexOf('='); if (p < 0) { name = part; value = String.Empty; } else { name = part.Substring(0, p); value = part.Substring(p + 1); } result[ConvertCamelNameToStyleName(name)] = value; } } return(result); }
/// <summary> /// Set one or more attributes from a POCO objet for the element. /// </summary> public static HElement Attr(this HElement element, Object attributes) { if (element != null && attributes != null) { foreach (var member in attributes.GetType().GetMembers().Where(m => m is PropertyInfo || m is FieldInfo)) { String mName = member.Name.Replace("_", "-"); if (member is PropertyInfo) { element.Attr(mName, Convert.ToString(((PropertyInfo)member).GetValue(attributes, null))); } else if (member is FieldInfo) { element.Attr(mName, Convert.ToString(((FieldInfo)member).GetValue(attributes))); } } } return(element); }
/// <summary> /// Apply style to an element /// </summary> static void SetStyle(HElement element, IDictionary <String, String> properties) { if (element != null && properties != null) { element.Attr( "style", String.Join( ";", properties .Where(kvp => !String.IsNullOrWhiteSpace(kvp.Value)) .Select(kvp => String.Concat(kvp.Key, "=", kvp.Value)) ) ); } }
/// <summary> /// Remove a list of data values to an element /// </summary> public static HElement RemoveData(this HElement element, params String[] keys) { if (element != null) { // Build the key list from the context if (keys != null && keys.Length > 0) { // Convert all keys to data keys keys = keys.Select(k => MakeDataKey(k)).ToArray(); } else { // Extract all data keys keys = element.Attributes().Where(attr => attr.Name.StartsWith("data-", StringComparison.OrdinalIgnoreCase)).Select(attr => attr.Name).ToArray(); } foreach (var key in keys) { element.Attr(key, null); } } return(element); }
/// <summary> /// Set a data value to an element /// </summary> public static HElement Data(this HElement element, String key, String value) { return(element.Attr(MakeDataKey(key), value)); }
/// <summary> /// Retrieve a data value from an element /// </summary> public static String Data(this HElement element, String key) { return(element.Attr(MakeDataKey(key))); }