5 // Cesar Lopez Nataren <cnataren@novell.com>
9 // Copyright (C) 2006-2010 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.
32 using System.Collections;
34 namespace System.Web.UI
36 public class XhtmlTextWriter : HtmlTextWriter
38 static Hashtable default_common_attrs;
39 static Hashtable default_suppress_common_attrs;
40 static Hashtable default_element_specific_attrs;
42 Hashtable common_attrs;
43 Hashtable suppress_common_attrs;
44 Hashtable element_specific_attrs;
46 static XhtmlTextWriter ()
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 ();
55 static void SetupHash (Hashtable hash, string [] values)
57 foreach (string str in values)
61 static string [] DefaultCommonAttributes = {
69 // XHTML elements whose CommonAttributes are supressed
71 static string [] DefaultSuppressCommonAttributes = {
81 static void SetupElementsSpecificAttributes ()
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);
87 string [] base_attrs_names = {"href"};
88 SetupElementSpecificAttributes ("base", base_attrs_names);
90 string [] blockquote_attrs_names = {"cite"};
91 SetupElementSpecificAttributes ("blockquote", blockquote_attrs_names);
93 string [] br_attrs_names = {"id", "class", "title"};
94 SetupElementSpecificAttributes ("br", br_attrs_names);
96 string [] form_attrs_names = {"action", "method", "enctype"};
97 SetupElementSpecificAttributes ("form", form_attrs_names);
99 string [] head_attrs_names = {"xml:lang"};
100 SetupElementSpecificAttributes ("head", head_attrs_names);
102 string [] html_attrs_names = {"version", "xml:lang", "xmlns"};
103 SetupElementSpecificAttributes ("html", html_attrs_names);
105 string [] img_attrs_names = {"src", "alt", "width", "longdesc", "height"};
106 SetupElementSpecificAttributes ("img", img_attrs_names);
108 string [] input_attrs_names = {"size", "accesskey", "title", "name", "type", "disabled",
109 "value", "src", "checked", "maxlength", "tabindex"};
110 SetupElementSpecificAttributes ("input", input_attrs_names);
112 string [] label_attrs_names = {"accesskey", "for"};
113 SetupElementSpecificAttributes ("label", label_attrs_names);
115 string [] li_attrs_names = {"value"};
116 SetupElementSpecificAttributes ("li", li_attrs_names);
118 string [] link_attrs_names = {"hreflang", "rev", "type", "charset", "rel", "href", "media"};
119 SetupElementSpecificAttributes ("link", link_attrs_names);
121 string [] meta_attrs_names = {"content", "name", "xml:lang", "http-equiv", "scheme"};
122 SetupElementSpecificAttributes ("meta", meta_attrs_names);
124 string [] object_attrs_names = {"codebase", "classid", "data", "standby", "name", "type",
125 "height", "archive", "declare", "width", "tabindex", "codetype"};
126 SetupElementSpecificAttributes ("object", object_attrs_names);
128 string [] ol_attrs_names = {"start"};
129 SetupElementSpecificAttributes ("ol", ol_attrs_names);
131 string [] optgroup_attrs_names = {"label", "disabled"};
132 SetupElementSpecificAttributes ("optgroup", optgroup_attrs_names);
134 string [] option_attrs_names = {"selected", "value"};
135 SetupElementSpecificAttributes ("option", option_attrs_names);
137 string [] param_attrs_names = {"id", "name", "valuetype", "value", "type"};
138 SetupElementSpecificAttributes ("param", param_attrs_names);
140 string [] pre_attrs_names = {"xml:space"};
141 SetupElementSpecificAttributes ("pre", pre_attrs_names);
143 string [] q_attrs_names = {"cite"};
144 SetupElementSpecificAttributes ("q", q_attrs_names);
146 string [] select_attrs_names = {"name", "tabindex", "disabled", "multiple", "size"};
147 SetupElementSpecificAttributes ("select", select_attrs_names);
149 string [] style_attrs_names = {"xml:lang", "xml:space", "type", "title", "media"};
150 SetupElementSpecificAttributes ("style", style_attrs_names);
152 string [] table_attrs_names = {"width", "summary"};
153 SetupElementSpecificAttributes ("table", table_attrs_names);
155 string [] textarea_attrs_names = {"name", "cols", "accesskey", "tabindex", "rows"};
156 SetupElementSpecificAttributes ("textarea", textarea_attrs_names);
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);
163 string [] title_attrs_names = {"xml:lang"};
164 SetupElementSpecificAttributes ("title", title_attrs_names);
166 string [] tr_attrs_names = {"align", "valign"};
167 SetupElementSpecificAttributes ("tr", tr_attrs_names);
170 static void SetupElementSpecificAttributes (string elementName, string [] attributesNames)
172 Hashtable attrs = new Hashtable (attributesNames.Length);
173 SetupHash (attrs, attributesNames);
174 default_element_specific_attrs.Add (elementName, attrs);
177 public XhtmlTextWriter (TextWriter writer)
178 : this (writer, DefaultTabString)
182 public XhtmlTextWriter (TextWriter writer, string tabString)
183 : base (writer, tabString)
187 protected Hashtable CommonAttributes {
189 if (common_attrs == null)
190 common_attrs = (Hashtable) default_common_attrs.Clone ();
195 protected Hashtable ElementSpecificAttributes {
197 if (element_specific_attrs == null)
198 element_specific_attrs = (Hashtable) default_element_specific_attrs.Clone ();
199 return element_specific_attrs;
203 protected Hashtable SuppressCommonAttributes {
205 if (suppress_common_attrs == null)
206 suppress_common_attrs = (Hashtable) default_suppress_common_attrs.Clone ();
207 return suppress_common_attrs;
211 public virtual void AddRecognizedAttribute (string elementName, string attributeName)
213 Hashtable elem_attrs = (Hashtable) ElementSpecificAttributes [elementName];
215 if (elem_attrs == null) {
216 Hashtable attrs = new Hashtable ();
217 attrs.Add (attributeName, true);
218 ElementSpecificAttributes.Add (elementName, attrs);
220 elem_attrs.Add (attributeName, true);
223 public override bool IsValidFormAttribute (string attributeName)
225 return attributeName == "action" || attributeName == "method" || attributeName == "enctype";
228 public virtual void RemoveRecognizedAttribute (string elementName, string attributeName)
230 Hashtable elem_attrs = (Hashtable) ElementSpecificAttributes [elementName];
232 if (elem_attrs != null)
233 elem_attrs.Remove (attributeName);
236 public virtual void SetDocType (XhtmlMobileDocType docType)
238 //doc_type = docType;
242 public override void WriteBreak ()
244 string tag = GetTagName (HtmlTextWriterTag.Br);
247 Write (TagRightChar);
250 protected override bool OnAttributeRender (string name, string value, HtmlTextWriterAttribute key)
252 // I tested every possible value of HtmlTextWriterAttribute
253 // and the MS implementation always throws ArgumentNullException
254 throw new ArgumentNullException ();
257 protected override bool OnStyleAttributeRender (string name, string value, HtmlTextWriterStyle key)
259 // I tested every possible value of HtmlTextWriterStyle
260 // and the MS implementation always returned false. Sigh