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