From 5882cdb03f84698191b341ca01aa425ca5754795 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Sun, 29 Mar 2015 15:54:53 -0700 Subject: [PATCH] MVC portion of aspnet/Razor#335 - set correct `MarkAsHtmlEncodedMethodName` value in `MvcRazorHost` - handle `HtmlString` values in `TagHelperOutput.Attributes` in `RazorPage` - special-case double-quotes in `HtmlString` values - add `static WriteTo()` method for use in tag helpers - handle non-`string` `output.Attributes` values in tag helpers - make `TagHelperContentWrapperTextWriter` a `public` class - provide a `TagHelperContent.Append(object, ...)` extension method - add `LinkTagHelper.Href` and `ScriptTagHelper.Src` properties - avoid Razor HTML-encoding these attribute values before their use - add `JavaScriptEncoder` properties in `LinkTagHelper` and `ScriptTagHelper` - allow encoding testing without unit testing the default encoder - handle MVC and Razor changes for this bug in existing tests - add functional tests of encodings - add test encoders to TestCommon project nits: - correct `InputTagHelper` to pass `type=""` through unchanged - set correct `TagHelperContentTypeName` value in `MvcRazorHost` - remove unnecessary `FormTagHelper.Method` and `OptionTagHelper.Selected` properties - remove complex ternaries and `ShouldAddFileVersion()` methods - add a few debug assertions - fix some odd wrapping - remove or `#if`-out unused `using`s - remove trailing whitespace --- .../MvcRazorHost.cs | 11 +- src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs | 115 ++++++++------ .../TagHelperContentWrapperTextWriter.cs | 62 ++++++++ .../AnchorTagHelper.cs | 8 +- .../FormTagHelper.cs | 25 +-- .../InputTagHelper.cs | 3 +- .../LinkTagHelper.cs | 81 ++++++---- .../OptionTagHelper.cs | 17 +-- .../ScriptTagHelper.cs | 143 +++++++++++------- .../TagHelperContentExtensions.cs | 44 ++++++ .../TagHelperOutputExtensions.cs | 34 +++-- .../AntiForgeryTestHelper.cs | 9 +- ...elpersWebSite.MvcTagHelper_Home.Index.html | 4 +- ...HelpersWebSite.MvcTagHelper_Home.Link.html | 50 +++--- ...lpersWebSite.MvcTagHelper_Home.Script.html | 24 +-- .../MvcTagHelpersTest.cs | 47 +++++- ...cTagHelper_Home.EditWarehouse.Encoded.html | 47 ++++++ ...bSite.MvcTagHelper_Home.Index.Encoded.html | 76 ++++++++++ ...ebSite.MvcTagHelper_Home.Link.Encoded.html | 131 ++++++++++++++++ ...bSite.MvcTagHelper_Home.Order.Encoded.html | 85 +++++++++++ ...er_Home.OrderUsingHtmlHelpers.Encoded.html | 84 ++++++++++ ...ite.MvcTagHelper_Home.Product.Encoded.html | 23 +++ ...Site.MvcTagHelper_Home.Script.Encoded.html | 116 ++++++++++++++ .../project.json | 1 + .../RazorPageTest.cs | 41 +++-- .../AnchorTagHelperTest.cs | 10 +- .../CacheTagHelperTest.cs | 23 ++- .../EnvironmentTagHelperTest.cs | 5 +- .../FormTagHelperTest.cs | 72 ++------- .../InputTagHelperTest.cs | 22 +-- .../LabelTagHelperTest.cs | 5 +- .../LinkTagHelperTest.cs | 111 ++++++-------- .../OptionTagHelperTest.cs | 94 ++++++------ .../ScriptTagHelperTest.cs | 77 +++------- .../SelectTagHelperTest.cs | 17 +-- .../TagHelperOutputExtensionsTest.cs | 54 +++---- .../TextAreaTagHelperTest.cs | 5 +- .../ValidationMessageTagHelperTest.cs | 10 +- .../ValidationSummaryTagHelperTest.cs | 10 +- .../TestHtmlEncoder.cs | 27 ++++ .../TestJavaScriptEncoder.cs | 27 ++++ .../TestUrlEncoder.cs | 27 ++++ .../Views/MvcTagHelper_Home/Index.cshtml | 4 +- .../Views/MvcTagHelper_Home/Link.cshtml | 10 +- .../Views/MvcTagHelper_Home/Script.cshtml | 6 +- .../TagHelpers/PrettyTagHelper.cs | 9 +- 46 files changed, 1342 insertions(+), 564 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs create mode 100644 src/Microsoft.AspNet.Mvc.TagHelpers/TagHelperContentExtensions.cs create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.EditWarehouse.Encoded.html create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Index.Encoded.html create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Link.Encoded.html create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Order.Encoded.html create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.Encoded.html create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.Encoded.html create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.Encoded.html create mode 100644 test/Microsoft.AspNet.Mvc.TestCommon/TestHtmlEncoder.cs create mode 100644 test/Microsoft.AspNet.Mvc.TestCommon/TestJavaScriptEncoder.cs create mode 100644 test/Microsoft.AspNet.Mvc.TestCommon/TestUrlEncoder.cs diff --git a/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs b/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs index 881a94e85b..5262f864fc 100644 --- a/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs +++ b/src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs @@ -3,7 +3,9 @@ using System.Collections.Generic; using System.IO; +#if NET45 using Microsoft.AspNet.FileProviders; +#endif using Microsoft.AspNet.Mvc.Razor.Directives; using Microsoft.AspNet.Mvc.Razor.Internal; using Microsoft.AspNet.Razor; @@ -18,6 +20,8 @@ namespace Microsoft.AspNet.Mvc.Razor public class MvcRazorHost : RazorEngineHost, IMvcRazorHost { private const string BaseType = "Microsoft.AspNet.Mvc.Razor.RazorPage"; + private const string HtmlHelperPropertyName = "Html"; + private static readonly string[] _defaultNamespaces = new[] { "System", @@ -28,7 +32,7 @@ namespace Microsoft.AspNet.Mvc.Razor }; private static readonly Chunk[] _defaultInheritedChunks = new[] { - new InjectChunk("Microsoft.AspNet.Mvc.Rendering.IHtmlHelper", "Html"), + new InjectChunk("Microsoft.AspNet.Mvc.Rendering.IHtmlHelper", HtmlHelperPropertyName), new InjectChunk("Microsoft.AspNet.Mvc.IViewComponentHelper", "Component"), new InjectChunk("Microsoft.AspNet.Mvc.IUrlHelper", "Url"), }; @@ -76,6 +80,8 @@ namespace Microsoft.AspNet.Mvc.Razor ScopeManagerBeginMethodName = nameof(TagHelperScopeManager.Begin), ScopeManagerEndMethodName = nameof(TagHelperScopeManager.End), + TagHelperContentTypeName = nameof(TagHelperContent), + // Can't use nameof because RazorPage is not accessible here. CreateTagHelperMethodName = "CreateTagHelper", StartTagHelperWritingScopeMethodName = "StartTagHelperWritingScope", @@ -83,6 +89,9 @@ namespace Microsoft.AspNet.Mvc.Razor WriteTagHelperAsyncMethodName = "WriteTagHelperAsync", WriteTagHelperToAsyncMethodName = "WriteTagHelperToAsync", + + // Can't use nameof because IHtmlHelper is (also) not accessible here. + MarkAsHtmlEncodedMethodName = HtmlHelperPropertyName + ".Raw", }) { ResolveUrlMethodName = "Href", diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs index 50a53ec79c..6f6c33e180 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs @@ -294,11 +294,10 @@ namespace Microsoft.AspNet.Mvc.Razor foreach (var attribute in tagHelperOutput.Attributes) { - var value = HtmlEncoder.HtmlEncode(attribute.Value); writer.Write(' '); writer.Write(attribute.Key); writer.Write("=\""); - writer.Write(value); + WriteTo(writer, HtmlEncoder, attribute.Value, escapeQuotes: true); writer.Write('"'); } @@ -366,26 +365,67 @@ namespace Microsoft.AspNet.Mvc.Razor /// public virtual void WriteTo([NotNull] TextWriter writer, object value) { - if (value != null && value != HtmlString.Empty) + WriteTo(writer, HtmlEncoder, value, escapeQuotes: false); + } + + /// + /// Writes the specified with HTML encoding to given . + /// + /// The instance to write to. + /// The to use when encoding . + /// The to write. + /// + /// If true escapes double quotes in a of type . + /// Otherwise writes values as-is. + /// + /// + /// s of type are written without encoding and the + /// is invoked for types. + /// For all other types, the encoded result of is written to the + /// . + /// + public static void WriteTo( + [NotNull] TextWriter writer, + [NotNull] IHtmlEncoder encoder, + object value, + bool escapeQuotes) + { + if (value == null || value == HtmlString.Empty) { - var helperResult = value as HelperResult; - if (helperResult != null) - { - helperResult.WriteTo(writer); - } - else - { - var htmlString = value as HtmlString; - if (htmlString != null) - { - writer.Write(htmlString); - } - else - { - WriteTo(writer, value.ToString()); - } - } + return; } + + var helperResult = value as HelperResult; + if (helperResult != null) + { + helperResult.WriteTo(writer); + return; + } + + var htmlString = value as HtmlString; + if (htmlString != null) + { + if (escapeQuotes) + { + // In this case the text likely came directly from the Razor source. Since the original string is + // an attribute value that may have been quoted with single quotes, must handle any double quotes + // in the value. Writing the value out surrounded by double quotes. + // + // Do not combine following condition with check of escapeQuotes; htmlString.ToString() can be + // expensive when the HtmlString is created with a StringCollectionTextWriter. + var stringValue = htmlString.ToString(); + if (stringValue.Contains("\"")) + { + writer.Write(stringValue.Replace("\"", """)); + return; + } + } + + htmlString.WriteTo(writer); + return; + } + + WriteTo(writer, encoder, value.ToString()); } /// @@ -394,10 +434,15 @@ namespace Microsoft.AspNet.Mvc.Razor /// The instance to write to. /// The to write. public virtual void WriteTo([NotNull] TextWriter writer, string value) + { + WriteTo(writer, HtmlEncoder, value); + } + + private static void WriteTo(TextWriter writer, IHtmlEncoder encoder, string value) { if (!string.IsNullOrEmpty(value)) { - HtmlEncoder.HtmlEncode(value, writer); + encoder.HtmlEncode(value, writer); } } @@ -766,33 +811,5 @@ namespace Microsoft.AspNet.Mvc.Razor throw new InvalidOperationException(Resources.FormatRazorPage_MethodCannotBeCalled(methodName)); } } - - private class TagHelperContentWrapperTextWriter : TextWriter - { - public TagHelperContentWrapperTextWriter(Encoding encoding) - { - Content = new DefaultTagHelperContent(); - Encoding = encoding; - } - - public TagHelperContent Content { get; } - - public override Encoding Encoding { get; } - - public override void Write(string value) - { - Content.Append(value); - } - - public override void Write(char value) - { - Content.Append(value.ToString()); - } - - public override string ToString() - { - return Content.ToString(); - } - } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs b/src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs new file mode 100644 index 0000000000..cf59592127 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.Razor/TagHelperContentWrapperTextWriter.cs @@ -0,0 +1,62 @@ +// 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 System.IO; +using System.Text; +using Microsoft.AspNet.Razor.Runtime.TagHelpers; +using Microsoft.Framework.Internal; + +namespace Microsoft.AspNet.Mvc.Razor +{ + /// + /// implementation which writes to a instance. + /// + public class TagHelperContentWrapperTextWriter : TextWriter + { + /// + /// Initializes a new instance of the class. + /// + /// The in which output is written. + public TagHelperContentWrapperTextWriter([NotNull] Encoding encoding) + : this(encoding, new DefaultTagHelperContent()) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The in which output is written. + /// The to write to. + public TagHelperContentWrapperTextWriter([NotNull] Encoding encoding, [NotNull] TagHelperContent content) + { + Content = content; + Encoding = encoding; + } + + /// + /// The this writes to. + /// + public TagHelperContent Content { get; } + + /// + public override Encoding Encoding { get; } + + /// + public override void Write(string value) + { + Content.Append(value); + } + + /// + public override void Write(char value) + { + Content.Append(value.ToString()); + } + + /// + public override string ToString() + { + return Content.ToString(); + } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/AnchorTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/AnchorTagHelper.cs index 0f356bcc20..5907872fe6 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/AnchorTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/AnchorTagHelper.cs @@ -153,7 +153,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // TODO: https://github.com/aspnet/Razor/issues/89 - We will not need this method once #89 is completed. private static Dictionary GetRouteValues( - TagHelperOutput output, IEnumerable> routePrefixedAttributes) + TagHelperOutput output, + IEnumerable> routePrefixedAttributes) { Dictionary routeValues = null; if (routePrefixedAttributes.Any()) @@ -161,10 +162,11 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Prefixed values should be treated as bound attributes, remove them from the output. output.RemoveRange(routePrefixedAttributes); - // Generator.GenerateForm does not accept a Dictionary for route values. + // Remove prefix from keys and convert all values to strings. HtmlString and similar classes are not + // meaningful to routing. routeValues = routePrefixedAttributes.ToDictionary( attribute => attribute.Key.Substring(RouteAttributePrefix.Length), - attribute => (object)attribute.Value); + attribute => (object)attribute.Value.ToString()); } return routeValues; diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/FormTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/FormTagHelper.cs index 4b24ecb609..1a241046b0 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/FormTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/FormTagHelper.cs @@ -27,7 +27,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Protected to ensure subclasses are correctly activated. Internal for ease of use when testing. [Activate] protected internal IHtmlGenerator Generator { get; set; } - + /// /// The name of the action method. /// @@ -41,12 +41,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers public string Controller { get; set; } /// - /// The HTTP method for processing the form, either GET or POST. - /// - public string Method { get; set; } - - /// - /// Whether the anti-forgery token should be generated. + /// Whether the anti-forgery token should be generated. /// /// Defaults to false if user provides an action attribute; true otherwise. [HtmlAttributeName(AntiForgeryAttributeName)] @@ -84,12 +79,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // User is using the FormTagHelper like a normal
tag. Anti-forgery default should be false to // not force the anti-forgery token on the user. antiForgeryDefault = false; - - // Restore method attribute. - if (Method != null) - { - output.CopyHtmlAttribute(nameof(Method), context); - } } else { @@ -98,7 +87,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Action, Controller, routeValues, - Method, + method: null, htmlAttributes: null); if (tagBuilder != null) @@ -120,7 +109,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // TODO: https://github.com/aspnet/Razor/issues/89 - We will not need this method once #89 is completed. private static Dictionary GetRouteValues( - TagHelperOutput output, IEnumerable> routePrefixedAttributes) + TagHelperOutput output, + IEnumerable> routePrefixedAttributes) { Dictionary routeValues = null; if (routePrefixedAttributes.Any()) @@ -128,10 +118,11 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Prefixed values should be treated as bound attributes, remove them from the output. output.RemoveRange(routePrefixedAttributes); - // Generator.GenerateForm does not accept a Dictionary for route values. + // Remove prefix from keys and convert all values to strings. HtmlString and similar classes are not + // meaningful to routing. routeValues = routePrefixedAttributes.ToDictionary( attribute => attribute.Key.Substring(RouteAttributePrefix.Length), - attribute => (object)attribute.Value); + attribute => (object)attribute.Value.ToString()); } return routeValues; diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs index 08c30ac708..78818cb5dc 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs @@ -8,7 +8,6 @@ using System.Linq; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Razor.Runtime.TagHelpers; -using Microsoft.AspNet.Razor.TagHelpers; namespace Microsoft.AspNet.Mvc.TagHelpers { @@ -112,7 +111,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers { // Pass through attributes that are also well-known HTML attributes. Must be done prior to any copying // from a TagBuilder. - if (!string.IsNullOrEmpty(InputTypeName)) + if (InputTypeName != null) { output.CopyHtmlAttribute("type", context); } diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/LinkTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/LinkTagHelper.cs index 1bf94f9e9d..28a6d65640 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/LinkTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/LinkTagHelper.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Globalization; using System.Linq; using Microsoft.AspNet.Hosting; @@ -10,7 +11,6 @@ using Microsoft.AspNet.Mvc.TagHelpers.Internal; using Microsoft.AspNet.Razor.Runtime.TagHelpers; using Microsoft.Framework.Caching.Memory; using Microsoft.Framework.Logging; -using Microsoft.Framework.Runtime; using Microsoft.Framework.WebEncoders; namespace Microsoft.AspNet.Mvc.TagHelpers @@ -98,6 +98,15 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Fallback = 2, } + /// + /// Address of the linked resource. + /// + /// + /// Passed through to the generated HTML in all cases. + /// + [HtmlAttributeName(HrefAttributeName)] + public string Href { get; set; } + /// /// A comma separated list of globbed file patterns of CSS stylesheets to load. /// The glob patterns are assessed relative to the application's 'webroot' setting. @@ -189,12 +198,21 @@ namespace Microsoft.AspNet.Mvc.TagHelpers [Activate] protected internal IHtmlEncoder HtmlEncoder { get; set; } + [Activate] + protected internal IJavaScriptStringEncoder JavaScriptEncoder { get; set; } + // Internal for ease of use when testing. protected internal GlobbingUrlBuilder GlobbingUrlBuilder { get; set; } /// public override void Process(TagHelperContext context, TagHelperOutput output) { + // Pass through attribute that is also a well-known HTML attribute. + if (Href != null) + { + output.CopyHtmlAttribute(HrefAttributeName, context); + } + var modeResult = AttributeMatcher.DetermineMode(context, ModeDetails); var logger = Logger ?? LoggerFactory.CreateLogger(); @@ -210,8 +228,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Get the highest matched mode var mode = modeResult.FullMatches.Select(match => match.Mode).Max(); - // NOTE: Values in TagHelperOutput.Attributes are already HtmlEncoded - var attributes = new Dictionary(output.Attributes); + // NOTE: Values in TagHelperOutput.Attributes may already be HTML-encoded. + var attributes = new Dictionary(output.Attributes); var builder = new DefaultTagHelperContent(); @@ -236,17 +254,17 @@ namespace Microsoft.AspNet.Mvc.TagHelpers output.Content.SetContent(builder); } - private void BuildGlobbedLinkTags(IDictionary attributes, TagHelperContent builder) + private void BuildGlobbedLinkTags(IDictionary attributes, TagHelperContent builder) { - // Build a tag for each matched href as well as the original one in the source file - string staticHref; - attributes.TryGetValue(HrefAttributeName, out staticHref); - EnsureGlobbingUrlBuilder(); - var urls = GlobbingUrlBuilder.BuildUrlList(staticHref, HrefInclude, HrefExclude); + // Build a tag for each matched href as well as the original one in the source file + var urls = GlobbingUrlBuilder.BuildUrlList(Href, HrefInclude, HrefExclude); foreach (var url in urls) { + // "url" values come from bound attributes and globbing. Must always be non-null. + Debug.Assert(url != null); + attributes[HrefAttributeName] = url; BuildLinkTag(attributes, builder); } @@ -260,10 +278,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers if (fallbackHrefs.Length > 0) { - if (ShouldAddFileVersion()) + if (FileVersion == true) { for (var i=0; i < fallbackHrefs.Length; i++) { + // fallbackHrefs come from bound attributes and globbing. Must always be non-null. + Debug.Assert(fallbackHrefs[i] != null); + fallbackHrefs[i] = _fileVersionProvider.AddFileVersionToPath(fallbackHrefs[i]); } } @@ -279,13 +300,15 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Build the "); + builder + .Append(""); } } @@ -311,7 +334,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers } } - private void BuildLinkTag(IDictionary attributes, TagHelperContent builder) + private void BuildLinkTag(IDictionary attributes, TagHelperContent builder) { EnsureFileVersionProvider(); builder.Append(""); } - - private bool ShouldAddFileVersion() - { - return FileVersion ?? false; - } } } \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/OptionTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/OptionTagHelper.cs index 10529cc6e8..e55ed3761c 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/OptionTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/OptionTagHelper.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Razor.Runtime.TagHelpers; -using Microsoft.AspNet.Razor.TagHelpers; namespace Microsoft.AspNet.Mvc.TagHelpers { @@ -28,14 +27,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers [Activate] protected internal ViewContext ViewContext { get; set; } - /// - /// Specifies that this <option> is pre-selected. - /// - /// - /// Passed through to the generated HTML in all cases. - /// - public string Selected { get; set; } - /// /// Specifies a value for the <option> element. /// @@ -59,12 +50,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers output.CopyHtmlAttribute(nameof(Value), context); } - if (Selected != null) - { - // This + + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.Encoded.html new file mode 100644 index 0000000000..5f5fd01a46 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.Encoded.html @@ -0,0 +1,84 @@ + + + + + + + + + +
+
+ + +
+
+ + +
+
+ + + +
+
+ + + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + +
+
+ + +
+
+ + + +
+
+ + +
+
+ + + +
+
+ + Male + Female + +
+
  • +
