copied mono-api-diff.cs from mono-2-2 branch so new patch can be applied and history...
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / TagBuilder.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\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
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Diagnostics.CodeAnalysis;\r
17     using System.Globalization;\r
18     using System.Text;\r
19     using System.Web;\r
20     using System.Web.Mvc.Resources;\r
21 \r
22     public class TagBuilder {\r
23         private string _idAttributeDotReplacement;\r
24 \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
30 \r
31         private string _innerHtml;\r
32 \r
33         public TagBuilder(string tagName) {\r
34             if (String.IsNullOrEmpty(tagName)) {\r
35                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "tagName");\r
36             }\r
37 \r
38             TagName = tagName;\r
39             Attributes = new SortedDictionary<string, string>(StringComparer.Ordinal);\r
40         }\r
41 \r
42         public IDictionary<string, string> Attributes {\r
43             get;\r
44             private set;\r
45         }\r
46 \r
47         public string IdAttributeDotReplacement {\r
48             get {\r
49                 if (String.IsNullOrEmpty(_idAttributeDotReplacement)) {\r
50                     _idAttributeDotReplacement = HtmlHelper.IdAttributeDotReplacement;\r
51                 }\r
52                 return _idAttributeDotReplacement;\r
53             }\r
54             set {\r
55                 _idAttributeDotReplacement = value;\r
56             }\r
57         }\r
58 \r
59         public string InnerHtml {\r
60             get {\r
61                 return _innerHtml ?? String.Empty;\r
62             }\r
63             set {\r
64                 _innerHtml = value;\r
65             }\r
66         }\r
67 \r
68         public string TagName {\r
69             get;\r
70             private set;\r
71         }\r
72 \r
73         public void AddCssClass(string value) {\r
74             string currentValue;\r
75 \r
76             if (Attributes.TryGetValue("class", out currentValue)) {\r
77                 Attributes["class"] = value + " " + currentValue;\r
78             }\r
79             else {\r
80                 Attributes["class"] = value;\r
81             }\r
82         }\r
83 \r
84         public void GenerateId(string name) {\r
85             if (!String.IsNullOrEmpty(name)) {\r
86                 MergeAttribute("id", name.Replace(".", IdAttributeDotReplacement));\r
87             }\r
88         }\r
89 \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
96             }\r
97             return sb.ToString();\r
98         }\r
99 \r
100         public void MergeAttribute(string key, string value) {\r
101             MergeAttribute(key, value, false /* replaceExisting */);\r
102         }\r
103 \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
107             }\r
108 \r
109             if (replaceExisting || !Attributes.ContainsKey(key)) {\r
110                 Attributes[key] = value;\r
111             }\r
112         }\r
113 \r
114         public void MergeAttributes<TKey, TValue>(IDictionary<TKey, TValue> attributes) {\r
115             MergeAttributes(attributes, false /* replaceExisting */);\r
116         }\r
117 \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
124                 }\r
125             }\r
126         }\r
127 \r
128         public void SetInnerText(string innerText) {\r
129             InnerHtml = HttpUtility.HtmlEncode(innerText);\r
130         }\r
131 \r
132         [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]\r
133         public override string ToString() {\r
134             return ToString(TagRenderMode.Normal);\r
135         }\r
136 \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
145                 default:\r
146                     return String.Format(CultureInfo.InvariantCulture, _elementFormatNormal, TagName, GetAttributesString(), InnerHtml);\r
147             }\r
148         }\r
149     }\r
150 }\r