Exemplo n.º 1
0
 /// <summary>
 /// Add a CSS class to the HTML tag. If the class is already found (case-sensitive), no change is applied.
 /// </summary>
 /// <param name="clsName">The name of the CSS class.</param>
 public virtual void AddClass(string clsName)
 {
     if (HTMLProperties.ContainsKey("class"))
     {
         if (!String.IsNullOrEmpty(HTMLProperties["class"]))
         {
             foreach (string cls in HTMLProperties["class"].Split(' '))
             {
                 if (cls.ToUpper() == clsName.ToUpper())
                 {
                     return;
                 }
             }
             HTMLProperties["class"] = HTMLProperties["class"] + " " + clsName;
         }
         else
         {
             HTMLProperties["class"] = clsName;
         }
     }
     else
     {
         HTMLProperties.Add("class", clsName);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Retrieve an attribute's value by name. If the value does not exist, returns an empty string.
 /// </summary>
 /// <param name="attName">The name of the attribute to retrieve.</param>
 /// <returns>Returns the value of the attribute or an empty string if not found.</returns>
 public override string Attr(string attName)
 {
     if (HTMLProperties.ContainsKey(attName))
     {
         return(HTMLProperties[attName]);
     }
     else
     {
         return("");
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// Similar to jQuery's attr() method. Retrieve an attribute value.<br/>
 /// If the attribute does not exist, returns an empty string.
 /// </summary>
 /// <param name="attName"></param>
 /// <returns></returns>
 public virtual string Attr(string attName)
 {
     if (HTMLProperties.ContainsKey(attName.ToLower()))
     {
         return(HTMLProperties[attName.ToLower()]);
     }
     else
     {
         return("");
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Set (or update) an attribute. Sanitization is optional.
 /// </summary>
 /// <param name="attName">The name of the attribute.</param>
 /// <param name="attValue">The value of the attribute.</param>
 /// <param name="sanitize">Whether to sanitize the value supplied.</param>
 /// <returns>Returns the value of the attribute.</returns>
 public string Attr(string attName, string attValue, bool sanitize)
 {
     if (HTMLProperties.ContainsKey(attName))
     {
         HTMLProperties[attName] = sanitize ? SanitizeForMarkup(attValue) : attValue;
     }
     else
     {
         HTMLProperties.Add(attName, sanitize ? SanitizeForMarkup(attValue) : attValue);
     }
     return(attValue);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Similar to jQuery's attr() method. Assign an attribute value.<br/>
 /// If the attribute does not exist, a new attribute is created.
 /// </summary>
 /// <param name="attName">The name of the attribute.</param>
 /// <param name="attValue">The value of the attribute.</param>
 /// <returns>Returns the value supplied for attValue.</returns>
 public virtual string Attr(string attName, string attValue)
 {
     if (HTMLProperties.ContainsKey(attName.ToLower()))
     {
         HTMLProperties[attName.ToLower()] = attValue;
     }
     else
     {
         HTMLProperties.Add(attName.ToLower(), attValue);
     }
     return(attValue);
 }
Exemplo n.º 6
0
 /// <summary>
 /// Returns 'true' if the HTML tag contains the supplied CSS class.<br/>
 /// Returns 'false' if the CSS class is not found or if the tag has no 'class' property.
 /// </summary>
 /// <param name="clsName">The name of the CSS class to check for.</param>
 /// <returns>Returns true if the supplied CSS class is found in the class="" attribute.</returns>
 public virtual bool IsClass(string clsName)
 {
     if (HTMLProperties.ContainsKey("class"))
     {
         foreach (string cls in HTMLProperties["class"].Split(' '))
         {
             if (cls == clsName)
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Exemplo n.º 7
0
 /// <summary>
 /// A class used to render XML tags. Attributes are case-sensitive and values are escaped.
 /// </summary>
 /// <param name="name">The name of the element<br/>ie: "a" for anchor tag, "table" for table tag.</param>
 /// <param name="properties">An anonymous object whose properties will become attributes of the XML tag.</param>
 public XmlTag(string name, object properties)
 {
     HTMLTagName = name;
     foreach (PropertyInfo pi in properties.GetType().GetProperties())
     {
         if (HTMLProperties.ContainsKey(pi.Name))
         {
             HTMLProperties[pi.Name] = SanitizeForMarkup(pi.GetValue(properties, null).ToString());
         }
         else
         {
             object oVal = (pi.GetValue(properties, null) ?? new Object());
             HTMLProperties.Add(pi.Name, SanitizeForMarkup(oVal.ToString()));
         }
     }
     SelfClosing = false;
 }
Exemplo n.º 8
0
 /// <summary>
 /// A class used to render HTML tags.
 /// </summary>
 /// <param name="name">The name of the element<br/>ie: "a" for anchor tag, "table" for table tag.</param>
 /// <param name="properties">An anonymous object whose properties are applied to the element.<br/>
 /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
 public HtmlTag(string name, object properties)
 {
     HTMLTagName = name;
     foreach (PropertyInfo pi in properties.GetType().GetProperties())
     {
         if (HTMLProperties.ContainsKey(pi.Name.ToLower()))
         {
             HTMLProperties[pi.Name.ToLower()] = pi.GetValue(properties, null).ToString();
         }
         else
         {
             object oVal = (pi.GetValue(properties, null) ?? new Object());
             HTMLProperties.Add(pi.Name.ToLower(), oVal.ToString());
         }
     }
     SelfClosing = false;
 }
Exemplo n.º 9
0
 /// <summary>
 /// Merge this object's tag attributes with another anonymous object.<br/>
 /// The anonymous object (argument) takes precedent.
 /// </summary>
 /// <param name="properties">The anonymous object whose properties will be applied to the tag.</param>
 /// <returns>Returns self.</returns>
 public virtual XmlTag MergeObjectProperties(object properties)
 {
     if (properties == null)
     {
         return(this);
     }
     foreach (PropertyInfo pi in properties.GetType().GetProperties())
     {
         if (HTMLProperties.ContainsKey(pi.Name))
         {
             HTMLProperties[pi.Name] = SanitizeForMarkup(pi.GetValue(properties, null).ToString());
         }
         else
         {
             HTMLProperties.Add(pi.Name, SanitizeForMarkup(pi.GetValue(properties, null).ToString()));
         }
     }
     return(this);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Adds new properties to the HTML tag and overrides existing properties.
 /// </summary>
 /// <param name="attribs">Attributes collected from an ASP.net server control.</param>
 /// <returns>Returns 'this'.</returns>
 public virtual HtmlTag MergeObjectProperties(AttributeCollection attribs)
 {
     if (attribs == null)
     {
         return(this);
     }
     foreach (var key in attribs.Keys)
     {
         if (HTMLProperties.ContainsKey(key.ToString().ToLower()))
         {
             HTMLProperties[key.ToString().ToLower()] = HTMLProperties[key.ToString().ToLower()] + attribs[key.ToString()];
         }
         else
         {
             HTMLProperties.Add(key.ToString().ToLower(), attribs[key.ToString()]);
         }
     }
     return(this);
 }
Exemplo n.º 11
0
 /// <summary>
 /// Adds new properties to the HTML tag and overrides existing properties.
 /// </summary>
 /// <param name="properties">An anonymous object whose properties are applied to the element.<br/>
 /// ie: new { Class = "cssClass", onchange = "jsFunction()" } </param>
 /// <returns>Returns 'this'.</returns>
 public virtual HtmlTag MergeObjectProperties(object properties)
 {
     if (properties == null)
     {
         return(this);
     }
     foreach (PropertyInfo pi in properties.GetType().GetProperties())
     {
         if (HTMLProperties.ContainsKey(pi.Name.ToLower()))
         {
             HTMLProperties[pi.Name.ToLower()] = pi.GetValue(properties, null).ToString();
         }
         else
         {
             HTMLProperties.Add(pi.Name.ToLower(), pi.GetValue(properties, null).ToString());
         }
     }
     return(this);
 }
Exemplo n.º 12
0
 /// <summary>
 /// Remove a CSS class from the HTML tag. If the class is not found, no change is applied.
 /// </summary>
 /// <param name="clsName">The name of the CSS class.</param>
 public virtual void RemoveClass(string clsName)
 {
     if (HTMLProperties.ContainsKey("class"))
     {
         if (!String.IsNullOrEmpty(HTMLProperties["class"]))
         {
             string newClasses = "";
             foreach (string cls in HTMLProperties["class"].Split(' '))
             {
                 if (cls != clsName)
                 {
                     newClasses += (newClasses == "" ? cls : " " + cls);
                 }
             }
             this.Attr("class", newClasses);
         }
     }
     else
     {
         return;
     }
 }
Exemplo n.º 13
0
 public virtual HtmlTag MergeObjectProperties(Dictionary <string, object> htmlAttributes)
 {
     if (htmlAttributes == null)
     {
         return(this);
     }
     if (htmlAttributes.Count < 1)
     {
         return(this);
     }
     foreach (KeyValuePair <string, object> kvp in htmlAttributes)
     {
         if (HTMLProperties.ContainsKey(kvp.Key.ToLower()))
         {
             HTMLProperties[kvp.Key.ToLower()] = htmlAttributes[kvp.Key].ToString();
         }
         else
         {
             HTMLProperties.Add(kvp.Key.ToLower(), htmlAttributes[kvp.Key].ToString());
         }
     }
     return(this);
 }