2 // ChtmlTextWriter.cs: Compact HTML
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.
35 using System.Collections;
37 namespace System.Web.UI {
39 public class ChtmlTextWriter : Html32TextWriter
41 static Hashtable global_suppressed_attrs;
43 static string [] global_suppressed_attributes = {
44 "onclick", "ondblclick", "onmousedown", "onmouseup",
45 "onmouseover", "onmousemove", "onmouseout",
46 "onkeypress", "onkeydown", "onkeyup"
49 static string [] recognized_attributes = {"div", "span"};
51 Hashtable recognized_attrs = new Hashtable (recognized_attributes.Length);
52 Hashtable suppressed_attrs = new Hashtable (recognized_attributes.Length);
54 static ChtmlTextWriter ()
56 SetupGlobalSuppressedAttrs (global_suppressed_attributes);
59 static void SetupGlobalSuppressedAttrs (string [] attrs)
61 global_suppressed_attrs = new Hashtable ();
62 PopulateHash (global_suppressed_attrs, global_suppressed_attributes);
65 static void PopulateHash (Hashtable hash, string [] keys)
67 foreach (string key in keys)
71 public ChtmlTextWriter (TextWriter writer)
72 : this (writer, DefaultTabString)
76 public ChtmlTextWriter (TextWriter writer, string tabString)
77 : base (writer, tabString)
80 // setup the recognized attrs
82 foreach (string key in recognized_attributes)
83 recognized_attrs.Add (key, new Hashtable ());
85 SetupSuppressedAttrs ();
89 void SetupSuppressedAttrs ()
92 // we don't make these static because they are not read-only
94 string [] div_suppressed_attributes = {
95 "accesskey", "cellspacing", "cellpadding",
99 string [] span_suppressed_attributes = {
100 "cellspacing", "cellpadding",
104 Init ("div", div_suppressed_attributes, suppressed_attrs);
105 Init ("span", span_suppressed_attributes, suppressed_attrs);
108 static void Init (string key, string [] attrs, Hashtable container)
110 Hashtable attrs_hash = new Hashtable (attrs.Length);
111 PopulateHash (attrs_hash, attrs);
112 container.Add (key, attrs_hash);
115 protected Hashtable GlobalSuppressedAttributes {
116 get { return global_suppressed_attrs; }
119 protected Hashtable RecognizedAttributes {
120 get { return recognized_attrs; }
123 protected Hashtable SuppressedAttributes {
124 get { return suppressed_attrs; }
127 public virtual void AddRecognizedAttribute (string elementName, string attributeName)
129 Hashtable elem_attrs = (Hashtable) recognized_attrs [elementName];
131 if (elem_attrs == null) {
132 elem_attrs = new Hashtable ();
133 elem_attrs.Add (attributeName, true);
134 recognized_attrs.Add (elementName, elem_attrs);
136 elem_attrs.Add (attributeName, true);
139 public virtual void RemoveRecognizedAttribute (string elementName, string attributeName)
141 Hashtable elem_attrs = (Hashtable) recognized_attrs [elementName];
143 if (elem_attrs != null)
144 elem_attrs.Remove (attributeName);
150 public override void WriteBreak ()
152 string br = GetTagName (HtmlTextWriterTag.Br);
154 Write (TagRightChar);
157 public override void WriteEncodedText (string text)
159 base.WriteEncodedText (text);
162 Hashtable attr_render = new Hashtable ();
164 protected override bool OnAttributeRender (string name, string value, HtmlTextWriterAttribute key)
167 // I checked every possible HtmlTextWriterAttribute key
168 // and always throws ArgumentNullException.
169 return (bool) attr_render [null];
172 protected override bool OnStyleAttributeRender (string styleAttrName, string value, HtmlTextWriterStyle key)
174 return key == HtmlTextWriterStyle.Display;
177 protected override bool OnTagRender (string name, HtmlTextWriterTag tag)
179 return tag != HtmlTextWriterTag.Span;