3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * Leen Toelen (toelen@hotmail.com)
\r
29 using System.Collections;
\r
31 using System.Web.UI;
\r
33 namespace System.Web.UI{
\r
35 public class HtmlTextWriter : System.IO.TextWriter {
\r
37 static HtmlTextWriter(){
\r
38 HtmlTextWriter._tagKeyLookupTable = new Hashtable(97);
\r
39 HtmlTextWriter._tagNameLookupArray = new TagInformation[97];
\r
40 HtmlTextWriter.RegisterTag("", HtmlTextWriterTag.Unknown, TagType.Other);
\r
41 HtmlTextWriter.RegisterTag("a", HtmlTextWriterTag.A, TagType.Inline);
\r
42 HtmlTextWriter.RegisterTag("acronym", HtmlTextWriterTag.Acronym, TagType.Inline);
\r
43 HtmlTextWriter.RegisterTag("address", HtmlTextWriterTag.Address, TagType.Other);
\r
44 HtmlTextWriter.RegisterTag("area", HtmlTextWriterTag.Area, TagType.Other);
\r
45 HtmlTextWriter.RegisterTag("b", HtmlTextWriterTag.B, TagType.Inline);
\r
46 HtmlTextWriter.RegisterTag("base", HtmlTextWriterTag.Base, TagType.NonClosing);
\r
47 HtmlTextWriter.RegisterTag("basefont", HtmlTextWriterTag.Basefont, TagType.NonClosing);
\r
48 HtmlTextWriter.RegisterTag("bdo", HtmlTextWriterTag.Bdo, TagType.Inline);
\r
49 HtmlTextWriter.RegisterTag("bgsound", HtmlTextWriterTag.Bgsound, TagType.NonClosing);
\r
50 HtmlTextWriter.RegisterTag("big", HtmlTextWriterTag.Big, TagType.Inline);
\r
51 HtmlTextWriter.RegisterTag("blockquote", HtmlTextWriterTag.Blockquote, TagType.Other);
\r
52 HtmlTextWriter.RegisterTag("body", HtmlTextWriterTag.Body, TagType.Other);
\r
53 HtmlTextWriter.RegisterTag("br", HtmlTextWriterTag.Br, TagType.Other);
\r
54 HtmlTextWriter.RegisterTag("button", HtmlTextWriterTag.Button, TagType.Inline);
\r
55 HtmlTextWriter.RegisterTag("caption", HtmlTextWriterTag.Caption, TagType.Other);
\r
56 HtmlTextWriter.RegisterTag("center", HtmlTextWriterTag.Center, TagType.Other);
\r
57 HtmlTextWriter.RegisterTag("cite", HtmlTextWriterTag.Cite, TagType.Inline);
\r
58 HtmlTextWriter.RegisterTag("code", HtmlTextWriterTag.Code, TagType.Inline);
\r
59 HtmlTextWriter.RegisterTag("col", HtmlTextWriterTag.Col, TagType.NonClosing);
\r
60 HtmlTextWriter.RegisterTag("colgroup", HtmlTextWriterTag.Colgroup, TagType.Other);
\r
61 HtmlTextWriter.RegisterTag("del", HtmlTextWriterTag.Del, TagType.Inline);
\r
62 HtmlTextWriter.RegisterTag("dd", HtmlTextWriterTag.Dd, TagType.Inline);
\r
63 HtmlTextWriter.RegisterTag("dfn", HtmlTextWriterTag.Dfn, TagType.Inline);
\r
64 HtmlTextWriter.RegisterTag("dir", HtmlTextWriterTag.Dir, TagType.Other);
\r
65 HtmlTextWriter.RegisterTag("div", HtmlTextWriterTag.Div, TagType.Other);
\r
66 HtmlTextWriter.RegisterTag("dl", HtmlTextWriterTag.Dl, TagType.Other);
\r
67 HtmlTextWriter.RegisterTag("dt", HtmlTextWriterTag.Dt, TagType.Inline);
\r
68 HtmlTextWriter.RegisterTag("em", HtmlTextWriterTag.Em, TagType.Inline);
\r
69 HtmlTextWriter.RegisterTag("embed", HtmlTextWriterTag.Embed, TagType.NonClosing);
\r
70 HtmlTextWriter.RegisterTag("fieldset", HtmlTextWriterTag.Fieldset, TagType.Other);
\r
71 HtmlTextWriter.RegisterTag("font", HtmlTextWriterTag.Font, TagType.Inline);
\r
72 HtmlTextWriter.RegisterTag("form", HtmlTextWriterTag.Form, TagType.Other);
\r
73 HtmlTextWriter.RegisterTag("frame", HtmlTextWriterTag.Frame, TagType.NonClosing);
\r
74 HtmlTextWriter.RegisterTag("frameset", HtmlTextWriterTag.Frameset, TagType.Other);
\r
75 HtmlTextWriter.RegisterTag("h1", HtmlTextWriterTag.H1, TagType.Other);
\r
76 HtmlTextWriter.RegisterTag("h2", HtmlTextWriterTag.H2, TagType.Other);
\r
77 HtmlTextWriter.RegisterTag("h3", HtmlTextWriterTag.H3, TagType.Other);
\r
78 HtmlTextWriter.RegisterTag("h4", HtmlTextWriterTag.H4, TagType.Other);
\r
79 HtmlTextWriter.RegisterTag("h5", HtmlTextWriterTag.H5, TagType.Other);
\r
80 HtmlTextWriter.RegisterTag("h6", HtmlTextWriterTag.H6, TagType.Other);
\r
81 HtmlTextWriter.RegisterTag("head", HtmlTextWriterTag.Head, TagType.Other);
\r
82 HtmlTextWriter.RegisterTag("hr", HtmlTextWriterTag.Hr, TagType.NonClosing);
\r
83 HtmlTextWriter.RegisterTag("html", HtmlTextWriterTag.Html, TagType.Other);
\r
84 HtmlTextWriter.RegisterTag("i", HtmlTextWriterTag.I, TagType.Inline);
\r
85 HtmlTextWriter.RegisterTag("iframe", HtmlTextWriterTag.Iframe, TagType.Other);
\r
86 HtmlTextWriter.RegisterTag("img", HtmlTextWriterTag.Img, TagType.NonClosing);
\r
87 HtmlTextWriter.RegisterTag("input", HtmlTextWriterTag.Input, TagType.NonClosing);
\r
88 HtmlTextWriter.RegisterTag("ins", HtmlTextWriterTag.Ins, TagType.Inline);
\r
89 HtmlTextWriter.RegisterTag("isindex", HtmlTextWriterTag.Isindex, TagType.NonClosing);
\r
90 HtmlTextWriter.RegisterTag("kbd", HtmlTextWriterTag.Kbd, TagType.Inline);
\r
91 HtmlTextWriter.RegisterTag("label", HtmlTextWriterTag.Label, TagType.Inline);
\r
92 HtmlTextWriter.RegisterTag("legend", HtmlTextWriterTag.Legend, TagType.Other);
\r
93 HtmlTextWriter.RegisterTag("li", HtmlTextWriterTag.Li, TagType.Inline);
\r
94 HtmlTextWriter.RegisterTag("link", HtmlTextWriterTag.Link, TagType.NonClosing);
\r
95 HtmlTextWriter.RegisterTag("map", HtmlTextWriterTag.Map, TagType.Other);
\r
96 HtmlTextWriter.RegisterTag("marquee", HtmlTextWriterTag.Marquee, TagType.Other);
\r
97 HtmlTextWriter.RegisterTag("menu", HtmlTextWriterTag.Menu, TagType.Other);
\r
98 HtmlTextWriter.RegisterTag("meta", HtmlTextWriterTag.Meta, TagType.NonClosing);
\r
99 HtmlTextWriter.RegisterTag("nobr", HtmlTextWriterTag.Nobr, TagType.Inline);
\r
100 HtmlTextWriter.RegisterTag("noframes", HtmlTextWriterTag.Noframes, TagType.Other);
\r
101 HtmlTextWriter.RegisterTag("noscript", HtmlTextWriterTag.Noscript, TagType.Other);
\r
102 HtmlTextWriter.RegisterTag("object", HtmlTextWriterTag.Object, TagType.Other);
\r
103 HtmlTextWriter.RegisterTag("ol", HtmlTextWriterTag.Ol, TagType.Other);
\r
104 HtmlTextWriter.RegisterTag("option", HtmlTextWriterTag.Option, TagType.Other);
\r
105 HtmlTextWriter.RegisterTag("p", HtmlTextWriterTag.P, TagType.Inline);
\r
106 HtmlTextWriter.RegisterTag("param", HtmlTextWriterTag.Param, TagType.Other);
\r
107 HtmlTextWriter.RegisterTag("pre", HtmlTextWriterTag.Pre, TagType.Other);
\r
108 HtmlTextWriter.RegisterTag("q", HtmlTextWriterTag.Q, TagType.Inline);
\r
109 HtmlTextWriter.RegisterTag("rt", HtmlTextWriterTag.Rt, TagType.Other);
\r
110 HtmlTextWriter.RegisterTag("ruby", HtmlTextWriterTag.Ruby, TagType.Other);
\r
111 HtmlTextWriter.RegisterTag("s", HtmlTextWriterTag.S, TagType.Inline);
\r
112 HtmlTextWriter.RegisterTag("samp", HtmlTextWriterTag.Samp, TagType.Inline);
\r
113 HtmlTextWriter.RegisterTag("script", HtmlTextWriterTag.Script, TagType.Other);
\r
114 HtmlTextWriter.RegisterTag("select", HtmlTextWriterTag.Select, TagType.Other);
\r
115 HtmlTextWriter.RegisterTag("small", HtmlTextWriterTag.Small, TagType.Other);
\r
116 HtmlTextWriter.RegisterTag("span", HtmlTextWriterTag.Span, TagType.Inline);
\r
117 HtmlTextWriter.RegisterTag("strike", HtmlTextWriterTag.Strike, TagType.Inline);
\r
118 HtmlTextWriter.RegisterTag("strong", HtmlTextWriterTag.Strong, TagType.Inline);
\r
119 HtmlTextWriter.RegisterTag("style", HtmlTextWriterTag.Style, TagType.Other);
\r
120 HtmlTextWriter.RegisterTag("sub", HtmlTextWriterTag.Sub, TagType.Inline);
\r
121 HtmlTextWriter.RegisterTag("sup", HtmlTextWriterTag.Sup, TagType.Inline);
\r
122 HtmlTextWriter.RegisterTag("table", HtmlTextWriterTag.Table, TagType.Other);
\r
123 HtmlTextWriter.RegisterTag("tbody", HtmlTextWriterTag.Tbody, TagType.Other);
\r
124 HtmlTextWriter.RegisterTag("td", HtmlTextWriterTag.Td, TagType.Inline);
\r
125 HtmlTextWriter.RegisterTag("textarea", HtmlTextWriterTag.Textarea, TagType.Inline);
\r
126 HtmlTextWriter.RegisterTag("tfoot", HtmlTextWriterTag.Tfoot, TagType.Other);
\r
127 HtmlTextWriter.RegisterTag("th", HtmlTextWriterTag.Th, TagType.Inline);
\r
128 HtmlTextWriter.RegisterTag("thead", HtmlTextWriterTag.Thead, TagType.Other);
\r
129 HtmlTextWriter.RegisterTag("title", HtmlTextWriterTag.Title, TagType.Other);
\r
130 HtmlTextWriter.RegisterTag("tr", HtmlTextWriterTag.Tr, TagType.Other);
\r
131 HtmlTextWriter.RegisterTag("tt", HtmlTextWriterTag.Tt, TagType.Inline);
\r
132 HtmlTextWriter.RegisterTag("u", HtmlTextWriterTag.U, TagType.Inline);
\r
133 HtmlTextWriter.RegisterTag("ul", HtmlTextWriterTag.Ul, TagType.Other);
\r
134 HtmlTextWriter.RegisterTag("var", HtmlTextWriterTag.Var, TagType.Inline);
\r
135 HtmlTextWriter.RegisterTag("wbr", HtmlTextWriterTag.Wbr, TagType.NonClosing);
\r
136 HtmlTextWriter.RegisterTag("xml", HtmlTextWriterTag.Xml, TagType.Other);
\r
138 HtmlTextWriter._attrKeyLookupTable = new Hashtable(40);
\r
139 HtmlTextWriter._attrNameLookupArray = new AttributeInformation[40];
\r
140 HtmlTextWriter.RegisterAttribute("accesskey", HtmlTextWriterAttribute.Accesskey, true);
\r
141 HtmlTextWriter.RegisterAttribute("align", HtmlTextWriterAttribute.Align, false);
\r
142 HtmlTextWriter.RegisterAttribute("alt", HtmlTextWriterAttribute.Alt, true);
\r
143 HtmlTextWriter.RegisterAttribute("background", HtmlTextWriterAttribute.Background, true);
\r
144 HtmlTextWriter.RegisterAttribute("bgcolor", HtmlTextWriterAttribute.Bgcolor, false);
\r
145 HtmlTextWriter.RegisterAttribute("border", HtmlTextWriterAttribute.Border, false);
\r
146 HtmlTextWriter.RegisterAttribute("bordercolor", HtmlTextWriterAttribute.Bordercolor, false);
\r
147 HtmlTextWriter.RegisterAttribute("cellpadding", HtmlTextWriterAttribute.Cellpadding, false);
\r
148 HtmlTextWriter.RegisterAttribute("cellspacing", HtmlTextWriterAttribute.Cellspacing, false);
\r
149 HtmlTextWriter.RegisterAttribute("checked", HtmlTextWriterAttribute.Checked, false);
\r
150 HtmlTextWriter.RegisterAttribute("class", HtmlTextWriterAttribute.Class, true);
\r
151 HtmlTextWriter.RegisterAttribute("cols", HtmlTextWriterAttribute.Cols, false);
\r
152 HtmlTextWriter.RegisterAttribute("colspan", HtmlTextWriterAttribute.Colspan, false);
\r
153 HtmlTextWriter.RegisterAttribute("disabled", HtmlTextWriterAttribute.Disabled, false);
\r
154 HtmlTextWriter.RegisterAttribute("for", HtmlTextWriterAttribute.For, false);
\r
155 HtmlTextWriter.RegisterAttribute("height", HtmlTextWriterAttribute.Height, false);
\r
156 HtmlTextWriter.RegisterAttribute("href", HtmlTextWriterAttribute.Href, true);
\r
157 HtmlTextWriter.RegisterAttribute("id", HtmlTextWriterAttribute.Id, false);
\r
158 HtmlTextWriter.RegisterAttribute("maxlength", HtmlTextWriterAttribute.Maxlength, false);
\r
159 HtmlTextWriter.RegisterAttribute("multiple", HtmlTextWriterAttribute.Multiple, false);
\r
160 HtmlTextWriter.RegisterAttribute("name", HtmlTextWriterAttribute.Name, false);
\r
161 HtmlTextWriter.RegisterAttribute("nowrap", HtmlTextWriterAttribute.Nowrap, false);
\r
162 HtmlTextWriter.RegisterAttribute("onchange", HtmlTextWriterAttribute.Onchange, true);
\r
163 HtmlTextWriter.RegisterAttribute("onclick", HtmlTextWriterAttribute.Onclick, true);
\r
164 HtmlTextWriter.RegisterAttribute("readonly", HtmlTextWriterAttribute.ReadOnly, false);
\r
165 HtmlTextWriter.RegisterAttribute("rows", HtmlTextWriterAttribute.Rows, false);
\r
166 HtmlTextWriter.RegisterAttribute("rowspan", HtmlTextWriterAttribute.Rowspan, false);
\r
167 HtmlTextWriter.RegisterAttribute("rules", HtmlTextWriterAttribute.Rules, false);
\r
168 HtmlTextWriter.RegisterAttribute("selected", HtmlTextWriterAttribute.Selected, false);
\r
169 HtmlTextWriter.RegisterAttribute("size", HtmlTextWriterAttribute.Size, false);
\r
170 HtmlTextWriter.RegisterAttribute("src", HtmlTextWriterAttribute.Src, true);
\r
171 HtmlTextWriter.RegisterAttribute("style", HtmlTextWriterAttribute.Style, false);
\r
172 HtmlTextWriter.RegisterAttribute("tabindex", HtmlTextWriterAttribute.Tabindex, false);
\r
173 HtmlTextWriter.RegisterAttribute("target", HtmlTextWriterAttribute.Target, false);
\r
174 HtmlTextWriter.RegisterAttribute("title", HtmlTextWriterAttribute.Title, true);
\r
175 HtmlTextWriter.RegisterAttribute("type", HtmlTextWriterAttribute.Type, false);
\r
176 HtmlTextWriter.RegisterAttribute("valign", HtmlTextWriterAttribute.Valign, false);
\r
177 HtmlTextWriter.RegisterAttribute("value", HtmlTextWriterAttribute.Value, true);
\r
178 HtmlTextWriter.RegisterAttribute("width", HtmlTextWriterAttribute.Width, false);
\r
179 HtmlTextWriter.RegisterAttribute("wrap", HtmlTextWriterAttribute.Wrap, false);
\r
181 HtmlTextWriter._styleKeyLookupTable = new Hashtable(14);
\r
182 HtmlTextWriter._styleNameLookupArray = new String[14];
\r
183 HtmlTextWriter.RegisterStyle("background-color", HtmlTextWriterStyle.BackgroundColor);
\r
184 HtmlTextWriter.RegisterStyle("background-image", HtmlTextWriterStyle.BackgroundImage);
\r
185 HtmlTextWriter.RegisterStyle("border-collapse", HtmlTextWriterStyle.BorderCollapse);
\r
186 HtmlTextWriter.RegisterStyle("border-color", HtmlTextWriterStyle.BorderColor);
\r
187 HtmlTextWriter.RegisterStyle("border-style", HtmlTextWriterStyle.BorderStyle);
\r
188 HtmlTextWriter.RegisterStyle("border-width", HtmlTextWriterStyle.BorderWidth);
\r
189 HtmlTextWriter.RegisterStyle("color", HtmlTextWriterStyle.Color);
\r
190 HtmlTextWriter.RegisterStyle("font-family", HtmlTextWriterStyle.FontFamily);
\r
191 HtmlTextWriter.RegisterStyle("font-size", HtmlTextWriterStyle.FontSize);
\r
192 HtmlTextWriter.RegisterStyle("font-style", HtmlTextWriterStyle.FontStyle);
\r
193 HtmlTextWriter.RegisterStyle("font-weight", HtmlTextWriterStyle.FontWeight);
\r
194 HtmlTextWriter.RegisterStyle("height", HtmlTextWriterStyle.Height);
\r
195 HtmlTextWriter.RegisterStyle("text-decoration", HtmlTextWriterStyle.TextDecoration);
\r
196 HtmlTextWriter.RegisterStyle("width", HtmlTextWriterStyle.Width);
\r
199 public HtmlTextWriter(TextWriter writer):this(writer, " "){}
\r
201 public HtmlTextWriter(TextWriter writer, string tabString) : base() {
\r
202 this.writer = writer;
\r
203 this.tabString = tabString;
\r
205 tabsPending = false;
\r
206 _httpWriter = writer as HttpWriter;
\r
207 _isDescendant = GetType() == typeof(HtmlTextWriter) == false;
\r
208 _attrList = new RenderAttribute[20];
\r
210 _styleList = new RenderStyle[20];
\r
212 _endTags = new TagStackEntry[16];
\r
217 public virtual void AddAttribute(HtmlTextWriterAttribute key, string value){
\r
218 if ((int) key >= 0 && (int) key < HtmlTextWriter._attrNameLookupArray.Length) {
\r
219 AttributeInformation attrInfo = HtmlTextWriter._attrNameLookupArray[(int) key];
\r
220 AddAttribute(attrInfo.name, value, key, attrInfo.encode);
\r
224 public virtual void AddAttribute(HtmlTextWriterAttribute key, string value, bool fEncode){
\r
225 if ((int) key >= 0 && (int) key < HtmlTextWriter._attrNameLookupArray.Length) {
\r
226 AddAttribute(HtmlTextWriter._attrNameLookupArray[(int) key].name, value, key, fEncode);
\r
230 public virtual void AddAttribute(string name, string value){
\r
231 HtmlTextWriterAttribute attr = GetAttributeKey(name);
\r
232 value = EncodeAttributeValue(GetAttributeKey(name), value);
\r
233 AddAttribute(name, value, attr);
\r
236 public virtual void AddAttribute(string name, string value, bool fEndode){
\r
237 value = EncodeAttributeValue(value, fEndode);
\r
238 AddAttribute(name, value, GetAttributeKey(name));
\r
241 protected virtual void AddAttribute(string name, string value, HtmlTextWriterAttribute key){
\r
242 AddAttribute(name, value, key, false);
\r
245 private void AddAttribute(string name, string value, HtmlTextWriterAttribute key, bool encode){
\r
246 if (_attrCount >= (int) _attrList.Length) {
\r
247 RenderAttribute[] rAttrArr = new RenderAttribute[_attrList.Length * 2];
\r
248 System.Array.Copy(_attrList, rAttrArr, (int) _attrList.Length);
\r
249 _attrList = rAttrArr;
\r
251 RenderAttribute rAttr;
\r
253 rAttr.value = value;
\r
255 rAttr.encode = encode;
\r
256 _attrList [_attrCount++] = rAttr;
\r
259 public virtual void AddStyleAttribute(HtmlTextWriterStyle key, string value){
\r
260 AddStyleAttribute(GetStyleName(key), value, key);
\r
263 public virtual void AddStyleAttribute(string name, string value){
\r
264 AddStyleAttribute(name, value, GetStyleKey(name));
\r
267 protected virtual void AddStyleAttribute(string name, string value, HtmlTextWriterStyle key){
\r
268 if (_styleCount >= (int) _styleList.Length) {
\r
269 RenderStyle[] rAttrArr = new RenderStyle[_styleList.Length * 2];
\r
270 System.Array.Copy(_styleList, rAttrArr, (int) _styleList.Length);
\r
271 _styleList = rAttrArr;
\r
275 rAttr.value = value;
\r
277 _styleList [_styleCount++] = rAttr;
\r
280 public override void Close(){
\r
284 protected virtual string EncodeAttributeValue(HtmlTextWriterAttribute attrKey, string value){
\r
286 if (0 <= (int) attrKey && (int) attrKey < HtmlTextWriter._attrNameLookupArray.Length)
\r
287 valid = HtmlTextWriter._attrNameLookupArray[(int) attrKey].encode;
\r
288 return EncodeAttributeValue(value, valid);
\r
291 protected string EncodeAttributeValue(string value, bool fEncode){
\r
296 return System.Web.HttpUtility.HtmlAttributeEncode(value);
\r
299 protected string EncodeUrl(string url){
\r
300 if (url.IndexOf(SpaceChar) < 0)
\r
302 System.Text.StringBuilder sb = new System.Text.StringBuilder();
\r
303 for(int i=0; i <= url.Length; i++){
\r
304 char temp = url[i];
\r
310 return sb.ToString();
\r
313 protected virtual void FilterAttributes(){
\r
315 for(int i=0; i < _styleCount; i++){
\r
316 RenderStyle rStyle = _styleList[i];
\r
317 if (OnStyleAttributeRender(rStyle.name, rStyle.value, rStyle.key)) {
\r
321 _styleCount = count;
\r
323 for(int i=0; i <= _attrCount; i++){
\r
324 RenderAttribute rAttr = _attrList[i];
\r
325 if (OnAttributeRender(rAttr.name, rAttr.value, rAttr.key)) {
\r
329 _attrCount = count;
\r
332 public override void Flush(){
\r
336 protected HtmlTextWriterAttribute GetAttributeKey(string attrName){
\r
337 if (attrName != null && attrName.Length > 0) {
\r
338 object attr = HtmlTextWriter._attrKeyLookupTable[attrName.ToLower()];
\r
340 return (HtmlTextWriterAttribute) attr;
\r
342 return (HtmlTextWriterAttribute) (-1);
\r
345 protected string GetAttributeName(HtmlTextWriterAttribute attrKey){
\r
346 if ((int) attrKey >= 0 && (int) attrKey < HtmlTextWriter._attrNameLookupArray.Length)
\r
347 return HtmlTextWriter._attrNameLookupArray[(int) attrKey].name;
\r
348 return System.String.Empty;
\r
351 protected HtmlTextWriterStyle GetStyleKey(string styleName){
\r
352 if (styleName != null && styleName.Length > 0) {
\r
353 object style = HtmlTextWriter._styleKeyLookupTable[styleName.ToLower()];
\r
355 return (HtmlTextWriterStyle) style;
\r
357 return (HtmlTextWriterStyle) (-1);
\r
360 protected string GetStyleName(HtmlTextWriterStyle styleKey){
\r
361 if ((int) styleKey >= 0 && (int) styleKey < HtmlTextWriter._styleNameLookupArray.Length)
\r
362 return HtmlTextWriter._styleNameLookupArray[(int) styleKey];
\r
363 return System.String.Empty;
\r
366 protected virtual HtmlTextWriterTag GetTagKey(string tagName){
\r
367 if (tagName != null && tagName.Length > 0) {
\r
368 object tag = HtmlTextWriter._tagKeyLookupTable[tagName.ToLower()];
\r
370 return (HtmlTextWriterTag) tag;
\r
375 protected virtual string GetTagName(HtmlTextWriterTag tagKey){
\r
376 if ((int) tagKey >= 0 && (int) tagKey < HtmlTextWriter._tagNameLookupArray.Length)
\r
377 return HtmlTextWriter._tagNameLookupArray[(int) tagKey].name;
\r
378 return System.String.Empty;
\r
381 protected bool IsAttributeDefined(HtmlTextWriterAttribute key){
\r
382 for (int i=0; i < _attrCount; i++) {
\r
383 if (_attrList[i].key == key)
\r
389 protected bool IsAttributeDefined(HtmlTextWriterAttribute key, out string value){
\r
391 for (int i=0; i < _attrCount; i++) {
\r
392 if (_attrList[i].key == key) {
\r
393 value = _attrList[i].value;
\r
400 protected bool IsStyleAttributeDefined(HtmlTextWriterStyle key){
\r
401 for (int i= 0; i < _styleCount; i++) {
\r
402 if (_styleList[i].key == key)
\r
408 protected bool IsStyleAttributeDefined(HtmlTextWriterStyle key, out string value){
\r
410 for( int i=0; i < _styleCount; i++) {
\r
411 if (_styleList[i].key == key) {
\r
412 value = _styleList[i].value;
\r
419 protected virtual bool OnAttributeRender(string name, string value, HtmlTextWriterAttribute key){
\r
423 protected virtual bool OnStyleAttributeRender(string name, string value, HtmlTextWriterStyle key){
\r
427 protected virtual bool OnTagRender(string name, HtmlTextWriterTag key){
\r
431 protected virtual void OutputTabs(){
\r
433 for(int i=0; i < indentLevel; i++) {
\r
434 writer.Write(tabString);
\r
436 tabsPending = false;
\r
440 protected string PopEndTag(){
\r
441 if (_endTagCount <= 0)
\r
442 throw new InvalidOperationException("A PopEndTag was called without a corresponding PushEndTag");
\r
444 TagKey = _endTags[_endTagCount].tagKey;
\r
445 return _endTags[_endTagCount].endTagText;
\r
448 protected void PushEndTag(string endTag){
\r
449 if (_endTagCount >= (int) _endTags.Length) {
\r
450 TagStackEntry[] temp = new TagStackEntry[(int) _endTags.Length * 2];
\r
451 System.Array.Copy(_endTags, temp, (int) _endTags.Length);
\r
454 _endTags[_endTagCount].tagKey = _tagKey;
\r
455 _endTags[_endTagCount].endTagText = endTag;
\r
459 protected static void RegisterAttribute(string name, HtmlTextWriterAttribute key){
\r
460 HtmlTextWriter.RegisterAttribute(name, key, false);
\r
463 private static void RegisterAttribute(string name, HtmlTextWriterAttribute key, bool fEncode){
\r
464 name = name.ToLower();
\r
465 HtmlTextWriter._attrKeyLookupTable.Add(name, key);
\r
466 if ((int) key < (int) HtmlTextWriter._attrNameLookupArray.Length)
\r
467 HtmlTextWriter._attrNameLookupArray[(int) key] = new AttributeInformation(name, fEncode);
\r
470 protected static void RegisterStyle(string name, HtmlTextWriterStyle key){
\r
471 name = name.ToLower();
\r
472 HtmlTextWriter._styleKeyLookupTable.Add(name, key);
\r
473 if ((int) key < (int) HtmlTextWriter._styleNameLookupArray.Length)
\r
474 HtmlTextWriter._styleNameLookupArray[(int) key] = name;
\r
477 protected static void RegisterTag(string name, HtmlTextWriterTag key){
\r
478 HtmlTextWriter.RegisterTag(name, key, TagType.Other);
\r
481 private static void RegisterTag(string name, HtmlTextWriterTag key, TagType type){
\r
482 name = name.ToLower();
\r
483 HtmlTextWriter._tagKeyLookupTable.Add(name, key);
\r
484 string fullTag = null;
\r
485 if ((int) type != 1 && (int) key != 0) {
\r
486 fullTag = EndTagLeftChars + name + TagRightChar;
\r
488 if ((int) key < HtmlTextWriter._tagNameLookupArray.Length)
\r
489 HtmlTextWriter._tagNameLookupArray[(int) key] = new TagInformation(name, type, fullTag);
\r
492 protected virtual string RenderAfterContent(){
\r
496 protected virtual string RenderAfterTag(){
\r
500 protected virtual string RenderBeforeContent(){
\r
504 protected virtual string RenderBeforeTag(){
\r
508 public virtual void RenderBeginTag(HtmlTextWriterTag tagKey){
\r
510 bool tagRendered = true;
\r
511 bool tagRender = true;
\r
512 if (_isDescendant) {
\r
513 tagRender = OnTagRender(_tagName, _tagKey);
\r
514 FilterAttributes();
\r
515 string beforeTag = RenderBeforeTag();
\r
516 if (beforeTag != null) {
\r
519 writer.Write(beforeTag);
\r
522 TagInformation currentTag = HtmlTextWriter._tagNameLookupArray[_tagIndex];
\r
523 if (currentTag.closingTag == null && currentTag.tagType == TagType.Other) {
\r
524 currentTag.closingTag = EndTagLeftChars + _tagName + TagRightChar;
\r
528 tagRendered = false;
\r
531 writer.Write(TagLeftChar);
\r
532 writer.Write(_tagName);
\r
533 RenderAttribute rAttr;
\r
534 string rAttrValue = null;
\r
535 for (int i=0; i < _attrCount; i++) {
\r
536 rAttr = _attrList[i];
\r
537 if (rAttr.key == HtmlTextWriterAttribute.Style)
\r
538 rAttrValue = rAttr.value;
\r
540 writer.Write(SpaceChar);
\r
541 writer.Write(rAttr.name);
\r
542 if (rAttr.value != null) {
\r
543 writer.Write(EqualsChar);
\r
544 writer.Write(DoubleQuoteChar);
\r
545 if (rAttr.encode) {
\r
546 if (_httpWriter == null) {
\r
547 System.Web.HttpUtility.HtmlAttributeEncode(rAttr.value, writer);
\r
550 System.Web.HttpUtility.HtmlAttributeEncode(rAttr.value, (TextWriter) _httpWriter);
\r
554 writer.Write(rAttr.value);
\r
556 writer.Write(DoubleQuoteChar);
\r
560 if (_styleCount > 0 || rAttrValue != null) {
\r
561 writer.Write(SpaceChar);
\r
562 writer.Write("style");
\r
563 writer.Write(EqualsChar);
\r
564 writer.Write(DoubleQuoteChar);
\r
565 RenderStyle rStyle;
\r
566 for (int i=0; i < _styleCount; i++) {
\r
567 rStyle = _styleList[i];
\r
568 writer.Write(rStyle.name);
\r
569 writer.Write(StyleEqualsChar);
\r
570 writer.Write(rStyle.value);
\r
571 writer.Write(SemicolonChar);
\r
573 if (rAttrValue != null)
\r
574 writer.Write(rAttrValue);
\r
575 writer.Write(DoubleQuoteChar);
\r
577 if (currentTag.tagType == TagType.NonClosing) {
\r
578 writer.Write(SpaceChar);
\r
579 writer.Write(SlashChar);
\r
580 writer.Write(TagRightChar);
\r
583 writer.Write(TagRightChar);
\r
585 string beforeContent = RenderBeforeContent();
\r
586 if (beforeContent != null) {
\r
589 writer.Write(beforeContent);
\r
592 if (currentTag.tagType == TagType.Inline)
\r
598 if (currentTag.closingTag == null) {
\r
599 currentTag.closingTag = EndTagLeftChars + _tagName + TagRightChar;
\r
602 if (_isDescendant) {
\r
603 string afterContent = RenderAfterContent();
\r
604 if (afterContent != null) {
\r
605 if (currentTag.closingTag != null)
\r
606 currentTag.closingTag = afterContent;
\r
608 string afterTag = RenderAfterTag();
\r
609 if (afterTag != null) {
\r
610 if (currentTag.closingTag != null)
\r
611 currentTag.closingTag = afterTag;
\r
614 PushEndTag(currentTag.closingTag);
\r
619 public virtual void RenderBeginTag(string tagName){
\r
621 RenderBeginTag(_tagKey);
\r
624 public virtual void RenderEndTag(){
\r
625 string endTagText = PopEndTag();
\r
626 if (endTagText != null) {
\r
627 if (HtmlTextWriter._tagNameLookupArray[_tagIndex].tagType == 0) {
\r
639 public override void Write(bool value){
\r
642 writer.Write(value);
\r
645 public override void Write(char value){
\r
648 writer.Write(value);
\r
651 public override void Write(char[] buffer){
\r
654 writer.Write(buffer);
\r
657 public override void Write(char[] buffer, int index, int count){
\r
660 writer.Write(buffer, index, count);
\r
663 public override void Write(double value){
\r
666 writer.Write(value);
\r
669 public override void Write(int value){
\r
672 writer.Write(value);
\r
675 public override void Write(long value){
\r
678 writer.Write(value);
\r
681 public override void Write(object value){
\r
684 writer.Write(value);
\r
687 public override void Write(float value){
\r
690 writer.Write(value);
\r
693 public override void Write(string s){
\r
699 public override void Write(string format, object arg0){
\r
702 writer.Write(format, arg0);
\r
705 public override void Write(string format, object arg0, object arg1){
\r
708 writer.Write(format, arg0, arg1);
\r
711 public override void Write(string format, params object[] arg){
\r
714 writer.Write(format, arg);
\r
717 public virtual void WriteAttribute(string name, string value){
\r
718 WriteAttribute(name, value, false);
\r
721 public virtual void WriteAttribute(string name, string value, bool fEncode){
\r
722 writer.Write(SpaceChar);
\r
723 writer.Write(name);
\r
724 if (value != null) {
\r
725 writer.Write(EqualsChar);
\r
726 writer.Write(DoubleQuoteChar);
\r
728 if (_httpWriter == null) {
\r
729 System.Web.HttpUtility.HtmlAttributeEncode(value, writer);
\r
732 System.Web.HttpUtility.HtmlAttributeEncode(value, (TextWriter) _httpWriter);
\r
736 writer.Write(value);
\r
738 writer.Write(DoubleQuoteChar);
\r
742 public virtual void WriteBeginTag(string tagName){
\r
745 writer.Write(TagLeftChar);
\r
746 writer.Write(tagName);
\r
749 public virtual void WriteEndTag(string tagName){
\r
752 writer.Write(TagLeftChar);
\r
753 writer.Write(SlashChar);
\r
754 writer.Write(tagName);
\r
755 writer.Write(TagRightChar);
\r
758 public virtual void WriteFullBeginTag(string tagName){
\r
761 writer.Write(TagLeftChar);
\r
762 writer.Write(tagName);
\r
763 writer.Write(TagRightChar);
\r
766 public override void WriteLine(){
\r
767 writer.WriteLine();
\r
768 tabsPending = true;
\r
771 public override void WriteLine(bool value){
\r
774 writer.WriteLine(value);
\r
775 tabsPending = true;
\r
778 public override void WriteLine(char value){
\r
781 writer.WriteLine(value);
\r
782 tabsPending = true;
\r
785 public override void WriteLine(char[] buffer){
\r
788 writer.WriteLine(buffer);
\r
789 tabsPending = true;
\r
792 public override void WriteLine(char[] buffer, int index, int count){
\r
795 writer.WriteLine(buffer, index, count);
\r
796 tabsPending = true;
\r
799 public override void WriteLine(double value){
\r
802 writer.WriteLine(value);
\r
803 tabsPending = true;
\r
806 public override void WriteLine(int value){
\r
809 writer.WriteLine(value);
\r
810 tabsPending = true;
\r
813 public override void WriteLine(long value){
\r
816 writer.WriteLine(value);
\r
817 tabsPending = true;
\r
820 public override void WriteLine(object value){
\r
823 writer.WriteLine(value);
\r
824 tabsPending = true;
\r
827 public override void WriteLine(float value){
\r
830 writer.WriteLine(value);
\r
831 tabsPending = true;
\r
834 public override void WriteLine(string s){
\r
837 writer.WriteLine(s);
\r
838 tabsPending = true;
\r
841 public override void WriteLine(string format, object arg0){
\r
844 writer.WriteLine(format, arg0);
\r
845 tabsPending = true;
\r
848 public override void WriteLine(string format, object arg0, object arg1){
\r
851 writer.WriteLine(format, arg0, arg1);
\r
852 tabsPending = true;
\r
855 public override void WriteLine(string format, params object[] arg){
\r
858 writer.WriteLine(format, arg);
\r
859 tabsPending = true;
\r
862 [CLSCompliant(false)]
\r
863 public override void WriteLine(uint value){
\r
866 writer.WriteLine(value);
\r
867 tabsPending = true;
\r
870 public void WriteLineNoTabs(string s){
\r
871 writer.WriteLine(s);
\r
874 public virtual void WriteStyleAttribute(string name, string value){
\r
875 WriteStyleAttribute(name, value, false);
\r
878 public virtual void WriteStyleAttribute(string name, string value, bool fEncode){
\r
879 writer.Write(name);
\r
880 writer.Write(StyleEqualsChar);
\r
882 if (_httpWriter == null) {
\r
883 System.Web.HttpUtility.HtmlAttributeEncode(value, writer);
\r
886 System.Web.HttpUtility.HtmlAttributeEncode(value, (TextWriter) _httpWriter);
\r
890 writer.Write(value);
\r
892 writer.Write(SemicolonChar);
\r
895 public override System.Text.Encoding Encoding {
\r
897 return writer.Encoding;
\r
901 public int Indent {
\r
903 return indentLevel;
\r
908 indentLevel = value;
\r
912 public TextWriter InnerWriter {
\r
918 _httpWriter = value as HttpWriter;
\r
922 public override string NewLine {
\r
924 return writer.NewLine;
\r
927 writer.NewLine = value;
\r
931 protected HtmlTextWriterTag TagKey {
\r
936 _tagIndex = (int) value;
\r
937 if (_tagIndex < 0 || _tagIndex >= (int) HtmlTextWriter._tagNameLookupArray.Length)
\r
938 throw new ArgumentOutOfRangeException("value");
\r
941 _tagName = HtmlTextWriter._tagNameLookupArray[_tagIndex].name;
\r
945 protected string TagName {
\r
951 _tagKey = GetTagKey(_tagName);
\r
952 _tagIndex = (int) _tagKey;
\r
956 public const string DefaultTabString = "\t";
\r
957 public const char DoubleQuoteChar = '"';
\r
958 public const string EndTagLeftChars = "</";
\r
959 public const char EqualsChar = '=';
\r
960 public const string EqualsDoubleQuoteString = "=\"";
\r
961 public const string SelfClosingChars = " /";
\r
962 public const string SelfClosingTagEnd = " />";
\r
963 public const char SemicolonChar = ';';
\r
964 public const char SingleQuoteChar = '\'';
\r
965 public const char SlashChar = '/';
\r
966 public const char SpaceChar = ' ';
\r
967 public const char StyleEqualsChar = ':';
\r
968 public const char TagLeftChar = '<';
\r
969 public const char TagRightChar = '>';
\r
971 private int _attrCount;
\r
972 private int _endTagCount;
\r
973 private int _styleCount;
\r
974 private int indentLevel;
\r
975 private int _inlineCount;
\r
976 private int _tagIndex;
\r
978 private bool _isDescendant;
\r
979 private bool tabsPending;
\r
981 private HtmlTextWriterTag _tagKey;
\r
982 private TextWriter writer;
\r
983 private HttpWriter _httpWriter;
\r
985 private static Hashtable _attrKeyLookupTable;
\r
986 private static Hashtable _styleKeyLookupTable;
\r
987 private static Hashtable _tagKeyLookupTable;
\r
989 private string _tagName;
\r
990 private string tabString;
\r
991 private static string[] _styleNameLookupArray;
\r
993 private RenderAttribute[] _attrList;
\r
994 private static AttributeInformation[] _attrNameLookupArray;
\r
995 private static TagInformation[] _tagNameLookupArray;
\r
996 private TagStackEntry[] _endTags;
\r
997 private RenderStyle[] _styleList;
\r
1001 struct AttributeInformation {
\r
1002 public bool encode;
\r
1003 public string name;
\r
1005 public AttributeInformation(string name, bool encode){
\r
1006 this.encode = encode;
\r
1011 struct RenderAttribute {
\r
1012 public bool encode;
\r
1013 public HtmlTextWriterAttribute key;
\r
1014 public string name;
\r
1015 public string value;
\r
1018 struct RenderStyle {
\r
1019 public HtmlTextWriterStyle key;
\r
1020 public string name;
\r
1021 public string value;
\r
1024 struct TagInformation {
\r
1025 public string closingTag;
\r
1026 public string name;
\r
1027 public TagType tagType;
\r
1029 public TagInformation(string name, TagType tagType, string closingTag){
\r
1031 this.tagType = tagType;
\r
1032 this.closingTag = closingTag;
\r
1036 struct TagStackEntry {
\r
1037 public string endTagText;
\r
1038 public HtmlTextWriterTag tagKey;
\r
1048 } // namespace System.Web.UI.HtmlControls
\r