/// <summary> /// Removing an attribute /// </summary> internal void RemoveAttribute(HAttribute attribute) { // If attribute is alone, reset the list if (attribute.nextAttribute == attribute) { this.lastAttribute = null; } else { // Find previous attribute var prev = attribute.nextAttribute; while (prev.nextAttribute != attribute) { prev = prev.nextAttribute; } // Clean the list prev.nextAttribute = attribute.nextAttribute; // If attribute is the last, then prev become the last if (this.lastAttribute == attribute) { this.lastAttribute = prev; } } // Detach attribute attribute.parent = null; attribute.nextAttribute = null; }
/// <summary> /// Create a new attribute from an other attribute /// </summary> /// <param name="other">Attribute to copy from.</param> /// <exception cref="ArgumentNullException"> /// Throws if the passed attribute is null. /// </exception> public HAttribute(HAttribute other) { if (other == null) { throw new ArgumentNullException("other"); } this.Name = other.Name; this.Value = other.Value; }
/// <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> /// Remove all attributes /// </summary> public void RemoveAttributes() { var attr = this.lastAttribute; while (attr != null) { attr.parent = null; var n = attr.nextAttribute; attr.nextAttribute = null; attr = n; } this.lastAttribute = null; }
/// <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> /// Insert a content before the <param name="insert" /> node. /// </summary> internal void Insert(HNode insert, object content) { if (content == null) { return; } HNode n = content as HNode; if (n != null) { InsertNode(insert, n); return; } string s = content as string; if (s != null) { InsertString(insert, s); return; } HAttribute a = content as HAttribute; if (a != null) { AddAttribute(a); return; } IEnumerable e = content as IEnumerable; if (e != null) { if (insert == FirstNodeRef) { foreach (object obj in e.Cast <Object>().Reverse()) { Insert(insert, obj); } } else { foreach (object obj in e) { Insert(insert, obj); } } return; } InsertString(insert, content.ToString()); }
/// <summary> /// Enumerate attributes with an optional name filter. /// </summary> /// <param name="name">Name filtered</param> /// <returns>Enumerate the attributes. If <paramref name="name"/> is null, all attributes are returned.</returns> IEnumerable <HAttribute> GetAttributes(String name) { HAttribute a = lastAttribute; if (a != null) { do { a = a.nextAttribute; if (name == null || String.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase)) { yield return(a); } } while (a.parent == this && a != lastAttribute); } }
/// <summary> /// Returns the <see cref="HAttribute"/> associated with a name. /// </summary> /// <param name="name"> /// The name of the <see cref="HAttribute"/> to get. /// </param> /// <returns> /// The <see cref="HAttribute"/> with the name passed in. If there is no attribute /// with this name then null is returned. /// </returns> public HAttribute Attribute(String name) { HAttribute a = lastAttribute; if (name != null && a != null) { do { a = a.nextAttribute; if (String.Equals(a.Name, name, StringComparison.OrdinalIgnoreCase)) { return(a); } } while (a != lastAttribute); } return(null); }
/// <summary> /// Set one attribute for the element. /// </summary> /// <param name="element">Element.</param> /// <param name="name">Attribute name.</param> /// <param name="value">Attribute value</param> /// <returns>The element</returns> public static HElement Attribute(this HElement element, String name, String value) { if (element != null && !String.IsNullOrWhiteSpace(name)) { var attr = element.Attribute(name); if (String.IsNullOrEmpty(value)) { if (attr != null) { attr.Remove(); } } else { if (attr == null) { attr = new HAttribute(name); element.Add(attr); } attr.Value = value; } } return(element); }
/// <summary> /// Adding an attribute /// </summary> internal override void AddAttribute(HAttribute attribute) { if (Attribute(attribute.Name) != null) { throw new InvalidOperationException(String.Format("The attribute '{0}' already defined", attribute.Name)); } // Attribute already affected in a parent if (attribute.parent != null) { attribute = new HAttribute(attribute); } // Insert attribute attribute.parent = this; if (lastAttribute == null) { attribute.nextAttribute = attribute; } else { attribute.nextAttribute = lastAttribute.nextAttribute; lastAttribute.nextAttribute = attribute; } lastAttribute = attribute; }
/// <summary> /// Internal adding an attribute. /// </summary> internal virtual void AddAttribute(HAttribute attribute) { }