/// <summary> /// Serialize an element /// </summary> protected virtual void SerializeElement(HElement element, TextWriter writer) { writer.Write("<{0}", element.Name); foreach (var attr in element.Attributes()) { SerializeAttribute(attr.Name, attr.Value, writer); } if (element.HasNodes || !IsVoidElement(element.Name)) { writer.Write(">"); if (IsRawElement(element.Name)) { SerializeRawText(element.Nodes(), writer); } else if (IsEscapableRawElement(element.Name)) { SerializeEscapableRawText(element.Nodes(), writer); } else { SerializeContainer(element, writer); } writer.Write("</{0}>", element.Name); } else { writer.Write(" />"); } }
/// <summary> /// Set some style properties from an object to an element /// </summary> public static HElement Css(this HElement element, Object properties) { if (element != null && properties != null) { var styles = element.Css(); if (properties is IDictionary <String, String> ) { foreach (var prop in ((IDictionary <String, String>)properties)) { styles[ConvertCamelNameToStyleName(prop.Key)] = prop.Value; } } else { foreach (var member in properties.GetType().GetMembers().Where(m => m is PropertyInfo || m is FieldInfo)) { String mName = ConvertCamelNameToStyleName(member.Name.Replace("_", "-")); if (member is PropertyInfo) { styles[mName] = Convert.ToString(((PropertyInfo)member).GetValue(properties, null)); } else if (member is FieldInfo) { styles[mName] = Convert.ToString(((FieldInfo)member).GetValue(properties)); } } } SetStyle(element, styles); } return(element); }
/// <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> /// Append the element to the end of the content of the target /// </summary> public static T AppendTo <T>(this T content, HElement target) where T : HNode { if (content != null && target != null) { target.Append(content); } return(content); }
/// <summary> /// Append content at the beginning of the element /// </summary> public static HElement Prepend(this HElement element, params object[] content) { if (element != null) { element.InsertFirst(content); } return(element); }
/// <summary> /// Append content at the end of the element /// </summary> public static HElement Append(this HElement element, params object[] content) { if (element != null) { element.Add(content); } return(element); }
/// <summary> /// Remove all nodes in the element. /// </summary> public static HElement Empty(this HElement element) { if (element != null) { element.RemoveNodes(); } return(element); }
/// <summary> /// Insert element before the target /// </summary> public static T InsertBefore <T>(this T element, HElement target) where T : HNode { if (element != null && target != null) { target.Before(element); } return(element); }
/// <summary> /// Set the content of the element to the specified text. /// </summary> public static HElement Text(this HElement element, String text) { if (element != null) { element.Empty().Append(new HText(text ?? String.Empty)); } return(element); }
/// <summary> /// Append the element to the beginning of the content of the target /// </summary> public static T PrependTo <T>(this T element, HElement target) where T : HNode { if (element != null && target != null) { target.Prepend(element); } return(element); }
/// <summary> /// Append the set of elements to the beginning of the content of the target /// </summary> public static IEnumerable <T> PrependTo <T>(this IEnumerable <T> content, HElement target) where T : HNode { if (content != null && target != null) { target.Prepend(content); } return(content); }
/// <summary> /// Adds the specified class(es) to the element. /// </summary> /// <param name="element">Element.</param> /// <param name="classNames">One or more classes to be added to the class attribute of the element.</param> /// <returns>Element</returns> public static HElement AddClass(this HElement element, IEnumerable <String> classNames) { if (element != null && classNames != null) { element.Attribute("class", String.Join(" ", element.GetClasses().Concat(classNames).Distinct(StringComparer.OrdinalIgnoreCase))); } return(element); }
/// <summary> /// Get the contents of the element /// </summary> public static IEnumerable <HNode> Contents(this HElement element) { if (element != null) { return(element.Nodes()); } return(Enumerable.Empty <HNode>()); }
/// <summary> /// Indicates if an element contains a data value /// </summary> public static bool HasData(this HElement element, String key) { if (element == null || String.IsNullOrWhiteSpace(key)) { return(false); } return(element.Attribute(MakeDataKey(key)) != null); }
/// <summary> /// Indicates if an element contains any data value /// </summary> public static bool HasData(this HElement element) { if (element == null) { return(false); } return(element.Attributes().Where(attr => attr.Name.StartsWith("data-", StringComparison.OrdinalIgnoreCase)).Any()); }
/// <summary> /// Get the siblings of the element /// </summary> public static IEnumerable <HElement> Siblings(this HElement element) { if (element != null && element.Parent != null) { return(element.Parent.Elements().Where(e => e != null && e != element).Distinct()); } return(Enumerable.Empty <HElement>()); }
/// <summary> /// Remove some class names to an element. /// </summary> /// <param name="element"></param> /// <param name="className"></param> /// <returns></returns> public static HElement RemoveClass(this HElement element, String[] className) { if (element != null && className != null && className.Length > 0) { var classes = element.GetClasses(); element.Attribute("class", String.Join(" ", classes.Except(className, StringComparer.OrdinalIgnoreCase))); } return(element); }
/// <summary> /// Extract classes from an element /// </summary> public static String[] GetClasses(this HElement element) { HAttribute clsAttr = element != null?element.Attribute("class") : null; if (clsAttr == null || clsAttr.Value == null) { return(new String[0]); } return(ExtractClassNames(clsAttr.Value)); }
/// <summary> /// Set the HTML content of the element. /// </summary> public static HElement Html(this HElement element, String html) { if (element != null) { element .Empty() .Append(HSerializer.DefaultSerializer.Deserialize(new StringReader(html ?? String.Empty)).ToArray()); } return(element); }
/// <summary> /// Determine if the element is assigned the given class. /// </summary> /// <param name="element">Source element.</param> /// <param name="className">The class name to search for.</param> /// <returns>Returns true if the class is assigned.</returns> public static bool HasClass(this HElement element, String className) { // If invalid argument then not found if (element == null || string.IsNullOrWhiteSpace(className)) { return(false); } var cls = element.GetClasses(); return(cls.Any(c => String.Equals(className, c, StringComparison.OrdinalIgnoreCase))); }
/// <summary> /// Set an object as key-value pairs of data to an element /// </summary> public static HElement Data(this HElement element, object values) { if (element != null && values != null) { foreach (var kv in UtilHelpers.ExtractKeyValues(values)) { element.Data(kv.Key, kv.Value); } } return(element); }
/// <summary> /// Wrap <paramref name="wrappingElement"/> around each element of the set /// </summary> public static IEnumerable <T> Wrap <T>(this IEnumerable <T> elements, HElement wrappingElement) where T : HNode { if (elements != null && wrappingElement != null) { foreach (var element in elements.ToList()) { element.Wrap(wrappingElement); } } return(elements); }
/// <summary> /// Remove all classes to an element. /// </summary> /// <param name="element"></param> /// <returns></returns> public static HElement RemoveClass(this HElement element) { if (element != null) { var attr = element.Attribute("class"); if (attr != null) { attr.Remove(); } } return(element); }
/// <summary> /// Get the combined text contents of the element, including his descendants. /// </summary> public static String Text(this HElement element) { if (element != null) { return(String.Concat( element .Nodes() .Select(n => (n is HText) ? ((HText)n).Value : (n is HElement) ? ((HElement)n).Text() : String.Empty) )); } return(String.Empty); }
/// <summary> /// Get the value of the attribute of the element. /// </summary> public static String Attr(this HElement element, String name) { if (element != null && !String.IsNullOrWhiteSpace(name)) { var attr = element.Attribute(name); if (attr != null) { return(attr.Value); } } return(null); }
/// <summary> /// Set a style property in the element /// </summary> public static HElement Css(this HElement element, String propertyName, String value) { if (element == null || String.IsNullOrWhiteSpace(propertyName)) { return(element); } var styles = element.Css(); styles[ConvertCamelNameToStyleName(propertyName)] = value; SetStyle(element, styles); return(element); }
/// <summary> /// Get the HTML content of an element /// </summary> public static String Html(this HElement element) { if (element != null) { StringBuilder result = new StringBuilder(); foreach (var content in element.Nodes()) { result.Append(HSerializer.DefaultSerializer.SerializeNode(content)); } return(result.ToString()); } return(String.Empty); }
/// <summary> /// Create a new element from an another. /// </summary> /// <param name="other"> /// Another element that will be copied to this element. /// </param> public HElement(HElement other) : base(other) { this.Name = other.Name; HAttribute a = other.lastAttribute; if (a != null) { do { a = a.nextAttribute; AddAttribute(new HAttribute(a)); } while (a != other.lastAttribute); } }
/// <summary> /// Enumerate all parents /// </summary> protected IEnumerable <HElement> GetParents(String name) { HContainer p = parent; while (p != null) { HElement e = p as HElement; if (e != null && (name == null || String.Equals(e.Name, name, StringComparison.OrdinalIgnoreCase))) { yield return(e); } p = p.parent; } }
/// <summary> /// Get the value of a style property in the element /// </summary> public static String Css(this HElement element, String propertyName) { if (element == null || String.IsNullOrWhiteSpace(propertyName)) { return(String.Empty); } var styles = element.Css(); String result; if (styles.TryGetValue(ConvertCamelNameToStyleName(propertyName), out result)) { return(result); } return(String.Empty); }