+ + +
+ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.Encoded.html new file mode 100644 index 0000000000..6356668cc9 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Product.Encoded.html @@ -0,0 +1,23 @@ + + + + + + + + + +
+
+ + +
+
+ + +
+ +
+ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.Encoded.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.Encoded.html new file mode 100644 index 0000000000..90239290b1 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Script.Encoded.html @@ -0,0 +1,116 @@ + + + + + + Script + + +

Script tag helper test

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json index 871e0e71bb..b0ce331fda 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/project.json @@ -32,6 +32,7 @@ "LoggingWebSite": "1.0.0", "LowercaseUrlsWebSite": "1.0.0-*", "Microsoft.AspNet.Mvc": "6.0.0-*", + "Microsoft.AspNet.Mvc.TestCommon": { "version": "6.0.0-*", "type": "build" }, "Microsoft.AspNet.Mvc.TestConfiguration": "1.0.0", "Microsoft.AspNet.Mvc.Xml": "6.0.0-*", "Microsoft.AspNet.TestHost": "1.0.0-*", diff --git a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageTest.cs b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageTest.cs index 859029ea65..d6e90b4e66 100644 --- a/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageTest.cs +++ b/test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageTest.cs @@ -658,9 +658,10 @@ namespace Microsoft.AspNet.Mvc.Razor // Assert var buffer = writer.BufferedWriter.Buffer; - Assert.Equal(2, buffer.BufferEntries.Count); + Assert.Equal(3, buffer.BufferEntries.Count); Assert.Equal("Hello world", buffer.BufferEntries[0]); - Assert.Same(stringCollectionWriter.Buffer.BufferEntries, buffer.BufferEntries[1]); + Assert.Equal("text1", buffer.BufferEntries[1]); + Assert.Equal("text2", buffer.BufferEntries[2]); } public static TheoryData WriteTagHelper_InputData @@ -672,21 +673,21 @@ namespace Microsoft.AspNet.Mvc.Razor { { // parameters: TagName, Attributes, SelfClosing, PreContent, Content, PostContent - GetTagHelperOutput("div", new Dictionary(), false, null, "Hello World!", null), + GetTagHelperOutput("div", new Dictionary(), false, null, "Hello World!", null), "
Hello World!
" }, { - GetTagHelperOutput(null, new Dictionary(), false, null, "Hello World!", null), + GetTagHelperOutput(null, new Dictionary(), false, null, "Hello World!", null), "Hello World!" }, { - GetTagHelperOutput(" ", new Dictionary(), false, null, "Hello World!", null), + GetTagHelperOutput(" ", new Dictionary(), false, null, "Hello World!", null), "Hello World!" }, { GetTagHelperOutput( "p", - new Dictionary() { { "test", "testVal" } }, + new Dictionary() { { "test", "testVal" } }, false, null, "Hello World!", @@ -696,7 +697,7 @@ namespace Microsoft.AspNet.Mvc.Razor { GetTagHelperOutput( "p", - new Dictionary() { { "test", "testVal" }, { "something", " spaced " } }, + new Dictionary() { { "test", "testVal" }, { "something", " spaced " } }, false, null, "Hello World!", @@ -706,7 +707,7 @@ namespace Microsoft.AspNet.Mvc.Razor { GetTagHelperOutput( "p", - new Dictionary() { { "test", "testVal" } }, + new Dictionary() { { "test", "testVal" } }, true, null, "Hello World!", @@ -716,7 +717,7 @@ namespace Microsoft.AspNet.Mvc.Razor { GetTagHelperOutput( "p", - new Dictionary() { { "test", "testVal" }, { "something", " spaced " } }, + new Dictionary() { { "test", "testVal" }, { "something", " spaced " } }, true, null, "Hello World!", @@ -724,31 +725,31 @@ namespace Microsoft.AspNet.Mvc.Razor "

