Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / XhtmlTextWriter.cs
1 //
2 // XhtmlTextWriter.cs
3 //
4 // Author:
5 //      Cesar Lopez Nataren <cnataren@novell.com>
6 //
7
8 //
9 // Copyright (C) 2006-2010 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.IO;
32 using System.Collections;
33
34 namespace System.Web.UI 
35 {
36         public class XhtmlTextWriter : HtmlTextWriter
37         {
38                 static Hashtable default_common_attrs;
39                 static Hashtable default_suppress_common_attrs;
40                 static Hashtable default_element_specific_attrs;
41
42                 Hashtable common_attrs;
43                 Hashtable suppress_common_attrs;
44                 Hashtable element_specific_attrs;
45
46                 static XhtmlTextWriter ()
47                 {
48                         default_common_attrs = new Hashtable (DefaultCommonAttributes.Length);
49                         SetupHash (default_common_attrs, DefaultCommonAttributes);
50                         default_suppress_common_attrs = new Hashtable (DefaultSuppressCommonAttributes.Length);
51                         SetupHash (default_suppress_common_attrs, DefaultSuppressCommonAttributes);
52                         SetupElementsSpecificAttributes ();
53                 }
54
55                 static void SetupHash (Hashtable hash, string [] values)
56                 {
57                         foreach (string str in values)
58                                 hash.Add (str, true);
59                 }
60
61                 static string [] DefaultCommonAttributes = {
62                         "class",
63                         "id",
64                         "title",
65                         "xml:lang"
66                 };
67
68                 //
69                 // XHTML elements whose CommonAttributes are supressed
70                 //
71                 static string [] DefaultSuppressCommonAttributes = {
72                         "base",
73                         "meta",
74                         "br",
75                         "head",
76                         "title",
77                         "html",
78                         "style"
79                 };
80
81                 static void SetupElementsSpecificAttributes ()
82                 {
83                         default_element_specific_attrs = new Hashtable ();
84                         string [] a_attrs_names = {"accesskey", "href", "charset", "hreflang", "rel", "type", "rev", "title", "tabindex"};
85                         SetupElementSpecificAttributes ("a", a_attrs_names);
86
87                         string [] base_attrs_names = {"href"};
88                         SetupElementSpecificAttributes ("base", base_attrs_names);
89
90                         string [] blockquote_attrs_names = {"cite"};
91                         SetupElementSpecificAttributes ("blockquote", blockquote_attrs_names);
92
93                         string [] br_attrs_names = {"id", "class", "title"};
94                         SetupElementSpecificAttributes ("br", br_attrs_names);
95
96                         string [] form_attrs_names = {"action", "method", "enctype"};
97                         SetupElementSpecificAttributes ("form", form_attrs_names);
98
99                         string [] head_attrs_names = {"xml:lang"};
100                         SetupElementSpecificAttributes ("head", head_attrs_names);
101
102                         string [] html_attrs_names = {"version", "xml:lang", "xmlns"};
103                         SetupElementSpecificAttributes ("html", html_attrs_names);
104
105                         string [] img_attrs_names = {"src", "alt", "width", "longdesc", "height"};
106                         SetupElementSpecificAttributes ("img", img_attrs_names);
107
108                         string [] input_attrs_names = {"size", "accesskey", "title", "name", "type", "disabled", 
109                                                        "value", "src", "checked", "maxlength", "tabindex"};
110                         SetupElementSpecificAttributes ("input", input_attrs_names);
111
112                         string [] label_attrs_names = {"accesskey", "for"};
113                         SetupElementSpecificAttributes ("label", label_attrs_names);
114
115                         string [] li_attrs_names = {"value"};
116                         SetupElementSpecificAttributes ("li", li_attrs_names);
117
118                         string [] link_attrs_names = {"hreflang", "rev", "type", "charset", "rel", "href", "media"};
119                         SetupElementSpecificAttributes ("link", link_attrs_names);
120
121                         string [] meta_attrs_names = {"content", "name", "xml:lang", "http-equiv", "scheme"};
122                         SetupElementSpecificAttributes ("meta", meta_attrs_names);
123
124                         string [] object_attrs_names = {"codebase", "classid", "data", "standby", "name", "type", 
125                                                         "height", "archive", "declare", "width", "tabindex", "codetype"};
126                         SetupElementSpecificAttributes ("object", object_attrs_names);
127
128                         string [] ol_attrs_names = {"start"};
129                         SetupElementSpecificAttributes ("ol", ol_attrs_names);
130
131                         string [] optgroup_attrs_names = {"label", "disabled"};
132                         SetupElementSpecificAttributes ("optgroup", optgroup_attrs_names);
133
134                         string [] option_attrs_names = {"selected", "value"};
135                         SetupElementSpecificAttributes ("option", option_attrs_names);
136
137                         string [] param_attrs_names = {"id", "name", "valuetype", "value", "type"};
138                         SetupElementSpecificAttributes ("param", param_attrs_names);
139
140                         string [] pre_attrs_names = {"xml:space"};
141                         SetupElementSpecificAttributes ("pre", pre_attrs_names);
142
143                         string [] q_attrs_names = {"cite"};
144                         SetupElementSpecificAttributes ("q", q_attrs_names);
145
146                         string [] select_attrs_names = {"name", "tabindex", "disabled", "multiple", "size"};
147                         SetupElementSpecificAttributes ("select", select_attrs_names);
148
149                         string [] style_attrs_names = {"xml:lang", "xml:space", "type", "title", "media"};
150                         SetupElementSpecificAttributes ("style", style_attrs_names);
151
152                         string [] table_attrs_names = {"width", "summary"};
153                         SetupElementSpecificAttributes ("table", table_attrs_names);
154
155                         string [] textarea_attrs_names = {"name", "cols", "accesskey", "tabindex", "rows"};
156                         SetupElementSpecificAttributes ("textarea", textarea_attrs_names);
157
158                         string [] td_and_th_attrs_names = {"headers", "align", "rowspan", "colspan", "axis",
159                                                            "scope", "abbr", "valign"};
160                         SetupElementSpecificAttributes ("td", td_and_th_attrs_names);
161                         SetupElementSpecificAttributes ("th", td_and_th_attrs_names);
162
163                         string [] title_attrs_names = {"xml:lang"};
164                         SetupElementSpecificAttributes ("title", title_attrs_names);
165
166                         string [] tr_attrs_names = {"align", "valign"};
167                         SetupElementSpecificAttributes ("tr", tr_attrs_names);
168                 }
169                 
170                 static void SetupElementSpecificAttributes (string elementName, string [] attributesNames)
171                 {
172                         Hashtable attrs = new Hashtable (attributesNames.Length);
173                         SetupHash (attrs, attributesNames);
174                         default_element_specific_attrs.Add (elementName, attrs);
175                 }
176
177                 public XhtmlTextWriter (TextWriter writer)
178                         : this (writer, DefaultTabString)
179                 {
180                 }
181
182                 public XhtmlTextWriter (TextWriter writer, string tabString)
183                         : base (writer, tabString)
184                 {
185                 }
186
187                 protected Hashtable CommonAttributes {
188                         get {
189                                 if (common_attrs == null)
190                                         common_attrs = (Hashtable) default_common_attrs.Clone ();
191                                 return common_attrs;
192                         }
193                 }
194
195                 protected Hashtable ElementSpecificAttributes {
196                         get {
197                                 if (element_specific_attrs == null)
198                                         element_specific_attrs = (Hashtable) default_element_specific_attrs.Clone ();
199                                 return element_specific_attrs;
200                         }
201                 }
202
203                 protected Hashtable  SuppressCommonAttributes {
204                         get {
205                                 if (suppress_common_attrs == null)
206                                         suppress_common_attrs = (Hashtable) default_suppress_common_attrs.Clone ();
207                                 return suppress_common_attrs;
208                         }
209                 }
210
211                 public virtual void AddRecognizedAttribute (string elementName, string attributeName)
212                 {
213                         Hashtable elem_attrs = (Hashtable) ElementSpecificAttributes [elementName];
214
215                         if (elem_attrs == null) {
216                                 Hashtable attrs = new Hashtable ();
217                                 attrs.Add (attributeName, true);
218                                 ElementSpecificAttributes.Add (elementName, attrs);
219                         } else
220                                 elem_attrs.Add (attributeName, true);
221                 }
222
223                 public override bool IsValidFormAttribute (string attributeName)
224                 {
225                         return attributeName == "action" || attributeName == "method" || attributeName == "enctype";
226                 }
227
228                 public virtual void RemoveRecognizedAttribute (string elementName, string attributeName)
229                 {
230                         Hashtable elem_attrs = (Hashtable) ElementSpecificAttributes [elementName];
231
232                         if (elem_attrs != null)
233                                 elem_attrs.Remove (attributeName);
234                 }
235
236                 public virtual void SetDocType (XhtmlMobileDocType docType)
237                 {
238                         //doc_type = docType;
239                 }
240
241                 // writes <br/>
242                 public override void WriteBreak ()
243                 {
244                         string tag = GetTagName (HtmlTextWriterTag.Br);
245                         WriteBeginTag (tag);
246                         Write (SlashChar);
247                         Write (TagRightChar);
248                 }
249
250                 protected override bool OnAttributeRender (string name, string value, HtmlTextWriterAttribute key)
251                 {
252                         // I tested every possible value of HtmlTextWriterAttribute
253                         // and the MS implementation always throws ArgumentNullException
254                         throw new ArgumentNullException ();
255                 }
256
257                 protected override bool OnStyleAttributeRender (string name, string value, HtmlTextWriterStyle key)
258                 {
259                         // I tested every possible value of HtmlTextWriterStyle
260                         // and the MS implementation always returned false. Sigh
261                         return false;
262                 }
263         }
264 }