HtmlTextWriter.cs 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. writer = writer;
  174. 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. _attrCount++;
  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. _styleCount++;
  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 tagRender = true;
  443. if (_isDescendant) {
  444. tagRender = OnTagRender(_tagName, _tagKey);
  445. FilterAttributes();
  446. string beforeTag = RenderBeforeTag();
  447. if (beforeTag != null) {
  448. if (tabsPending)
  449. OutputTabs();
  450. writer.Write(beforeTag);
  451. }
  452. }
  453. TagInformation currentTag = HtmlTextWriter._tagNameLookupArray[_tagIndex];
  454. bool tagRendered=true;
  455. if (!tagRender)
  456. tagRendered = false;
  457. if (tagRender) {
  458. if (tabsPending)
  459. OutputTabs();
  460. writer.Write(TagLeftChar);
  461. writer.Write(_tagName);
  462. RenderAttribute rAttr;
  463. string rAttrValue = null;
  464. for (int i=0; i <= _attrCount; i++) {
  465. rAttr = _attrList[i];
  466. if (rAttr.key == HtmlTextWriterAttribute.Style)
  467. rAttrValue = rAttr.value;
  468. else {
  469. writer.Write(SpaceChar);
  470. writer.Write(rAttr.name);
  471. if (rAttr.value != null) {
  472. writer.Write(EqualsChar);
  473. writer.Write(DoubleQuoteChar);
  474. if (rAttr.encode) {
  475. if (_httpWriter == null) {
  476. System.Web.HttpUtility.HtmlAttributeEncode(rAttr.value, writer);
  477. }
  478. else {
  479. System.Web.HttpUtility.HtmlAttributeEncode(rAttr.value, (TextWriter) _httpWriter);
  480. }
  481. }
  482. else {
  483. writer.Write(rAttr.value);
  484. }
  485. writer.Write(DoubleQuoteChar);
  486. }
  487. }
  488. }
  489. if (_styleCount > 0 || rAttrValue != null) {
  490. writer.Write(SpaceChar);
  491. writer.Write("style");
  492. writer.Write(EqualsChar);
  493. writer.Write(DoubleQuoteChar);
  494. RenderStyle rStyle;
  495. for (int i=0; i <= _styleCount; i++) {
  496. rStyle = _styleList[i];
  497. writer.Write(rStyle.name);
  498. writer.Write(StyleEqualsChar);
  499. writer.Write(rStyle.value);
  500. writer.Write(SemicolonChar);
  501. }
  502. if (rAttrValue != null)
  503. writer.Write(rAttrValue);
  504. writer.Write(DoubleQuoteChar);
  505. }
  506. if (currentTag.tagType == TagType.NonClosing) {
  507. writer.Write(SpaceChar);
  508. writer.Write(SlashChar);
  509. writer.Write(TagRightChar);
  510. }
  511. else
  512. writer.Write(TagRightChar);
  513. }
  514. string beforeContent = RenderBeforeContent();
  515. if (beforeContent != null) {
  516. if (tabsPending)
  517. OutputTabs();
  518. writer.Write(beforeContent);
  519. }
  520. if (tagRendered) {
  521. if (currentTag.tagType == TagType.Inline)
  522. _inlineCount++;
  523. else {
  524. WriteLine();
  525. Indent++;
  526. }
  527. if (currentTag.closingTag == null) {
  528. currentTag.closingTag = EndTagLeftChars + _tagName + TagRightChar;
  529. }
  530. }
  531. if (_isDescendant) {
  532. string afterContent = RenderAfterContent();
  533. if (afterContent != null) {
  534. if (currentTag.closingTag != null)
  535. currentTag.closingTag = afterContent;
  536. }
  537. string afterTag = RenderAfterTag();
  538. if (afterTag != null) {
  539. if (currentTag.closingTag != null)
  540. currentTag.closingTag = afterTag;
  541. }
  542. }
  543. PushEndTag(currentTag.closingTag);
  544. _attrCount = 0;
  545. _styleCount = 0;
  546. }
  547. public virtual void RenderBeginTag(string tagName){
  548. TagName = tagName;
  549. RenderBeginTag(_tagKey);
  550. }
  551. public virtual void RenderEndTag(){
  552. string endTagText = PopEndTag();
  553. if (endTagText != null) {
  554. if (HtmlTextWriter._tagNameLookupArray[_tagIndex].tagType == 0) {
  555. _inlineCount--;
  556. Write(endTagText);
  557. }
  558. else{
  559. WriteLine();
  560. Indent--;
  561. Write(endTagText);
  562. }
  563. }
  564. }
  565. public override void Write(bool value){
  566. if (tabsPending)
  567. OutputTabs();
  568. writer.Write(value);
  569. }
  570. public override void Write(char value){
  571. if (tabsPending)
  572. OutputTabs();
  573. writer.Write(value);
  574. }
  575. public override void Write(char[] buffer){
  576. if (tabsPending)
  577. OutputTabs();
  578. writer.Write(buffer);
  579. }
  580. public override void Write(char[] buffer, int index, int count){
  581. if (tabsPending)
  582. OutputTabs();
  583. writer.Write(buffer, index, count);
  584. }
  585. public override void Write(double value){
  586. if (tabsPending)
  587. OutputTabs();
  588. writer.Write(value);
  589. }
  590. public override void Write(int value){
  591. if (tabsPending)
  592. OutputTabs();
  593. writer.Write(value);
  594. }
  595. public override void Write(long value){
  596. if (tabsPending)
  597. OutputTabs();
  598. writer.Write(value);
  599. }
  600. public override void Write(object value){
  601. if (tabsPending)
  602. OutputTabs();
  603. writer.Write(value);
  604. }
  605. public override void Write(float value){
  606. if (tabsPending)
  607. OutputTabs();
  608. writer.Write(value);
  609. }
  610. public override void Write(string s){
  611. if (tabsPending)
  612. OutputTabs();
  613. writer.Write(s);
  614. }
  615. public override void Write(string format, object arg0){
  616. if (tabsPending)
  617. OutputTabs();
  618. writer.Write(format, arg0);
  619. }
  620. public override void Write(string format, object arg0, object arg1){
  621. if (tabsPending)
  622. OutputTabs();
  623. writer.Write(format, arg0, arg1);
  624. }
  625. public override void Write(string format, params object[] arg){
  626. if (tabsPending)
  627. OutputTabs();
  628. writer.Write(format, arg);
  629. }
  630. public virtual void WriteAttribute(string name, string value){
  631. WriteAttribute(name, value, false);
  632. }
  633. public virtual void WriteAttribute(string name, string value, bool fEncode){
  634. writer.Write(SpaceChar);
  635. writer.Write(name);
  636. if (value != null) {
  637. writer.Write(EqualsChar);
  638. writer.Write(DoubleQuoteChar);
  639. if (fEncode) {
  640. if (_httpWriter == null) {
  641. System.Web.HttpUtility.HtmlAttributeEncode(value, writer);
  642. }
  643. else{
  644. System.Web.HttpUtility.HtmlAttributeEncode(value, (TextWriter) _httpWriter);
  645. }
  646. }
  647. else{
  648. writer.Write(value);
  649. }
  650. writer.Write(DoubleQuoteChar);
  651. }
  652. }
  653. public virtual void WriteBeginTag(string tagName){
  654. if (tabsPending)
  655. OutputTabs();
  656. writer.Write(TagLeftChar);
  657. writer.Write(tagName);
  658. }
  659. public virtual void WriteEndTag(string tagName){
  660. if (tabsPending)
  661. OutputTabs();
  662. writer.Write(TagLeftChar);
  663. writer.Write(SlashChar);
  664. writer.Write(tagName);
  665. writer.Write(TagRightChar);
  666. }
  667. public virtual void WriteFullBeginTag(string tagName){
  668. if (tabsPending)
  669. OutputTabs();
  670. writer.Write(TagLeftChar);
  671. writer.Write(tagName);
  672. writer.Write(TagRightChar);
  673. }
  674. public override void WriteLine(){
  675. writer.WriteLine();
  676. tabsPending = true;
  677. }
  678. public override void WriteLine(bool value){
  679. if (tabsPending)
  680. OutputTabs();
  681. writer.WriteLine(value);
  682. tabsPending = true;
  683. }
  684. public override void WriteLine(char value){
  685. if (tabsPending)
  686. OutputTabs();
  687. writer.WriteLine(value);
  688. tabsPending = true;
  689. }
  690. public override void WriteLine(char[] buffer){
  691. if (tabsPending)
  692. OutputTabs();
  693. writer.WriteLine(buffer);
  694. tabsPending = true;
  695. }
  696. public override void WriteLine(char[] buffer, int index, int count){
  697. if (tabsPending)
  698. OutputTabs();
  699. writer.WriteLine(buffer, index, count);
  700. tabsPending = true;
  701. }
  702. public override void WriteLine(double value){
  703. if (tabsPending)
  704. OutputTabs();
  705. writer.WriteLine(value);
  706. tabsPending = true;
  707. }
  708. public override void WriteLine(int value){
  709. if (tabsPending)
  710. OutputTabs();
  711. writer.WriteLine(value);
  712. tabsPending = true;
  713. }
  714. public override void WriteLine(long value){
  715. if (tabsPending)
  716. OutputTabs();
  717. writer.WriteLine(value);
  718. tabsPending = true;
  719. }
  720. public override void WriteLine(object value){
  721. if (tabsPending)
  722. OutputTabs();
  723. writer.WriteLine(value);
  724. tabsPending = true;
  725. }
  726. public override void WriteLine(float value){
  727. if (tabsPending)
  728. OutputTabs();
  729. writer.WriteLine(value);
  730. tabsPending = true;
  731. }
  732. public override void WriteLine(string s){
  733. if (tabsPending)
  734. OutputTabs();
  735. writer.WriteLine(s);
  736. tabsPending = true;
  737. }
  738. public override void WriteLine(string format, object arg0){
  739. if (tabsPending)
  740. OutputTabs();
  741. writer.WriteLine(format, arg0);
  742. tabsPending = true;
  743. }
  744. public override void WriteLine(string format, object arg0, object arg1){
  745. if (tabsPending)
  746. OutputTabs();
  747. writer.WriteLine(format, arg0, arg1);
  748. tabsPending = true;
  749. }
  750. public override void WriteLine(string format, params object[] arg){
  751. if (tabsPending)
  752. OutputTabs();
  753. writer.WriteLine(format, arg);
  754. tabsPending = true;
  755. }
  756. [CLSCompliant(false)]
  757. public override void WriteLine(uint value){
  758. if (tabsPending)
  759. OutputTabs();
  760. writer.WriteLine(value);
  761. tabsPending = true;
  762. }
  763. public void WriteLineNoTabs(string s){
  764. writer.WriteLine(s);
  765. }
  766. public virtual void WriteStyleAttribute(string name, string value){
  767. WriteStyleAttribute(name, value, false);
  768. }
  769. public virtual void WriteStyleAttribute(string name, string value, bool fEncode){
  770. writer.Write(name);
  771. writer.Write(StyleEqualsChar);
  772. if (fEncode) {
  773. if (_httpWriter == null) {
  774. System.Web.HttpUtility.HtmlAttributeEncode(value, writer);
  775. }
  776. else{
  777. System.Web.HttpUtility.HtmlAttributeEncode(value, (TextWriter) _httpWriter);
  778. }
  779. }
  780. else {
  781. writer.Write(value);
  782. }
  783. writer.Write(SemicolonChar);
  784. }
  785. public override System.Text.Encoding Encoding {
  786. get{
  787. return writer.Encoding;
  788. }
  789. }
  790. public int Indent {
  791. get{
  792. return indentLevel;
  793. }
  794. set{
  795. if (value < 0)
  796. value = 0;
  797. indentLevel = value;
  798. }
  799. }
  800. public TextWriter InnerWriter {
  801. get{
  802. return writer;
  803. }
  804. set{
  805. writer = value;
  806. _httpWriter = value as HttpWriter;
  807. }
  808. }
  809. public override string NewLine {
  810. get{
  811. return writer.NewLine;
  812. }
  813. set{
  814. writer.NewLine = value;
  815. }
  816. }
  817. protected HtmlTextWriterTag TagKey {
  818. get{
  819. return _tagKey;
  820. }
  821. set{
  822. _tagIndex = (int) value;
  823. if (_tagIndex < 0 || _tagIndex >= (int) HtmlTextWriter._tagNameLookupArray.Length)
  824. throw new ArgumentOutOfRangeException("value");
  825. _tagKey = value;
  826. if (value != 0)
  827. _tagName = HtmlTextWriter._tagNameLookupArray[_tagIndex].name;
  828. }
  829. }
  830. protected string TagName {
  831. get{
  832. return _tagName;
  833. }
  834. set{
  835. _tagName = value;
  836. _tagKey = GetTagKey(_tagName);
  837. _tagIndex = (int) _tagKey;
  838. }
  839. }
  840. public const string DefaultTabString = "\t";
  841. public const char DoubleQuoteChar = '"';
  842. public const string EndTagLeftChars = "</";
  843. public const char EqualsChar = '=';
  844. public const string EqualsDoubleQuoteString = "=\"";
  845. public const string SelfClosingChars = " /";
  846. public const string SelfClosingTagEnd = " />";
  847. public const char SemicolonChar = ';';
  848. public const char SingleQuoteChar = '\'';
  849. public const char SlashChar = '/';
  850. public const char SpaceChar = ' ';
  851. public const char StyleEqualsChar = ':';
  852. public const char TagLeftChar = '<';
  853. public const char TagRightChar = '>';
  854. private int _attrCount;
  855. private int _endTagCount;
  856. private int _styleCount;
  857. private int indentLevel;
  858. private int _inlineCount;
  859. private int _tagIndex;
  860. private bool _isDescendant;
  861. private bool tabsPending;
  862. private HtmlTextWriterTag _tagKey;
  863. private TextWriter writer;
  864. private HttpWriter _httpWriter;
  865. private static Hashtable _attrKeyLookupTable;
  866. private static Hashtable _styleKeyLookupTable;
  867. private static Hashtable _tagKeyLookupTable;
  868. private string _tagName;
  869. private string tabString;
  870. private static string[] _styleNameLookupArray;
  871. private RenderAttribute[] _attrList;
  872. private static AttributeInformation[] _attrNameLookupArray;
  873. private static TagInformation[] _tagNameLookupArray;
  874. private TagStackEntry[] _endTags;
  875. private RenderStyle[] _styleList;
  876. } //HtmlTextWriter
  877. struct AttributeInformation {
  878. public bool encode;
  879. public string name;
  880. public AttributeInformation(string name, bool encode){
  881. this.encode = encode;
  882. this.name = name;
  883. }
  884. }
  885. struct RenderAttribute {
  886. public bool encode;
  887. public HtmlTextWriterAttribute key;
  888. public string name;
  889. public string value;
  890. }
  891. struct RenderStyle {
  892. public HtmlTextWriterStyle key;
  893. public string name;
  894. public string value;
  895. }
  896. struct TagInformation {
  897. public string closingTag;
  898. public string name;
  899. public TagType tagType;
  900. public TagInformation(string name, TagType tagType, string closingTag){
  901. this.name = name;
  902. this.tagType = tagType;
  903. this.closingTag = closingTag;
  904. }
  905. }
  906. struct TagStackEntry {
  907. public string endTagText;
  908. public HtmlTextWriterTag tagKey;
  909. }
  910. enum TagType {
  911. Inline,
  912. NonClosing,
  913. Other
  914. }
  915. } // namespace System.Web.UI.HtmlControls