CleanHtmlTextWriter.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // CleanHtmlTextWriter.cs
  3. //
  4. // An HtmlTextWriter that cleans stuff up for you a bit. Helps writing tests
  5. // because you do not have to reproduce the attribute order, etc.
  6. //
  7. // Author:
  8. // Ben Maurer <[email protected]>
  9. //
  10. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. //#define TEST_THIS
  32. using System.Web.UI;
  33. using System;
  34. using System.IO;
  35. using System.Collections;
  36. class CleanHtmlTextWriter : HtmlTextWriter {
  37. public CleanHtmlTextWriter (TextWriter tw) : base (tw)
  38. {
  39. tw.NewLine = "\n";
  40. }
  41. ArrayList pending_attrs = new ArrayList ();
  42. ArrayList pending_styles = new ArrayList ();
  43. class PendingStyle : IComparable {
  44. public string name, value;
  45. public HtmlTextWriterStyle s;
  46. public PendingStyle (string name, string value, HtmlTextWriterStyle s)
  47. {
  48. this.name = name;
  49. this.value = value;
  50. this.s = s;
  51. }
  52. public int CompareTo (object o)
  53. {
  54. return string.CompareOrdinal (name, ((PendingStyle) o).name);
  55. }
  56. }
  57. class PendingAttribute : IComparable {
  58. public string name, value;
  59. public HtmlTextWriterAttribute a;
  60. public bool encode;
  61. public bool know_encode;
  62. public PendingAttribute (string name, string value, HtmlTextWriterAttribute a, bool encode, bool know_encode)
  63. {
  64. this.name = name;
  65. this.value = value;
  66. this.a = a;
  67. this.encode = encode;
  68. this.know_encode = know_encode;
  69. }
  70. public int CompareTo (object o)
  71. {
  72. return string.CompareOrdinal (name, ((PendingAttribute) o).name);
  73. }
  74. }
  75. bool filtering = false;
  76. //
  77. // Some idiot at microsoft did not do a sanity check on this api,
  78. // thus forcing me to deal with some serious pain.
  79. //
  80. public override void AddAttribute (HtmlTextWriterAttribute key, string value)
  81. {
  82. if (filtering) {
  83. base.AddAttribute (key, value);
  84. return;
  85. }
  86. pending_attrs.Add (new PendingAttribute (GetAttributeName (key), value, key, false, false));
  87. }
  88. public override void AddAttribute (HtmlTextWriterAttribute key, string value, bool fEncode)
  89. {
  90. if (filtering) {
  91. base.AddAttribute (key, value, fEncode);
  92. return;
  93. }
  94. pending_attrs.Add (new PendingAttribute (GetAttributeName (key), value, key, fEncode, true));
  95. }
  96. public override void AddAttribute (string name, string value)
  97. {
  98. if (filtering) {
  99. base.AddAttribute (name, value);
  100. return;
  101. }
  102. pending_attrs.Add (new PendingAttribute (name, value, 0, false, false));
  103. }
  104. public override void AddAttribute (string name, string value, bool fEncode)
  105. {
  106. if (filtering) {
  107. base.AddAttribute (name, value, fEncode);
  108. return;
  109. }
  110. pending_attrs.Add (new PendingAttribute (name, value, 0, fEncode, true));
  111. }
  112. protected override void AddAttribute (string name, string value, HtmlTextWriterAttribute key)
  113. {
  114. if (filtering) {
  115. base.AddAttribute (name, value, key);
  116. return;
  117. }
  118. pending_attrs.Add (new PendingAttribute (name, value, key, false, false));
  119. }
  120. // TODO: use the above retardation in this stuff
  121. protected override void AddStyleAttribute (string name, string value, HtmlTextWriterStyle s)
  122. {
  123. pending_styles.Add (new PendingStyle (name, value, s));
  124. }
  125. protected override void FilterAttributes ()
  126. {
  127. pending_attrs.Sort ();
  128. pending_styles.Sort ();
  129. filtering = true;
  130. foreach (PendingAttribute a in pending_attrs) {
  131. if (a.a == 0) {
  132. if (a.know_encode)
  133. base.AddAttribute (a.name, a.value, a.encode);
  134. else
  135. base.AddAttribute (a.name, a.value, a.encode);
  136. } else {
  137. if (a.know_encode)
  138. base.AddAttribute (a.a, a.value, a.encode);
  139. else
  140. base.AddAttribute (a.a, a.value, a.encode);
  141. }
  142. }
  143. foreach (PendingStyle s in pending_styles)
  144. base.AddStyleAttribute (s.name, s.value, s.s);
  145. filtering = false;
  146. pending_attrs.Clear ();
  147. pending_styles.Clear ();
  148. base.FilterAttributes ();
  149. }
  150. #if TEST_THIS
  151. static void Main ()
  152. {
  153. HtmlTextWriter w = new CleanHtmlTextWriter (Console.Out);
  154. w.AddAttribute (HtmlTextWriterAttribute.Name, "abcd");
  155. w.AddAttribute (HtmlTextWriterAttribute.Id, "efg");
  156. w.RenderBeginTag (HtmlTextWriterTag.Input);
  157. w.RenderEndTag ();
  158. Console.WriteLine ();
  159. }
  160. #endif
  161. }