[asp.net] HtmlForum.Name rendering updates + test updates
[mono.git] / mcs / class / System.Web / System.Web.UI / ChtmlTextWriter.cs
1 //
2 // ChtmlTextWriter.cs: Compact HTML
3 //
4 // Author:
5 //      Cesar Lopez Nataren <cnataren@novell.com>
6 //
7
8 //
9 // Copyright (C) 2006-2010 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 using System;
32 using System.IO;
33 using System.Collections;
34
35 namespace System.Web.UI {
36
37         public class ChtmlTextWriter : Html32TextWriter
38         {
39                 static Hashtable global_suppressed_attrs;
40
41                 static string [] global_suppressed_attributes = {
42                         "onclick", "ondblclick", "onmousedown", "onmouseup",
43                         "onmouseover", "onmousemove", "onmouseout",
44                         "onkeypress", "onkeydown", "onkeyup"
45                 };
46
47                 static string [] recognized_attributes = {"div", "span"};
48
49                 Hashtable recognized_attrs = new Hashtable (recognized_attributes.Length);
50                 Hashtable suppressed_attrs = new Hashtable (recognized_attributes.Length);
51
52                 static ChtmlTextWriter ()
53                 {
54                         SetupGlobalSuppressedAttrs (global_suppressed_attributes);
55                 }
56
57                 static void SetupGlobalSuppressedAttrs (string [] attrs)
58                 {
59                         global_suppressed_attrs = new Hashtable ();
60                         PopulateHash (global_suppressed_attrs, global_suppressed_attributes);
61                 }
62
63                 static void PopulateHash (Hashtable hash, string [] keys)
64                 {
65                         foreach (string key in keys)
66                                 hash.Add (key, true);
67                 }
68
69                 public ChtmlTextWriter (TextWriter writer)
70                         : this (writer, DefaultTabString)
71                 {
72                 }
73
74                 public ChtmlTextWriter (TextWriter writer, string tabString)
75                         : base (writer, tabString)
76                 {
77                         //
78                         // setup the recognized attrs
79                         //
80                         foreach (string key in recognized_attributes)
81                                 recognized_attrs.Add (key, new Hashtable ());
82
83                         SetupSuppressedAttrs ();
84                 }
85
86
87                 void SetupSuppressedAttrs ()
88                 {
89                         //
90                         // we don't make these static because they are not read-only
91                         //
92                         string [] div_suppressed_attributes = {
93                                 "accesskey", "cellspacing", "cellpadding",
94                                 "gridlines", "rules"
95                         };
96
97                         string [] span_suppressed_attributes = {
98                                 "cellspacing", "cellpadding",
99                                 "gridlines", "rules"
100                         };
101
102                         Init ("div", div_suppressed_attributes, suppressed_attrs);
103                         Init ("span", span_suppressed_attributes, suppressed_attrs);
104                 }
105
106                 static void Init (string key, string [] attrs, Hashtable container)
107                 {
108                         Hashtable attrs_hash = new Hashtable (attrs.Length);
109                         PopulateHash (attrs_hash, attrs);
110                         container.Add (key, attrs_hash);
111                 }
112
113                 protected Hashtable GlobalSuppressedAttributes {
114                         get { return global_suppressed_attrs; }
115                 }
116
117                 protected Hashtable RecognizedAttributes {
118                         get { return recognized_attrs; }
119                 }
120
121                 protected Hashtable SuppressedAttributes {
122                         get { return suppressed_attrs; }
123                 }
124
125                 public virtual void AddRecognizedAttribute (string elementName, string attributeName)
126                 {
127                         Hashtable elem_attrs = (Hashtable) recognized_attrs [elementName];
128
129                         if (elem_attrs == null) {
130                                 elem_attrs = new Hashtable ();
131                                 elem_attrs.Add (attributeName, true);
132                                 recognized_attrs.Add (elementName, elem_attrs);
133                         } else
134                                 elem_attrs.Add (attributeName, true);
135                 }
136
137                 public virtual void RemoveRecognizedAttribute (string elementName, string attributeName)
138                 {
139                         Hashtable elem_attrs = (Hashtable) recognized_attrs [elementName];
140
141                         if (elem_attrs != null)
142                                 elem_attrs.Remove (attributeName);
143                 }
144
145                 //
146                 // writes <br>
147                 //
148                 public override void WriteBreak ()
149                 {
150                         string br = GetTagName (HtmlTextWriterTag.Br);
151                         WriteBeginTag (br);
152                         Write (TagRightChar);
153                 }
154
155                 public override void WriteEncodedText (string text)
156                 {
157                         base.WriteEncodedText (text);
158                 }
159
160                 Hashtable attr_render = new Hashtable ();
161
162                 protected override bool OnAttributeRender (string name, string value, HtmlTextWriterAttribute key)
163                 {
164                         // FIXME:
165                         // I checked every possible HtmlTextWriterAttribute key
166                         // and always throws ArgumentNullException.
167                         return (bool) attr_render [null];
168                 }
169
170                 protected override bool OnStyleAttributeRender (string styleAttrName, string value, HtmlTextWriterStyle key)
171                 {
172                         return key == HtmlTextWriterStyle.Display;
173                 }
174
175                 protected override bool OnTagRender (string name, HtmlTextWriterTag tag)
176                 {
177                         return tag != HtmlTextWriterTag.Span;
178                 }
179         }
180 }