2 // HtmlTextWriterTest.cs
3 // - Unit tests for System.Web.UI.HtmlTextWriter
6 // Sebastien Pouliot <sebastien@ximian.com>
7 // Ben Maurer <bmaurer@novell.com>
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using HtwTag
= System
.Web
.UI
.HtmlTextWriterTag
;
36 using HtwAttribute
= System
.Web
.UI
.HtmlTextWriterAttribute
;
37 using HtwStyle
= System
.Web
.UI
.HtmlTextWriterStyle
;
39 using NUnit
.Framework
;
41 namespace MonoTests
.System
.Web
.UI
{
44 public class HtmlTextWriterTest
{
50 public void SetupTests ()
52 sw
= new StringWriter ();
53 sw
.NewLine
= "\n"; // Keep sanity.
54 w
= new HtmlTextWriter (sw
);
58 public void DefaultConstFields ()
60 Assert
.AreEqual ("=\"", HtmlTextWriter
.EqualsDoubleQuoteString
, "EqualsDoubleQuoteString");
65 public void NullWriter ()
67 HtmlTextWriter htw
= new HtmlTextWriter (null);
68 Assert
.IsNotNull (htw
, "NullWriter");
69 Assert
.IsNull (htw
.InnerWriter
, "InnerWriter");
73 public void WriteAttributes_NullValue ()
75 w
.WriteAttribute ("name", null);
76 Assert
.AreEqual (" name", w
.InnerWriter
.ToString ());
82 w
.RenderBeginTag ("a");
85 Assert
.AreEqual ("<a></a>", sw
.ToString ());
89 public void WriteATagByKey ()
91 w
.RenderBeginTag (HtwTag
.A
);
94 Assert
.AreEqual ("<a></a>", sw
.ToString ());
98 public void WriteDivTagByKey ()
100 w
.RenderBeginTag (HtwTag
.Div
);
103 Assert
.AreEqual ("<div>\n\n</div>", sw
.ToString ());
107 public void WriteOptionTagByKey ()
109 w
.RenderBeginTag (HtwTag
.Option
);
112 Assert
.AreEqual ("<option>\n\n</option>", sw
.ToString ());
116 public void WriteDivAndATagByKey ()
118 w
.RenderBeginTag (HtwTag
.Div
);
119 w
.RenderBeginTag (HtwTag
.A
);
123 Assert
.AreEqual ("<div>\n\t<a></a>\n</div>", sw
.ToString ());
127 public void NestedIndent ()
129 w
.RenderBeginTag (HtwTag
.Div
);
130 w
.RenderBeginTag (HtwTag
.Div
);
134 Assert
.AreEqual ("<div>\n\t<div>\n\n\t</div>\n</div>", sw
.ToString ());
138 public void WriteDivAndATagByKeyWithAttr ()
140 w
.RenderBeginTag (HtwTag
.Div
);
142 w
.AddAttribute (HtwAttribute
.Href
, "http://slashdot.org/");
143 w
.RenderBeginTag (HtwTag
.A
);
146 Assert
.AreEqual ("<div>\n\t<a href=\"http://slashdot.org/\"></a>\n</div>", sw
.ToString ());
150 public void WriteDivTagWithStyle ()
152 w
.AddAttribute ("id", "foo");
153 w
.AddAttribute ("style", "align:left");
154 w
.AddStyleAttribute (HtwStyle
.Color
, "red");
155 w
.AddStyleAttribute (HtwStyle
.FontWeight
, "bold");
156 w
.RenderBeginTag (HtwTag
.Div
);
158 Assert
.AreEqual ("<div id=\"foo\" style=\"color:red;font-weight:bold;align:left\">\n\n</div>", sw
.ToString ());
162 public void EscapeJScript ()
164 w
.AddAttribute (HtwAttribute
.Onclick
, "this.style.color = \"red\"");
165 w
.RenderBeginTag (HtwTag
.Div
);
167 Assert
.AreEqual ("<div onclick=\"this.style.color = "red"\">\n\n</div>", sw
.ToString ());
171 public void EscapeUrl ()
173 w
.AddAttribute (HtwAttribute
.Href
, "http://www.example.com/search?hl=en&q=i+love+nunit&btnG=Google+Search");
174 w
.RenderBeginTag (HtwTag
.A
);
176 Assert
.AreEqual ("<a href=\"http://www.example.com/search?hl=en&q=i+love+nunit&btnG=Google+Search\"></a>", sw
.ToString ());
179 // Which attrs fall here
181 public void NoEscapeAttrName ()
183 w
.AddAttribute (HtwAttribute
.Name
, "cookies&cream");
184 w
.RenderBeginTag (HtwTag
.Div
);
186 Assert
.AreEqual ("<div name=\"cookies&cream\">\n\n</div>", sw
.ToString ());
190 public void NoEscapeAttrName2 () {
191 w
.AddAttribute ("name", "cookies&cream");
192 w
.RenderBeginTag (HtwTag
.Div
);
194 Assert
.AreEqual ("<div name=\"cookies&cream\">\n\n</div>", sw
.ToString ());
198 public void NoEscapeAttrName3 () {
199 w
.AddAttribute (HtwAttribute
.Name
, "cookies&cream", true);
200 w
.RenderBeginTag (HtwTag
.Div
);
202 Assert
.AreEqual ("<div name=\"cookies&cream\">\n\n</div>", sw
.ToString ());
206 public void NoEscapeAttrName4 () {
207 w
.AddAttribute ("NaMe", "cookies&cream");
208 w
.RenderBeginTag (HtwTag
.Div
);
210 Assert
.AreEqual ("<div NaMe=\"cookies&cream\">\n\n</div>", sw
.ToString ());
214 public void EscapeAttribute1 () {
215 sw
= new StringWriter ();
216 sw
.NewLine
= "\n"; // Keep sanity.
217 Poker w1
= new Poker (sw
);
219 w1
.AddAttribute ("attr", "cookies&cream");
220 w1
.RenderBeginTag (HtwTag
.Div
);
223 Assert
.AreEqual ("cookies&cream", w1
.AttrValue_At_AddAttribute
, "AttrValue_At_AddAttribute");
224 Assert
.AreEqual ("cookies&cream", w1
.AttrValue_At_OnAttributeRender
, "AttrValue_At_OnAttributeRender");
225 Assert
.AreEqual ("<div attr=\"cookies&cream\">\n\n</div>", sw
.ToString ());
229 public void EscapeAttribute2 () {
230 sw
= new StringWriter ();
231 sw
.NewLine
= "\n"; // Keep sanity.
232 Poker w1
= new Poker (sw
);
234 w1
.AddAttribute ("attr", "cookies&cream", false);
235 w1
.RenderBeginTag (HtwTag
.Div
);
238 Assert
.AreEqual ("cookies&cream", w1
.AttrValue_At_AddAttribute
, "AttrValue_At_AddAttribute");
239 Assert
.AreEqual ("cookies&cream", w1
.AttrValue_At_OnAttributeRender
, "AttrValue_At_OnAttributeRender");
240 Assert
.AreEqual ("<div attr=\"cookies&cream\">\n\n</div>", sw
.ToString ());
244 public void EscapeAttribute3 () {
245 sw
= new StringWriter ();
246 sw
.NewLine
= "\n"; // Keep sanity.
247 Poker w1
= new Poker (sw
);
249 w1
.AddAttribute ("attr", "cookies&cream", true);
250 w1
.RenderBeginTag (HtwTag
.Div
);
253 Assert
.AreEqual ("cookies&cream", w1
.AttrValue_At_AddAttribute
, "AttrValue_At_AddAttribute");
254 Assert
.AreEqual ("cookies&cream", w1
.AttrValue_At_OnAttributeRender
, "AttrValue_At_OnAttributeRender");
255 Assert
.AreEqual ("<div attr=\"cookies&cream\">\n\n</div>", sw
.ToString ());
259 public void WriteInput ()
261 w
.RenderBeginTag (HtwTag
.Input
);
263 Assert
.AreEqual ("<input />", sw
.ToString ());
266 public void WriteInputStringTag ()
268 w
.RenderBeginTag ("input");
270 Assert
.AreEqual ("<input />", sw
.ToString ());
274 public void WriteDivStringTag ()
276 w
.RenderBeginTag ("div");
278 Assert
.AreEqual ("<div>\n\n</div>", sw
.ToString ());
282 public void WriteUnknownTag ()
284 w
.RenderBeginTag ("somerandomtag");
286 Assert
.AreEqual ("<somerandomtag>\n\n</somerandomtag>", sw
.ToString ());
290 public void WritePartialBlock ()
292 w
.RenderBeginTag ("div");
293 Assert
.AreEqual ("<div>\n", sw
.ToString ());
297 public void WritePartialInline ()
299 w
.RenderBeginTag ("a");
300 Assert
.AreEqual ("<a>", sw
.ToString ());
304 public void WritePartialSelfClosing ()
306 w
.RenderBeginTag ("input");
307 Assert
.AreEqual ("<input />", sw
.ToString ());
311 [ExpectedException (typeof (InvalidOperationException
))]
312 public void NoMatchingClose ()
318 public void AttributeAfterBegin ()
320 w
.RenderBeginTag ("div");
321 w
.AddAttribute ("class", "foo");
323 w
.RenderBeginTag ("div");
326 Assert
.AreEqual ("<div>\n\n</div><div class=\"foo\">\n\n</div>", sw
.ToString ());
330 public void StyleAttribute ()
332 w
.WriteStyleAttribute ("a", "b");
334 Assert
.AreEqual ("a:b;", sw
.ToString ());
338 public void TagByNameGetsCaseChanged ()
340 w
.RenderBeginTag ("InPuT");
341 Assert
.AreEqual ("<input />", sw
.ToString ());
345 public void EnsurePopWorks ()
347 w
.RenderBeginTag (HtwTag
.A
);
348 w
.RenderBeginTag (HtwTag
.B
);
352 Assert
.AreEqual ("<a><b></b></a>", sw
.ToString ());
356 public void AddAttributeTwice_SameValue ()
358 w
.AddAttribute (HtmlTextWriterAttribute
.Border
, "0");
359 w
.AddAttribute (HtmlTextWriterAttribute
.Border
, "0");
360 w
.RenderBeginTag ("div");
363 Assert
.AreEqual ("<div border=\"0\" border=\"0\">\n\n</div>", sw
.ToString ());
367 public void AddAttributeTwice_DifferentValue ()
369 w
.AddAttribute (HtmlTextWriterAttribute
.Border
, "0");
370 w
.AddAttribute (HtmlTextWriterAttribute
.Border
, "1");
371 w
.RenderBeginTag ("div");
374 Assert
.AreEqual ("<div border=\"0\" border=\"1\">\n\n</div>", sw
.ToString ());
378 public void AddStyleAttribute1 ()
380 w
.AddStyleAttribute (HtmlTextWriterStyle
.BackgroundImage
, "http://www.example.com/");
381 w
.RenderBeginTag ("div");
384 Assert
.AreEqual ("<div style=\"background-image:url(http://www.example.com/);\">\n\n</div>", sw
.ToString ());
388 public void AddStyleAttribute3 ()
390 sw
= new StringWriter ();
391 sw
.NewLine
= "\n"; // Keep sanity.
392 Poker w1
= new Poker (sw
);
394 w1
.AddStyleAttribute ("mystyle", "my value&space");
395 w1
.RenderBeginTag ("div");
397 Assert
.AreEqual ("my value&space", w1
.StyleValue_At_AddStyleAttribute
, "StyleValue_At_AddStyleAttribute");
398 Assert
.AreEqual ("my value&space", w1
.StyleValue_At_OnStyleAttributeRender
, "StyleValue_At_OnStyleAttributeRender");
399 Assert
.AreEqual ("<div style=\"mystyle:my value&space;\">\n\n</div>", sw
.ToString ());
403 public void WriteAttribute1 ()
405 w
.WriteAttribute ("attr", "my value&space");
406 Assert
.AreEqual (" attr=\"my value&space\"", sw
.ToString ());
410 public void WriteAttribute2 ()
412 w
.WriteAttribute ("attr", "my value&space", false);
413 Assert
.AreEqual (" attr=\"my value&space\"", sw
.ToString ());
417 public void WriteAttribute3 ()
419 w
.WriteAttribute ("attr", "my value&space", true);
420 Assert
.AreEqual (" attr=\"my value&space\"", sw
.ToString ());
424 public void WriteStyleAttribute1 ()
426 w
.WriteStyleAttribute ("mystyle", "my value&space");
427 Assert
.AreEqual ("mystyle:my value&space;", sw
.ToString ());
431 public void WriteStyleAttribute2 ()
433 w
.WriteStyleAttribute ("mystyle", "my value&space", false);
434 Assert
.AreEqual ("mystyle:my value&space;", sw
.ToString ());
438 public void WriteStyleAttribute3 ()
440 w
.WriteStyleAttribute ("mystyle", "my value&space", true);
441 Assert
.AreEqual ("mystyle:my value&space;", sw
.ToString ());
445 public void WriteStyleAttribute_BackgroundImage1 ()
447 w
.WriteStyleAttribute ("background-image", "http://www.mainsoft.com/space here?a=b&c=d");
448 Assert
.AreEqual ("background-image:http://www.mainsoft.com/space here?a=b&c=d;", sw
.ToString ());
452 public void WriteStyleAttribute_BackgroundImage2 ()
454 w
.WriteStyleAttribute ("BackGround-Image", "http://www.mainsoft.com/space here?a=b&c=d");
455 Assert
.AreEqual ("BackGround-Image:http://www.mainsoft.com/space here?a=b&c=d;", sw
.ToString ());
459 public void AddStyleAttribute_BackgroundImage1 ()
461 sw
= new StringWriter ();
462 sw
.NewLine
= "\n"; // Keep sanity.
463 Poker w1
= new Poker (sw
);
465 w1
.AddStyleAttribute (HtmlTextWriterStyle
.BackgroundImage
, "http://www.mainsoft.com/space here?a=b&c=d");
466 w1
.RenderBeginTag ("div");
469 Assert
.AreEqual ("http://www.mainsoft.com/space here?a=b&c=d", w1
.StyleValue_At_AddStyleAttribute
, "StyleValue_At_AddStyleAttribute");
470 Assert
.AreEqual ("http://www.mainsoft.com/space here?a=b&c=d", w1
.StyleValue_At_OnStyleAttributeRender
, "StyleValue_At_OnStyleAttributeRender");
471 Assert
.AreEqual ("<div style=\"background-image:url(http://www.mainsoft.com/space%20here?a=b&c=d);\">\n\n</div>", sw
.ToString ());
475 public void AddStyleAttribute_BackgroundImage2 ()
477 sw
= new StringWriter ();
478 sw
.NewLine
= "\n"; // Keep sanity.
479 Poker w1
= new Poker (sw
);
481 w1
.AddStyleAttribute ("background-image", "http://www.mainsoft.com/space here?a=b&c=d");
482 w1
.RenderBeginTag ("div");
485 Assert
.AreEqual ("http://www.mainsoft.com/space here?a=b&c=d", w1
.StyleValue_At_AddStyleAttribute
, "StyleValue_At_AddStyleAttribute");
486 Assert
.AreEqual ("http://www.mainsoft.com/space here?a=b&c=d", w1
.StyleValue_At_OnStyleAttributeRender
, "StyleValue_At_OnStyleAttributeRender");
487 Assert
.AreEqual ("<div style=\"background-image:url(http://www.mainsoft.com/space%20here?a=b&c=d);\">\n\n</div>", sw
.ToString ());
491 public void AddStyleAttribute_BackgroundImage3 ()
493 sw
= new StringWriter ();
494 sw
.NewLine
= "\n"; // Keep sanity.
495 Poker w1
= new Poker (sw
);
497 w1
.AddStyleAttribute ("BackGround-Image", "http://www.mainsoft.com/space here?a=b&c=d");
498 w1
.RenderBeginTag ("div");
501 Assert
.AreEqual ("http://www.mainsoft.com/space here?a=b&c=d", w1
.StyleValue_At_AddStyleAttribute
, "StyleValue_At_AddStyleAttribute");
502 Assert
.AreEqual ("http://www.mainsoft.com/space here?a=b&c=d", w1
.StyleValue_At_OnStyleAttributeRender
, "StyleValue_At_OnStyleAttributeRender");
503 Assert
.AreEqual ("<div style=\"BackGround-Image:url(http://www.mainsoft.com/space%20here?a=b&c=d);\">\n\n</div>", sw
.ToString ());
508 public void AddStyleAttribute2 ()
510 w
.AddStyleAttribute (HtmlTextWriterStyle
.BackgroundColor
, "Aqua");
511 w
.RenderBeginTag ("div");
514 Assert
.AreEqual ("<div style=\"background-color:Aqua;\">\n\n</div>", sw
.ToString ());
518 public void WriteIndentation ()
520 w
.RenderBeginTag (HtwTag
.Div
);
524 Assert
.AreEqual ("<div>\n\tHello\n</div>", sw
.ToString ());
528 public void WriteIndentation2 ()
530 w
.RenderBeginTag (HtwTag
.Div
);
534 Assert
.AreEqual ("<div>\n\t\n</div>", sw
.ToString ());
538 public void WriteIndentation3 ()
540 w
.RenderBeginTag (HtwTag
.Div
);
543 Assert
.AreEqual ("<div>\n\n</div>", sw
.ToString ());
547 public void WriteBreakTest ()
550 Assert
.AreEqual ("<br />", sw
.ToString (), "WriteBreakTest01");
553 class MyHttpTextWriter
: HtmlTextWriter
{
555 public MyHttpTextWriter(TextWriter tw
) : base(tw
) {
558 protected override bool OnAttributeRender(string name
, string value, HtmlTextWriterAttribute key
) {
561 case 1: Assert
.AreEqual(HtmlTextWriterAttribute
.Border
, key
);
563 case 2: Assert
.AreEqual((HtmlTextWriterAttribute
)(-1), key
);
566 return base.OnAttributeRender (name
, value, key
);
571 class Poker
: HtmlTextWriter
573 public string StyleValue_At_OnStyleAttributeRender
;
574 public string StyleValue_At_AddStyleAttribute
;
575 public string AttrValue_At_OnAttributeRender
;
576 public string AttrValue_At_AddAttribute
;
578 public Poker (TextWriter tw
)
583 protected override bool OnAttributeRender (string name
, string value, HtmlTextWriterAttribute key
)
585 AttrValue_At_OnAttributeRender
= value;
586 return base.OnAttributeRender (name
, value, key
);
589 protected override bool OnStyleAttributeRender (string name
, string value, HtmlTextWriterStyle key
)
591 StyleValue_At_OnStyleAttributeRender
= value;
592 return base.OnStyleAttributeRender (name
, value, key
);
595 protected override void AddStyleAttribute (string name
, string value, HtmlTextWriterStyle key
)
597 StyleValue_At_AddStyleAttribute
= value;
598 base.AddStyleAttribute (name
, value, key
);
601 protected override void AddAttribute (string name
, string value, HtmlTextWriterAttribute key
)
603 AttrValue_At_AddAttribute
= value;
604 base.AddAttribute (name
, value, key
);
610 public void TestOnAttributeRender() {
611 MyHttpTextWriter myw
= new MyHttpTextWriter (sw
);
613 myw
.AddAttribute ("border", "0");
614 myw
.AddAttribute ("BoRDeR", "0");
615 myw
.AddAttribute ("error", "0");
616 myw
.RenderBeginTag ("div");