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

Commit 6a61a8f

Browse files
committed
Update HtmlHelper to use IHtmlGenerator
- make a few more methods available as `internal static` in `DefaultHtmlGenerator` - remove `IHtmlGenerator.GenerateOption()`; now `internal static` nits: - add `IHtmlGenerator.IdAttributeDotReplacement` - move `DefaultHtmlGenerator.IdAttributeDotReplacement` after constructor - move `HtmlHelper.ActionLink()` below static methods - move newly-`internal` methods together in `DefaultHtmlGenerator` - correct placement of `DefaultHtmlGenerator.GetValidationAttributes()` comment
1 parent 1b181d3 commit 6a61a8f

File tree

6 files changed

+266
-905
lines changed

6 files changed

+266
-905
lines changed

src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultDisplayTemplates.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private static string BooleanTemplateDropDownList(IHtmlHelper html, bool? value)
5555
foreach (var item in TriStateValues(value))
5656
{
5757
var encodedText = html.Encode(item.Text);
58-
var option = HtmlHelper.GenerateOption(item, encodedText);
58+
var option = DefaultHtmlGenerator.GenerateOption(item, encodedText);
5959
builder.Append(option);
6060
}
6161

src/Microsoft.AspNet.Mvc.Core/Rendering/Html/DefaultHtmlGenerator.cs

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ public class DefaultHtmlGenerator : IHtmlGenerator
2626
private readonly IModelMetadataProvider _metadataProvider;
2727
private readonly IUrlHelper _urlHelper;
2828

29-
public string IdAttributeDotReplacement { get; set; }
30-
3129
/// <summary>
3230
/// Initializes a new instance of the <see cref="DefaultHtmlGenerator"/> class.
3331
/// </summary>
@@ -43,6 +41,9 @@ public DefaultHtmlGenerator(
4341
_urlHelper = urlHelper;
4442
}
4543

44+
/// <inheritdoc />
45+
public string IdAttributeDotReplacement { get; set; }
46+
4647
/// <inheritdoc />
4748
public string Encode(string value)
4849
{
@@ -252,32 +253,6 @@ public virtual TagBuilder GenerateLabel(
252253
return tagBuilder;
253254
}
254255

255-
/// <inheritdoc />
256-
public virtual TagBuilder GenerateOption(SelectListItem item, string encodedText)
257-
{
258-
var tagBuilder = new TagBuilder("option")
259-
{
260-
InnerHtml = encodedText,
261-
};
262-
263-
if (item.Value != null)
264-
{
265-
tagBuilder.Attributes["value"] = item.Value;
266-
}
267-
268-
if (item.Selected)
269-
{
270-
tagBuilder.Attributes["selected"] = "selected";
271-
}
272-
273-
if (item.Disabled)
274-
{
275-
tagBuilder.Attributes["disabled"] = "disabled";
276-
}
277-
278-
return tagBuilder;
279-
}
280-
281256
/// <inheritdoc />
282257
public virtual TagBuilder GeneratePassword(
283258
[NotNull] ViewContext viewContext,
@@ -725,6 +700,56 @@ public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
725700
new ClientModelValidationContext(metadata, _metadataProvider)));
726701
}
727702

703+
internal static string EvalString(ViewContext viewContext, string key, string format)
704+
{
705+
return Convert.ToString(viewContext.ViewData.Eval(key, format), CultureInfo.CurrentCulture);
706+
}
707+
708+
/// <remarks>
709+
/// Not used directly in HtmlHelper. Exposed for use in DefaultDisplayTemplates.
710+
/// </remarks>
711+
internal static TagBuilder GenerateOption(SelectListItem item, string encodedText)
712+
{
713+
var tagBuilder = new TagBuilder("option")
714+
{
715+
InnerHtml = encodedText,
716+
};
717+
718+
if (item.Value != null)
719+
{
720+
tagBuilder.Attributes["value"] = item.Value;
721+
}
722+
723+
if (item.Selected)
724+
{
725+
tagBuilder.Attributes["selected"] = "selected";
726+
}
727+
728+
if (item.Disabled)
729+
{
730+
tagBuilder.Attributes["disabled"] = "disabled";
731+
}
732+
733+
return tagBuilder;
734+
}
735+
736+
internal static string GetFullHtmlFieldName(ViewContext viewContext, string name)
737+
{
738+
var fullName = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
739+
return fullName;
740+
}
741+
742+
internal static object GetModelStateValue(ViewContext viewContext, string key, Type destinationType)
743+
{
744+
ModelState modelState;
745+
if (viewContext.ViewData.ModelState.TryGetValue(key, out modelState) && modelState.Value != null)
746+
{
747+
return modelState.Value.ConvertTo(destinationType, culture: null);
748+
}
749+
750+
return null;
751+
}
752+
728753
protected virtual TagBuilder GenerateInput(
729754
[NotNull] ViewContext viewContext,
730755
InputType inputType,
@@ -843,6 +868,8 @@ protected virtual TagBuilder GenerateLink(
843868
return tagBuilder;
844869
}
845870

871+
// Only render attributes if client-side validation is enabled, and then only if we've
872+
// never rendered validation for a field with this name in this form.
846873
protected virtual IDictionary<string, object> GetValidationAttributes(
847874
ViewContext viewContext,
848875
ModelMetadata metadata,
@@ -876,13 +903,6 @@ private static string EvalString(ViewContext viewContext, string key)
876903
return Convert.ToString(viewContext.ViewData.Eval(key), CultureInfo.CurrentCulture);
877904
}
878905

879-
// Only render attributes if client-side validation is enabled, and then only if we've
880-
// never rendered validation for a field with this name in this form.
881-
private static string EvalString(ViewContext viewContext, string key, string format)
882-
{
883-
return Convert.ToString(viewContext.ViewData.Eval(key, format), CultureInfo.CurrentCulture);
884-
}
885-
886906
// Only need a dictionary if htmlAttributes is non-null. TagBuilder.MergeAttributes() is fine with null.
887907
private static IDictionary<string, object> GetHtmlAttributeDictionaryOrNull(object htmlAttributes)
888908
{
@@ -899,12 +919,6 @@ private static IDictionary<string, object> GetHtmlAttributeDictionaryOrNull(obje
899919
return htmlAttributeDictionary;
900920
}
901921

902-
private static string GetFullHtmlFieldName(ViewContext viewContext, string name)
903-
{
904-
var fullName = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
905-
return fullName;
906-
}
907-
908922
private static string GetInputTypeString(InputType inputType)
909923
{
910924
switch (inputType)
@@ -924,17 +938,6 @@ private static string GetInputTypeString(InputType inputType)
924938
}
925939
}
926940

927-
private static object GetModelStateValue(ViewContext viewContext, string key, Type destinationType)
928-
{
929-
ModelState modelState;
930-
if (viewContext.ViewData.ModelState.TryGetValue(key, out modelState) && modelState.Value != null)
931-
{
932-
return modelState.Value.ConvertTo(destinationType, culture: null);
933-
}
934-
935-
return null;
936-
}
937-
938941
private static IEnumerable<SelectListItem> GetSelectListItems([NotNull] ViewContext viewContext, string name)
939942
{
940943
var value = viewContext.ViewData.Eval(name);

0 commit comments

Comments
 (0)