HtmlTextWriter.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. if (_styleCount >= (int) _styleList.Length) {
  231. RenderStyle[] rAttrArr = new RenderStyle[_styleList.Length * 2];
  232. System.Array.Copy(_styleList, rAttrArr, (int) _styleList.Length);
  233. _styleList = rAttrArr;
  234. }
  235. RenderStyle rAttr;
  236. rAttr.name = name;
  237. rAttr.value = value;
  238. rAttr.key = key;
  239. _styleList [_styleCount++] = rAttr;
  240. }
  241. public override void Close(){
  242. writer.Close();
  243. }
  244. protected virtual string EncodeAttributeValue(HtmlTextWriterAttribute attrKey, string value){
  245. bool valid = true;
  246. if (0 <= (int) attrKey && (int) attrKey < HtmlTextWriter._attrNameLookupArray.Length)
  247. valid = HtmlTextWriter._attrNameLookupArray[(int) attrKey].encode;
  248. return EncodeAttributeValue(value, valid);
  249. }
  250. protected string EncodeAttributeValue(string value, bool fEncode){
  251. if (value == null)
  252. return null;
  253. if (!(fEncode))
  254. return value;
  255. return System.Web.HttpUtility.HtmlAttributeEncode(value);
  256. }
  257. protected string EncodeUrl(string url){
  258. if (url.IndexOf(SpaceChar) < 0)
  259. return url;
  260. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  261. for(int i=0; i <= url.Length; i++){
  262. char temp = url[i];
  263. if (temp != 32)
  264. sb.Append(temp);
  265. else
  266. sb.Append("%20");
  267. }
  268. return sb.ToString();
  269. }
  270. protected virtual void FilterAttributes(){
  271. int count = 0;
  272. for(int i=0; i < _styleCount; i++){
  273. RenderStyle rStyle = _styleList[i];
  274. if (OnStyleAttributeRender(rStyle.name, rStyle.value, rStyle.key)) {
  275. count++;
  276. }
  277. }
  278. _styleCount = count;
  279. count = 0;
  280. for(int i=0; i <= _attrCount; i++){
  281. RenderAttribute rAttr = _attrList[i];
  282. if (OnAttributeRender(rAttr.name, rAttr.value, rAttr.key)) {
  283. count++;
  284. }
  285. }
  286. _attrCount = count;
  287. }
  288. public override void Flush(){
  289. writer.Flush();
  290. }
  291. protected HtmlTextWriterAttribute GetAttributeKey(string attrName){
  292. if (attrName != null && attrName.Length > 0) {
  293. object attr = HtmlTextWriter._attrKeyLookupTable[attrName.ToLower()];
  294. if (attr != null)
  295. return (HtmlTextWriterAttribute) attr;
  296. }
  297. return (HtmlTextWriterAttribute) (-1);
  298. }
  299. protected string GetAttributeName(HtmlTextWriterAttribute attrKey){
  300. if ((int) attrKey >= 0 && (int) attrKey < HtmlTextWriter._attrNameLookupArray.Length)
  301. return HtmlTextWriter._attrNameLookupArray[(int) attrKey].name;
  302. return System.String.Empty;
  303. }
  304. protected HtmlTextWriterStyle GetStyleKey(string styleName){
  305. if (styleName != null && styleName.Length > 0) {
  306. object style = HtmlTextWriter._styleKeyLookupTable[styleName.ToLower()];
  307. if (style != null)
  308. return (HtmlTextWriterStyle) style;
  309. }
  310. return (HtmlTextWriterStyle) (-1);
  311. }
  312. protected string GetStyleName(HtmlTextWriterStyle styleKey){
  313. if ((int) styleKey >= 0 && (int) styleKey < HtmlTextWriter._styleNameLookupArray.Length)
  314. return HtmlTextWriter._styleNameLookupArray[(int) styleKey];
  315. return System.String.Empty;
  316. }
  317. protected virtual HtmlTextWriterTag GetTagKey(string tagName){
  318. if (tagName != null && tagName.Length > 0) {
  319. object tag = HtmlTextWriter._tagKeyLookupTable[tagName.ToLower()];
  320. if (tag != null)
  321. return (HtmlTextWriterTag) tag;
  322. }
  323. return 0;
  324. }
  325. protected virtual string GetTagName(HtmlTextWriterTag tagKey){
  326. if ((int) tagKey >= 0 && (int) tagKey < HtmlTextWriter._tagNameLookupArray.Length)
  327. return HtmlTextWriter._tagNameLookupArray[(int) tagKey].name;
  328. return System.String.Empty;
  329. }
  330. protected bool IsAttributeDefined(HtmlTextWriterAttribute key){
  331. for (int i=0; i < _attrCount; i++) {
  332. if (_attrList[i].key == key)
  333. return true;
  334. }
  335. return false;
  336. }
  337. protected bool IsAttributeDefined(HtmlTextWriterAttribute key, ref string value){
  338. value = null;
  339. for (int i=0; i < _attrCount; i++) {
  340. if (_attrList[i].key == key) {
  341. value = _attrList[i].value;
  342. return true;
  343. }
  344. }
  345. return false;
  346. }
  347. protected bool IsStyleAttributeDefined(HtmlTextWriterStyle key){
  348. for (int i= 0; i < _styleCount; i++) {
  349. if (_styleList[i].key == key)
  350. return true;
  351. }
  352. return false;
  353. }
  354. protected bool IsStyleAttributeDefined(HtmlTextWriterStyle key, ref string value){
  355. value = null;
  356. for( int i=0; i < _styleCount; i++) {
  357. if (_styleList[i].key == key) {
  358. value = _styleList[i].value;
  359. return true;
  360. }
  361. }
  362. return false;
  363. }
  364. protected virtual bool OnAttributeRender(string name, string value, HtmlTextWriterAttribute key){
  365. return true;
  366. }
  367. protected virtual bool OnStyleAttributeRender(string name, string value, HtmlTextWriterStyle key){
  368. return true;
  369. }
  370. protected virtual bool OnTagRender(string name, HtmlTextWriterTag key){
  371. return true;
  372. }
  373. protected virtual void OutputTabs(){
  374. if (tabsPending) {
  375. for(int i=0; i < indentLevel; i++) {
  376. writer.Write(tabString);
  377. }
  378. tabsPending = false;
  379. }
  380. }
  381. protected string PopEndTag(){
  382. if (_endTagCount <= 0)
  383. throw new InvalidOperationException("A PopEndTag was called without a corresponding PushEndTag");
  384. _endTagCount--;
  385. TagKey = _endTags[_endTagCount].tagKey;
  386. return _endTags[_endTagCount].endTagText;
  387. }
  388. protected void PushEndTag(string endTag){
  389. if (_endTagCount >= (int) _endTags.Length) {
  390. TagStackEntry[] temp = new TagStackEntry[(int) _endTags.Length * 2];
  391. System.Array.Copy(_endTags, temp, (int) _endTags.Length);
  392. _endTags = temp;
  393. }
  394. _endTags[_endTagCount].tagKey = _tagKey;
  395. _endTags[_endTagCount].endTagText = endTag;
  396. _endTagCount++;
  397. }
  398. protected static void RegisterAttribute(string name, HtmlTextWriterAttribute key){
  399. HtmlTextWriter.RegisterAttribute(name, key, false);
  400. }
  401. private static void RegisterAttribute(string name, HtmlTextWriterAttribute key, bool fEncode){
  402. name = name.ToLower();
  403. HtmlTextWriter._attrKeyLookupTable.Add(name, key);
  404. if ((int) key < (int) HtmlTextWriter._attrNameLookupArray.Length)
  405. HtmlTextWriter._attrNameLookupArray[(int) key] = new AttributeInformation(name, fEncode);
  406. }
  407. protected static void RegisterStyle(string name, HtmlTextWriterStyle key){
  408. name = name.ToLower();
  409. HtmlTextWriter._styleKeyLookupTable.Add(name, key);
  410. if ((int) key < (int) HtmlTextWriter._styleNameLookupArray.Length)
  411. HtmlTextWriter._styleNameLookupArray[(int) key] = name;
  412. }
  413. protected static void RegisterTag(string name, HtmlTextWriterTag key){
  414. HtmlTextWriter.RegisterTag(name, key, TagType.Other);
  415. }
  416. private static void RegisterTag(string name, HtmlTextWriterTag key, TagType type){
  417. name = name.ToLower();
  418. HtmlTextWriter._tagKeyLookupTable.Add(name, key);
  419. string fullTag = null;
  420. if ((int) type != 1 && (int) key != 0) {
  421. fullTag = EndTagLeftChars + name + TagRightChar;
  422. }
  423. if ((int) key < HtmlTextWriter._tagNameLookupArray.Length)
  424. HtmlTextWriter._tagNameLookupArray[(int) key] = new TagInformation(name, type, fullTag);
  425. }
  426. protected virtual string RenderAfterContent(){
  427. return null;
  428. }
  429. protected virtual string RenderAfterTag(){
  430. return null;
  431. }
  432. protected virtual string RenderBeforeContent(){
  433. return null;
  434. }
  435. protected virtual string RenderBeforeTag(){
  436. return null;
  437. }
  438. public virtual void RenderBeginTag(HtmlTextWriterTag tagKey){
  439. TagKey = tagKey;
  440. bool tagRendered = true;
  441. bool tagRender = true;
  442. if (_isDescendant) {
  443. tagRender = OnTagRender(_tagName, _tagKey);
  444. FilterAttributes();
  445. string beforeTag = RenderBeforeTag();
  446. if (beforeTag != null) {
  447. if (tabsPending)
  448. OutputTabs();
  449. writer.Write(beforeTag);
  450. }
  451. }
  452. TagInformation currentTag = HtmlTextWriter._tagNameLookupArray[_tagIndex];
  453. if (currentTag.closingTag == null)
  454. currentTag.closingTag = EndTagLeftChars + _tagName + TagRightChar;
  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