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

Commit 0b48e7f

Browse files
author
N. Taylor Mullen
committed
Add copy helpers for TagHelperOutput from TagBuilder.
- Added a new test project for tag helper specific code. - Tested out Merge and MergeAttributes. #1319
1 parent 11c1354 commit 0b48e7f

File tree

6 files changed

+198
-1
lines changed

6 files changed

+198
-1
lines changed

Mvc.sln

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 14
4-
VisualStudioVersion = 14.0.22013.1
4+
VisualStudioVersion = 14.0.22208.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{DAAE4C74-D06F-4874-A166-33305D2643CE}"
77
EndProject
@@ -84,6 +84,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ApiExplorerWebSite", "test\
8484
EndProject
8585
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "VersioningWebSite", "test\WebSites\VersioningWebSite\VersioningWebSite.kproj", "{C6304029-78C8-4604-99BE-2078DCA1DD36}"
8686
EndProject
87+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ReflectedModelWebSite", "test\WebSites\ReflectedModelWebSite\ReflectedModelWebSite.kproj", "{C2EF54F8-8886-4260-A322-44F76245F95D}"
88+
EndProject
8789
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TagHelpersWebSite", "test\WebSites\TagHelpersWebSite\TagHelpersWebSite.kproj", "{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}"
8890
EndProject
8991
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "FilesWebSite", "test\WebSites\FilesWebSite\FilesWebSite.kproj", "{0EF9860B-10D7-452F-B0F4-A405B88BEBB3}"
@@ -101,6 +103,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TagHelperSample.Web", "samp
101103
EndProject
102104
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Mvc.TagHelpers", "src\Microsoft.AspNet.Mvc.TagHelpers\Microsoft.AspNet.Mvc.TagHelpers.kproj", "{B2347320-308E-4D2B-AEC8-005DFA68B0C9}"
103105
EndProject
106+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Mvc.TagHelpers.Test", "test\Microsoft.AspNet.Mvc.TagHelpers.Test\Microsoft.AspNet.Mvc.TagHelpers.Test.kproj", "{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}"
107+
EndProject
104108
Global
105109
GlobalSection(SolutionConfigurationPlatforms) = preSolution
106110
Debug|Any CPU = Debug|Any CPU
@@ -551,6 +555,16 @@ Global
551555
{B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
552556
{B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|Mixed Platforms.Build.0 = Release|Any CPU
553557
{B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|x86.ActiveCfg = Release|Any CPU
558+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
559+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Debug|Any CPU.Build.0 = Debug|Any CPU
560+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
561+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
562+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Debug|x86.ActiveCfg = Debug|Any CPU
563+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Release|Any CPU.ActiveCfg = Release|Any CPU
564+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Release|Any CPU.Build.0 = Release|Any CPU
565+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
566+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Release|Mixed Platforms.Build.0 = Release|Any CPU
567+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Release|x86.ActiveCfg = Release|Any CPU
554568
EndGlobalSection
555569
GlobalSection(SolutionProperties) = preSolution
556570
HideSolutionNode = FALSE
@@ -601,5 +615,6 @@ Global
601615
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
602616
{2223120F-D675-40DA-8CD8-11DC14A0B2C7} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
603617
{B2347320-308E-4D2B-AEC8-005DFA68B0C9} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
618+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
604619
EndGlobalSection
605620
EndGlobal
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
[assembly: InternalsVisibleTo("Microsoft.AspNet.Mvc.TagHelpers.Test")]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNet.Mvc.Rendering;
5+
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
6+
7+
namespace Microsoft.AspNet.Mvc.TagHelpers
8+
{
9+
internal class TagHelperOutputHelper
10+
{
11+
public static void Merge(TagHelperOutput tagHelperOutput, TagBuilder tagBuilder)
12+
{
13+
tagHelperOutput.TagName = tagBuilder.TagName;
14+
tagHelperOutput.Content = tagBuilder.InnerHtml;
15+
16+
MergeAttributes(tagHelperOutput, tagBuilder);
17+
}
18+
19+
public static void MergeAttributes(TagHelperOutput tagHelperOutput, TagBuilder tagBuilder)
20+
{
21+
foreach (var attribute in tagBuilder.Attributes)
22+
{
23+
if (!tagHelperOutput.Attributes.ContainsKey(attribute.Key))
24+
{
25+
tagHelperOutput.Attributes.Add(attribute.Key, attribute.Value);
26+
}
27+
}
28+
}
29+
}
30+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Microsoft.AspNet.Mvc.Rendering;
7+
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
8+
using Xunit;
9+
10+
namespace Microsoft.AspNet.Mvc.TagHelpers
11+
{
12+
public class TagHelperOutputHelperTest
13+
{
14+
[Fact]
15+
public void MergeAttributes_DoesNotReplace_TagHelperOutputAttributeValues()
16+
{
17+
// Arrange
18+
var tagHelperOutput = new TagHelperOutput(
19+
"p",
20+
attributes: new Dictionary<string, string>(),
21+
content: string.Empty);
22+
var expectedAttribute = new KeyValuePair<string, string>("class", "btn");
23+
tagHelperOutput.Attributes.Add(expectedAttribute);
24+
25+
var tagBuilder = new TagBuilder("p");
26+
tagBuilder.Attributes.Add("class", "hello");
27+
28+
// Act
29+
TagHelperOutputHelper.MergeAttributes(tagHelperOutput, tagBuilder);
30+
31+
// Assert
32+
var attribute = Assert.Single(tagHelperOutput.Attributes);
33+
Assert.Equal(expectedAttribute, attribute);
34+
}
35+
36+
[Fact]
37+
public void MergeAttributes_Maintains_TagHelperOutputAttributeValues()
38+
{
39+
// Arrange
40+
var tagHelperOutput = new TagHelperOutput(
41+
"p",
42+
attributes: new Dictionary<string, string>(),
43+
content: string.Empty);
44+
var expectedAttribute = new KeyValuePair<string, string>("class", "btn");
45+
tagHelperOutput.Attributes.Add(expectedAttribute);
46+
47+
var tagBuilder = new TagBuilder("p");
48+
49+
// Act
50+
TagHelperOutputHelper.MergeAttributes(tagHelperOutput, tagBuilder);
51+
52+
// Assert
53+
var attribute = Assert.Single(tagHelperOutput.Attributes);
54+
Assert.Equal(expectedAttribute, attribute);
55+
}
56+
57+
[Fact]
58+
public void MergeAttributes_Combines_TagHelperOutputAttributeValues()
59+
{
60+
// Arrange
61+
var tagHelperOutput = new TagHelperOutput(
62+
"p",
63+
attributes: new Dictionary<string, string>(),
64+
content: string.Empty);
65+
var expectedOutputAttribute = new KeyValuePair<string, string>("class", "btn");
66+
tagHelperOutput.Attributes.Add(expectedOutputAttribute);
67+
68+
var tagBuilder = new TagBuilder("p");
69+
var expectedBuilderAttribute = new KeyValuePair<string, string>("for", "hello");
70+
tagBuilder.Attributes.Add(expectedBuilderAttribute);
71+
72+
// Act
73+
TagHelperOutputHelper.MergeAttributes(tagHelperOutput, tagBuilder);
74+
75+
// Assert
76+
Assert.Equal(tagHelperOutput.Attributes.Count, 2);
77+
Assert.Equal(expectedOutputAttribute, tagHelperOutput.Attributes.First());
78+
Assert.Equal(expectedBuilderAttribute, tagHelperOutput.Attributes.Last());
79+
}
80+
81+
[Fact]
82+
public void Merge_CombinesAllTagHelperOutputAndTagBuilderProperties()
83+
{
84+
// Arrange
85+
var tagHelperOutput = new TagHelperOutput(
86+
"p",
87+
attributes: new Dictionary<string, string>(),
88+
content: "Hello from tagHelperOutput");
89+
var expectedOutputAttribute = new KeyValuePair<string, string>("class", "btn");
90+
tagHelperOutput.Attributes.Add(expectedOutputAttribute);
91+
92+
var tagBuilder = new TagBuilder("div");
93+
var expectedBuilderAttribute = new KeyValuePair<string, string>("for", "hello");
94+
tagBuilder.Attributes.Add(expectedBuilderAttribute);
95+
tagBuilder.InnerHtml = "Hello from tagBuilder.";
96+
97+
// Act
98+
TagHelperOutputHelper.Merge(tagHelperOutput, tagBuilder);
99+
100+
// Assert
101+
Assert.Equal("div", tagHelperOutput.TagName);
102+
Assert.Equal("Hello from tagBuilder.", tagHelperOutput.Content);
103+
Assert.Equal(tagHelperOutput.Attributes.Count, 2);
104+
Assert.Equal(expectedOutputAttribute, tagHelperOutput.Attributes.First());
105+
Assert.Equal(expectedBuilderAttribute, tagHelperOutput.Attributes.Last());
106+
}
107+
}
108+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="__ToolsVersion__" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>eb1c7de2-b8bc-4b01-8a09-162db15a0f65</ProjectGuid>
10+
<OutputType>Library</OutputType>
11+
</PropertyGroup>
12+
13+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'" Label="Configuration">
14+
</PropertyGroup>
15+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'" Label="Configuration">
16+
</PropertyGroup>
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"dependencies": {
3+
"Microsoft.AspNet.Mvc.TagHelpers": "",
4+
"Microsoft.AspNet.Testing": "1.0.0-*",
5+
"Xunit.KRunner": "1.0.0-*"
6+
},
7+
"commands": {
8+
"test": "Xunit.KRunner"
9+
},
10+
"frameworks": {
11+
"aspnet50": {
12+
"dependencies": {
13+
"Moq": "4.2.1312.1622"
14+
}
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)