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

Commit c2d3425

Browse files
author
NTaylorMullen
committed
Addressed code review comments.
1 parent 57a201b commit c2d3425

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

src/Microsoft.AspNet.Mvc.TagHelpers/FormTagHelper.cs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using Microsoft.AspNet.Mvc.Rendering;
8+
using Microsoft.AspNet.Mvc.TagHelpers.Internal;
89
using Microsoft.AspNet.Razor.Runtime.TagHelpers;
910
using Microsoft.AspNet.Razor.TagHelpers;
1011

@@ -16,11 +17,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
1617
[ContentBehavior(ContentBehavior.Append)]
1718
public class FormTagHelper : TagHelper
1819
{
19-
[Activate]
20-
private ViewContext ViewContext { get; set; }
20+
private const string RouteAttributePrefix = "route-";
2121

2222
[Activate]
23-
private AntiForgery AntiForgery { get; set; }
23+
private ViewContext ViewContext { get; set; }
2424

2525
[Activate]
2626
private IHtmlGenerator Generator { get; set; }
@@ -54,72 +54,68 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
5454
}
5555
else
5656
{
57-
// TODO: Make this behavior optional once https://github.com/aspnet/Razor/issues/121 is completed.
58-
output.Content = AntiForgery.GetHtml(ViewContext.HttpContext).ToString();
59-
6057
var routeValues = PullRouteValues(output.Attributes);
6158
var tagBuilder = Generator.GenerateForm(ViewContext,
6259
Action,
6360
Controller,
6461
routeValues,
6562
Method,
66-
htmlAttributes: new Dictionary<string, object>());
63+
htmlAttributes: null);
64+
65+
TagHelperOutputHelper.Merge(tagBuilder, output);
6766

68-
TagHelperOutputHelper.MergeAttributes(output, tagBuilder);
67+
// TODO: Make this behavior optional once https://github.com/aspnet/Razor/issues/121 is completed.
68+
var antiForgeryTag = Generator.GenerateAntiForgery(ViewContext);
69+
output.Content += antiForgeryTag.ToString(TagRenderMode.SelfClosing);
6970
}
7071
}
7172

7273
// TODO: We will not need this method once https://github.com/aspnet/Razor/issues/89 is completed.
7374
private static Dictionary<string, object> PullRouteValues(IDictionary<string, string> htmlAttributes)
7475
{
75-
var routeAttributePrefix = "route-";
76-
7776
// We're only interested in HTML attributes that have the desired routeAttributePrefix.
7877
var routeAttributes = htmlAttributes.Where(attribute =>
79-
attribute.Key.StartsWith(routeAttributePrefix, StringComparison.OrdinalIgnoreCase));
78+
attribute.Key.StartsWith(RouteAttributePrefix, StringComparison.OrdinalIgnoreCase));
8079

81-
// We need to remove any route based HTML attributes from the HTML attributes dictionary because
82-
// they shouldn't be treated as HTML attributes, they're route values.
80+
// Route based HTML attributes shouldn't be treated as HTML attributes, they're only route values.
8381
foreach (var attribute in routeAttributes)
8482
{
8583
htmlAttributes.Remove(attribute.Key);
8684
}
8785

88-
// We build a Dictionary<string, object> because Generator.GenerateForm does not accept a
89-
// Dictionary <string, string>.
90-
return routeAttributes.ToDictionary(attribute => attribute.Key.Substring(routeAttributePrefix.Length),
86+
// Generator.GenerateForm does not accept a Dictionary<string, string> for routeValues.
87+
return routeAttributes.ToDictionary(attribute => attribute.Key.Substring(RouteAttributePrefix.Length),
9188
attribute => (object)attribute.Value);
9289
}
9390

9491
private void RestoreBoundHtmlAttributes(TagHelperContext context, TagHelperOutput output)
9592
{
96-
var attributesToRestore = new List<string>();
97-
9893
if (Action != null)
9994
{
100-
attributesToRestore.Add(nameof(Action));
95+
RestoreBoundHtmlAttribute(nameof(Action), context, output);
10196
}
10297

10398
if (Controller != null)
10499
{
105-
attributesToRestore.Add(nameof(Controller));
100+
RestoreBoundHtmlAttribute(nameof(Controller), context, output);
106101
}
107102

108103
if (Method != null)
109104
{
110-
attributesToRestore.Add(nameof(Method));
105+
RestoreBoundHtmlAttribute(nameof(Method), context, output);
111106
}
107+
}
112108

113-
foreach (var attributeName in attributesToRestore)
114-
{
115-
// We need to look for the KeyValuePair<string, object> attribute so we can ensure that the attribute
116-
// that we re-add to the output object has the same attribute name as the one the user typed.
117-
var entry = context.AllAttributes.Single(attribute =>
118-
attribute.Key.Equals(attributeName, StringComparison.OrdinalIgnoreCase));
119-
var originalAttribute = new KeyValuePair<string, string>(entry.Key, entry.Value.ToString());
109+
private static void RestoreBoundHtmlAttribute(string boundAttributeName,
110+
TagHelperContext context,
111+
TagHelperOutput output)
112+
{
113+
// We look for the original attribute so we can then retrieve the exact attribute name the user typed.
114+
var entry = context.AllAttributes.Single(attribute =>
115+
attribute.Key.Equals(boundAttributeName, StringComparison.OrdinalIgnoreCase));
116+
var originalAttribute = new KeyValuePair<string, string>(entry.Key, entry.Value.ToString());
120117

121-
output.Attributes.Add(originalAttribute);
122-
}
118+
output.Attributes.Add(originalAttribute);
123119
}
124120
}
125121
}

0 commit comments

Comments
 (0)