2 // ChtmlTextWriter.cs: Compact HTML
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.
33 using System.Collections;
35 namespace System.Web.UI {
37 public class ChtmlTextWriter : Html32TextWriter
39 static Hashtable global_suppressed_attrs;
41 static string [] global_suppressed_attributes = {
42 "onclick", "ondblclick", "onmousedown", "onmouseup",
43 "onmouseover", "onmousemove", "onmouseout",
44 "onkeypress", "onkeydown", "onkeyup"
47 static string [] recognized_attributes = {"div", "span"};
49 Hashtable recognized_attrs = new Hashtable (recognized_attributes.Length);
50 Hashtable suppressed_attrs = new Hashtable (recognized_attributes.Length);
52 static ChtmlTextWriter ()
54 SetupGlobalSuppressedAttrs (global_suppressed_attributes);
57 static void SetupGlobalSuppressedAttrs (string [] attrs)
59 global_suppressed_attrs = new Hashtable ();
60 PopulateHash (global_suppressed_attrs, global_suppressed_attributes);
63 static void PopulateHash (Hashtable hash, string [] keys)
65 foreach (string key in keys)
69 public ChtmlTextWriter (TextWriter writer)
70 : this (writer, DefaultTabString)
74 public ChtmlTextWriter (TextWriter writer, string tabString)
75 : base (writer, tabString)
78 // setup the recognized attrs
80 foreach (string key in recognized_attributes)
81 recognized_attrs.Add (key, new Hashtable ());
83 SetupSuppressedAttrs ();
87 void SetupSuppressedAttrs ()
90 // we don't make these static because they are not read-only
92 string [] div_suppressed_attributes = {
93 "accesskey", "cellspacing", "cellpadding",
97 string [] span_suppressed_attributes = {
98 "cellspacing", "cellpadding",
102 Init ("div", div_suppressed_attributes, suppressed_attrs);
103 Init ("span", span_suppressed_attributes, suppressed_attrs);
106 static void Init (string key, string [] attrs, Hashtable container)
108 Hashtable attrs_hash = new Hashtable (attrs.Length);
109 PopulateHash (attrs_hash, attrs);
110 container.Add (key, attrs_hash);
113 protected Hashtable GlobalSuppressedAttributes {
114 get { return global_suppressed_attrs; }
117 protected Hashtable RecognizedAttributes {
118 get { return recognized_attrs; }
121 protected Hashtable SuppressedAttributes {
122 get { return suppressed_attrs; }
125 public virtual void AddRecognizedAttribute (string elementName, string attributeName)
127 Hashtable elem_attrs = (Hashtable) recognized_attrs [elementName];
129 if (elem_attrs == null) {
130 elem_attrs = new Hashtable ();
131 elem_attrs.Add (attributeName, true);
132 recognized_attrs.Add (elementName, elem_attrs);
134 elem_attrs.Add (attributeName, true);
137 public virtual void RemoveRecognizedAttribute (string elementName, string attributeName)
139 Hashtable elem_attrs = (Hashtable) recognized_attrs [elementName];
141 if (elem_attrs != null)
142 elem_attrs.Remove (attributeName);
148 public override void WriteBreak ()
150 string br = GetTagName (HtmlTextWriterTag.Br);
152 Write (TagRightChar);
155 public override void WriteEncodedText (string text)
157 base.WriteEncodedText (text);
160 Hashtable attr_render = new Hashtable ();
162 protected override bool OnAttributeRender (string name, string value, HtmlTextWriterAttribute key)
165 // I checked every possible HtmlTextWriterAttribute key
166 // and always throws ArgumentNullException.
167 return (bool) attr_render [null];
170 protected override bool OnStyleAttributeRender (string styleAttrName, string value, HtmlTextWriterStyle key)
172 return key == HtmlTextWriterStyle.Display;
175 protected override bool OnTagRender (string name, HtmlTextWriterTag tag)
177 return tag != HtmlTextWriterTag.Span;