New test.
[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 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 #if NET_2_0
32
33 using System.IO;
34 using System.Collections;
35
36 namespace System.Web.UI {
37
38         public class XhtmlTextWriter : HtmlTextWriter
39         {
40                 Hashtable common_attrs = new Hashtable (DefaultCommonAttributes.Length);
41                 Hashtable suppress_common_attrs = new Hashtable (DefaultSuppressCommonAttributes.Length);
42                 Hashtable element_specific_attrs = new Hashtable ();
43
44                 Hashtable attr_render = new Hashtable ();
45
46                 XhtmlMobileDocType doc_type;
47
48                 static string [] DefaultCommonAttributes = {
49                         "class",
50                         "id",
51                         "title",
52                         "xml:lang"
53                 };
54
55                 //
56                 // XHTML elements whose CommonAttributes are supressed
57                 //
58                 static string [] DefaultSuppressCommonAttributes = {
59                         "base",
60                         "meta",
61                         "br",
62                         "head",
63                         "title",
64                         "html",
65                         "style"
66                 };
67
68                 public XhtmlTextWriter (TextWriter writer)
69                         : this (writer, DefaultTabString)
70                 {
71                 }
72
73                 public XhtmlTextWriter (TextWriter writer, string tabString)
74                         : base (writer, tabString)
75                 {
76                         SetupCommonAttributes ();
77                         SetupSuppressCommonAttributes ();
78                         SetupElementsSpecificAttributes ();
79                 }
80
81                 void SetupHash (Hashtable hash, string [] values) {
82                         foreach (string str in values)
83                                 hash.Add (str, true);
84                 }
85
86                 //
87                 // if you need to add a new default common attribute,
88                 // add the literal as a member of the DefaultCommonAttributes array
89                 //
90                 void SetupCommonAttributes ()
91                 {
92                         SetupHash (common_attrs, DefaultCommonAttributes);
93                 }
94
95                 //
96                 // if you need to add a new suppressed common attribute,
97                 // add the literal as a member of the SuppressCommonAttrs array
98                 //
99                 void SetupSuppressCommonAttributes ()
100                 {
101                         SetupHash (suppress_common_attrs, DefaultSuppressCommonAttributes);
102                 }
103
104                 //
105                 // I did not make them static because different instances of XhtmlTextWriter's
106                 // do not share the changes made to the element's attributes tables,
107                 // they are not read-only.
108                 //
109                 Hashtable a_attrs, base_attrs, blockquote_attrs, br_attrs, form_attrs, head_attrs;
110                 Hashtable html_attrs, img_attrs, input_attrs, label_attrs, li_attrs, link_attrs;
111                 Hashtable meta_attrs, object_attrs, ol_attrs, optgroup_attrs, option_attrs, param_attrs;
112                 Hashtable pre_attrs, q_attrs, select_attrs, style_attrs, table_attrs, textarea_attrs;
113                 Hashtable td_attrs, th_attrs, title_attrs, tr_attrs;
114
115                 void SetupElementsSpecificAttributes ()
116                 {
117                         string [] a_attrs_names = {"accesskey", "href", "charset", "hreflang", "rel", "type", "rev", "title", "tabindex"};
118                         SetupElementSpecificAttributes ("a", a_attrs, a_attrs_names);
119
120                         string [] base_attrs_names = {"href"};
121                         SetupElementSpecificAttributes ("base", base_attrs, base_attrs_names);
122
123                         string [] blockquote_attrs_names = {"cite"};
124                         SetupElementSpecificAttributes ("blockquote", blockquote_attrs, blockquote_attrs_names);
125
126                         string [] br_attrs_names = {"id", "class", "title"};
127                         SetupElementSpecificAttributes ("br", br_attrs, br_attrs_names);
128
129                         string [] form_attrs_names = {"action", "method", "enctype"};
130                         SetupElementSpecificAttributes ("form", form_attrs, form_attrs_names);
131
132                         string [] head_attrs_names = {"xml:lang"};
133                         SetupElementSpecificAttributes ("head", head_attrs, head_attrs_names);
134
135                         string [] html_attrs_names = {"version", "xml:lang", "xmlns"};
136                         SetupElementSpecificAttributes ("html", html_attrs, html_attrs_names);
137
138                         string [] img_attrs_names = {"src", "alt", "width", "longdesc", "height"};
139                         SetupElementSpecificAttributes ("img", img_attrs, img_attrs_names);
140
141                         string [] input_attrs_names = {"size", "accesskey", "title", "name", "type", "disabled", 
142                                                        "value", "src", "checked", "maxlength", "tabindex"};
143                         SetupElementSpecificAttributes ("input", input_attrs, input_attrs_names);
144
145                         string [] label_attrs_names = {"accesskey", "for"};
146                         SetupElementSpecificAttributes ("label", label_attrs, label_attrs_names);
147
148                         string [] li_attrs_names = {"value"};
149                         SetupElementSpecificAttributes ("li", li_attrs, li_attrs_names);
150
151                         string [] link_attrs_names = {"hreflang", "rev", "type", "charset", "rel", "href", "media"};
152                         SetupElementSpecificAttributes ("link", link_attrs, link_attrs_names);
153
154                         string [] meta_attrs_names = {"content", "name", "xml:lang", "http-equiv", "scheme"};
155                         SetupElementSpecificAttributes ("meta", meta_attrs, meta_attrs_names);
156
157                         string [] object_attrs_names = {"codebase", "classid", "data", "standby", "name", "type", 
158                                                         "height", "archive", "declare", "width", "tabindex", "codetype"};
159                         SetupElementSpecificAttributes ("object", object_attrs, object_attrs_names);
160
161                         string [] ol_attrs_names = {"start"};
162                         SetupElementSpecificAttributes ("ol", ol_attrs, ol_attrs_names);
163
164                         string [] optgroup_attrs_names = {"label", "disabled"};
165                         SetupElementSpecificAttributes ("optgroup", optgroup_attrs, optgroup_attrs_names);
166
167                         string [] option_attrs_names = {"selected", "value"};
168                         SetupElementSpecificAttributes ("option", option_attrs, option_attrs_names);
169
170                         string [] param_attrs_names = {"id", "name", "valuetype", "value", "type"};
171                         SetupElementSpecificAttributes ("param", param_attrs, param_attrs_names);
172
173                         string [] pre_attrs_names = {"xml:space"};
174                         SetupElementSpecificAttributes ("pre", pre_attrs, pre_attrs_names);
175
176                         string [] q_attrs_names = {"cite"};
177                         SetupElementSpecificAttributes ("q", q_attrs, q_attrs_names);
178
179                         string [] select_attrs_names = {"name", "tabindex", "disabled", "multiple", "size"};
180                         SetupElementSpecificAttributes ("select", select_attrs, select_attrs_names);
181
182                         string [] style_attrs_names = {"xml:lang", "xml:space", "type", "title", "media"};
183                         SetupElementSpecificAttributes ("style", style_attrs, style_attrs_names);
184
185                         string [] table_attrs_names = {"width", "summary"};
186                         SetupElementSpecificAttributes ("table", table_attrs, table_attrs_names);
187
188                         string [] textarea_attrs_names = {"name", "cols", "accesskey", "tabindex", "rows"};
189                         SetupElementSpecificAttributes ("textarea", textarea_attrs, textarea_attrs_names);
190
191                         string [] td_and_th_attrs_names = {"headers", "align", "rowspan", "colspan", "axis",
192                                                            "scope", "abbr", "valign"};
193                         SetupElementSpecificAttributes ("td", td_attrs, td_and_th_attrs_names);
194                         SetupElementSpecificAttributes ("th", th_attrs, td_and_th_attrs_names);
195
196                         string [] title_attrs_names = {"xml:lang"};
197                         SetupElementSpecificAttributes ("title", title_attrs, title_attrs_names);
198
199                         string [] tr_attrs_names = {"align", "valign"};
200                         SetupElementSpecificAttributes ("tr", tr_attrs, tr_attrs_names);
201                 }
202                 
203                 void SetupElementSpecificAttributes (string elementName, Hashtable attrs, string [] attributesNames)
204                 {
205                         attrs = new Hashtable (attributesNames.Length);
206                         InitElementAttributes (attrs, attributesNames);
207                         element_specific_attrs.Add (elementName, attrs);
208                 }
209
210                 void InitElementAttributes (Hashtable attrs, string [] attributesNames)
211                 {
212                         SetupHash (attrs, attributesNames);
213                 }
214
215                 protected Hashtable CommonAttributes {
216                         get { return common_attrs; }
217                 }
218
219                 protected Hashtable ElementSpecificAttributes {
220                         get { return element_specific_attrs; }
221                 }
222
223                 protected Hashtable  SuppressCommonAttributes {
224                         get { return suppress_common_attrs; }
225                 }
226
227                 public virtual void AddRecognizedAttribute (string elementName, string attributeName)
228                 {
229                         Hashtable elem_attrs = (Hashtable) element_specific_attrs [elementName];
230
231                         if (elem_attrs == null) {
232                                 Hashtable attrs = new Hashtable ();
233                                 attrs.Add (attributeName, true);
234                                 element_specific_attrs.Add (elementName, attrs);
235                         } else
236                                 elem_attrs.Add (attributeName, true);
237                 }
238
239                 public override bool IsValidFormAttribute (string attributeName)
240                 {
241                         return attributeName == "action" || attributeName == "method" || attributeName == "enctype";
242                 }
243
244                 public virtual void RemoveRecognizedAttribute (string elementName, string attributeName)
245                 {
246                         Hashtable elem_attrs = (Hashtable) element_specific_attrs [elementName];
247
248                         if (elem_attrs != null)
249                                 elem_attrs.Remove (attributeName);
250                 }
251
252                 public virtual void SetDocType (XhtmlMobileDocType docType)
253                 {
254                         doc_type = docType;
255                 }
256
257                 // writes <br/>
258                 public override void WriteBreak ()
259                 {
260                         string tag = GetTagName (HtmlTextWriterTag.Br);
261                         WriteBeginTag (tag);
262                         Write (SlashChar);
263                         Write (TagRightChar);
264                 }
265
266                 [MonoTODO]
267                 protected override bool OnAttributeRender (string name, string value, HtmlTextWriterAttribute key)
268                 {
269                         // I tested every possible value of HtmlTextWriterAttribute
270                         // and the MS implementation always throws ArgumentNullException
271                         return (bool) attr_render [null];
272                 }
273
274                 [MonoTODO]
275                 protected override bool OnStyleAttributeRender (string name, string value, HtmlTextWriterStyle key)
276                 {
277                         // I tested every possible value of HtmlTextWriterStyle
278                         // and the MS implementation always returned false. Sigh
279                         return false;
280                 }
281         }
282 }
283
284 #endif