5 // Cesar Lopez Nataren <cnataren@novell.com>
9 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
36 namespace System.Web.UI {
38 public class XhtmlTextWriter : HtmlTextWriter
40 Hashtable common_attrs = new Hashtable (DefaultCommonAttributes.Length);
41 Hashtable suppress_common_attrs = new Hashtable (DefaultSuppressCommonAttributes.Length);
42 Hashtable element_specific_attrs = new Hashtable ();
44 Hashtable attr_render = new Hashtable ();
46 //XhtmlMobileDocType doc_type;
48 static string [] DefaultCommonAttributes = {
56 // XHTML elements whose CommonAttributes are supressed
58 static string [] DefaultSuppressCommonAttributes = {
68 public XhtmlTextWriter (TextWriter writer)
69 : this (writer, DefaultTabString)
73 public XhtmlTextWriter (TextWriter writer, string tabString)
74 : base (writer, tabString)
76 SetupCommonAttributes ();
77 SetupSuppressCommonAttributes ();
78 SetupElementsSpecificAttributes ();
81 void SetupHash (Hashtable hash, string [] values) {
82 foreach (string str in values)
87 // if you need to add a new default common attribute,
88 // add the literal as a member of the DefaultCommonAttributes array
90 void SetupCommonAttributes ()
92 SetupHash (common_attrs, DefaultCommonAttributes);
96 // if you need to add a new suppressed common attribute,
97 // add the literal as a member of the SuppressCommonAttrs array
99 void SetupSuppressCommonAttributes ()
101 SetupHash (suppress_common_attrs, DefaultSuppressCommonAttributes);
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.
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;
115 void SetupElementsSpecificAttributes ()
117 string [] a_attrs_names = {"accesskey", "href", "charset", "hreflang", "rel", "type", "rev", "title", "tabindex"};
118 SetupElementSpecificAttributes ("a", a_attrs, a_attrs_names);
120 string [] base_attrs_names = {"href"};
121 SetupElementSpecificAttributes ("base", base_attrs, base_attrs_names);
123 string [] blockquote_attrs_names = {"cite"};
124 SetupElementSpecificAttributes ("blockquote", blockquote_attrs, blockquote_attrs_names);
126 string [] br_attrs_names = {"id", "class", "title"};
127 SetupElementSpecificAttributes ("br", br_attrs, br_attrs_names);
129 string [] form_attrs_names = {"action", "method", "enctype"};
130 SetupElementSpecificAttributes ("form", form_attrs, form_attrs_names);
132 string [] head_attrs_names = {"xml:lang"};
133 SetupElementSpecificAttributes ("head", head_attrs, head_attrs_names);
135 string [] html_attrs_names = {"version", "xml:lang", "xmlns"};
136 SetupElementSpecificAttributes ("html", html_attrs, html_attrs_names);
138 string [] img_attrs_names = {"src", "alt", "width", "longdesc", "height"};
139 SetupElementSpecificAttributes ("img", img_attrs, img_attrs_names);
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);
145 string [] label_attrs_names = {"accesskey", "for"};
146 SetupElementSpecificAttributes ("label", label_attrs, label_attrs_names);
148 string [] li_attrs_names = {"value"};
149 SetupElementSpecificAttributes ("li", li_attrs, li_attrs_names);
151 string [] link_attrs_names = {"hreflang", "rev", "type", "charset", "rel", "href", "media"};
152 SetupElementSpecificAttributes ("link", link_attrs, link_attrs_names);
154 string [] meta_attrs_names = {"content", "name", "xml:lang", "http-equiv", "scheme"};
155 SetupElementSpecificAttributes ("meta", meta_attrs, meta_attrs_names);
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);
161 string [] ol_attrs_names = {"start"};
162 SetupElementSpecificAttributes ("ol", ol_attrs, ol_attrs_names);
164 string [] optgroup_attrs_names = {"label", "disabled"};
165 SetupElementSpecificAttributes ("optgroup", optgroup_attrs, optgroup_attrs_names);
167 string [] option_attrs_names = {"selected", "value"};
168 SetupElementSpecificAttributes ("option", option_attrs, option_attrs_names);
170 string [] param_attrs_names = {"id", "name", "valuetype", "value", "type"};
171 SetupElementSpecificAttributes ("param", param_attrs, param_attrs_names);
173 string [] pre_attrs_names = {"xml:space"};
174 SetupElementSpecificAttributes ("pre", pre_attrs, pre_attrs_names);
176 string [] q_attrs_names = {"cite"};
177 SetupElementSpecificAttributes ("q", q_attrs, q_attrs_names);
179 string [] select_attrs_names = {"name", "tabindex", "disabled", "multiple", "size"};
180 SetupElementSpecificAttributes ("select", select_attrs, select_attrs_names);
182 string [] style_attrs_names = {"xml:lang", "xml:space", "type", "title", "media"};
183 SetupElementSpecificAttributes ("style", style_attrs, style_attrs_names);
185 string [] table_attrs_names = {"width", "summary"};
186 SetupElementSpecificAttributes ("table", table_attrs, table_attrs_names);
188 string [] textarea_attrs_names = {"name", "cols", "accesskey", "tabindex", "rows"};
189 SetupElementSpecificAttributes ("textarea", textarea_attrs, textarea_attrs_names);
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);
196 string [] title_attrs_names = {"xml:lang"};
197 SetupElementSpecificAttributes ("title", title_attrs, title_attrs_names);
199 string [] tr_attrs_names = {"align", "valign"};
200 SetupElementSpecificAttributes ("tr", tr_attrs, tr_attrs_names);
203 void SetupElementSpecificAttributes (string elementName, Hashtable attrs, string [] attributesNames)
205 attrs = new Hashtable (attributesNames.Length);
206 InitElementAttributes (attrs, attributesNames);
207 element_specific_attrs.Add (elementName, attrs);
210 void InitElementAttributes (Hashtable attrs, string [] attributesNames)
212 SetupHash (attrs, attributesNames);
215 protected Hashtable CommonAttributes {
216 get { return common_attrs; }
219 protected Hashtable ElementSpecificAttributes {
220 get { return element_specific_attrs; }
223 protected Hashtable SuppressCommonAttributes {
224 get { return suppress_common_attrs; }
227 public virtual void AddRecognizedAttribute (string elementName, string attributeName)
229 Hashtable elem_attrs = (Hashtable) element_specific_attrs [elementName];
231 if (elem_attrs == null) {
232 Hashtable attrs = new Hashtable ();
233 attrs.Add (attributeName, true);
234 element_specific_attrs.Add (elementName, attrs);
236 elem_attrs.Add (attributeName, true);
239 public override bool IsValidFormAttribute (string attributeName)
241 return attributeName == "action" || attributeName == "method" || attributeName == "enctype";
244 public virtual void RemoveRecognizedAttribute (string elementName, string attributeName)
246 Hashtable elem_attrs = (Hashtable) element_specific_attrs [elementName];
248 if (elem_attrs != null)
249 elem_attrs.Remove (attributeName);
252 public virtual void SetDocType (XhtmlMobileDocType docType)
254 //doc_type = docType;
258 public override void WriteBreak ()
260 string tag = GetTagName (HtmlTextWriterTag.Br);
263 Write (TagRightChar);
266 protected override bool OnAttributeRender (string name, string value, HtmlTextWriterAttribute key)
268 // I tested every possible value of HtmlTextWriterAttribute
269 // and the MS implementation always throws ArgumentNullException
270 return (bool) attr_render [null];
273 protected override bool OnStyleAttributeRender (string name, string value, HtmlTextWriterStyle key)
275 // I tested every possible value of HtmlTextWriterStyle
276 // and the MS implementation always returned false. Sigh