HtmlTextWriter.cs 35 KB

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