1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections.Generic;
\r
16 using System.Diagnostics.CodeAnalysis;
\r
17 using System.Globalization;
\r
20 using System.Web.Mvc.Resources;
\r
22 public class TagBuilder {
\r
23 private string _idAttributeDotReplacement;
\r
25 private const string _attributeFormat = @" {0}=""{1}""";
\r
26 private const string _elementFormatEndTag = "</{0}>";
\r
27 private const string _elementFormatNormal = "<{0}{1}>{2}</{0}>";
\r
28 private const string _elementFormatSelfClosing = "<{0}{1} />";
\r
29 private const string _elementFormatStartTag = "<{0}{1}>";
\r
31 private string _innerHtml;
\r
33 public TagBuilder(string tagName) {
\r
34 if (String.IsNullOrEmpty(tagName)) {
\r
35 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "tagName");
\r
39 Attributes = new SortedDictionary<string, string>(StringComparer.Ordinal);
\r
42 public IDictionary<string, string> Attributes {
\r
47 public string IdAttributeDotReplacement {
\r
49 if (String.IsNullOrEmpty(_idAttributeDotReplacement)) {
\r
50 _idAttributeDotReplacement = HtmlHelper.IdAttributeDotReplacement;
\r
52 return _idAttributeDotReplacement;
\r
55 _idAttributeDotReplacement = value;
\r
59 public string InnerHtml {
\r
61 return _innerHtml ?? String.Empty;
\r
68 public string TagName {
\r
73 public void AddCssClass(string value) {
\r
74 string currentValue;
\r
76 if (Attributes.TryGetValue("class", out currentValue)) {
\r
77 Attributes["class"] = value + " " + currentValue;
\r
80 Attributes["class"] = value;
\r
84 public void GenerateId(string name) {
\r
85 if (!String.IsNullOrEmpty(name)) {
\r
86 MergeAttribute("id", name.Replace(".", IdAttributeDotReplacement));
\r
90 private string GetAttributesString() {
\r
91 StringBuilder sb = new StringBuilder();
\r
92 foreach (var attribute in Attributes) {
\r
93 string key = attribute.Key;
\r
94 string value = HttpUtility.HtmlAttributeEncode(attribute.Value);
\r
95 sb.AppendFormat(CultureInfo.InvariantCulture, _attributeFormat, key, value);
\r
97 return sb.ToString();
\r
100 public void MergeAttribute(string key, string value) {
\r
101 MergeAttribute(key, value, false /* replaceExisting */);
\r
104 public void MergeAttribute(string key, string value, bool replaceExisting) {
\r
105 if (String.IsNullOrEmpty(key)) {
\r
106 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "key");
\r
109 if (replaceExisting || !Attributes.ContainsKey(key)) {
\r
110 Attributes[key] = value;
\r
114 public void MergeAttributes<TKey, TValue>(IDictionary<TKey, TValue> attributes) {
\r
115 MergeAttributes(attributes, false /* replaceExisting */);
\r
118 public void MergeAttributes<TKey, TValue>(IDictionary<TKey, TValue> attributes, bool replaceExisting) {
\r
119 if (attributes != null) {
\r
120 foreach (var entry in attributes) {
\r
121 string key = Convert.ToString(entry.Key, CultureInfo.InvariantCulture);
\r
122 string value = Convert.ToString(entry.Value, CultureInfo.InvariantCulture);
\r
123 MergeAttribute(key, value, replaceExisting);
\r
128 public void SetInnerText(string innerText) {
\r
129 InnerHtml = HttpUtility.HtmlEncode(innerText);
\r
132 [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
\r
133 public override string ToString() {
\r
134 return ToString(TagRenderMode.Normal);
\r
137 public string ToString(TagRenderMode renderMode) {
\r
138 switch (renderMode) {
\r
139 case TagRenderMode.StartTag:
\r
140 return String.Format(CultureInfo.InvariantCulture, _elementFormatStartTag, TagName, GetAttributesString());
\r
141 case TagRenderMode.EndTag:
\r
142 return String.Format(CultureInfo.InvariantCulture, _elementFormatEndTag, TagName);
\r
143 case TagRenderMode.SelfClosing:
\r
144 return String.Format(CultureInfo.InvariantCulture, _elementFormatSelfClosing, TagName, GetAttributesString());
\r
146 return String.Format(CultureInfo.InvariantCulture, _elementFormatNormal, TagName, GetAttributesString(), InnerHtml);
\r