44using System . Collections . Generic ;
55using System . Linq ;
66using Microsoft . AspNet . Mvc . Rendering ;
7+ using Microsoft . AspNet . Mvc . TagHelpers . Internal ;
78using Microsoft . AspNet . Razor . Runtime . TagHelpers ;
89using Xunit ;
910
@@ -19,20 +20,90 @@ public void MergeAttributes_DoesNotReplace_TagHelperOutputAttributeValues()
1920 "p" ,
2021 attributes : new Dictionary < string , string > ( ) ,
2122 content : string . Empty ) ;
22- var expectedAttribute = new KeyValuePair < string , string > ( "class " , "btn" ) ;
23+ var expectedAttribute = new KeyValuePair < string , string > ( "type " , "btn" ) ;
2324 tagHelperOutput . Attributes . Add ( expectedAttribute ) ;
2425
2526 var tagBuilder = new TagBuilder ( "p" ) ;
26- tagBuilder . Attributes . Add ( "class " , "hello" ) ;
27+ tagBuilder . Attributes . Add ( "type " , "hello" ) ;
2728
2829 // Act
29- TagHelperOutputHelper . MergeAttributes ( tagHelperOutput , tagBuilder ) ;
30+ TagHelperOutputHelper . MergeAttributes ( tagBuilder , tagHelperOutput ) ;
3031
3132 // Assert
3233 var attribute = Assert . Single ( tagHelperOutput . Attributes ) ;
3334 Assert . Equal ( expectedAttribute , attribute ) ;
3435 }
3536
37+ [ Fact ]
38+ public void MergeAttributes_AppendsClass_TagHelperOutputAttributeValues ( )
39+ {
40+ // Arrange
41+ var tagHelperOutput = new TagHelperOutput (
42+ "p" ,
43+ attributes : new Dictionary < string , string > ( ) ,
44+ content : string . Empty ) ;
45+ tagHelperOutput . Attributes . Add ( "class" , "Hello" ) ;
46+
47+ var tagBuilder = new TagBuilder ( "p" ) ;
48+ tagBuilder . Attributes . Add ( "class" , "btn" ) ;
49+
50+ var expectedAttribute = new KeyValuePair < string , string > ( "class" , "Hello btn" ) ;
51+
52+ // Act
53+ TagHelperOutputHelper . MergeAttributes ( tagBuilder , tagHelperOutput ) ;
54+
55+ // Assert
56+ var attribute = Assert . Single ( tagHelperOutput . Attributes ) ;
57+ Assert . Equal ( expectedAttribute , attribute ) ;
58+ }
59+
60+ [ Fact ]
61+ public void MergeAttributes_DoesNotEncode_TagHelperOutputAttributeValues ( )
62+ {
63+ // Arrange
64+ var tagHelperOutput = new TagHelperOutput (
65+ "p" ,
66+ attributes : new Dictionary < string , string > ( ) ,
67+ content : string . Empty ) ;
68+
69+ var tagBuilder = new TagBuilder ( "p" ) ;
70+ var expectedAttribute = new KeyValuePair < string , string > ( "visible" , "val < 3" ) ;
71+ tagBuilder . Attributes . Add ( expectedAttribute ) ;
72+
73+ // Act
74+ TagHelperOutputHelper . MergeAttributes ( tagBuilder , tagHelperOutput ) ;
75+
76+ // Assert
77+ var attribute = Assert . Single ( tagHelperOutput . Attributes ) ;
78+ Assert . Equal ( expectedAttribute , attribute ) ;
79+ }
80+
81+ [ Fact ]
82+ public void MergeAttributes_CopiesMultiple_TagHelperOutputAttributeValues ( )
83+ {
84+ // Arrange
85+ var tagHelperOutput = new TagHelperOutput (
86+ "p" ,
87+ attributes : new Dictionary < string , string > ( ) ,
88+ content : string . Empty ) ;
89+
90+ var tagBuilder = new TagBuilder ( "p" ) ;
91+ var expectedAttribute1 = new KeyValuePair < string , string > ( "class" , "btn" ) ;
92+ var expectedAttribute2 = new KeyValuePair < string , string > ( "class2" , "btn" ) ;
93+ tagBuilder . Attributes . Add ( expectedAttribute1 ) ;
94+ tagBuilder . Attributes . Add ( expectedAttribute2 ) ;
95+
96+ // Act
97+ TagHelperOutputHelper . MergeAttributes ( tagBuilder , tagHelperOutput ) ;
98+
99+ // Assert
100+ Assert . Equal ( 2 , tagHelperOutput . Attributes . Count ) ;
101+ var attribute = Assert . Single ( tagHelperOutput . Attributes , kvp => kvp . Key . Equals ( "class" ) ) ;
102+ Assert . Equal ( expectedAttribute1 . Value , attribute . Value ) ;
103+ attribute = Assert . Single ( tagHelperOutput . Attributes , kvp => kvp . Key . Equals ( "class2" ) ) ;
104+ Assert . Equal ( expectedAttribute2 . Value , attribute . Value ) ;
105+ }
106+
36107 [ Fact ]
37108 public void MergeAttributes_Maintains_TagHelperOutputAttributeValues ( )
38109 {
@@ -47,7 +118,7 @@ public void MergeAttributes_Maintains_TagHelperOutputAttributeValues()
47118 var tagBuilder = new TagBuilder ( "p" ) ;
48119
49120 // Act
50- TagHelperOutputHelper . MergeAttributes ( tagHelperOutput , tagBuilder ) ;
121+ TagHelperOutputHelper . MergeAttributes ( tagBuilder , tagHelperOutput ) ;
51122
52123 // Assert
53124 var attribute = Assert . Single ( tagHelperOutput . Attributes ) ;
@@ -70,12 +141,14 @@ public void MergeAttributes_Combines_TagHelperOutputAttributeValues()
70141 tagBuilder . Attributes . Add ( expectedBuilderAttribute ) ;
71142
72143 // Act
73- TagHelperOutputHelper . MergeAttributes ( tagHelperOutput , tagBuilder ) ;
144+ TagHelperOutputHelper . MergeAttributes ( tagBuilder , tagHelperOutput ) ;
74145
75146 // Assert
76147 Assert . Equal ( tagHelperOutput . Attributes . Count , 2 ) ;
77- Assert . Equal ( expectedOutputAttribute , tagHelperOutput . Attributes . First ( ) ) ;
78- Assert . Equal ( expectedBuilderAttribute , tagHelperOutput . Attributes . Last ( ) ) ;
148+ var attribute = Assert . Single ( tagHelperOutput . Attributes , kvp => kvp . Key . Equals ( "class" ) ) ;
149+ Assert . Equal ( expectedOutputAttribute . Value , attribute . Value ) ;
150+ attribute = Assert . Single ( tagHelperOutput . Attributes , kvp => kvp . Key . Equals ( "for" ) ) ;
151+ Assert . Equal ( expectedBuilderAttribute . Value , attribute . Value ) ;
79152 }
80153
81154 [ Fact ]
@@ -95,7 +168,7 @@ public void Merge_CombinesAllTagHelperOutputAndTagBuilderProperties()
95168 tagBuilder . InnerHtml = "Hello from tagBuilder." ;
96169
97170 // Act
98- TagHelperOutputHelper . Merge ( tagHelperOutput , tagBuilder ) ;
171+ TagHelperOutputHelper . Merge ( tagBuilder , tagHelperOutput ) ;
99172
100173 // Assert
101174 Assert . Equal ( "div" , tagHelperOutput . TagName ) ;
0 commit comments