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