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

Commit 489acde

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 3b8e63c commit 489acde

File tree

6 files changed

+209
-12
lines changed

6 files changed

+209
-12
lines changed

Mvc.sln

Lines changed: 27 additions & 12 deletions
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
@@ -83,7 +83,9 @@ EndProject
8383
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ApiExplorerWebSite", "test\WebSites\ApiExplorerWebSite\ApiExplorerWebSite.kproj", "{61061528-071E-424E-965A-07BCC2F02672}"
8484
EndProject
8585
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "VersioningWebSite", "test\WebSites\VersioningWebSite\VersioningWebSite.kproj", "{C6304029-78C8-4604-99BE-2078DCA1DD36}"
86+
EndProject
8687
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}"
@@ -94,6 +96,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "ApplicationModelWebSite", "
9496
EndProject
9597
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}"
9698
EndProject
99+
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}"
100+
EndProject
97101
Global
98102
GlobalSection(SolutionConfigurationPlatforms) = preSolution
99103
Debug|Any CPU = Debug|Any CPU
@@ -444,6 +448,16 @@ Global
444448
{C6304029-78C8-4604-99BE-2078DCA1DD36}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
445449
{C6304029-78C8-4604-99BE-2078DCA1DD36}.Release|Mixed Platforms.Build.0 = Release|Any CPU
446450
{C6304029-78C8-4604-99BE-2078DCA1DD36}.Release|x86.ActiveCfg = Release|Any CPU
451+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
452+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Any CPU.Build.0 = Debug|Any CPU
453+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
454+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
455+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|x86.ActiveCfg = Debug|Any CPU
456+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Any CPU.ActiveCfg = Release|Any CPU
457+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Any CPU.Build.0 = Release|Any CPU
458+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
459+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Mixed Platforms.Build.0 = Release|Any CPU
460+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|x86.ActiveCfg = Release|Any CPU
447461
{0EF9860B-10D7-452F-B0F4-A405B88BEBB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
448462
{0EF9860B-10D7-452F-B0F4-A405B88BEBB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
449463
{0EF9860B-10D7-452F-B0F4-A405B88BEBB3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -474,16 +488,6 @@ Global
474488
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
475489
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|Mixed Platforms.Build.0 = Release|Any CPU
476490
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657}.Release|x86.ActiveCfg = Release|Any CPU
477-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
478-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Any CPU.Build.0 = Debug|Any CPU
479-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
480-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
481-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Debug|x86.ActiveCfg = Debug|Any CPU
482-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Any CPU.ActiveCfg = Release|Any CPU
483-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Any CPU.Build.0 = Release|Any CPU
484-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
485-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|Mixed Platforms.Build.0 = Release|Any CPU
486-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47}.Release|x86.ActiveCfg = Release|Any CPU
487491
{B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
488492
{B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Debug|Any CPU.Build.0 = Debug|Any CPU
489493
{B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -494,6 +498,16 @@ Global
494498
{B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
495499
{B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|Mixed Platforms.Build.0 = Release|Any CPU
496500
{B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|x86.ActiveCfg = Release|Any CPU
501+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
502+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Debug|Any CPU.Build.0 = Debug|Any CPU
503+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
504+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
505+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Debug|x86.ActiveCfg = Debug|Any CPU
506+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Release|Any CPU.ActiveCfg = Release|Any CPU
507+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Release|Any CPU.Build.0 = Release|Any CPU
508+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
509+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Release|Mixed Platforms.Build.0 = Release|Any CPU
510+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65}.Release|x86.ActiveCfg = Release|Any CPU
497511
EndGlobalSection
498512
GlobalSection(SolutionProperties) = preSolution
499513
HideSolutionNode = FALSE
@@ -534,10 +548,11 @@ Global
534548
{A192E504-2881-41DC-90D1-B7F1DD1134E8} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
535549
{61061528-071E-424E-965A-07BCC2F02672} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
536550
{C6304029-78C8-4604-99BE-2078DCA1DD36} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
551+
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
537552
{0EF9860B-10D7-452F-B0F4-A405B88BEBB3} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
538553
{2B2B9876-903C-4065-8D62-2EE832BBA106} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
539554
{CAE52CB7-0FAC-4B5B-8251-B0FF837DB657} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
540-
{6DB9B8D0-80F7-4E70-BBB0-0B4C04D79A47} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
541555
{B2347320-308E-4D2B-AEC8-005DFA68B0C9} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
556+
{EB1C7DE2-B8BC-4B01-8A09-162DB15A0F65} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
542557
EndGlobalSection
543558
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)