" }, { - GetTagHelperOutput("p", new Dictionary(), false, "Hello World!", null, null), + GetTagHelperOutput("p", new Dictionary(), false, "Hello World!", null, null), "

Hello World!

" }, { - GetTagHelperOutput("p", new Dictionary(), false, null, "Hello World!", null), + GetTagHelperOutput("p", new Dictionary(), false, null, "Hello World!", null), "

Hello World!

" }, { - GetTagHelperOutput("p", new Dictionary(), false, null, null, "Hello World!"), + GetTagHelperOutput("p", new Dictionary(), false, null, null, "Hello World!"), "

Hello World!

" }, { - GetTagHelperOutput("p", new Dictionary(), false, "Hello", "Test", "World!"), + GetTagHelperOutput("p", new Dictionary(), false, "Hello", "Test", "World!"), "

HelloTestWorld!

" }, { - GetTagHelperOutput("p", new Dictionary(), true, "Hello", "Test", "World!"), + GetTagHelperOutput("p", new Dictionary(), true, "Hello", "Test", "World!"), "

" }, { - GetTagHelperOutput("custom", new Dictionary(), false, "Hello", "Test", "World!"), + GetTagHelperOutput("custom", new Dictionary(), false, "Hello", "Test", "World!"), "HelloTestWorld!" }, { - GetTagHelperOutput("random", new Dictionary(), true, "Hello", "Test", "World!"), + GetTagHelperOutput("random", new Dictionary(), true, "Hello", "Test", "World!"), "" } }; @@ -807,8 +808,7 @@ namespace Microsoft.AspNet.Mvc.Razor }, startTagHelperWritingScope: () => { }, endTagHelperWritingScope: () => defaultTagHelperContent); - tagHelperExecutionContext.Output = - new TagHelperOutput("p", new Dictionary()); + tagHelperExecutionContext.Output = new TagHelperOutput("p", new Dictionary()); if (childContentRetrieved) { await tagHelperExecutionContext.GetChildContentAsync(); @@ -840,8 +840,7 @@ namespace Microsoft.AspNet.Mvc.Razor executeChildContentAsync: () => { return Task.FromResult(result: true); }, startTagHelperWritingScope: () => { }, endTagHelperWritingScope: () => new DefaultTagHelperContent()); - tagHelperExecutionContext.Output = - new TagHelperOutput("p", new Dictionary()); + tagHelperExecutionContext.Output = new TagHelperOutput("p", new Dictionary()); tagHelperExecutionContext.Output.Content.SetContent("Hello World!"); // Act @@ -886,7 +885,7 @@ namespace Microsoft.AspNet.Mvc.Razor private static TagHelperOutput GetTagHelperOutput( string tagName, - IDictionary attributes, + IDictionary attributes, bool selfClosing, string preContent, string content, diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/AnchorTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/AnchorTagHelperTest.cs index 2175874fcd..6cc434c00c 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/AnchorTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/AnchorTagHelperTest.cs @@ -43,7 +43,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); var output = new TagHelperOutput( expectedTagName, - attributes: new Dictionary + attributes: new Dictionary { { "id", "myanchor" }, { "asp-route-foo", "bar" }, @@ -97,7 +97,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); var output = new TagHelperOutput( "a", - attributes: new Dictionary()); + attributes: new Dictionary()); output.Content.SetContent(string.Empty); var generator = new Mock(MockBehavior.Strict); @@ -139,7 +139,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); var output = new TagHelperOutput( "a", - attributes: new Dictionary()); + attributes: new Dictionary()); output.Content.SetContent(string.Empty); var generator = new Mock(); @@ -180,7 +180,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var anchorTagHelper = new AnchorTagHelper(); var output = new TagHelperOutput( "a", - attributes: new Dictionary() + attributes: new Dictionary() { { "href", "http://www.contoso.com" } }); @@ -218,7 +218,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers typeof(AnchorTagHelper).GetProperty(propertyName).SetValue(anchorTagHelper, "Home"); var output = new TagHelperOutput( "a", - attributes: new Dictionary()); + attributes: new Dictionary()); var expectedErrorMessage = "Cannot determine an 'href' attribute for . An with a specified " + "'asp-route' must not have an 'asp-action' or 'asp-controller' attribute."; diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/CacheTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/CacheTagHelperTest.cs index 01960f5c9f..06168c4153 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/CacheTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/CacheTagHelperTest.cs @@ -17,7 +17,6 @@ using Microsoft.AspNet.Routing; using Microsoft.Framework.Caching.Memory; using Microsoft.Framework.Caching.Memory.Infrastructure; using Microsoft.Framework.Expiration.Interfaces; -using Microsoft.Framework.WebEncoders; using Moq; using Xunit; @@ -245,7 +244,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var childContent = "original-child-content"; var cache = new MemoryCache(new MemoryCacheOptions()); var tagHelperContext1 = GetTagHelperContext(id, childContent); - var tagHelperOutput1 = new TagHelperOutput("cache", new Dictionary()); + var tagHelperOutput1 = new TagHelperOutput("cache", new Dictionary()); var cacheTagHelper1 = new CacheTagHelper { VaryByQuery = "key1,key2", @@ -266,7 +265,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange - 2 var tagHelperContext2 = GetTagHelperContext(id, "different-content"); - var tagHelperOutput2 = new TagHelperOutput("cache", new Dictionary()); + var tagHelperOutput2 = new TagHelperOutput("cache", new Dictionary()); var cacheTagHelper2 = new CacheTagHelper { VaryByQuery = "key1,key2", @@ -295,7 +294,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var cache = new MemoryCache(new MemoryCacheOptions()); var tagHelperContext1 = GetTagHelperContext(id, childContent1); var tagHelperOutput1 = new TagHelperOutput("cache", - new Dictionary { { "attr", "value" } }); + new Dictionary { { "attr", "value" } }); tagHelperOutput1.PreContent.Append(""); tagHelperOutput1.PostContent.SetContent(""); var cacheTagHelper1 = new CacheTagHelper @@ -320,7 +319,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var tagHelperContext2 = GetTagHelperContext(id, childContent2); var tagHelperOutput2 = new TagHelperOutput( "cache", - new Dictionary { { "attr", "value" } }); + new Dictionary { { "attr", "value" } }); tagHelperOutput2.PreContent.SetContent(""); tagHelperOutput2.PostContent.SetContent(""); var cacheTagHelper2 = new CacheTagHelper @@ -530,7 +529,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object }); var tagHelperContext1 = GetTagHelperContext(id, childContent1); var tagHelperOutput1 = new TagHelperOutput("cache", - new Dictionary { { "attr", "value" } }); + new Dictionary { { "attr", "value" } }); tagHelperOutput1.PreContent.SetContent(""); tagHelperOutput1.PostContent.SetContent(""); var cacheTagHelper1 = new CacheTagHelper @@ -553,7 +552,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var childContent2 = "different-content"; var tagHelperContext2 = GetTagHelperContext(id, childContent2); var tagHelperOutput2 = new TagHelperOutput("cache", - new Dictionary { { "attr", "value" } }); + new Dictionary { { "attr", "value" } }); tagHelperOutput2.PreContent.SetContent(""); tagHelperOutput2.PostContent.SetContent(""); var cacheTagHelper2 = new CacheTagHelper @@ -587,7 +586,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object }); var tagHelperContext1 = GetTagHelperContext(id, childContent1); var tagHelperOutput1 = new TagHelperOutput("cache", - new Dictionary { { "attr", "value" } }); + new Dictionary { { "attr", "value" } }); tagHelperOutput1.PreContent.SetContent(""); tagHelperOutput1.PostContent.SetContent(""); var cacheTagHelper1 = new CacheTagHelper @@ -611,7 +610,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var childContent2 = "different-content"; var tagHelperContext2 = GetTagHelperContext(id, childContent2); var tagHelperOutput2 = new TagHelperOutput("cache", - new Dictionary { { "attr", "value" } }); + new Dictionary { { "attr", "value" } }); tagHelperOutput2.PreContent.SetContent(""); tagHelperOutput2.PostContent.SetContent(""); var cacheTagHelper2 = new CacheTagHelper @@ -644,7 +643,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object }); var tagHelperContext1 = GetTagHelperContext(id, childContent1); var tagHelperOutput1 = new TagHelperOutput("cache", - new Dictionary { { "attr", "value" } }); + new Dictionary { { "attr", "value" } }); tagHelperOutput1.PreContent.SetContent(""); tagHelperOutput1.PostContent.SetContent(""); var cacheTagHelper1 = new CacheTagHelper @@ -668,7 +667,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var childContent2 = "different-content"; var tagHelperContext2 = GetTagHelperContext(id, childContent2); var tagHelperOutput2 = new TagHelperOutput("cache", - new Dictionary { { "attr", "value" } }); + new Dictionary { { "attr", "value" } }); tagHelperOutput2.PreContent.SetContent(""); tagHelperOutput2.PostContent.SetContent(""); var cacheTagHelper2 = new CacheTagHelper @@ -712,7 +711,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers return Task.FromResult(expectedContent); }); var tagHelperOutput = new TagHelperOutput("cache", - new Dictionary { { "attr", "value" } }); + new Dictionary { { "attr", "value" } }); tagHelperOutput.PreContent.SetContent(""); tagHelperOutput.PostContent.SetContent(""); var cacheTagHelper = new CacheTagHelper diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/EnvironmentTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/EnvironmentTagHelperTest.cs index a4f453406e..148c22577c 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/EnvironmentTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/EnvironmentTagHelperTest.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNet.Hosting; using Microsoft.AspNet.Razor.Runtime.TagHelpers; -using Microsoft.Framework.WebEncoders; using Moq; using Xunit; @@ -147,9 +146,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Test }); } - private TagHelperOutput MakeTagHelperOutput(string tagName, IDictionary attributes = null) + private TagHelperOutput MakeTagHelperOutput(string tagName, IDictionary attributes = null) { - attributes = attributes ?? new Dictionary(); + attributes = attributes ?? new Dictionary(); return new TagHelperOutput(tagName, attributes); } diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/FormTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/FormTagHelperTest.cs index 15c4343425..04a02c7a9e 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/FormTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/FormTagHelperTest.cs @@ -10,7 +10,6 @@ using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Razor.Runtime.TagHelpers; using Microsoft.AspNet.Routing; -using Microsoft.Framework.OptionsModel; using Microsoft.Framework.WebEncoders; using Moq; using Xunit; @@ -45,7 +44,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); var output = new TagHelperOutput( expectedTagName, - attributes: new Dictionary + attributes: new Dictionary { { "id", "myform" }, { "asp-route-foo", "bar" }, @@ -67,7 +66,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers AntiForgery = true, Controller = "home", Generator = htmlGenerator, - Method = "post", ViewContext = viewContext, }; @@ -110,7 +108,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); var output = new TagHelperOutput( "form", - attributes: new Dictionary()); + attributes: new Dictionary()); var generator = new Mock(MockBehavior.Strict); generator .Setup(mock => mock.GenerateForm( @@ -159,10 +157,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.SetContent("Something"); return Task.FromResult(tagHelperContent); }); - var expectedAttribute = new KeyValuePair("asp-ROUTEE-NotRoute", "something"); + var expectedAttribute = new KeyValuePair("asp-ROUTEE-NotRoute", "something"); var output = new TagHelperOutput( "form", - attributes: new Dictionary() + attributes: new Dictionary() { { "asp-route-val", "hello" }, { "asp-roUte--Foo", "bar" } @@ -232,10 +230,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); var output = new TagHelperOutput( "form", - attributes: new Dictionary()); + attributes: new Dictionary()); var generator = new Mock(MockBehavior.Strict); generator - .Setup(mock => mock.GenerateForm(viewContext, "Index", "Home", null, "POST", null)) + .Setup(mock => mock.GenerateForm(viewContext, "Index", "Home", null, null, null)) .Returns(new TagBuilder("form", new HtmlEncoder())) .Verifiable(); var formTagHelper = new FormTagHelper @@ -244,7 +242,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers AntiForgery = false, Controller = "Home", Generator = generator.Object, - Method = "POST", ViewContext = viewContext, }; @@ -260,52 +257,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Assert.Empty(output.PostContent.GetContent()); } - [Theory] - [InlineData("my-action")] - [InlineData("http://www.contoso.com")] - [InlineData("my/action")] - public async Task ProcessAsync_RestoresBoundAttributesIfActionIsSpecified(string htmlAction) - { - // Arrange - var formTagHelper = new FormTagHelper - { - Method = "POST" - }; - var output = new TagHelperOutput("form", - attributes: new Dictionary - { - { "aCTiON", htmlAction }, - }); - - var context = new TagHelperContext( - allAttributes: new Dictionary() - { - { "METhod", "POST" } - }, - items: new Dictionary(), - uniqueId: "test", - getChildContentAsync: () => - { - var tagHelperContent = new DefaultTagHelperContent(); - tagHelperContent.SetContent("Something"); - return Task.FromResult(tagHelperContent); - }); - - // Act - await formTagHelper.ProcessAsync(context, output); - - // Assert - Assert.Equal("form", output.TagName); - Assert.Equal(2, output.Attributes.Count); - var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("aCTiON")); - Assert.Equal(htmlAction, attribute.Value); - attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("METhod")); - Assert.Equal("POST", attribute.Value); - Assert.Empty(output.PreContent.GetContent()); - Assert.True(output.Content.IsEmpty); - Assert.Empty(output.PostContent.GetContent()); - } - [Theory] [InlineData(true, "")] [InlineData(false, "")] @@ -328,7 +279,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }; var output = new TagHelperOutput("form", - attributes: new Dictionary + attributes: new Dictionary { { "aCTiON", "my-action" }, }); @@ -351,7 +302,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Assert.Equal("form", output.TagName); Assert.False(output.SelfClosing); var attribute = Assert.Single(output.Attributes); - Assert.Equal(new KeyValuePair("aCTiON", "my-action"), attribute); + Assert.Equal(new KeyValuePair("aCTiON", "my-action"), attribute); Assert.Empty(output.PreContent.GetContent()); Assert.True(output.Content.IsEmpty); Assert.Equal(expectedPostContent, output.PostContent.GetContent()); @@ -364,13 +315,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers public async Task ProcessAsync_ThrowsIfActionConflictsWithBoundAttributes(string propertyName) { // Arrange - var formTagHelper = new FormTagHelper - { - Method = "POST" - }; + var formTagHelper = new FormTagHelper(); var tagHelperOutput = new TagHelperOutput( "form", - attributes: new Dictionary + attributes: new Dictionary { { "action", "my-action" }, }); diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/InputTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/InputTagHelperTest.cs index 05c644d947..806abfc296 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/InputTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/InputTagHelperTest.cs @@ -81,7 +81,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string expectedValue) { // Arrange - var expectedAttributes = new Dictionary + var expectedAttributes = new Dictionary { { "class", "form-control" }, { "type", "text" }, @@ -105,7 +105,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.SetContent("Something"); return Task.FromResult(tagHelperContent); }); - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "class", "form-control" }, }; @@ -166,7 +166,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.SetContent("Something"); return Task.FromResult(tagHelperContent); }); - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "class", "form-control" }, }; @@ -242,7 +242,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers contextAttributes["type"] = inputTypeName; // Support restoration of type attribute, if any. } - var expectedAttributes = new Dictionary + var expectedAttributes = new Dictionary { { "class", "form-control hidden-control" }, { "type", inputTypeName ?? "hidden" }, // Generator restores type attribute; adds "hidden" if none. @@ -262,7 +262,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.SetContent("Something"); return Task.FromResult(tagHelperContent); }); - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "class", "form-control" }, }; @@ -341,7 +341,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers contextAttributes["type"] = inputTypeName; // Support restoration of type attribute, if any. } - var expectedAttributes = new Dictionary + var expectedAttributes = new Dictionary { { "class", "form-control password-control" }, { "type", inputTypeName ?? "password" }, // Generator restores type attribute; adds "password" if none. @@ -361,7 +361,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.SetContent("Something"); return Task.FromResult(tagHelperContent); }); - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "class", "form-control" }, }; @@ -436,7 +436,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers contextAttributes["type"] = inputTypeName; // Support restoration of type attribute, if any. } - var expectedAttributes = new Dictionary + var expectedAttributes = new Dictionary { { "class", "form-control radio-control" }, { "type", inputTypeName ?? "radio" }, // Generator restores type attribute; adds "radio" if none. @@ -457,7 +457,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.SetContent("Something"); return Task.FromResult(tagHelperContent); }); - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "class", "form-control" }, }; @@ -542,7 +542,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers contextAttributes["type"] = inputTypeName; // Support restoration of type attribute, if any. } - var expectedAttributes = new Dictionary + var expectedAttributes = new Dictionary { { "class", "form-control text-control" }, { "type", inputTypeName ?? "text" }, // Generator restores type attribute; adds "text" if none. @@ -562,7 +562,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.SetContent("Something"); return Task.FromResult(tagHelperContent); }); - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "class", "form-control" }, }; diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/LabelTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/LabelTagHelperTest.cs index 1733b80fd1..7e15c4c5b8 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/LabelTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/LabelTagHelperTest.cs @@ -7,7 +7,6 @@ using System.Threading.Tasks; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Razor.Runtime.TagHelpers; -using Microsoft.Framework.WebEncoders; using Xunit; namespace Microsoft.AspNet.Mvc.TagHelpers @@ -165,7 +164,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers { // Arrange var expectedTagName = "not-label"; - var expectedAttributes = new Dictionary + var expectedAttributes = new Dictionary { { "class", "form-control" }, { "for", tagHelperOutputContent.ExpectedId } @@ -196,7 +195,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.SetContent(tagHelperOutputContent.OriginalChildContent); return Task.FromResult(tagHelperContent); }); - var htmlAttributes = new Dictionary + var htmlAttributes = new Dictionary { { "class", "form-control" }, }; diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/LinkTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/LinkTagHelperTest.cs index c0d529d624..23e45957f2 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/LinkTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/LinkTagHelperTest.cs @@ -179,6 +179,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var helper = new LinkTagHelper { HtmlEncoder = new HtmlEncoder(), + JavaScriptEncoder = new JavaScriptStringEncoder(), Logger = logger.Object, HostingEnvironment = hostingEnvironment, ViewContext = viewContext, @@ -202,8 +203,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var context = MakeTagHelperContext( attributes: new Dictionary { - ["rel"] = "stylesheet", - ["data-extra"] = "something", + ["rel"] = new HtmlString("stylesheet"), + ["data-extra"] = new HtmlString("something"), ["href"] = "test.css", ["asp-fallback-href"] = "test.css", ["asp-fallback-test-class"] = "hidden", @@ -211,11 +212,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers ["asp-fallback-test-value"] = "hidden" }); var output = MakeTagHelperOutput("link", - attributes: new Dictionary + attributes: new Dictionary { - ["rel"] = "stylesheet", - ["data-extra"] = "something", - ["href"] = "test.css" + ["rel"] = new HtmlString("stylesheet"), + ["data-extra"] = new HtmlString("something"), }); var logger = new Mock>(); var hostingEnvironment = MakeHostingEnvironment(); @@ -223,6 +223,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var helper = new LinkTagHelper { HtmlEncoder = new HtmlEncoder(), + JavaScriptEncoder = new JavaScriptStringEncoder(), Logger = logger.Object, HostingEnvironment = hostingEnvironment, ViewContext = viewContext, @@ -230,6 +231,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers FallbackTestClass = "hidden", FallbackTestProperty = "visibility", FallbackTestValue = "hidden", + Href = "test.css", Cache = MakeCache(), }; @@ -374,14 +376,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var context = MakeTagHelperContext( attributes: new Dictionary { + ["rel"] = new HtmlString("stylesheet"), ["href"] = "/css/site.css", - ["rel"] = "stylesheet", ["asp-href-include"] = "**/*.css" }); - var output = MakeTagHelperOutput("link", attributes: new Dictionary + var output = MakeTagHelperOutput("link", attributes: new Dictionary { - ["href"] = "/css/site.css", - ["rel"] = "stylesheet" + ["rel"] = new HtmlString("stylesheet"), }); var logger = new Mock>(); var hostingEnvironment = MakeHostingEnvironment(); @@ -396,6 +397,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Logger = logger.Object, HostingEnvironment = hostingEnvironment, ViewContext = viewContext, + Href = "/css/site.css", HrefInclude = "**/*.css", Cache = MakeCache(), }; @@ -404,8 +406,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers helper.Process(context, output); // Assert - Assert.Equal("" + - "", output.Content.GetContent()); + Assert.Equal( + "" + + "", + output.Content.GetContent()); } [Fact] @@ -415,14 +419,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var context = MakeTagHelperContext( attributes: new Dictionary { - ["href"] = "/css/site.css", ["rel"] = "stylesheet", + ["href"] = "/css/site.css", ["asp-href-include"] = "**/*.css" }); - var output = MakeTagHelperOutput("link", attributes: new Dictionary + var output = MakeTagHelperOutput("link", attributes: new Dictionary { - ["href"] = "/css/site.css", - ["rel"] = "stylesheet" + ["rel"] = "stylesheet", }); var logger = new Mock>(); var hostingEnvironment = MakeHostingEnvironment(); @@ -437,6 +440,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Logger = logger.Object, HostingEnvironment = hostingEnvironment, ViewContext = viewContext, + Href = "/css/site.css", HrefInclude = "**/*.css", Cache = MakeCache(), }; @@ -445,8 +449,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers helper.Process(context, output); // Assert - Assert.Equal("" + - "", output.Content.GetContent()); + Assert.Equal( + "" + + "", + output.Content.GetContent()); } [Fact] @@ -456,14 +462,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var context = MakeTagHelperContext( attributes: new Dictionary { + ["rel"] = new HtmlString("stylesheet"), ["href"] = "/css/site.css", - ["rel"] = "stylesheet", ["asp-file-version"] = "true" }); - var output = MakeTagHelperOutput("link", attributes: new Dictionary + var output = MakeTagHelperOutput("link", attributes: new Dictionary { - ["href"] = "/css/site.css", - ["rel"] = "stylesheet" + ["rel"] = new HtmlString("stylesheet"), }); var logger = new Mock>(); var hostingEnvironment = MakeHostingEnvironment(); @@ -474,6 +479,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Logger = logger.Object, HostingEnvironment = hostingEnvironment, ViewContext = viewContext, + Href = "/css/site.css", HrefInclude = "**/*.css", FileVersion = true, Cache = MakeCache(), @@ -483,8 +489,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers helper.Process(context, output); // Assert - Assert.Equal("", output.Content.GetContent()); + Assert.Equal( + "", + output.Content.GetContent()); } [Fact] @@ -494,14 +501,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var context = MakeTagHelperContext( attributes: new Dictionary { + ["rel"] = new HtmlString("stylesheet"), ["href"] = "/bar/css/site.css", - ["rel"] = "stylesheet", ["asp-file-version"] = "true" }); - var output = MakeTagHelperOutput("link", attributes: new Dictionary + var output = MakeTagHelperOutput("link", attributes: new Dictionary { - ["href"] = "/bar/css/site.css", - ["rel"] = "stylesheet" + ["rel"] = new HtmlString("stylesheet"), }); var logger = new Mock>(); var hostingEnvironment = MakeHostingEnvironment(); @@ -512,6 +518,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Logger = logger.Object, HostingEnvironment = hostingEnvironment, ViewContext = viewContext, + Href = "/bar/css/site.css", HrefInclude = "**/*.css", FileVersion = true, Cache = MakeCache(), @@ -521,8 +528,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers helper.Process(context, output); // Assert - Assert.Equal("", output.Content.GetContent()); + Assert.Equal( + "", + output.Content.GetContent()); } [Fact] @@ -532,15 +540,14 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var context = MakeTagHelperContext( attributes: new Dictionary { + ["rel"] = new HtmlString("stylesheet"), ["href"] = "/css/site.css", - ["rel"] = "stylesheet", ["asp-href-include"] = "**/*.css", ["asp-file-version"] = "true" }); - var output = MakeTagHelperOutput("link", attributes: new Dictionary + var output = MakeTagHelperOutput("link", attributes: new Dictionary { - ["href"] = "/css/site.css", - ["rel"] = "stylesheet" + ["rel"] = new HtmlString("stylesheet"), }); var logger = new Mock>(); var hostingEnvironment = MakeHostingEnvironment(); @@ -555,6 +562,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers Logger = logger.Object, HostingEnvironment = hostingEnvironment, ViewContext = viewContext, + Href = "/css/site.css", HrefInclude = "**/*.css", FileVersion = true, Cache = MakeCache(), @@ -562,11 +570,12 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Act helper.Process(context, output); - + // Assert - Assert.Equal("", output.Content.GetContent()); + Assert.Equal( + "" + + "", + output.Content.GetContent()); } private static ViewContext MakeViewContext(string requestPathBase = null) @@ -607,9 +616,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); } - private static TagHelperOutput MakeTagHelperOutput(string tagName, IDictionary attributes = null) + private static TagHelperOutput MakeTagHelperOutput(string tagName, IDictionary attributes = null) { - attributes = attributes ?? new Dictionary(); + attributes = attributes ?? new Dictionary(); return new TagHelperOutput(tagName, attributes); } @@ -671,27 +680,5 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); return cache.Object; } - - private class TestHtmlEncoder : IHtmlEncoder - { - public string HtmlEncode(string value) - { - return "HtmlEncode[[" + value + "]]"; - } - - public void HtmlEncode(string value, int startIndex, int charCount, TextWriter output) - { - output.Write("HtmlEncode[["); - output.Write(value.Substring(startIndex, charCount)); - output.Write("]]"); - } - - public void HtmlEncode(char[] value, int startIndex, int charCount, TextWriter output) - { - output.Write("HtmlEncode[["); - output.Write(value, startIndex, charCount); - output.Write("]]"); - } - } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/OptionTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/OptionTagHelperTest.cs index 3ec949e6b8..8f222a0344 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/OptionTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/OptionTagHelperTest.cs @@ -26,7 +26,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers null, null, null, null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" } }, @@ -36,7 +36,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers null, string.Empty, "value", null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" }, { "selected", "" } }, @@ -46,7 +46,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers null, "selected", "value", null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" }, { "selected", "selected" } }, @@ -56,7 +56,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers null, null, "value", Enumerable.Empty(), GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" } }, @@ -66,7 +66,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers null, null, "value", new [] { string.Empty, }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" } }, @@ -76,7 +76,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers null, string.Empty, "value", new [] { string.Empty, }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" }, { "selected", "" } }, @@ -86,7 +86,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers null, null, "value", new [] { "value", }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" }, { "selected", "selected" } }, @@ -96,7 +96,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers null, null, "value", new [] { string.Empty, "value", }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" }, { "selected", "selected" } }, @@ -106,7 +106,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string.Empty, null, null, null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" } }, @@ -116,7 +116,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string.Empty, string.Empty, null, null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "selected", "" } }, @@ -126,7 +126,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string.Empty, "selected", null, null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "selected", "selected" } }, @@ -136,7 +136,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string.Empty, null, null, Enumerable.Empty(), GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" } }, @@ -146,7 +146,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string.Empty, null, null, new [] { string.Empty, }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "selected", "selected" } }, @@ -156,7 +156,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string.Empty, string.Empty, null, new [] { string.Empty, }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "selected", "" } }, @@ -166,7 +166,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string.Empty, null, null, new [] { "text", }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" } }, @@ -176,18 +176,17 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string.Empty, null, null, new [] { string.Empty, "text", }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "selected", "selected" } }, "") }, - { "text", null, null, null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" } }, @@ -197,7 +196,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", string.Empty, null, null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "selected", "" } }, @@ -207,7 +206,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", "selected", null, null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "selected", "selected" } }, @@ -217,7 +216,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", null, null, Enumerable.Empty(), GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" } }, @@ -227,7 +226,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", null, null, new [] { string.Empty, }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" } }, @@ -237,7 +236,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", null, null, new [] { "text", }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "selected", "selected" } }, @@ -247,7 +246,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", string.Empty, null, new [] { "text", }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "selected", "" } }, @@ -257,18 +256,17 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", null, null, new [] { string.Empty, "text", }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "selected", "selected" } }, "text") }, - { "text", string.Empty, "value", null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" }, { "selected", "" } }, @@ -278,7 +276,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", "selected", "value", null, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" }, { "selected", "selected" } }, @@ -288,7 +286,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", null, "value", Enumerable.Empty(), GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" } }, @@ -298,7 +296,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", null, "value", new [] { string.Empty, }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" } }, @@ -308,7 +306,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", string.Empty, "value", new [] { string.Empty, }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" }, { "selected", "" } }, @@ -318,7 +316,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", null, "value", new [] { "text", }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" } }, @@ -328,7 +326,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", null, "value", new [] { "value", }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" }, { "selected", "selected" } }, @@ -338,7 +336,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers "text", null, "value", new [] { string.Empty, "value", }, GetTagHelperOutput( "not-option", - new Dictionary + new Dictionary { { "label", "my-label" }, { "value", "value" }, { "selected", "selected" } }, @@ -381,17 +379,21 @@ namespace Microsoft.AspNet.Mvc.TagHelpers TagHelperOutput expectedTagHelperOutput) { // Arrange - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "label", "my-label" }, }; + if (selected != null) + { + originalAttributes.Add("selected", selected); + } - var contextAttributes = new Dictionary + var contextAttributes = new Dictionary(originalAttributes); + if (value != null) { - { "label", "my-label" }, - { "selected", selected }, - { "value", value }, - }; + contextAttributes.Add("value", value); + } + var tagHelperContext = new TagHelperContext( contextAttributes, items: new Dictionary(), @@ -402,6 +404,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.SetContent(originalContent); return Task.FromResult(tagHelperContent); }); + var output = new TagHelperOutput(expectedTagHelperOutput.TagName, originalAttributes) { SelfClosing = false, @@ -418,7 +421,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var tagHelper = new OptionTagHelper { Generator = htmlGenerator, - Selected = selected, Value = value, ViewContext = viewContext, }; @@ -446,9 +448,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers TagHelperOutput ignored) { // Arrange - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "label", "my-label" }, + { "selected", selected }, }; var originalTagName = "not-option"; @@ -487,7 +490,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers viewContext.FormContext.FormData[SelectTagHelper.SelectedValuesFormDataKey] = selectedValues; var tagHelper = new OptionTagHelper { - Selected = selected, Value = value, ViewContext = viewContext, }; @@ -507,9 +509,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers TagHelperOutput ignoredOutput) { // Arrange - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "label", "my-label" }, + { "selected", selected }, }; var originalTagName = "not-option"; @@ -541,7 +544,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var tagHelper = new OptionTagHelper { - Selected = selected, Value = value, }; @@ -551,7 +553,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers } private static TagHelperOutput GetTagHelperOutput( - string tagName, IDictionary attributes, string content) + string tagName, IDictionary attributes, string content) { var tagHelperOutput = new TagHelperOutput(tagName, attributes); tagHelperOutput.Content.SetContent(content); diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ScriptTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ScriptTagHelperTest.cs index 720aa801f3..54e713e313 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ScriptTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ScriptTagHelperTest.cs @@ -223,6 +223,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var helper = new ScriptTagHelper { HtmlEncoder = new HtmlEncoder(), + JavaScriptEncoder = new JavaScriptStringEncoder(), Logger = logger, HostingEnvironment = hostingEnvironment, ViewContext = viewContext, @@ -441,10 +442,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var viewContext = MakeViewContext(); var output = MakeTagHelperOutput("src", - attributes: new Dictionary + attributes: new Dictionary { ["data-extra"] = "something", - ["src"] = "/blank.js", ["data-more"] = "else", }); @@ -454,11 +454,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var helper = new ScriptTagHelper { HtmlEncoder = new HtmlEncoder(), + JavaScriptEncoder = new JavaScriptStringEncoder(), Logger = logger, ViewContext = viewContext, HostingEnvironment = hostingEnvironment, FallbackSrc = "~/blank.js", FallbackTestExpression = "http://www.example.com/blank.js", + Src = "/blank.js", Cache = MakeCache(), }; @@ -467,7 +469,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Assert Assert.StartsWith( - "\r\n\r\n", output.Content.GetContent()); } @@ -679,10 +675,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers ["asp-src-include"] = "*.js", ["asp-file-version"] = "true" }); - var output = MakeTagHelperOutput("script", attributes: new Dictionary - { - ["src"] = "/js/site.js" - }); + var output = MakeTagHelperOutput("script", attributes: new Dictionary()); var logger = new Mock>(); var hostingEnvironment = MakeHostingEnvironment(); var viewContext = MakeViewContext(); @@ -698,6 +691,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers SrcInclude = "*.js", FileVersion = true, HtmlEncoder = new TestHtmlEncoder(), + JavaScriptEncoder = new TestJavaScriptEncoder(), + Src = "/js/site.js", Cache = MakeCache(), }; @@ -748,9 +743,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers return viewContext; } - private TagHelperOutput MakeTagHelperOutput(string tagName, IDictionary attributes = null) + private TagHelperOutput MakeTagHelperOutput(string tagName, IDictionary attributes = null) { - attributes = attributes ?? new Dictionary(); + attributes = attributes ?? new Dictionary(); return new TagHelperOutput(tagName, attributes); } @@ -817,27 +812,5 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); return cache.Object; } - - private class TestHtmlEncoder : IHtmlEncoder - { - public string HtmlEncode(string value) - { - return "HtmlEncode[[" + value + "]]"; - } - - public void HtmlEncode(string value, int startIndex, int charCount, TextWriter output) - { - output.Write("HtmlEncode[["); - output.Write(value.Substring(startIndex, charCount)); - output.Write("]]"); - } - - public void HtmlEncode(char[] value, int startIndex, int charCount, TextWriter output) - { - output.Write("HtmlEncode[["); - output.Write(value, startIndex, charCount); - output.Write("]]"); - } - } } } \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/SelectTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/SelectTagHelperTest.cs index edcc515f3d..fb21004ba7 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/SelectTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/SelectTagHelperTest.cs @@ -9,7 +9,6 @@ using System.Threading.Tasks; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Razor.Runtime.TagHelpers; -using Microsoft.Framework.WebEncoders; using Moq; using Xunit; @@ -172,13 +171,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string ignored) { // Arrange - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "class", "form-control" }, }; var originalPostContent = "original content"; - var expectedAttributes = new Dictionary(originalAttributes) + var expectedAttributes = new Dictionary(originalAttributes) { { "id", nameAndId.Id }, { "name", nameAndId.Name }, @@ -258,13 +257,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string expectedOptions) { // Arrange - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "class", "form-control" }, }; var originalPostContent = "original content"; - var expectedAttributes = new Dictionary(originalAttributes) + var expectedAttributes = new Dictionary(originalAttributes) { { "id", nameAndId.Id }, { "name", nameAndId.Name }, @@ -359,13 +358,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string expectedOptions) { // Arrange - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { "class", "form-control" }, }; var originalPostContent = "original content"; - var expectedAttributes = new Dictionary(originalAttributes) + var expectedAttributes = new Dictionary(originalAttributes) { { "id", nameAndId.Id }, { "name", nameAndId.Name }, @@ -465,7 +464,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Provided for completeness. Select tag helper does not confirm AllAttributes set is consistent. { attributeName, attributeValue }, }; - var originalAttributes = new Dictionary + var originalAttributes = new Dictionary { { attributeName, attributeValue }, }; @@ -547,7 +546,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers { // Arrange var contextAttributes = new Dictionary(); - var originalAttributes = new Dictionary(); + var originalAttributes = new Dictionary(); var propertyName = "Property1"; var tagName = "select"; diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TagHelperOutputExtensionsTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TagHelperOutputExtensionsTest.cs index 7a8e8bfa70..43cef4b14a 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TagHelperOutputExtensionsTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TagHelperOutputExtensionsTest.cs @@ -21,7 +21,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary()); + attributes: new Dictionary()); var tagHelperContext = new TagHelperContext( allAttributes: new Dictionary(StringComparer.Ordinal) { @@ -35,7 +35,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.Append("Something"); return Task.FromResult(tagHelperContent); }); - var expectedAttribute = new KeyValuePair(attributeName, attributeValue); + var expectedAttribute = new KeyValuePair(attributeName, attributeValue); // Act tagHelperOutput.CopyHtmlAttribute("hello", tagHelperContext); @@ -52,11 +52,11 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var attributeName = "hello"; var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary() + attributes: new Dictionary() { { attributeName, "world2" } }); - var expectedAttribute = new KeyValuePair(attributeName, "world2"); + var expectedAttribute = new KeyValuePair(attributeName, "world2"); var tagHelperContext = new TagHelperContext( allAttributes: new Dictionary(StringComparer.Ordinal) { @@ -85,12 +85,12 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary() + attributes: new Dictionary() { { "route-Hello", "World" }, { "Route-I", "Am" } }); - var expectedAttribute = new KeyValuePair("type", "btn"); + var expectedAttribute = new KeyValuePair("type", "btn"); tagHelperOutput.Attributes.Add(expectedAttribute); var attributes = tagHelperOutput.FindPrefixedAttributes("route-"); @@ -108,7 +108,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary() + attributes: new Dictionary() { { "routeHello", "World" }, { "Routee-I", "Am" } @@ -131,8 +131,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary()); - var expectedAttribute = new KeyValuePair("type", "btn"); + attributes: new Dictionary()); + var expectedAttribute = new KeyValuePair("type", "btn"); tagHelperOutput.Attributes.Add(expectedAttribute); var tagBuilder = new TagBuilder("p", new HtmlEncoder()); @@ -152,13 +152,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary()); + attributes: new Dictionary()); tagHelperOutput.Attributes.Add("class", "Hello"); var tagBuilder = new TagBuilder("p", new HtmlEncoder()); tagBuilder.Attributes.Add("class", "btn"); - var expectedAttribute = new KeyValuePair("class", "Hello btn"); + var expectedAttribute = new KeyValuePair("class", "Hello btn"); // Act tagHelperOutput.MergeAttributes(tagBuilder); @@ -178,7 +178,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary()); + attributes: new Dictionary()); tagHelperOutput.Attributes.Add(originalName, "Hello"); var tagBuilder = new TagBuilder("p", new HtmlEncoder()); @@ -189,7 +189,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Assert var attribute = Assert.Single(tagHelperOutput.Attributes); - Assert.Equal(new KeyValuePair(originalName, "Hello btn"), attribute); + Assert.Equal(new KeyValuePair(originalName, "Hello btn"), attribute); } [Fact] @@ -198,11 +198,11 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary()); + attributes: new Dictionary()); var tagBuilder = new TagBuilder("p", new HtmlEncoder()); - var expectedAttribute = new KeyValuePair("visible", "val < 3"); - tagBuilder.Attributes.Add(expectedAttribute); + var expectedAttribute = new KeyValuePair("visible", "val < 3"); + tagBuilder.Attributes.Add("visible", "val < 3"); // Act tagHelperOutput.MergeAttributes(tagBuilder); @@ -218,13 +218,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary()); + attributes: new Dictionary()); var tagBuilder = new TagBuilder("p", new HtmlEncoder()); - var expectedAttribute1 = new KeyValuePair("class", "btn"); - var expectedAttribute2 = new KeyValuePair("class2", "btn"); - tagBuilder.Attributes.Add(expectedAttribute1); - tagBuilder.Attributes.Add(expectedAttribute2); + var expectedAttribute1 = new KeyValuePair("class", "btn"); + var expectedAttribute2 = new KeyValuePair("class2", "btn"); + tagBuilder.Attributes.Add("class", "btn"); + tagBuilder.Attributes.Add("class2", "btn"); // Act tagHelperOutput.MergeAttributes(tagBuilder); @@ -243,8 +243,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary()); - var expectedAttribute = new KeyValuePair("class", "btn"); + attributes: new Dictionary()); + var expectedAttribute = new KeyValuePair("class", "btn"); tagHelperOutput.Attributes.Add(expectedAttribute); var tagBuilder = new TagBuilder("p", new HtmlEncoder()); @@ -263,13 +263,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // Arrange var tagHelperOutput = new TagHelperOutput( "p", - attributes: new Dictionary()); - var expectedOutputAttribute = new KeyValuePair("class", "btn"); + attributes: new Dictionary()); + var expectedOutputAttribute = new KeyValuePair("class", "btn"); tagHelperOutput.Attributes.Add(expectedOutputAttribute); var tagBuilder = new TagBuilder("p", new HtmlEncoder()); - var expectedBuilderAttribute = new KeyValuePair("for", "hello"); - tagBuilder.Attributes.Add(expectedBuilderAttribute); + var expectedBuilderAttribute = new KeyValuePair("for", "hello"); + tagBuilder.Attributes.Add("for", "hello"); // Act tagHelperOutput.MergeAttributes(tagBuilder); diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TextAreaTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TextAreaTagHelperTest.cs index 4cbfbae251..7b644bd765 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TextAreaTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/TextAreaTagHelperTest.cs @@ -7,7 +7,6 @@ using System.Threading.Tasks; using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Razor.Runtime.TagHelpers; -using Microsoft.Framework.WebEncoders; using Xunit; namespace Microsoft.AspNet.Mvc.TagHelpers @@ -89,7 +88,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers string expectedContent) { // Arrange - var expectedAttributes = new Dictionary + var expectedAttributes = new Dictionary { { "class", "form-control" }, { "id", nameAndId.Id }, @@ -123,7 +122,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers tagHelperContent.SetContent("Something"); return Task.FromResult(tagHelperContent); }); - var htmlAttributes = new Dictionary + var htmlAttributes = new Dictionary { { "class", "form-control" }, }; diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ValidationMessageTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ValidationMessageTagHelperTest.cs index 5fc828640a..bfec75c2aa 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ValidationMessageTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ValidationMessageTagHelperTest.cs @@ -49,7 +49,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); var output = new TagHelperOutput( expectedTagName, - attributes: new Dictionary + attributes: new Dictionary { { "id", "myvalidationmessage" } }); @@ -105,7 +105,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); var output = new TagHelperOutput( "span", - attributes: new Dictionary()); + attributes: new Dictionary()); output.PreContent.SetContent(expectedPreContent); output.Content.SetContent(expectedContent); output.PostContent.SetContent(expectedPostContent); @@ -145,7 +145,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }; var output = new TagHelperOutput( "span", - attributes: new Dictionary()); + attributes: new Dictionary()); output.Content.SetContent(outputContent); var context = new TagHelperContext( @@ -204,7 +204,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }; var output = new TagHelperOutput( "span", - attributes: new Dictionary()); + attributes: new Dictionary()); var context = new TagHelperContext( allAttributes: new Dictionary(), @@ -259,7 +259,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var expectedPostContent = "original post-content"; var output = new TagHelperOutput( "span", - attributes: new Dictionary()); + attributes: new Dictionary()); output.PreContent.SetContent(expectedPreContent); output.Content.SetContent(expectedContent); output.PostContent.SetContent(expectedPostContent); diff --git a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ValidationSummaryTagHelperTest.cs b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ValidationSummaryTagHelperTest.cs index 4baef341e7..42ffd90f9a 100644 --- a/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ValidationSummaryTagHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.TagHelpers.Test/ValidationSummaryTagHelperTest.cs @@ -55,7 +55,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers }); var output = new TagHelperOutput( expectedTagName, - attributes: new Dictionary + attributes: new Dictionary { { "class", "form-control" } }); @@ -102,7 +102,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var expectedPostContent = "original post-content"; var output = new TagHelperOutput( "div", - attributes: new Dictionary()); + attributes: new Dictionary()); output.PreContent.SetContent(expectedPreContent); output.Content.SetContent(expectedContent); output.PostContent.SetContent(expectedPostContent); @@ -144,7 +144,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var expectedContent = "original content"; var output = new TagHelperOutput( "div", - attributes: new Dictionary()); + attributes: new Dictionary()); output.PreContent.SetContent(expectedPreContent); output.Content.SetContent(expectedContent); output.PostContent.SetContent("Content of validation summary"); @@ -201,7 +201,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var expectedPostContent = "original post-content"; var output = new TagHelperOutput( "div", - attributes: new Dictionary()); + attributes: new Dictionary()); output.PreContent.SetContent(expectedPreContent); output.Content.SetContent(expectedContent); output.PostContent.SetContent(expectedPostContent); @@ -236,7 +236,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var expectedContent = "original content"; var output = new TagHelperOutput( "div", - attributes: new Dictionary()); + attributes: new Dictionary()); output.PreContent.SetContent(expectedPreContent); output.Content.SetContent(expectedContent); output.PostContent.SetContent("Content of validation message"); diff --git a/test/Microsoft.AspNet.Mvc.TestCommon/TestHtmlEncoder.cs b/test/Microsoft.AspNet.Mvc.TestCommon/TestHtmlEncoder.cs new file mode 100644 index 0000000000..21cac68cf0 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.TestCommon/TestHtmlEncoder.cs @@ -0,0 +1,27 @@ +// 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 System.IO; + +namespace Microsoft.Framework.WebEncoders +{ + internal class TestHtmlEncoder : IHtmlEncoder + { + public string HtmlEncode(string value) + { + return $"HtmlEncode[[{ value }]]"; + } + + public void HtmlEncode(string value, int startIndex, int charCount, TextWriter output) + { + output.Write($"HtmlEncode[[{ value.Substring(startIndex, charCount) }]]"); + } + + public void HtmlEncode(char[] value, int startIndex, int charCount, TextWriter output) + { + output.Write("HtmlEncode[["); + output.Write(value, startIndex, charCount); + output.Write("]]"); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.TestCommon/TestJavaScriptEncoder.cs b/test/Microsoft.AspNet.Mvc.TestCommon/TestJavaScriptEncoder.cs new file mode 100644 index 0000000000..214fad05e2 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.TestCommon/TestJavaScriptEncoder.cs @@ -0,0 +1,27 @@ +// 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 System.IO; + +namespace Microsoft.Framework.WebEncoders +{ + internal class TestJavaScriptEncoder : IJavaScriptStringEncoder + { + public string JavaScriptStringEncode(string value) + { + return $"JavaScriptEncode[[{ value }]]"; + } + + public void JavaScriptStringEncode(string value, int startIndex, int charCount, TextWriter output) + { + output.Write($"JavaScriptEncode[[{ value.Substring(startIndex, charCount) }]]"); + } + + public void JavaScriptStringEncode(char[] value, int startIndex, int charCount, TextWriter output) + { + output.Write("JavaScriptEncode[["); + output.Write(value, startIndex, charCount); + output.Write("]]"); + } + } +} \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.TestCommon/TestUrlEncoder.cs b/test/Microsoft.AspNet.Mvc.TestCommon/TestUrlEncoder.cs new file mode 100644 index 0000000000..e3465a3620 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.TestCommon/TestUrlEncoder.cs @@ -0,0 +1,27 @@ +// 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 System.IO; + +namespace Microsoft.Framework.WebEncoders +{ + internal class TestUrlEncoder : IUrlEncoder + { + public string UrlEncode(string value) + { + return $"UrlEncode[[{ value }]]"; + } + + public void UrlEncode(string value, int startIndex, int charCount, TextWriter output) + { + output.Write($"UrlEncode[[{ value.Substring(startIndex, charCount) }]]"); + } + + public void UrlEncode(char[] value, int startIndex, int charCount, TextWriter output) + { + output.Write("UrlEncode[["); + output.Write(value, startIndex, charCount); + output.Write("]]"); + } + } +} \ No newline at end of file diff --git a/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Index.cshtml b/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Index.cshtml index b9b1d201d0..6645b9131e 100644 --- a/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Index.cshtml +++ b/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Index.cshtml @@ -7,10 +7,10 @@

