From f152ea700419b8244677e10adf02aec2a9dc7f9e Mon Sep 17 00:00:00 2001 From: "N. Taylor Mullen" Date: Thu, 23 Apr 2015 16:02:07 -0700 Subject: [PATCH] React to aspnet/Razor#279 changes. - Updated all tests and TagHelpers to utilize the new TagHelperOutput.Attributes and TagHelperContext.AllAttribute types. aspnet/Razor#279 --- src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs | 2 +- .../AnchorTagHelper.cs | 6 +- .../FormTagHelper.cs | 6 +- .../InputTagHelper.cs | 15 +- .../Internal/AttributeMatcher.cs | 6 +- .../LinkTagHelper.cs | 10 +- .../OptionTagHelper.cs | 2 +- .../ScriptTagHelper.cs | 22 +- .../TagHelperOutputExtensions.cs | 35 +- ...ite.MvcTagHelper_Home.Product.Encoded.html | 2 +- ...persWebSite.MvcTagHelper_Home.Product.html | 2 +- .../RazorPageTest.cs | 66 ++-- .../AnchorTagHelperTest.cs | 23 +- .../CacheTagHelperTest.cs | 40 +- .../EnvironmentTagHelperTest.cs | 12 +- .../FormTagHelperTest.cs | 48 +-- .../InputTagHelperTest.cs | 141 +++++-- .../Internal/AttributeMatcherTest.cs | 11 +- .../LabelTagHelperTest.cs | 8 +- .../LinkTagHelperTest.cs | 222 ++++++++--- .../OptionTagHelperTest.cs | 78 ++-- .../ScriptTagHelperTest.cs | 130 +++++-- .../SelectTagHelperTest.cs | 30 +- .../TagHelperOutputExtensionsTest.cs | 353 ++++++++++++++++-- .../TextAreaTagHelperTest.cs | 8 +- .../ValidationMessageTagHelperTest.cs | 38 +- .../ValidationSummaryTagHelperTest.cs | 24 +- .../TagHelpers/ATagHelper.cs | 4 +- 28 files changed, 956 insertions(+), 388 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs index 5d85e48c37..463b2a32bb 100644 --- a/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs +++ b/src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs @@ -288,7 +288,7 @@ namespace Microsoft.AspNet.Mvc.Razor foreach (var attribute in tagHelperOutput.Attributes) { writer.Write(' '); - writer.Write(attribute.Key); + writer.Write(attribute.Name); writer.Write("=\""); WriteTo(writer, HtmlEncoder, attribute.Value, escapeQuotes: true); writer.Write('"'); diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/AnchorTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/AnchorTagHelper.cs index 1aa66bc63c..ebe6252eab 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/AnchorTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/AnchorTagHelper.cs @@ -82,7 +82,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var routePrefixedAttributes = output.FindPrefixedAttributes(RouteAttributePrefix); // If "href" is already set, it means the user is attempting to use a normal anchor. - if (output.Attributes.ContainsKey(Href)) + if (output.Attributes.ContainsName(Href)) { if (Action != null || Controller != null || @@ -154,7 +154,7 @@ 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) + IEnumerable routePrefixedAttributes) { Dictionary routeValues = null; if (routePrefixedAttributes.Any()) @@ -165,7 +165,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // 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 => attribute.Name.Substring(RouteAttributePrefix.Length), attribute => (object)attribute.Value.ToString()); } diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/FormTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/FormTagHelper.cs index 02db12682f..cce5aa97ff 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/FormTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/FormTagHelper.cs @@ -72,7 +72,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var routePrefixedAttributes = output.FindPrefixedAttributes(RouteAttributePrefix); // If "action" is already set, it means the user is attempting to use a normal
. - if (output.Attributes.ContainsKey(HtmlActionAttributeName)) + if (output.Attributes.ContainsName(HtmlActionAttributeName)) { if (Action != null || Controller != null || Route != null || routePrefixedAttributes.Any()) { @@ -146,7 +146,7 @@ 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) + IEnumerable routePrefixedAttributes) { Dictionary routeValues = null; if (routePrefixedAttributes.Any()) @@ -157,7 +157,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers // 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 => attribute.Name.Substring(RouteAttributePrefix.Length), attribute => (object)attribute.Value.ToString()); } diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs index 09ebb5b702..12e5aee175 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/InputTagHelper.cs @@ -155,7 +155,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers } // inputType may be more specific than default the generator chooses below. - if (!output.Attributes.ContainsKey("type")) + if (!output.Attributes.ContainsName("type")) { output.Attributes["type"] = inputType; } @@ -218,9 +218,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers } // Prepare to move attributes from current element to generated just below. - var htmlAttributes = output.Attributes.ToDictionary( - attribute => attribute.Key, - attribute => (object)attribute.Value); + var htmlAttributes = new Dictionary(StringComparer.OrdinalIgnoreCase); + + // Construct attributes correctly (first attribute wins). + foreach (var attribute in output.Attributes) + { + if (!htmlAttributes.ContainsKey(attribute.Name)) + { + htmlAttributes.Add(attribute.Name, attribute.Value); + } + } var tagBuilder = Generator.GenerateCheckBox( ViewContext, diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/Internal/AttributeMatcher.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/Internal/AttributeMatcher.cs index 4152f2fa21..08953a1b7d 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/Internal/AttributeMatcher.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/Internal/AttributeMatcher.cs @@ -86,10 +86,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal foreach (var attribute in requiredAttributes) { - if (!context.AllAttributes.ContainsKey(attribute) || + if (!context.AllAttributes.ContainsName(attribute) || context.AllAttributes[attribute] == null || - (typeof(string).IsAssignableFrom(context.AllAttributes[attribute].GetType()) && - string.IsNullOrWhiteSpace(context.AllAttributes[attribute] as string))) + (typeof(string).IsAssignableFrom(context.AllAttributes[attribute].Value.GetType()) && + string.IsNullOrWhiteSpace(context.AllAttributes[attribute].Value as string))) { // Missing attribute! missingAttributes.Add(attribute); diff --git a/src/Microsoft.AspNet.Mvc.TagHelpers/LinkTagHelper.cs b/src/Microsoft.AspNet.Mvc.TagHelpers/LinkTagHelper.cs index 0bc7c12cb2..caf69579a7 100644 --- a/src/Microsoft.AspNet.Mvc.TagHelpers/LinkTagHelper.cs +++ b/src/Microsoft.AspNet.Mvc.TagHelpers/LinkTagHelper.cs @@ -236,7 +236,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers var mode = modeResult.FullMatches.Select(match => match.Mode).Max(); // NOTE: Values in TagHelperOutput.Attributes may already be HTML-encoded. - var attributes = new Dictionary(output.Attributes); + var attributes = new TagHelperAttributeList(output.Attributes); var builder = new DefaultTagHelperContent(); @@ -261,7 +261,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers output.Content.SetContent(builder); } - private void BuildGlobbedLinkTags(IDictionary attributes, TagHelperContent builder) + private void BuildGlobbedLinkTags(TagHelperAttributeList attributes, TagHelperContent builder) { EnsureGlobbingUrlBuilder(); @@ -341,7 +341,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers } } - private void BuildLinkTag(IDictionary attributes, TagHelperContent builder) + private void BuildLinkTag(TagHelperAttributeList attributes, TagHelperContent builder) { EnsureFileVersionProvider(); builder.Append(" is already selected. - if (!output.Attributes.ContainsKey("selected")) + if (!output.Attributes.ContainsName("selected")) { // Is this