From a9c2c4c5eb87253f866bb7cc3d013fb65cd819b2 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Thu, 9 Oct 2014 16:25:12 -0700 Subject: [PATCH] Add `TextAreaTagHelper` and Microsoft.AspNet.Mvc.TagHelpers project - rest of #1243 and all of #1244 - needs a few utility methods for copying `TagBuilder` to `TagHelperOutput` - but works as-is --- Mvc.sln | 13 ++++ .../Microsoft.AspNet.Mvc.TagHelpers.kproj | 19 ++++++ .../TextAreaTagHelper.cs | 60 +++++++++++++++++++ .../project.json | 19 ++++++ 4 files changed, 111 insertions(+) create mode 100644 src/Microsoft.AspNet.Mvc.TagHelpers/Microsoft.AspNet.Mvc.TagHelpers.kproj create mode 100644 src/Microsoft.AspNet.Mvc.TagHelpers/TextAreaTagHelper.cs create mode 100644 src/Microsoft.AspNet.Mvc.TagHelpers/project.json diff --git a/Mvc.sln b/Mvc.sln index 788e9a8325..8ecd806165 100644 --- a/Mvc.sln +++ b/Mvc.sln @@ -100,6 +100,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Mvc.WebApi EndProject Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TagHelperSample.Web", "samples\TagHelperSample.Web\TagHelperSample.Web.kproj", "{2223120F-D675-40DA-8CD8-11DC14A0B2C7}" EndProject +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}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -530,6 +532,16 @@ Global {2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|Mixed Platforms.Build.0 = Release|Any CPU {2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|x86.ActiveCfg = Release|Any CPU + {B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Debug|x86.ActiveCfg = Debug|Any CPU + {B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|Any CPU.Build.0 = Release|Any CPU + {B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|Mixed Platforms.Build.0 = Release|Any CPU + {B2347320-308E-4D2B-AEC8-005DFA68B0C9}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -578,5 +590,6 @@ Global {B2B7BC91-688E-4C1E-A71F-CE948D958DDF} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C} {5DE8E4D9-AACD-4B5F-819F-F091383FB996} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1} {2223120F-D675-40DA-8CD8-11DC14A0B2C7} = {DAAE4C74-D06F-4874-A166-33305D2643CE} + {B2347320-308E-4D2B-AEC8-005DFA68B0C9} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E} EndGlobalSection EndGlobal diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/Microsoft.AspNet.Mvc.TagHelpers.kproj b/src/Microsoft.AspNet.Mvc.TagHelpers/Microsoft.AspNet.Mvc.TagHelpers.kproj new file mode 100644 index 0000000000..ee8a251170 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/Microsoft.AspNet.Mvc.TagHelpers.kproj @@ -0,0 +1,19 @@ + + + + 14.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + + b2347320-308e-4d2b-aec8-005dfa68b0c9 + Library + Microsoft.AspNet.Mvc.TagHelpers + + + + 2.0 + + + \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/TextAreaTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/TextAreaTagHelper.cs new file mode 100644 index 0000000000..46f9a960f0 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/TextAreaTagHelper.cs @@ -0,0 +1,60 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Mvc.Rendering; +using Microsoft.AspNet.Razor.TagHelpers; +using Microsoft.AspNet.Razor.Runtime.TagHelpers; + +namespace Microsoft.AspNet.Mvc.TagHelpers +{ + /// + /// implementation targeting <textarea> elements. + /// + [ContentBehavior(ContentBehavior.Replace)] + public class TextAreaTagHelper : TagHelper + { + [Activate] + private IHtmlGenerator Generator { get; set; } + + [Activate] + private ViewContext ViewContext { get; set; } + + /// + /// An expression to be evaluated against the current model. + /// + public ModelExpression For { get; set; } + + /// + /// Does nothing unless user binds "for" attribute in Razor source. + public override void Process(TagHelperContext context, TagHelperOutput output) + { + if (For != null) + { + var tagBuilder = Generator.GenerateTextArea( + ViewContext, + For.Metadata, + For.Name, + rows: 0, + columns: 0, + htmlAttributes: null); + + if (tagBuilder != null) + { + output.SelfClosing = false; + + // TODO: Use infrastructure from PR #1322 to copy from tagBuilder. + foreach (var attribute in tagBuilder.Attributes) + { + if (!output.Attributes.ContainsKey(attribute.Key)) + { + output.Attributes.Add(attribute.Key, attribute.Value); + } + } + + output.Content = tagBuilder.InnerHtml; + output.TagName = tagBuilder.TagName; + } + } + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/project.json b/src/Microsoft.AspNet.Mvc.TagHelpers/project.json new file mode 100644 index 0000000000..98b1ca956e --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/project.json @@ -0,0 +1,19 @@ +{ + "version": "6.0.0-*", + "compilationOptions": { + "warningsAsErrors": true + }, + "dependencies": { + "Microsoft.AspNet.Mvc.Razor": "" + }, + "frameworks": { + "aspnet50": { + "dependencies": {} + }, + "aspnetcore50": { + "dependencies": { + "System.Runtime": "4.0.20.0" + } + } + } +} \ No newline at end of file