Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 3b4fe50

Browse files
author
NTaylorMullen
committed
Addressed code review comments.
1 parent 3012a1a commit 3b4fe50

File tree

3 files changed

+91
-19
lines changed

3 files changed

+91
-19
lines changed

src/Microsoft.AspNet.Mvc.TagHelpers/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/Microsoft.AspNet.Mvc.TagHelpers/TagHelperOutputHelper.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using Microsoft.AspNet.Mvc.Rendering;
56
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
67

7-
namespace Microsoft.AspNet.Mvc.TagHelpers
8+
namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
89
{
9-
internal class TagHelperOutputHelper
10+
public class TagHelperOutputHelper
1011
{
11-
public static void Merge(TagHelperOutput tagHelperOutput, TagBuilder tagBuilder)
12+
public static void Merge(TagBuilder tagBuilder, TagHelperOutput tagHelperOutput)
1213
{
1314
tagHelperOutput.TagName = tagBuilder.TagName;
1415
tagHelperOutput.Content = tagBuilder.InnerHtml;
1516

16-
MergeAttributes(tagHelperOutput, tagBuilder);
17+
MergeAttributes(tagBuilder, tagHelperOutput);
1718
}
1819

19-
public static void MergeAttributes(TagHelperOutput tagHelperOutput, TagBuilder tagBuilder)
20+
public static void MergeAttributes(TagBuilder tagBuilder, TagHelperOutput tagHelperOutput)
2021
{
2122
foreach (var attribute in tagBuilder.Attributes)
2223
{
2324
if (!tagHelperOutput.Attributes.ContainsKey(attribute.Key))
2425
{
2526
tagHelperOutput.Attributes.Add(attribute.Key, attribute.Value);
2627
}
28+
else if (attribute.Key.Equals("class", StringComparison.Ordinal))
29+
{
30+
tagHelperOutput.Attributes["class"] += " " + attribute.Value;
31+
}
2732
}
2833
}
2934
}

test/Microsoft.AspNet.Mvc.TagHelpers.Test/TagHelperOutputHelperTest.cs

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.Linq;
66
using Microsoft.AspNet.Mvc.Rendering;
7+
using Microsoft.AspNet.Mvc.TagHelpers.Internal;
78
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
89
using 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

Comments
 (0)