4
4
using System . Collections . Generic ;
5
5
using System . Linq ;
6
6
using Microsoft . AspNet . Mvc . Rendering ;
7
+ using Microsoft . AspNet . Mvc . TagHelpers . Internal ;
7
8
using Microsoft . AspNet . Razor . Runtime . TagHelpers ;
8
9
using Xunit ;
9
10
@@ -19,20 +20,90 @@ public void MergeAttributes_DoesNotReplace_TagHelperOutputAttributeValues()
19
20
"p" ,
20
21
attributes : new Dictionary < string , string > ( ) ,
21
22
content : string . Empty ) ;
22
- var expectedAttribute = new KeyValuePair < string , string > ( "class " , "btn" ) ;
23
+ var expectedAttribute = new KeyValuePair < string , string > ( "type " , "btn" ) ;
23
24
tagHelperOutput . Attributes . Add ( expectedAttribute ) ;
24
25
25
26
var tagBuilder = new TagBuilder ( "p" ) ;
26
- tagBuilder . Attributes . Add ( "class " , "hello" ) ;
27
+ tagBuilder . Attributes . Add ( "type " , "hello" ) ;
27
28
28
29
// Act
29
- TagHelperOutputHelper . MergeAttributes ( tagHelperOutput , tagBuilder ) ;
30
+ TagHelperOutputHelper . MergeAttributes ( tagBuilder , tagHelperOutput ) ;
30
31
31
32
// Assert
32
33
var attribute = Assert . Single ( tagHelperOutput . Attributes ) ;
33
34
Assert . Equal ( expectedAttribute , attribute ) ;
34
35
}
35
36
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
+
36
107
[ Fact ]
37
108
public void MergeAttributes_Maintains_TagHelperOutputAttributeValues ( )
38
109
{
@@ -47,7 +118,7 @@ public void MergeAttributes_Maintains_TagHelperOutputAttributeValues()
47
118
var tagBuilder = new TagBuilder ( "p" ) ;
48
119
49
120
// Act
50
- TagHelperOutputHelper . MergeAttributes ( tagHelperOutput , tagBuilder ) ;
121
+ TagHelperOutputHelper . MergeAttributes ( tagBuilder , tagHelperOutput ) ;
51
122
52
123
// Assert
53
124
var attribute = Assert . Single ( tagHelperOutput . Attributes ) ;
@@ -70,12 +141,14 @@ public void MergeAttributes_Combines_TagHelperOutputAttributeValues()
70
141
tagBuilder . Attributes . Add ( expectedBuilderAttribute ) ;
71
142
72
143
// Act
73
- TagHelperOutputHelper . MergeAttributes ( tagHelperOutput , tagBuilder ) ;
144
+ TagHelperOutputHelper . MergeAttributes ( tagBuilder , tagHelperOutput ) ;
74
145
75
146
// Assert
76
147
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 ) ;
79
152
}
80
153
81
154
[ Fact ]
@@ -95,7 +168,7 @@ public void Merge_CombinesAllTagHelperOutputAndTagBuilderProperties()
95
168
tagBuilder . InnerHtml = "Hello from tagBuilder." ;
96
169
97
170
// Act
98
- TagHelperOutputHelper . Merge ( tagHelperOutput , tagBuilder ) ;
171
+ TagHelperOutputHelper . Merge ( tagBuilder , tagHelperOutput ) ;
99
172
100
173
// Assert
101
174
Assert . Equal ( "div" , tagHelperOutput . TagName ) ;
0 commit comments