-
Notifications
You must be signed in to change notification settings - Fork 73
/
HtmlTagTester.cs
1178 lines (978 loc) · 36.3 KB
/
HtmlTagTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Shouldly;
using Xunit;
using System.Text;
namespace HtmlTags.Testing
{
public class core_behavior_tests
{
[Fact]
public void render_a_tag()
{
var tag = new HtmlTag("p");
tag.ToString().ShouldBe("<p></p>");
}
[Fact]
public void render_original_case()
{
var tag = new HtmlTag("tAbLe");
tag.ToString().ShouldBe("<tAbLe></tAbLe>");
}
[Fact]
public void render_original_case_for_void_tags()
{
var tag = new HtmlTag("INPUT");
tag.ToString().ShouldBe("<INPUT>");
}
[Fact]
public void render_a_tag_with_inner_text()
{
var tag = new HtmlTag("p").Text("some text");
tag.ToString().ShouldBe("<p>some text</p>");
}
[Fact]
public void appending_inner_text()
{
var tag = new HtmlTag("p").AppendText("some text").AppendText(" more text");
tag.ToString().ShouldBe("<p>some text more text</p>");
}
[Fact]
public void appending_inner_text_with_tags()
{
var tag = new HtmlTag("p").AppendText("some text").Append("span").AppendText("more text");
tag.ToString().ShouldBe("<p>some text<span></span>more text</p>");
}
[Fact]
public void the_inner_text_is_html_encoded_by_default()
{
var tag = new HtmlTag("div");
tag.Text("<b>Hi</b>");
tag.ToString().ShouldBe("<div><b>Hi</b></div>");
}
[Fact]
public void can_opt_out_of_html_encoded_inner_text()
{
var tag = new HtmlTag("div");
tag.Text("<b>Hi</b>");
tag.Encoded(false);
tag.ToString().ShouldBe("<div><b>Hi</b></div>");
}
[Fact]
public void should_respect_subclass_encode_preference()
{
var tag = new NonEncodedTag("div");
tag.Text("<b>Hi</b>");
tag.ToString().ShouldBe("<div><b>Hi</b></div>");
}
[Fact]
public void implements_to_html_string_for_aspnet_4_compatibility()
{
var tag = new HtmlTag("p");
tag.ToHtmlString().ShouldBe("<p></p>");
}
[Fact]
public void has_closing_tag_by_default()
{
new HtmlTag("div").HasClosingTag().ShouldBeTrue();
}
[Fact]
public void renders_closing_tag_by_default()
{
new HtmlTag("div").ToString().ShouldBe("<div></div>");
}
[Fact]
public void do_not_write_closing_tag()
{
var tag = new HtmlTag("span").Id("id");
tag.NoClosingTag();
tag.ToString().ShouldBe("<span id=\"id\">");
}
[Fact]
public void when_no_closing_tag_then_has_is_false()
{
new HtmlTag("div").NoClosingTag().HasClosingTag().ShouldBeFalse();
}
[Fact]
public void when_no_tag_do_not_write_opening_or_closing_tag()
{
new HtmlTag("div").NoTag().AppendHtml("Hello")
.ToString().ShouldBe("Hello");
}
[Fact]
public void when_no_tag_HasClosingTag_is_false()
{
new HtmlTag("div").NoTag().HasClosingTag().ShouldBeFalse();
}
[Fact]
public void when_no_tag_HasTag_is_false()
{
new HtmlTag("div").NoTag().HasTag().ShouldBeFalse();
}
[Fact]
public void when_placeholder_do_not_write_opening_or_closing_tag()
{
HtmlTag.Placeholder().AppendHtml("Hello")
.ToString().ShouldBe("Hello");
}
[Fact]
public void when_placeholder_HasClosingTag_is_false()
{
HtmlTag.Placeholder().HasClosingTag().ShouldBeFalse();
}
[Fact]
public void when_placeholder_HasTag_is_false()
{
HtmlTag.Placeholder().HasTag().ShouldBeFalse();
}
[Fact]
public void tag_with_NoClosingTag_should_be_wrapped_correctly_with_tag_with_closingTag()
{
var wrapper = new HtmlTag("div");
var tag = new HtmlTag("input").NoClosingTag();
wrapper.Append(tag);
wrapper.ToString().ShouldBe("<div><input></div>");
}
[Fact]
public void initialize_tag_via_constructor()
{
var tag = new HtmlTag("div", x =>
{
x.Id("me");
x.Attr("title", "tooltip");
});
tag.ToString().ShouldBe("<div id=\"me\" title=\"tooltip\"></div>");
}
[Fact]
public void recognize_input_elements()
{
new HtmlTag("span").IsInputElement().ShouldBeFalse();
new HtmlTag("input").IsInputElement().ShouldBeTrue();
new HtmlTag("select").IsInputElement().ShouldBeTrue();
new HtmlTag("textarea").IsInputElement().ShouldBeTrue();
}
//
// HTML attributes
//
[Fact]
public void render_a_single_attribute()
{
var tag = new HtmlTag("table").Attr("cellPadding", 2);
tag.ToString().ShouldBe("<table cellPadding=\"2\"></table>");
}
[Fact]
public void render_multiple_attributes()
{
var tag = new HtmlTag("table").Attr("cellPadding", "2").Attr("cellSpacing", "3");
tag.ToString().ShouldBe("<table cellPadding=\"2\" cellSpacing=\"3\"></table>");
}
[Fact]
public void attributes_are_encoded_by_default()
{
const string options = "options: availableMeals, optionsText: 'mealName'";
var expectedAfterEncodingText = options.Replace("'", "'");
var tag = new HtmlTag("div").Attr("data-bind", options);
tag.ToString().ShouldContain(expectedAfterEncodingText);
}
[Fact]
public void attributes_can_be_unencoded_if_needed()
{
const string options = "options: availableMeals, optionsText: 'mealName'";
var tag = new HtmlTag("div").UnencodedAttr("bind", options);
tag.ToString().ShouldContain(options);
}
[Fact]
public void remove_attribute()
{
var tag = new HtmlTag("div");
tag.Attr("foo", "bar");
tag.HasAttr("foo").ShouldBeTrue();
tag.RemoveAttr("foo");
tag.HasAttr("foo").ShouldBeFalse();
}
[Fact]
public void render_id()
{
var tag = new HtmlTag("div").Id("theDiv");
tag.ToString().ShouldBe("<div id=\"theDiv\"></div>");
}
[Fact]
public void retrieve_a_set_id()
{
var tag = new HtmlTag("div").Id("the-div");
tag.Id().ShouldBe("the-div");
}
[Fact]
public void set_the_title_attribute()
{
var tag = new HtmlTag("div").Title("My Title");
tag.ToString().ShouldBe("<div title=\"My Title\"></div>");
}
[Fact]
public void retrieve_the_title()
{
var tag = new HtmlTag("div").Title("My Title");
tag.Title().ShouldBe("My Title");
}
[Fact]
public void attr_can_be_used_to_add_css_class()
{
var tag = new HtmlTag("a");
tag.Attr("class", "test-class");
tag.HasClass("test-class").ShouldBeTrue();
}
[Fact]
public void attr_add_multiple_classes_with_space_separated_classes()
{
var tag = new HtmlTag("a");
tag.AddClass("added-class");
tag.Attr("class", "test-class1 test-class2");
tag.HasClass("added-class").ShouldBeTrue();
tag.HasClass("test-class1").ShouldBeTrue();
tag.HasClass("test-class2").ShouldBeTrue();
}
[Fact]
public void attr_add_multiple_classes_with_multiple_space_separated_classes()
{
var tag = new HtmlTag("a");
tag.Attr("class", "test-class1 test-class2 test-class3 test-class4 ");
tag.HasClass("test-class1").ShouldBeTrue();
tag.HasClass("test-class2").ShouldBeTrue();
tag.HasClass("test-class3").ShouldBeTrue();
tag.HasClass("test-class4").ShouldBeTrue();
tag.HasClass(string.Empty).ShouldBeFalse();
}
[Fact]
public void add_multiple_classes_at_once_with_duplicates()
{
var tag = new HtmlTag("div").Text("text");
tag.AddClass("a b c a c");
tag.ToString().ShouldBe("<div class=\"a b c\">text</div>");
}
[Fact]
public void add_multiple_classes_at_once()
{
var tag = new HtmlTag("div").Text("text");
tag.AddClass("a b c");
tag.ToString().ShouldBe("<div class=\"a b c\">text</div>");
}
[Fact]
public void add_multiple_classes_at_once_with_multiple_spaces()
{
var tag = new HtmlTag("div").Text("text");
tag.AddClass("a b c d e ");
tag.ToString().ShouldBe("<div class=\"a b c d e\">text</div>");
}
[Fact]
public void replace_a_single_attribute()
{
var tag = new HtmlTag("table")
.Attr("cellPadding", 2)
.Attr("cellPadding", 5);
tag.ToString().ShouldBe("<table cellPadding=\"5\"></table>");
}
[Fact]
public void set_an_attribute_to_null_should_remove_the_attribute()
{
var tag = new HtmlTag("div");
tag.Attr("name", "bill");
tag.HasAttr("name").ShouldBeTrue();
tag.Attr("name", null);
tag.HasAttr("name").ShouldBeFalse();
}
[Fact]
public void set_the_class_attribute_to_null_should_remove_all_classes()
{
var tag = new HtmlTag("div");
tag.AddClass("first");
tag.AddClass("second");
tag.Attr("class", null);
tag.ToString().ShouldBe("<div></div>");
}
[Fact]
public void set_the_class_attribute_to_empty_string_should_remove_all_classes()
{
var tag = new HtmlTag("div");
tag.AddClass("first");
tag.AddClass("second");
tag.Attr("class", string.Empty);
tag.ToString().ShouldBe("<div></div>");
}
[Fact]
public void removing_the_class_attribute_should_remove_all_classes()
{
var tag = new HtmlTag("div");
tag.AddClass("first");
tag.AddClass("second");
tag.RemoveAttr("class");
tag.ToString().ShouldBe("<div></div>");
}
[Fact]
public void should_report_has_class_attribute_if_any_classes_added()
{
var tag = new HtmlTag("div");
tag.HasAttr("class").ShouldBeFalse();
tag.AddClass("first");
tag.HasAttr("class").ShouldBeTrue();
}
[Fact]
public void retrieve_a_non_existing_attr_should_return_an_empty_string()
{
new HtmlTag("div").Attr("new").ShouldBe(string.Empty);
}
[Fact]
public void boolean_attr() {
var tag = new HtmlTag("input").BooleanAttr("required");
tag.HasAttr("required").ShouldBeTrue();
}
[Fact]
public void render_boolean_attr() {
var tag = new HtmlTag("input").BooleanAttr("required");
tag.ToString().ShouldBe("<input required=\"\">");
}
[Fact]
public void set_an_attribute_to_empty_string_should_not_remove_the_attribute() {
var tag = new HtmlTag("div");
tag.Attr("name", "bill");
tag.HasAttr("name").ShouldBeTrue();
tag.Attr("name", string.Empty);
tag.HasAttr("name").ShouldBeTrue();
}
[Fact]
public void retrieve_an_empty_attr_should_return_an_empty_string() {
var tag = new HtmlTag("option");
tag.Attr("value", string.Empty);
tag.Attr("value").ShouldBe(string.Empty);
}
[Fact]
public void render_empty_attr() {
var tag = new HtmlTag("option").Attr("value", string.Empty);
tag.ToString().ShouldBe("<option value=\"\"></option>");
}
//
// CSS classes
//
[Fact]
public void add_a_class()
{
var tag = new HtmlTag("div");
tag.AddClass("a");
tag.ToString().ShouldBe("<div class=\"a\"></div>");
}
[Fact]
public void render_multiple_classes()
{
var tag = new HtmlTag("div").Text("text");
tag.AddClass("a");
tag.AddClass("b");
tag.AddClass("c");
tag.ToString().ShouldBe("<div class=\"a b c\">text</div>");
}
[Fact]
public void render_multiple_classes_with_a_single_method_call()
{
var tag = new HtmlTag("div").Text("text");
tag.AddClasses("a", "b", "c");
tag.ToString().ShouldBe("<div class=\"a b c\">text</div>");
}
[Fact]
public void render_multiple_classes_from_a_sequence()
{
var tag = new HtmlTag("div").Text("text");
var sequenceOfClasses = new List<string>{"a", "b", "c"};
tag.AddClasses(sequenceOfClasses);
tag.ToString().ShouldBe("<div class=\"a b c\">text</div>");
}
[Fact]
public void remove_a_class()
{
var tag = new HtmlTag("div").AddClasses("a", "b", "c");
tag.RemoveClass("b");
tag.ToString().ShouldBe("<div class=\"a c\"></div>");
}
[Fact]
public void render_a_single_class_even_though_it_is_registered_more_than_once()
{
var tag = new HtmlTag("div").Text("text");
tag.AddClass("a");
tag.ToString().ShouldBe("<div class=\"a\">text</div>");
tag.AddClass("a");
tag.ToString().ShouldBe("<div class=\"a\">text</div>");
}
[Fact]
public void can_get_classes_from_tag()
{
var tag = new HtmlTag("div");
var classes = new[] { "class1", "class2" };
tag.AddClasses(classes);
var tagClasses = tag.GetClasses();
tagClasses.ShouldHaveCount(2);
tagClasses.Except(classes).ShouldHaveCount(0);
}
[Fact]
public void do_not_allow_special_characters_in_class_names()
{
CssClassNameValidator.AllowInvalidCssClassNames = false;
var tag = new HtmlTag("div").Text("text");
Exception<ArgumentException>.ShouldBeThrownBy(() => { tag.AddClass("$test@@"); });
}
[Fact]
public void do_not_allow_start_with_number_in_class_names()
{
CssClassNameValidator.AllowInvalidCssClassNames = false;
var tag = new HtmlTag("div").Text("text");
Exception<ArgumentException>.ShouldBeThrownBy(() => { tag.AddClass("4test"); });
}
[Fact]
public void do_not_allow_first_double_dashes_in_class_names()
{
CssClassNameValidator.AllowInvalidCssClassNames = false;
var tag = new HtmlTag("div").Text("text");
Exception<ArgumentException>.ShouldBeThrownBy(() => { tag.AddClass("--test"); });
}
[Fact]
public void class_name_must_have_at_least_two_chars_if_starts_with_dash()
{
CssClassNameValidator.AllowInvalidCssClassNames = false;
var tag = new HtmlTag("div").Text("text");
Exception<ArgumentException>.ShouldBeThrownBy(() => { tag.AddClass("-"); });
}
[Fact]
public void valid_class_names()
{
var tag = new HtmlTag("div").Text("text");
tag.AddClass("-test");
tag.ToString().ShouldBe("<div class=\"-test\">text</div>");
tag.AddClass("_test");
tag.ToString().ShouldBe("<div class=\"-test _test\">text</div>");
tag.AddClass("TEST_2-test");
tag.ToString().ShouldBe("<div class=\"-test _test TEST_2-test\">text</div>");
tag.AddClass("-just-4-test");
tag.ToString().ShouldBe("<div class=\"-test _test TEST_2-test -just-4-test\">text</div>");
}
[Fact]
public void do_allow_a_class_that_is_a_json_blob_with_spaces()
{
var tag = new HtmlTag("div").AddClass("{a:1, a:2}");
tag.ToString().ShouldContain("class=\"{a:1, a:2}\"");
tag = new HtmlTag("div").AddClass("[1, 2, 3]");
tag.ToString().ShouldContain("class=\"[1, 2, 3]\"");
}
[Fact]
public void class_name_with_wrong_json()
{
var tag = new HtmlTag("div");
Exception<ArgumentException>.ShouldBeThrownBy(() => { tag.AddClass("[1,2,3}"); });
Exception<ArgumentException>.ShouldBeThrownBy(() => { tag.AddClass("{a:1, a:2]"); });
}
//
// inline styles
//
[Fact]
public void write_styles()
{
new HtmlTag("div")
.Style("padding-left", "20px")
.Style("padding-right", "30px")
.ToString()
.ShouldBe("<div style=\"padding-left:20px;padding-right:30px\"></div>");
}
[Fact]
public void removing_the_style_attribute_should_remove_all_styles()
{
var tag = new HtmlTag("div");
tag.Style("padding-left", "20px").Style("padding-right", "30px");
tag.HasStyle("padding-left").ShouldBeTrue();
tag.HasStyle("padding-right").ShouldBeTrue();
tag.RemoveAttr("style");
tag.HasStyle("padding-left").ShouldBeFalse();
tag.HasStyle("padding-right").ShouldBeFalse();
}
[Fact]
public void setting_the_style_attribute_to_empty_string_should_remove_all_styles()
{
var tag = new HtmlTag("div");
tag.Style("padding-left", "20px").Style("padding-right", "30px");
tag.HasStyle("padding-left").ShouldBeTrue();
tag.Attr("style", string.Empty);
tag.HasStyle("padding-left").ShouldBeFalse();
}
[Fact]
public void setting_the_style_attribute_to_null_should_remove_all_styles()
{
var tag = new HtmlTag("div");
tag.Style("padding-left", "20px").Style("padding-right", "30px");
tag.HasStyle("padding-left").ShouldBeTrue();
tag.Attr("style", null);
tag.HasStyle("padding-left").ShouldBeFalse();
}
[Fact]
public void hasattr_should_be_true_for_style_if_any_styles_exist()
{
var tag = new HtmlTag("div");
tag.HasAttr("style").ShouldBeFalse();
tag.Style("padding-left", "20px");
tag.HasAttr("style").ShouldBeTrue();
}
[Fact]
public void attr_should_not_encode()
{
var tag = new HtmlTag("a");
tag.Attr("href", "#name");
tag.ToString().ShouldBe("<a href=\"#name\"></a>");
}
}
public class children_tests
{
[Fact]
public void add_a_child_tag()
{
var tag = new HtmlTag("div").Append(new HtmlTag("span").Text("something"));
tag.ToString().ShouldBe("<div><span>something</span></div>");
}
[Fact]
public void create_a_new_tag_as_child_of_existing_tag()
{
var existing = new HtmlTag("div");
var newTag = new HtmlTag("span", existing);
newTag.Text("something");
existing.ToString().ShouldBe("<div><span>something</span></div>");
}
[Fact]
public void append_adds_a_new_child_and_return_the_original()
{
var parent = new HtmlTag("div");
var resultOfAppend = parent.Append("p");
resultOfAppend.ShouldBeSameAs(parent);
parent.ToString().ShouldBe("<div><p></p></div>");
}
[Fact]
public void append_nested_children()
{
var parent = new HtmlTag("div");
parent.Append("p > span");
parent.ToString().ShouldBe("<div><p><span></span></p></div>");
}
[Fact]
public void append_all_tags_from_a_tag_source()
{
var tagSource = new TagList(new[]{new HtmlTag("br"), new HtmlTag("hr") });
var parent = new HtmlTag("div");
parent.Append(tagSource);
parent.ToString().ShouldBe("<div><br><hr></div>");
}
[Fact]
public void can_control_br_tag_behavior_even_if_using_regular_html_tag()
{
var tagSource = new TagList(new[] { new HtmlTag("br"), new HtmlTag("hr") });
var parent = new HtmlTag("div");
parent.Append(tagSource);
parent.ToString().ShouldBe("<div><br><hr></div>");
}
[Fact]
public void append_all_tags_from_a_sequence()
{
var sequence = new[] { new HtmlTag("br"), new HtmlTag("hr") };
var parent = new HtmlTag("div");
parent.Append(sequence);
parent.ToString().ShouldBe("<div><br><hr></div>");
}
[Fact]
public void append_and_modify_the_innermost_child()
{
var tag = new HtmlTag("body").Append("div > form > input", child => child.Id("first-name"));
tag.ToString().ShouldBe("<body><div><form><input id=\"first-name\"></form></div></body>");
}
[Fact]
public void insert_a_new_child_tag_as_the_first_child()
{
var tag = new HtmlTag("div");
tag.Append("span");
tag.InsertFirst(new HtmlTag("p"));
tag.ToString().ShouldBe("<div><p></p><span></span></div>");
}
[Fact]
public void add_returns_the_newly_created_child_tag()
{
var original = new HtmlTag("div");
var child = original.Add("span");
child.ToString().ShouldBe("<span></span>");
original.ToString().ShouldBe("<div><span></span></div>");
}
[Fact]
public void add_and_return_a_child_tag_by_type()
{
var original = new HtmlTag("div");
var child = original.Add<HiddenTag>();
child.ToString().ShouldBe("<input type=\"hidden\">");
original.ToString().ShouldBe("<div><input type=\"hidden\"></div>");
}
[Fact]
public void add_multiple_levels_of_nesting()
{
var tag = new HtmlTag("table");
tag.Add("tbody/tr/td").Text("some text");
tag.ToString()
.ShouldBe("<table><tbody><tr><td>some text</td></tr></tbody></table>");
}
[Fact]
public void nesting_also_supports_jquery_direct_child_syntax()
{
var tag = new HtmlTag("table");
tag.Add("tbody > tr > td").Text("some text");
tag.ToString()
.ShouldBe("<table><tbody><tr><td>some text</td></tr></tbody></table>");
}
[Fact]
public void add_multiple_levels_of_nesting_with_initializer()
{
var tag = new HtmlTag("html").Modify(x =>
{
x.Add("head", head =>
{
head.Add("title").Text("The title");
head.Add("style").Text("the style");
});
x.Add("body/div").Text("inner text of div");
});
tag.ToString().ShouldBe(
"<html><head><title>The title</title><style>the style</style></head><body><div>inner text of div</div></body></html>");
}
[Fact]
public void remove_existing_children_and_add_new_children()
{
var tag = new HtmlTag("div").Append("br").Append("p");
tag.Children.ShouldHaveCount(2);
tag.ReplaceChildren(new HtmlTag("hr"));
tag.Children.ShouldHaveCount(1);
tag.ToString().ShouldBe("<div><hr></div>");
}
[Fact]
public void does_not_return_children_or_siblings_when_treated_as_a_tag_source()
{
var original = new HtmlTag("div");
original.Add("span");
original.After(new HtmlTag("p"));
var tagSource = (ITagSource) original;
var allTags = tagSource.AllTags().ToArray();
allTags.ShouldHaveCount(1);
allTags[0].ShouldBeSameAs(original);
}
}
public class sibling_tests
{
[Fact]
public void set_the_next_sibling_via_next_property()
{
var tag = new HtmlTag("span").Text("something");
tag.Next = new HtmlTag("span").Text("next");
tag.ToString().ShouldBe("<span>something</span><span>next</span>");
}
[Fact]
public void set_the_next_sibling_via_next_property_should_overwrite_any_existing_sibling()
{
var tag = new HtmlTag("span").Text("something");
tag.Next = new HtmlTag("span").Text("next");
tag.Next = new HtmlTag("span").Text("brother");
tag.ToString().ShouldBe("<span>something</span><span>brother</span>");
}
[Fact]
public void set_the_next_sibling_via_the_after_method()
{
var tag = new HtmlTag("span").Text("something");
tag.After(new HtmlTag("span").Text("next"));
tag.ToString().ShouldBe("<span>something</span><span>next</span>");
}
[Fact]
public void set_the_next_sibling_via_the_after_method_preserves_any_existing_sibling()
{
var tag = new HtmlTag("span").Text("something");
tag.After(new HtmlTag("span").Text("first brother"));
tag.After(new HtmlTag("span").Text("second brother"));
tag.ToString().ShouldBe("<span>something</span><span>second brother</span><span>first brother</span>");
}
[Fact]
public void retrieve_the_next_sibling()
{
var tag = new HtmlTag("span").Text("something");
var nextSibling = new HtmlTag("span").Text("first brother");
tag.After(nextSibling);
tag.After().ShouldBeSameAs(nextSibling);
}
[Fact]
public void get_the_next_sibling_via_property_is_equivelent_to_after()
{
var tag = new HtmlTag("span").Text("something");
var nextSibling = new HtmlTag("span").Text("first brother");
tag.After(nextSibling);
tag.Next.ShouldBeSameAs(nextSibling);
}
}
public class output_control_tests
{
[Fact]
public void hide_renders_the_tag_but_sets_style_to_display_none()
{
var tag = new HtmlTag("div").Hide();
tag.Style("display").ShouldBe("none");
}
[Fact]
public void render_set_to_true_by_default()
{
var tag = new HtmlTag("div");
tag.Render().ShouldBeTrue();
tag.ToString().ShouldBe("<div></div>");
}
[Fact]
public void render_set_to_false_always_returns_an_empty_string()
{
new HtmlTag("div").Render(false).ToString().ShouldBe("");
}
[Fact]
public void tags_are_authorized_by_default()
{
new HtmlTag("div").Authorized().ShouldBeTrue();
}
[Fact]
public void unauthorized_tags_will_always_return_an_empty_string_regardless_of_render_setting()
{
var tag = new HtmlTag("div").Authorized(false);
tag.ToString().ShouldBeEmpty();
tag.Render(true).ToString().ShouldBeEmpty();
tag.Render(false).ToString().ShouldBeEmpty();
}
[Fact]
public void Empty_can_be_used_as_a_non_rendering_placeholder()
{
var tag = new HtmlTag("body").Append(HtmlTag.Empty());
tag.ToString().Equals("<body></body>");
}
[Fact]
public void rendering_a_NoTag_should_not_render_anything()
{
new NoTag().AddClass("important").Text("foo").ToString().ShouldBe("");
}
}
public class wrapping_tags_tests
{
[Fact]
public void wrap_with_returns_a_new_tag_with_the_original_as_the_first_child()
{
var tag = new HtmlTag("a");
var wrapped = tag.WrapWith("span");
wrapped.ShouldNotBeSameAs(tag);
wrapped.TagName().ShouldBe("span");
wrapped.FirstChild().ShouldBeSameAs(tag);
}
[Fact]
public void wrap_with_another_tag_returns_the_wrapping_tag_with_the_original_as_its_child()
{
var tag = new HtmlTag("a");
var wrapper = new HtmlTag("span");
tag.WrapWith(wrapper).ShouldBeSameAs(wrapper);
wrapper.FirstChild().ShouldBeSameAs(tag);
}
[Fact]
public void wrapped_tag_will_be_rendered_if_the_original_tag_was_to_be_rendered()
{
var tag = new HtmlTag("a");
tag.Render().ShouldBeTrue();
tag.WrapWith("span").Render().ShouldBeTrue();
}
[Fact]
public void wrapped_tag_will_not_be_rendered_if_the_original_tag_was_not_to_be_rendered()
{
var tag = new HtmlTag("a").Render(false);
tag.WrapWith("span").Render().ShouldBeFalse();
}
[Fact]
public void wrapped_tag_will_be_authorized_if_the_original_tag_was_authorized()
{
var tag = new HtmlTag("a");
tag.Authorized().ShouldBeTrue();
tag.WrapWith("span").Authorized().ShouldBeTrue();
}
[Fact]
public void wrapped_tag_will_not_be_authorized_if_the_original_tag_was_not_authorized()
{
var tag = new HtmlTag("a").Authorized(false);
tag.WrapWith("span").Authorized().ShouldBeFalse();
}
}
public class custom_data_attribute_tests
{
[Fact]
public void add_custom_data()
{
var tag = new HtmlTag("div");
tag.Data("integer", 1);
tag.Data("string", "b-value");
tag.Data("bool", true);
tag.Data("setting", new ListValue {Value = "RED", Display = "Red"});
tag.ToString().ShouldBe("<div data-integer=\"1\" data-string=\"b-value\" data-bool=\"true\" data-setting=\"{"Display":"Red","Value":"RED"}\"></div>");
}
[Fact]
public void retrieve_a_previously_set_custom_data_value()
{
var tag = new HtmlTag("div");
tag.Data("age", 42);
tag.Data("age").ShouldBe(42);
}
[Fact]
public void retrieve_a_non_existing_custom_data_should_return_null()
{
var tag = new HtmlTag("div");
tag.Data("name").ShouldBeNull();
}
[Fact]