/// <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> /// 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> /// 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))); }