MvcTagHelperTest Index diff --git a/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Link.cshtml b/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Link.cshtml index 08f42bc575..460d97a620 100644 --- a/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Link.cshtml +++ b/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Link.cshtml @@ -7,13 +7,13 @@ Link - + - + - + @@ -37,8 +37,8 @@ - diff --git a/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Script.cshtml b/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Script.cshtml index 861d1a9779..1945aab3c5 100644 --- a/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Script.cshtml +++ b/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/Script.cshtml @@ -11,15 +11,15 @@

Script tag helper test

- - - diff --git a/test/WebSites/TagHelpersWebSite/TagHelpers/PrettyTagHelper.cs b/test/WebSites/TagHelpersWebSite/TagHelpers/PrettyTagHelper.cs index 8ae7fc18c8..5bc7a2b5be 100644 --- a/test/WebSites/TagHelpersWebSite/TagHelpers/PrettyTagHelper.cs +++ b/test/WebSites/TagHelpersWebSite/TagHelpers/PrettyTagHelper.cs @@ -23,12 +23,14 @@ namespace TagHelpersWebSite.TagHelpers public bool? MakePretty { get; set; } + public string Style { get; set; } + [Activate] public ViewContext ViewContext { get; set; } public override void Process(TagHelperContext context, TagHelperOutput output) { - // Need to check if output.TagName == null in-case the ConditionTagHelper calls into SuppressOutput and + // Need to check if output.TagName == null in-case the ConditionTagHelper calls into SuppressOutput and // therefore sets the TagName to null. if (MakePretty.HasValue && !MakePretty.Value || output.TagName == null) @@ -40,9 +42,8 @@ namespace TagHelpersWebSite.TagHelpers if (PrettyTagStyles.TryGetValue(output.TagName, out prettyStyle)) { - var style = string.Empty; - - if (output.Attributes.TryGetValue("style", out style)) + var style = Style ?? string.Empty; + if (!string.IsNullOrEmpty(style)) { style += ";"; }