HtmlTextWriter.cs 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /* System.Web.UI
  2. * Authors
  3. * Leen Toelen ([email protected])
  4. */
  5. using System;
  6. using System.IO;
  7. using System.Collections;
  8. using System.Web;
  9. using System.Web.UI;
  10. namespace System.Web.UI{
  11. public class HtmlTextWriter : System.IO.TextWriter {
  12. static HtmlTextWriter(){
  13. HtmlTextWriter._tagKeyLookupTable = new Hashtable(97);
  14. HtmlTextWriter._tagNameLookupArray = new TagInformation[97];
  15. HtmlTextWriter.RegisterTag("", HtmlTextWriterTag.Unknown, TagType.Other);
  16. HtmlTextWriter.RegisterTag("a", HtmlTextWriterTag.A, TagType.Inline);
  17. HtmlTextWriter.RegisterTag("acronym", HtmlTextWriterTag.Acronym, TagType.Inline);
  18. HtmlTextWriter.RegisterTag("address", HtmlTextWriterTag.Address, TagType.Other);
  19. HtmlTextWriter.RegisterTag("area", HtmlTextWriterTag.Area, TagType.Other);
  20. HtmlTextWriter.RegisterTag("b", HtmlTextWriterTag.B, TagType.Inline);
  21. HtmlTextWriter.RegisterTag("base", HtmlTextWriterTag.Base, TagType.NonClosing);
  22. HtmlTextWriter.RegisterTag("basefont", HtmlTextWriterTag.Basefont, TagType.NonClosing);
  23. HtmlTextWriter.RegisterTag("bdo", HtmlTextWriterTag.Bdo, TagType.Inline);
  24. HtmlTextWriter.RegisterTag("bgsound", HtmlTextWriterTag.Bgsound, TagType.NonClosing);
  25. HtmlTextWriter.RegisterTag("big", HtmlTextWriterTag.Big, TagType.Inline);
  26. HtmlTextWriter.RegisterTag("blockquote", HtmlTextWriterTag.Blockquote, TagType.Other);
  27. HtmlTextWriter.RegisterTag("body", HtmlTextWriterTag.Body, TagType.Other);
  28. HtmlTextWriter.RegisterTag("br", HtmlTextWriterTag.Br, TagType.Other);
  29. HtmlTextWriter.RegisterTag("button", HtmlTextWriterTag.Button, TagType.Inline);
  30. HtmlTextWriter.RegisterTag("caption", HtmlTextWriterTag.Caption, TagType.Other);
  31. HtmlTextWriter.RegisterTag("center", HtmlTextWriterTag.Center, TagType.Other);
  32. HtmlTextWriter.RegisterTag("cite", HtmlTextWriterTag.Cite, TagType.Inline);
  33. HtmlTextWriter.RegisterTag("code", HtmlTextWriterTag.Code, TagType.Inline);
  34. HtmlTextWriter.RegisterTag("col", HtmlTextWriterTag.Col, TagType.NonClosing);
  35. HtmlTextWriter.RegisterTag("colgroup", HtmlTextWriterTag.Colgroup, TagType.Other);
  36. HtmlTextWriter.RegisterTag("del", HtmlTextWriterTag.Del, TagType.Inline);
  37. HtmlTextWriter.RegisterTag("dd", HtmlTextWriterTag.Dd, TagType.Inline);
  38. HtmlTextWriter.RegisterTag("dfn", HtmlTextWriterTag.Dfn, TagType.Inline);
  39. HtmlTextWriter.RegisterTag("dir", HtmlTextWriterTag.Dir, TagType.Other);
  40. HtmlTextWriter.RegisterTag("div", HtmlTextWriterTag.Div, TagType.Other);
  41. HtmlTextWriter.RegisterTag("dl", HtmlTextWriterTag.Dl, TagType.Other);
  42. HtmlTextWriter.RegisterTag("dt", HtmlTextWriterTag.Dt, TagType.Inline);
  43. HtmlTextWriter.RegisterTag("em", HtmlTextWriterTag.Em, TagType.Inline);
  44. HtmlTextWriter.RegisterTag("embed", HtmlTextWriterTag.Embed, TagType.NonClosing);
  45. HtmlTextWriter.RegisterTag("fieldset", HtmlTextWriterTag.Fieldset, TagType.Other);
  46. HtmlTextWriter.RegisterTag("font", HtmlTextWriterTag.Font, TagType.Inline);
  47. HtmlTextWriter.RegisterTag("form", HtmlTextWriterTag.Form, TagType.Other);
  48. HtmlTextWriter.RegisterTag("frame", HtmlTextWriterTag.Frame, TagType.NonClosing);
  49. HtmlTextWriter.RegisterTag("frameset", HtmlTextWriterTag.Frameset, TagType.Other);
  50. HtmlTextWriter.RegisterTag("h1", HtmlTextWriterTag.H1, TagType.Other);
  51. HtmlTextWriter.RegisterTag("h2", HtmlTextWriterTag.H2, TagType.Other);
  52. HtmlTextWriter.RegisterTag("h3", HtmlTextWriterTag.H3, TagType.Other);
  53. HtmlTextWriter.RegisterTag("h4", HtmlTextWriterTag.H4, TagType.Other);
  54. HtmlTextWriter.RegisterTag("h5", HtmlTextWriterTag.H5, TagType.Other);
  55. HtmlTextWriter.RegisterTag("h6", HtmlTextWriterTag.H6, TagType.Other);
  56. HtmlTextWriter.RegisterTag("head", HtmlTextWriterTag.Head, TagType.Other);
  57. HtmlTextWriter.RegisterTag("hr", HtmlTextWriterTag.Hr, TagType.NonClosing);
  58. HtmlTextWriter.RegisterTag("html", HtmlTextWriterTag.Html, TagType.Other);
  59. HtmlTextWriter.RegisterTag("i", HtmlTextWriterTag.I, TagType.Inline);
  60. HtmlTextWriter.RegisterTag("iframe", HtmlTextWriterTag.Iframe, TagType.Other);
  61. HtmlTextWriter.RegisterTag("img", HtmlTextWriterTag.Img, TagType.NonClosing);
  62. HtmlTextWriter.RegisterTag("input", HtmlTextWriterTag.Input, TagType.NonClosing);
  63. HtmlTextWriter.RegisterTag("ins", HtmlTextWriterTag.Ins, TagType.Inline);
  64. HtmlTextWriter.RegisterTag("isindex", HtmlTextWriterTag.Isindex, TagType.NonClosing);
  65. HtmlTextWriter.RegisterTag("kbd", HtmlTextWriterTag.Kbd, TagType.Inline);
  66. HtmlTextWriter.RegisterTag("label", HtmlTextWriterTag.Label, TagType.Inline);
  67. HtmlTextWriter.RegisterTag("legend", HtmlTextWriterTag.Legend, TagType.Other);
  68. HtmlTextWriter.RegisterTag("li", HtmlTextWriterTag.Li, TagType.Inline);
  69. HtmlTextWriter.RegisterTag("link", HtmlTextWriterTag.Link, TagType.NonClosing);
  70. HtmlTextWriter.RegisterTag("map", HtmlTextWriterTag.Map, TagType.Other);
  71. HtmlTextWriter.RegisterTag("marquee", HtmlTextWriterTag.Marquee, TagType.Other);
  72. HtmlTextWriter.RegisterTag("menu", HtmlTextWriterTag.Menu, TagType.Other);
  73. HtmlTextWriter.RegisterTag("meta", HtmlTextWriterTag.Meta, TagType.NonClosing);
  74. HtmlTextWriter.RegisterTag("nobr", HtmlTextWriterTag.Nobr, TagType.Inline);
  75. HtmlTextWriter.RegisterTag("noframes", HtmlTextWriterTag.Noframes, TagType.Other);
  76. HtmlTextWriter.RegisterTag("noscript", HtmlTextWriterTag.Noscript, TagType.Other);
  77. HtmlTextWriter.RegisterTag("object", HtmlTextWriterTag.Object, TagType.Other);
  78. HtmlTextWriter.RegisterTag("ol", HtmlTextWriterTag.Ol, TagType.Other);
  79. HtmlTextWriter.RegisterTag("option", HtmlTextWriterTag.Option, TagType.Other);
  80. HtmlTextWriter.RegisterTag("p", HtmlTextWriterTag.P, TagType.Inline);
  81. HtmlTextWriter.RegisterTag("param", HtmlTextWriterTag.Param, TagType.Other);
  82. HtmlTextWriter.RegisterTag("pre", HtmlTextWriterTag.Pre, TagType.Other);
  83. HtmlTextWriter.RegisterTag("q", HtmlTextWriterTag.Q, TagType.Inline);
  84. HtmlTextWriter.RegisterTag("rt", HtmlTextWriterTag.Rt, TagType.Other);
  85. HtmlTextWriter.RegisterTag("ruby", HtmlTextWriterTag.Ruby, TagType.Other);
  86. HtmlTextWriter.RegisterTag("s", HtmlTextWriterTag.S, TagType.Inline);
  87. HtmlTextWriter.RegisterTag("samp", HtmlTextWriterTag.Samp, TagType.Inline);
  88. HtmlTextWriter.RegisterTag("script", HtmlTextWriterTag.Script, TagType.Other);
  89. HtmlTextWriter.RegisterTag("select", HtmlTextWriterTag.Select, TagType.Other);
  90. HtmlTextWriter.RegisterTag("small", HtmlTextWriterTag.Small, TagType.Other);
  91. HtmlTextWriter.RegisterTag("span", HtmlTextWriterTag.Span, TagType.Inline);
  92. HtmlTextWriter.RegisterTag("strike", HtmlTextWriterTag.Strike, TagType.Inline);
  93. HtmlTextWriter.RegisterTag("strong", HtmlTextWriterTag.Strong, TagType.Inline);
  94. HtmlTextWriter.RegisterTag("style", HtmlTextWriterTag.Style, TagType.Other);
  95. HtmlTextWriter.RegisterTag("sub", HtmlTextWriterTag.Sub, TagType.Inline);
  96. HtmlTextWriter.RegisterTag("sup", HtmlTextWriterTag.Sup, TagType.Inline);
  97. HtmlTextWriter.RegisterTag("table", HtmlTextWriterTag.Table, TagType.Other);
  98. HtmlTextWriter.RegisterTag("tbody", HtmlTextWriterTag.Tbody, TagType.Other);
  99. HtmlTextWriter.RegisterTag("td", HtmlTextWriterTag.Td, TagType.Inline);
  100. HtmlTextWriter.RegisterTag("textarea", HtmlTextWriterTag.Textarea, TagType.Inline);
  101. HtmlTextWriter.RegisterTag("tfoot", HtmlTextWriterTag.Tfoot, TagType.Other);
  102. HtmlTextWriter.RegisterTag("th", HtmlTextWriterTag.Th, TagType.Inline);
  103. HtmlTextWriter.RegisterTag("thead", HtmlTextWriterTag.Thead, TagType.Other);
  104. HtmlTextWriter.RegisterTag("title", HtmlTextWriterTag.Title, TagType.Other);
  105. HtmlTextWriter.RegisterTag("tr", HtmlTextWriterTag.Tr, TagType.Other);
  106. HtmlTextWriter.RegisterTag("tt", HtmlTextWriterTag.Tt, TagType.Inline);
  107. HtmlTextWriter.RegisterTag("u", HtmlTextWriterTag.U, TagType.Inline);
  108. HtmlTextWriter.RegisterTag("ul", HtmlTextWriterTag.Ul, TagType.Other);
  109. HtmlTextWriter.RegisterTag("var", HtmlTextWriterTag.Var, TagType.Inline);
  110. HtmlTextWriter.RegisterTag("wbr", HtmlTextWriterTag.Wbr, TagType.NonClosing);
  111. HtmlTextWriter.RegisterTag("xml", HtmlTextWriterTag.Xml, TagType.Other);
  112. HtmlTextWriter._attrKeyLookupTable = new Hashtable(40);
  113. HtmlTextWriter._attrNameLookupArray = new AttributeInformation[40];
  114. HtmlTextWriter.RegisterAttribute("accesskey", HtmlTextWriterAttribute.Accesskey, true);
  115. HtmlTextWriter.RegisterAttribute("align", HtmlTextWriterAttribute.Align, false);
  116. HtmlTextWriter.RegisterAttribute("alt", HtmlTextWriterAttribute.Alt, true);
  117. HtmlTextWriter.RegisterAttribute("background", HtmlTextWriterAttribute.Background, true);
  118. HtmlTextWriter.RegisterAttribute("bgcolor", HtmlTextWriterAttribute.Bgcolor, false);
  119. HtmlTextWriter.RegisterAttribute("border", HtmlTextWriterAttribute.Border, false);
  120. HtmlTextWriter.RegisterAttribute("bordercolor", HtmlTextWriterAttribute.Bordercolor, false);
  121. HtmlTextWriter.RegisterAttribute("cellpadding", HtmlTextWriterAttribute.Cellpadding, false);
  122. HtmlTextWriter.RegisterAttribute("cellspacing", HtmlTextWriterAttribute.Cellspacing, false);
  123. HtmlTextWriter.RegisterAttribute("checked", HtmlTextWriterAttribute.Checked, false);
  124. HtmlTextWriter.RegisterAttribute("class", HtmlTextWriterAttribute.Class, true);
  125. HtmlTextWriter.RegisterAttribute("cols", HtmlTextWriterAttribute.Cols, false);
  126. HtmlTextWriter.RegisterAttribute("colspan", HtmlTextWriterAttribute.Colspan, false);
  127. HtmlTextWriter.RegisterAttribute("disabled", HtmlTextWriterAttribute.Disabled, false);
  128. HtmlTextWriter.RegisterAttribute("for", HtmlTextWriterAttribute.For, false);
  129. HtmlTextWriter.RegisterAttribute("height", HtmlTextWriterAttribute.Height, false);
  130. HtmlTextWriter.RegisterAttribute("href", HtmlTextWriterAttribute.Href, true);
  131. HtmlTextWriter.RegisterAttribute("id", HtmlTextWriterAttribute.Id, false);
  132. HtmlTextWriter.RegisterAttribute("maxlength", HtmlTextWriterAttribute.Maxlength, false);
  133. HtmlTextWriter.RegisterAttribute("multiple", HtmlTextWriterAttribute.Multiple, false);
  134. HtmlTextWriter.RegisterAttribute("name", HtmlTextWriterAttribute.Name, false);
  135. HtmlTextWriter.RegisterAttribute("nowrap", HtmlTextWriterAttribute.Nowrap, false);
  136. HtmlTextWriter.RegisterAttribute("onchange", HtmlTextWriterAttribute.Onchange, true);
  137. HtmlTextWriter.RegisterAttribute("onclick", HtmlTextWriterAttribute.Onclick, true);
  138. HtmlTextWriter.RegisterAttribute("readonly", HtmlTextWriterAttribute.ReadOnly, false);
  139. HtmlTextWriter.RegisterAttribute("rows", HtmlTextWriterAttribute.Rows, false);
  140. HtmlTextWriter.RegisterAttribute("rowspan", HtmlTextWriterAttribute.Rowspan, false);
  141. HtmlTextWriter.RegisterAttribute("rules", HtmlTextWriterAttribute.Rules, false);
  142. HtmlTextWriter.RegisterAttribute("selected", HtmlTextWriterAttribute.Selected, false);
  143. HtmlTextWriter.RegisterAttribute("size", HtmlTextWriterAttribute.Size, false);
  144. HtmlTextWriter.RegisterAttribute("src", HtmlTextWriterAttribute.Src, true);
  145. HtmlTextWriter.RegisterAttribute("style", HtmlTextWriterAttribute.Style, false);
  146. HtmlTextWriter.RegisterAttribute("tabindex", HtmlTextWriterAttribute.Tabindex, false);
  147. HtmlTextWriter.RegisterAttribute("target", HtmlTextWriterAttribute.Target, false);
  148. HtmlTextWriter.RegisterAttribute("title", HtmlTextWriterAttribute.Title, true);
  149. HtmlTextWriter.RegisterAttribute("type", HtmlTextWriterAttribute.Type, false);
  150. HtmlTextWriter.RegisterAttribute("valign", HtmlTextWriterAttribute.Valign, false);
  151. HtmlTextWriter.RegisterAttribute("value", HtmlTextWriterAttribute.Value, true);
  152. HtmlTextWriter.RegisterAttribute("width", HtmlTextWriterAttribute.Width, false);
  153. HtmlTextWriter.RegisterAttribute("wrap", HtmlTextWriterAttribute.Wrap, false);
  154. HtmlTextWriter._styleKeyLookupTable = new Hashtable(14);
  155. HtmlTextWriter._styleNameLookupArray = new String[14];
  156. HtmlTextWriter.RegisterStyle("background-color", HtmlTextWriterStyle.BackgroundColor);
  157. HtmlTextWriter.RegisterStyle("background-image", HtmlTextWriterStyle.BackgroundImage);
  158. HtmlTextWriter.RegisterStyle("border-collapse", HtmlTextWriterStyle.BorderCollapse);
  159. HtmlTextWriter.RegisterStyle("border-color", HtmlTextWriterStyle.BorderColor);
  160. HtmlTextWriter.RegisterStyle("border-style", HtmlTextWriterStyle.BorderStyle);
  161. HtmlTextWriter.RegisterStyle("border-width", HtmlTextWriterStyle.BorderWidth);
  162. HtmlTextWriter.RegisterStyle("color", HtmlTextWriterStyle.Color);
  163. HtmlTextWriter.RegisterStyle("font-family", HtmlTextWriterStyle.FontFamily);
  164. HtmlTextWriter.RegisterStyle("font-size", HtmlTextWriterStyle.FontSize);
  165. HtmlTextWriter.RegisterStyle("font-style", HtmlTextWriterStyle.FontStyle);
  166. HtmlTextWriter.RegisterStyle("font-weight", HtmlTextWriterStyle.FontWeight);
  167. HtmlTextWriter.RegisterStyle("height", HtmlTextWriterStyle.Height);
  168. HtmlTextWriter.RegisterStyle("text-decoration", HtmlTextWriterStyle.TextDecoration);
  169. HtmlTextWriter.RegisterStyle("width", HtmlTextWriterStyle.Width);
  170. }
  171. public HtmlTextWriter(TextWriter writer):this(writer, " "){}
  172. public HtmlTextWriter(TextWriter writer, string tabString) : base() {
  173. this.writer = writer;
  174. this.tabString = tabString;
  175. indentLevel = 0;
  176. tabsPending = false;
  177. _httpWriter = writer as HttpWriter;
  178. _isDescendant = GetType() == typeof(HtmlTextWriter) == false;
  179. _attrList = new RenderAttribute[20];
  180. _attrCount = 0;
  181. _styleList = new RenderStyle[20];
  182. _styleCount = 0;
  183. _endTags = new TagStackEntry[16];
  184. _endTagCount = 0;
  185. _inlineCount = 0;
  186. }
  187. public virtual void AddAttribute(HtmlTextWriterAttribute key, string value){
  188. if ((int) key >= 0 && (int) key < HtmlTextWriter._attrNameLookupArray.Length) {
  189. AttributeInformation attrInfo = HtmlTextWriter._attrNameLookupArray[(int) key];
  190. AddAttribute(attrInfo.name, value, key, attrInfo.encode);
  191. }
  192. }
  193. public virtual void AddAttribute(HtmlTextWriterAttribute key, string value, bool fEncode){
  194. if ((int) key >= 0 && (int) key < HtmlTextWriter._attrNameLookupArray.Length) {
  195. AddAttribute(HtmlTextWriter._attrNameLookupArray[(int) key].name, value, key, fEncode);
  196. }
  197. }
  198. public virtual void AddAttribute(string name, string value){
  199. HtmlTextWriterAttribute attr = GetAttributeKey(name);
  200. value = EncodeAttributeValue(GetAttributeKey(name), value);
  201. AddAttribute(name, value, attr);
  202. }
  203. public virtual void AddAttribute(string name, string value, bool fEndode){
  204. value = EncodeAttributeValue(value, fEndode);
  205. AddAttribute(name, value, GetAttributeKey(name));
  206. }
  207. protected virtual void AddAttribute(string name, string value, HtmlTextWriterAttribute key){
  208. AddAttribute(name, value, key, false);
  209. }
  210. private void AddAttribute(string name, string value, HtmlTextWriterAttribute key, bool encode){
  211. if (_attrCount >= (int) _attrList.Length) {
  212. RenderAttribute[] rAttrArr = new RenderAttribute[_attrList.Length * 2];
  213. System.Array.Copy(_attrList, rAttrArr, (int) _attrList.Length);
  214. _attrList = rAttrArr;
  215. }
  216. RenderAttribute rAttr;
  217. rAttr.name = name;
  218. rAttr.value = value;
  219. rAttr.key = key;
  220. rAttr.encode = encode;
  221. _attrList [_attrCount++] = rAttr;
  222. }
  223. public virtual void AddStyleAttribute(HtmlTextWriterStyle key, string value){
  224. AddStyleAttribute(GetStyleName(key), value, key);
  225. }
  226. public virtual void AddStyleAttribute(string name, string value){
  227. AddStyleAttribute(name, value, GetStyleKey(name));
  228. }
  229. protected virtual void AddStyleAttribute(string name, string value, HtmlTextWriterStyle key){
  230. RenderStyle[] tagRender;
  231. RenderStyle beforeTag;
  232. if (_styleCount >= (int) _styleList.Length) {
  233. RenderStyle[] rAttrArr = new RenderStyle[_styleList.Length * 2];
  234. System.Array.Copy(_styleList, rAttrArr, (int) _styleList.Length);
  235. _styleList = rAttrArr;
  236. }
  237. RenderStyle rAttr;
  238. rAttr.name = name;
  239. rAttr.value = value;
  240. rAttr.key = key;
  241. _styleList [_styleCount++] = rAttr;
  242. }
  243. public override void Close(){
  244. writer.Close();
  245. }
  246. protected virtual string EncodeAttributeValue(HtmlTextWriterAttribute attrKey, string value){
  247. bool valid = true;
  248. if (0 <= (int) attrKey && (int) attrKey < HtmlTextWriter._attrNameLookupArray.Length)
  249. valid = HtmlTextWriter._attrNameLookupArray[(int) attrKey].encode;
  250. return EncodeAttributeValue(value, valid);
  251. }
  252. protected string EncodeAttributeValue(string value, bool fEncode){
  253. if (value == null)
  254. return null;
  255. if (!(fEncode))
  256. return value;
  257. return System.Web.HttpUtility.HtmlAttributeEncode(value);
  258. }
  259. protected string EncodeUrl(string url){
  260. if (url.IndexOf(SpaceChar) < 0)
  261. return url;
  262. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  263. for(int i=0; i <= url.Length; i++){
  264. char temp = url[i];
  265. if (temp != 32)
  266. sb.Append(temp);
  267. else
  268. sb.Append("%20");
  269. }
  270. return sb.ToString();
  271. }
  272. protected virtual void FilterAttributes(){
  273. int count = 0;
  274. for(int i=0; i < _styleCount; i++){
  275. RenderStyle rStyle = _styleList[i];
  276. if (OnStyleAttributeRender(rStyle.name, rStyle.value, rStyle.key)) {
  277. count++;
  278. }
  279. }
  280. _styleCount = count;
  281. count = 0;
  282. for(int i=0; i <= _attrCount; i++){
  283. RenderAttribute rAttr = _attrList[i];
  284. if (OnAttributeRender(rAttr.name, rAttr.value, rAttr.key)) {
  285. count++;
  286. }
  287. }
  288. _attrCount = count;
  289. }
  290. public override void Flush(){
  291. writer.Flush();
  292. }
  293. protected HtmlTextWriterAttribute GetAttributeKey(string attrName){
  294. if (attrName != null && attrName.Length > 0) {
  295. object attr = HtmlTextWriter._attrKeyLookupTable[attrName.ToLower()];
  296. if (attr != null)
  297. return (HtmlTextWriterAttribute) attr;
  298. }
  299. return (HtmlTextWriterAttribute) (-1);
  300. }
  301. protected string GetAttributeName(HtmlTextWriterAttribute attrKey){
  302. if ((int) attrKey >= 0 && (int) attrKey < HtmlTextWriter._attrNameLookupArray.Length)
  303. return HtmlTextWriter._attrNameLookupArray[(int) attrKey].name;
  304. return System.String.Empty;
  305. }
  306. protected HtmlTextWriterStyle GetStyleKey(string styleName){
  307. if (styleName != null && styleName.Length > 0) {
  308. object style = HtmlTextWriter._styleKeyLookupTable[styleName.ToLower()];
  309. if (style != null)
  310. return (HtmlTextWriterStyle) style;
  311. }
  312. return (HtmlTextWriterStyle) (-1);
  313. }
  314. protected string GetStyleName(HtmlTextWriterStyle styleKey){
  315. if ((int) styleKey >= 0 && (int) styleKey < HtmlTextWriter._styleNameLookupArray.Length)
  316. return HtmlTextWriter._styleNameLookupArray[(int) styleKey];
  317. return System.String.Empty;
  318. }
  319. protected virtual HtmlTextWriterTag GetTagKey(string tagName){
  320. if (tagName != null && tagName.Length > 0) {
  321. object tag = HtmlTextWriter._tagKeyLookupTable[tagName.ToLower()];
  322. if (tag != null)
  323. return (HtmlTextWriterTag) tag;
  324. }
  325. return 0;
  326. }
  327. protected virtual string GetTagName(HtmlTextWriterTag tagKey){
  328. if ((int) tagKey >= 0 && (int) tagKey < HtmlTextWriter._tagNameLookupArray.Length)
  329. return HtmlTextWriter._tagNameLookupArray[(int) tagKey].name;
  330. return System.String.Empty;
  331. }
  332. protected bool IsAttributeDefined(HtmlTextWriterAttribute key){
  333. for (int i=0; i < _attrCount; i++) {
  334. if (_attrList[i].key == key)
  335. return true;
  336. }
  337. return false;
  338. }
  339. protected bool IsAttributeDefined(HtmlTextWriterAttribute key, ref string value){
  340. value = null;
  341. for (int i=0; i < _attrCount; i++) {
  342. if (_attrList[i].key == key) {
  343. value = _attrList[i].value;
  344. return true;
  345. }
  346. }
  347. return false;
  348. }
  349. protected bool IsStyleAttributeDefined(HtmlTextWriterStyle key){
  350. for (int i= 0; i < _styleCount; i++) {
  351. if (_styleList[i].key == key)
  352. return true;
  353. }
  354. return false;
  355. }
  356. protected bool IsStyleAttributeDefined(HtmlTextWriterStyle key, ref string value){
  357. value = null;
  358. for( int i=0; i < _styleCount; i++) {
  359. if (_styleList[i].key == key) {
  360. value = _styleList[i].value;
  361. return true;
  362. }
  363. }
  364. return false;
  365. }
  366. protected virtual bool OnAttributeRender(string name, string value, HtmlTextWriterAttribute key){
  367. return true;
  368. }
  369. protected virtual bool OnStyleAttributeRender(string name, string value, HtmlTextWriterStyle key){
  370. return true;
  371. }
  372. protected virtual bool OnTagRender(string name, HtmlTextWriterTag key){
  373. return true;
  374. }
  375. protected virtual void OutputTabs(){
  376. if (tabsPending) {
  377. for(int i=0; i <= indentLevel; i++) {
  378. writer.Write(tabString);
  379. }
  380. tabsPending = false;
  381. }
  382. }
  383. protected string PopEndTag(){
  384. if (_endTagCount <= 0)
  385. throw new InvalidOperationException("A PopEndTag was called without a corresponding PushEndTag");
  386. _endTagCount--;
  387. TagKey = _endTags[_endTagCount].tagKey;
  388. return _endTags[_endTagCount].endTagText;
  389. }
  390. protected void PushEndTag(string endTag){
  391. if (_endTagCount >= (int) _endTags.Length) {
  392. TagStackEntry[] temp = new TagStackEntry[(int) _endTags.Length * 2];
  393. System.Array.Copy(_endTags, temp, (int) _endTags.Length);
  394. _endTags = temp;
  395. }
  396. _endTags[_endTagCount].tagKey = _tagKey;
  397. _endTags[_endTagCount].endTagText = endTag;
  398. _endTagCount++;
  399. }
  400. protected static void RegisterAttribute(string name, HtmlTextWriterAttribute key){
  401. HtmlTextWriter.RegisterAttribute(name, key, false);
  402. }
  403. private static void RegisterAttribute(string name, HtmlTextWriterAttribute key, bool fEncode){
  404. name = name.ToLower();
  405. HtmlTextWriter._attrKeyLookupTable.Add(name, key);
  406. if ((int) key < (int) HtmlTextWriter._attrNameLookupArray.Length)
  407. HtmlTextWriter._attrNameLookupArray[(int) key] = new AttributeInformation(name, fEncode);
  408. }
  409. protected static void RegisterStyle(string name, HtmlTextWriterStyle key){
  410. name = name.ToLower();
  411. HtmlTextWriter._styleKeyLookupTable.Add(name, key);
  412. if ((int) key < (int) HtmlTextWriter._styleNameLookupArray.Length)
  413. HtmlTextWriter._styleNameLookupArray[(int) key] = name;
  414. }
  415. protected static void RegisterTag(string name, HtmlTextWriterTag key){
  416. HtmlTextWriter.RegisterTag(name, key, TagType.Other);
  417. }
  418. private static void RegisterTag(string name, HtmlTextWriterTag key, TagType type){
  419. name = name.ToLower();
  420. HtmlTextWriter._tagKeyLookupTable.Add(name, key);
  421. string fullTag = null;
  422. if ((int) type != 1 && (int) key != 0) {
  423. fullTag = EndTagLeftChars + name + TagRightChar;
  424. }
  425. if ((int) key < HtmlTextWriter._tagNameLookupArray.Length)
  426. HtmlTextWriter._tagNameLookupArray[(int) key] = new TagInformation(name, type, fullTag);
  427. }
  428. protected virtual string RenderAfterContent(){
  429. return null;
  430. }
  431. protected virtual string RenderAfterTag(){
  432. return null;
  433. }
  434. protected virtual string RenderBeforeContent(){
  435. return null;
  436. }
  437. protected virtual string RenderBeforeTag(){
  438. return null;
  439. }
  440. public virtual void RenderBeginTag(HtmlTextWriterTag tagKey){
  441. TagKey = tagKey;
  442. bool tagRendered = true;
  443. bool tagRender = true;
  444. if (_isDescendant) {
  445. tagRender = OnTagRender(_tagName, _tagKey);
  446. FilterAttributes();
  447. string beforeTag = RenderBeforeTag();
  448. if (beforeTag != null) {
  449. if (tabsPending)
  450. OutputTabs();
  451. writer.Write(beforeTag);
  452. }
  453. }
  454. TagInformation currentTag = HtmlTextWriter._tagNameLookupArray[_tagIndex];
  455. if (tagRender) {
  456. tagRendered = false;
  457. if (tabsPending)
  458. OutputTabs();
  459. writer.Write(TagLeftChar);
  460. writer.Write(_tagName);
  461. RenderAttribute rAttr;
  462. string rAttrValue = null;
  463. for (int i=0; i < _attrCount; i++) {
  464. rAttr = _attrList[i];
  465. if (rAttr.key == HtmlTextWriterAttribute.Style)
  466. rAttrValue = rAttr.value;
  467. else {
  468. writer.Write(SpaceChar);
  469. writer.Write(rAttr.name);
  470. if (rAttr.value != null) {
  471. writer.Write(EqualsChar);
  472. writer.Write(DoubleQuoteChar);
  473. if (rAttr.encode) {
  474. if (_httpWriter == null) {
  475. System.Web.HttpUtility.HtmlAttributeEncode(rAttr.value, writer);
  476. }
  477. else {
  478. System.Web.HttpUtility.HtmlAttributeEncode(rAttr.value, (TextWriter) _httpWriter);
  479. }
  480. }
  481. else {
  482. writer.Write(rAttr.value);
  483. }
  484. writer.Write(DoubleQuoteChar);
  485. }
  486. }
  487. }
  488. if (_styleCount > 0 || rAttrValue != null) {
  489. writer.Write(SpaceChar);
  490. writer.Write("style");
  491. writer.Write(EqualsChar);
  492. writer.Write(DoubleQuoteChar);
  493. RenderStyle rStyle;
  494. for (int i=0; i < _styleCount; i++) {
  495. rStyle = _styleList[i];
  496. writer.Write(rStyle.name);
  497. writer.Write(StyleEqualsChar);
  498. writer.Write(rStyle.value);
  499. writer.Write(SemicolonChar);
  500. }
  501. if (rAttrValue != null)
  502. writer.Write(rAttrValue);
  503. writer.Write(DoubleQuoteChar);
  504. }
  505. if (currentTag.tagType == TagType.NonClosing) {
  506. writer.Write(SpaceChar);
  507. writer.Write(SlashChar);
  508. writer.Write(TagRightChar);
  509. }
  510. else
  511. writer.Write(TagRightChar);
  512. }
  513. string beforeContent = RenderBeforeContent();
  514. if (beforeContent != null) {
  515. if (tabsPending)
  516. OutputTabs();
  517. writer.Write(beforeContent);
  518. }
  519. if (tagRendered) {
  520. if (currentTag.tagType == TagType.Inline)
  521. _inlineCount++;
  522. else {
  523. WriteLine();
  524. Indent++;
  525. }
  526. if (currentTag.closingTag == null) {
  527. currentTag.closingTag = EndTagLeftChars + _tagName + TagRightChar;
  528. }
  529. }
  530. if (_isDescendant) {
  531. string afterContent = RenderAfterContent();
  532. if (afterContent != null) {
  533. if (currentTag.closingTag != null)
  534. currentTag.closingTag = afterContent;
  535. }
  536. string afterTag = RenderAfterTag();
  537. if (afterTag != null) {
  538. if (currentTag.closingTag != null)
  539. currentTag.closingTag = afterTag;
  540. }
  541. }
  542. PushEndTag(currentTag.closingTag);
  543. _attrCount = 0;
  544. _styleCount = 0;
  545. }
  546. public virtual void RenderBeginTag(string tagName){
  547. TagName = tagName;
  548. RenderBeginTag(_tagKey);
  549. }
  550. public virtual void RenderEndTag(){
  551. string endTagText = PopEndTag();
  552. if (endTagText != null) {
  553. if (HtmlTextWriter._tagNameLookupArray[_tagIndex].tagType == 0) {
  554. _inlineCount--;
  555. Write(endTagText);
  556. }
  557. else{
  558. WriteLine();
  559. Indent--;
  560. Write(endTagText);
  561. }
  562. }
  563. }
  564. public override void Write(bool value){
  565. if (tabsPending)
  566. OutputTabs();
  567. writer.Write(value);
  568. }
  569. public override void Write(char value){
  570. if (tabsPending)
  571. OutputTabs();
  572. writer.Write(value);
  573. }
  574. public override void Write(char[] buffer){
  575. if (tabsPending)
  576. OutputTabs();
  577. writer.Write(buffer);
  578. }
  579. public override void Write(char[] buffer, int index, int count){
  580. if (tabsPending)
  581. OutputTabs();
  582. writer.Write(buffer, index, count);
  583. }
  584. public override void Write(double value){
  585. if (tabsPending)
  586. OutputTabs();
  587. writer.Write(value);
  588. }
  589. public override void Write(int value){
  590. if (tabsPending)
  591. OutputTabs();
  592. writer.Write(value);
  593. }
  594. public override void Write(long value){
  595. if (tabsPending)
  596. OutputTabs();
  597. writer.Write(value);
  598. }
  599. public override void Write(object value){
  600. if (tabsPending)
  601. OutputTabs();
  602. writer.Write(value);
  603. }
  604. public override void Write(float value){
  605. if (tabsPending)
  606. OutputTabs();
  607. writer.Write(value);
  608. }
  609. public override void Write(string s){
  610. if (tabsPending)
  611. OutputTabs();
  612. writer.Write(s);
  613. }
  614. public override void Write(string format, object arg0){
  615. if (tabsPending)
  616. OutputTabs();
  617. writer.Write(format, arg0);
  618. }
  619. public override void Write(string format, object arg0, object arg1){
  620. if (tabsPending)
  621. OutputTabs();
  622. writer.Write(format, arg0, arg1);
  623. }
  624. public override void Write(string format, params object[] arg){
  625. if (tabsPending)
  626. OutputTabs();
  627. writer.Write(format, arg);
  628. }
  629. public virtual void WriteAttribute(string name, string value){
  630. WriteAttribute(name, value, false);
  631. }
  632. public virtual void WriteAttribute(string name, string value, bool fEncode){
  633. writer.Write(SpaceChar);
  634. writer.Write(name);
  635. if (value != null) {
  636. writer.Write(EqualsChar);
  637. writer.Write(DoubleQuoteChar);
  638. if (fEncode) {
  639. if (_httpWriter == null) {
  640. System.Web.HttpUtility.HtmlAttributeEncode(value, writer);
  641. }
  642. else{
  643. System.Web.HttpUtility.HtmlAttributeEncode(value, (TextWriter) _httpWriter);
  644. }
  645. }
  646. else{
  647. writer.Write(value);
  648. }
  649. writer.Write(DoubleQuoteChar);
  650. }
  651. }
  652. public virtual void WriteBeginTag(string tagName){
  653. if (tabsPending)
  654. OutputTabs();
  655. writer.Write(TagLeftChar);
  656. writer.Write(tagName);
  657. }
  658. public virtual void WriteEndTag(string tagName){
  659. if (tabsPending)
  660. OutputTabs();
  661. writer.Write(TagLeftChar);
  662. writer.Write(SlashChar);
  663. writer.Write(tagName);
  664. writer.Write(TagRightChar);
  665. }
  666. public virtual void WriteFullBeginTag(string tagName){
  667. if (tabsPending)
  668. OutputTabs();
  669. writer.Write(TagLeftChar);
  670. writer.Write(tagName);
  671. writer.Write(TagRightChar);
  672. }
  673. public override void WriteLine(){
  674. writer.WriteLine();
  675. tabsPending = true;
  676. }
  677. public override void WriteLine(bool value){
  678. if (tabsPending)
  679. OutputTabs();
  680. writer.WriteLine(value);
  681. tabsPending = true;
  682. }
  683. public override void WriteLine(char value){
  684. if (tabsPending)
  685. OutputTabs();
  686. writer.WriteLine(value);
  687. tabsPending = true;
  688. }
  689. public override void WriteLine(char[] buffer){
  690. if (tabsPending)
  691. OutputTabs();
  692. writer.WriteLine(buffer);
  693. tabsPending = true;
  694. }
  695. public override void WriteLine(char[] buffer, int index, int count){
  696. if (tabsPending)
  697. OutputTabs();
  698. writer.WriteLine(buffer, index, count);
  699. tabsPending = true;
  700. }
  701. public override void WriteLine(double value){
  702. if (tabsPending)
  703. OutputTabs();
  704. writer.WriteLine(value);
  705. tabsPending = true;
  706. }
  707. public override void WriteLine(int value){
  708. if (tabsPending)
  709. OutputTabs();
  710. writer.WriteLine(value);
  711. tabsPending = true;
  712. }
  713. public override void WriteLine(long value){
  714. if (tabsPending)
  715. OutputTabs();
  716. writer.WriteLine(value);
  717. tabsPending = true;
  718. }
  719. public override void WriteLine(object value){
  720. if (tabsPending)
  721. OutputTabs();
  722. writer.WriteLine(value);
  723. tabsPending = true;
  724. }
  725. public override void WriteLine(float value){
  726. if (tabsPending)
  727. OutputTabs();
  728. writer.WriteLine(value);
  729. tabsPending = true;
  730. }
  731. public override void WriteLine(string s){
  732. if (tabsPending)
  733. OutputTabs();
  734. writer.WriteLine(s);
  735. tabsPending = true;
  736. }
  737. public override void WriteLine(string format, object arg0){
  738. if (tabsPending)
  739. OutputTabs();
  740. writer.WriteLine(format, arg0);
  741. tabsPending = true;
  742. }
  743. public override void WriteLine(string format, object arg0, object arg1){
  744. if (tabsPending)
  745. OutputTabs();
  746. writer.WriteLine(format, arg0, arg1);
  747. tabsPending = true;
  748. }
  749. public override void WriteLine(string format, params object[] arg){
  750. if (tabsPending)
  751. OutputTabs();
  752. writer.WriteLine(format, arg);
  753. tabsPending = true;
  754. }
  755. [CLSCompliant(false)]
  756. public override void WriteLine(uint value){
  757. if (tabsPending)
  758. OutputTabs();
  759. writer.WriteLine(value);
  760. tabsPending = true;
  761. }
  762. public void WriteLineNoTabs(string s){
  763. writer.WriteLine(s);
  764. }
  765. public virtual void WriteStyleAttribute(string name, string value){
  766. WriteStyleAttribute(name, value, false);
  767. }
  768. public virtual void WriteStyleAttribute(string name, string value, bool fEncode){
  769. writer.Write(name);
  770. writer.Write(StyleEqualsChar);
  771. if (fEncode) {
  772. if (_httpWriter == null) {
  773. System.Web.HttpUtility.HtmlAttributeEncode(value, writer);
  774. }
  775. else{
  776. System.Web.HttpUtility.HtmlAttributeEncode(value, (TextWriter) _httpWriter);
  777. }
  778. }
  779. else {
  780. writer.Write(value);
  781. }
  782. writer.Write(SemicolonChar);
  783. }
  784. public override System.Text.Encoding Encoding {
  785. get{
  786. return writer.Encoding;
  787. }
  788. }
  789. public int Indent {
  790. get{
  791. return indentLevel;
  792. }
  793. set{
  794. if (value < 0)
  795. value = 0;
  796. indentLevel = value;
  797. }
  798. }
  799. public TextWriter InnerWriter {
  800. get{
  801. return writer;
  802. }
  803. set{
  804. writer = value;
  805. _httpWriter = value as HttpWriter;
  806. }
  807. }
  808. public override string NewLine {
  809. get{
  810. return writer.NewLine;
  811. }
  812. set{
  813. writer.NewLine = value;
  814. }
  815. }
  816. protected HtmlTextWriterTag TagKey {
  817. get{
  818. return _tagKey;
  819. }
  820. set{
  821. _tagIndex = (int) value;
  822. if (_tagIndex < 0 || _tagIndex >= (int) HtmlTextWriter._tagNameLookupArray.Length)
  823. throw new ArgumentOutOfRangeException("value");
  824. _tagKey = value;
  825. if (value != 0)
  826. _tagName = HtmlTextWriter._tagNameLookupArray[_tagIndex].name;
  827. }
  828. }
  829. protected string TagName {
  830. get{
  831. return _tagName;
  832. }
  833. set{
  834. _tagName = value;
  835. _tagKey = GetTagKey(_tagName);
  836. _tagIndex = (int) _tagKey;
  837. }
  838. }
  839. public const string DefaultTabString = "\t";
  840. public const char DoubleQuoteChar = '"';
  841. public const string EndTagLeftChars = "</";
  842. public const char EqualsChar = '=';
  843. public const string EqualsDoubleQuoteString = "=\"";
  844. public const string SelfClosingChars = " /";
  845. public const string SelfClosingTagEnd = " />";
  846. public const char SemicolonChar = ';';
  847. public const char SingleQuoteChar = '\'';
  848. public const char SlashChar = '/';
  849. public const char SpaceChar = ' ';
  850. public const char StyleEqualsChar = ':';
  851. public const char TagLeftChar = '<';
  852. public const char TagRightChar = '>';
  853. private int _attrCount;
  854. private int _endTagCount;
  855. private int _styleCount;
  856. private int indentLevel;
  857. private int _inlineCount;
  858. private int _tagIndex;
  859. private bool _isDescendant;
  860. private bool tabsPending;
  861. private HtmlTextWriterTag _tagKey;
  862. private TextWriter writer;
  863. private HttpWriter _httpWriter;
  864. private static Hashtable _attrKeyLookupTable;
  865. private static Hashtable _styleKeyLookupTable;
  866. private static Hashtable _tagKeyLookupTable;
  867. private string _tagName;
  868. private string tabString;
  869. private static string[] _styleNameLookupArray;
  870. private RenderAttribute[] _attrList;
  871. private static AttributeInformation[] _attrNameLookupArray;
  872. private static TagInformation[] _tagNameLookupArray;
  873. private TagStackEntry[] _endTags;
  874. private RenderStyle[] _styleList;
  875. } //HtmlTextWriter
  876. struct AttributeInformation {
  877. public bool encode;
  878. public string name;
  879. public AttributeInformation(string name, bool encode){
  880. this.encode = encode;
  881. this.name = name;
  882. }
  883. }
  884. struct RenderAttribute {
  885. public bool encode;
  886. public HtmlTextWriterAttribute key;
  887. public string name;
  888. public string value;
  889. }
  890. struct RenderStyle {
  891. public HtmlTextWriterStyle key;
  892. public string name;
  893. public string value;
  894. }
  895. struct TagInformation {
  896. public string closingTag;
  897. public string name;
  898. public TagType tagType;
  899. public TagInformation(string name, TagType tagType, string closingTag){
  900. this.name = name;
  901. this.tagType = tagType;
  902. this.closingTag = closingTag;
  903. }
  904. }
  905. struct TagStackEntry {
  906. public string endTagText;
  907. public HtmlTextWriterTag tagKey;
  908. }
  909. enum TagType {
  910. Inline,
  911. NonClosing,
  912. Other
  913. }
  914. } // namespace System.Web.UI.HtmlControls