1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
//------------------------------------------------------------------------------
// <copyright file="ChtmlTextWriter.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
// ChtmlTextWriter.cs
//
namespace System.Web.UI {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.Util;
using System.Globalization;
using System.Security.Permissions;
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public class ChtmlTextWriter : Html32TextWriter {
private Hashtable _recognizedAttributes = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
private Hashtable _suppressedAttributes = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
private Hashtable _globalSuppressedAttributes = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
public ChtmlTextWriter(TextWriter writer) : this(writer, DefaultTabString) {
}
public ChtmlTextWriter(TextWriter writer, string tabString) : base(writer, tabString) {
_globalSuppressedAttributes["onclick"] = true;
_globalSuppressedAttributes["ondblclick"] = true;
_globalSuppressedAttributes["onmousedown"] = true;
_globalSuppressedAttributes["onmouseup"] = true;
_globalSuppressedAttributes["onmouseover"] = true;
_globalSuppressedAttributes["onmousemove"] = true;
_globalSuppressedAttributes["onmouseout"] = true;
_globalSuppressedAttributes["onkeypress"] = true;
_globalSuppressedAttributes["onkeydown"] = true;
_globalSuppressedAttributes["onkeyup"] = true;
// Supress certain element attribute pairs that can happen when Html32TextWriter performs the div table
// substitution.
RemoveRecognizedAttributeInternal("div", "accesskey"); // VSWhidbey 270254
RemoveRecognizedAttributeInternal("div", "cellspacing");
RemoveRecognizedAttributeInternal("div", "cellpadding");
RemoveRecognizedAttributeInternal("div", "gridlines");
RemoveRecognizedAttributeInternal("div", "rules");
RemoveRecognizedAttributeInternal("span", "cellspacing");
RemoveRecognizedAttributeInternal("span", "cellpadding");
RemoveRecognizedAttributeInternal("span", "gridlines");
RemoveRecognizedAttributeInternal("span", "rules");
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public virtual void AddRecognizedAttribute(string elementName, string attributeName) {
Hashtable eltAttributes = (Hashtable) _recognizedAttributes[elementName];
if (eltAttributes == null) {
eltAttributes = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
_recognizedAttributes[elementName] = eltAttributes;
}
eltAttributes.Add(attributeName, true);
}
/// <devdoc>
/// Override to filter out unnecessary attributes
/// </devdoc>
protected override bool OnAttributeRender(string name, string value, HtmlTextWriterAttribute key) {
Hashtable elementRecognizedAttributes = (Hashtable)_recognizedAttributes[TagName];
if (elementRecognizedAttributes != null && elementRecognizedAttributes[name] != null) {
return true;
}
if (_globalSuppressedAttributes[name] != null) {
return false;
}
Hashtable elementSuppressedAttributes = (Hashtable)_suppressedAttributes[TagName];
if (elementSuppressedAttributes != null && elementSuppressedAttributes[name] != null) {
return false;
}
return true;
}
protected override bool OnStyleAttributeRender(string name,string value, HtmlTextWriterStyle key) {
if (key == HtmlTextWriterStyle.TextDecoration) {
if (StringUtil.EqualsIgnoreCase("line-through", value)) {
return false;
}
}
return base.OnStyleAttributeRender(name, value, key);
}
protected override bool OnTagRender(string name, HtmlTextWriterTag key) {
return base.OnTagRender(name, key) && key != HtmlTextWriterTag.Span;
}
/// <devdoc>
/// <para>[To be supplied.]</para>
/// </devdoc>
public virtual void RemoveRecognizedAttribute(string elementName, string attributeName) {
RemoveRecognizedAttributeInternal(elementName, attributeName);
}
private void RemoveRecognizedAttributeInternal(string elementName, string attributeName) {
// Since recognized attributes have precedence, we need to do two operations: add this attribute
// to the suppressed list, and remove it from the recognized list.
Hashtable eltAttributes = (Hashtable) _suppressedAttributes[elementName];
if (eltAttributes == null) {
eltAttributes = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
_suppressedAttributes[elementName] = eltAttributes;
}
eltAttributes.Add(attributeName, true);
eltAttributes = (Hashtable)_recognizedAttributes[elementName];
if (eltAttributes == null) {
eltAttributes = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
_recognizedAttributes[elementName] = eltAttributes;
}
// Note: Hashtable::Remove silently continues if the key does not exist.
eltAttributes.Remove(attributeName);
}
public override void WriteBreak() {
Write("<br>");
}
public override void WriteEncodedText(String text) {
if (null == text || text.Length == 0) {
return;
}
int length = text.Length;
int start = -1;
for(int pos = 0; pos < length; pos++) {
int ch = text[pos];
if(ch > 160 && ch < 256) {
if(start != -1) {
base.WriteEncodedText(text.Substring(start, pos - start));
start = -1;
}
base.Write(text[pos]);
}
else {
if(start == -1) {
start = pos;
}
}
}
if(start != -1) {
if(start == 0) {
base.WriteEncodedText(text);
}
else {
base.WriteEncodedText(text.Substring(start, length - start));
}
}
}
protected Hashtable RecognizedAttributes {
get {
return _recognizedAttributes;
}
}
protected Hashtable SuppressedAttributes {
get {
return _suppressedAttributes;
}
}
protected Hashtable GlobalSuppressedAttributes {
get {
return _globalSuppressedAttributes;
}
}
}
}
|