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
This commit is contained in:
parent
ba8b3d14a4
commit
3b7b0f867d
|
|
@ -55,7 +55,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
foreach (var item in TriStateValues(value))
|
foreach (var item in TriStateValues(value))
|
||||||
{
|
{
|
||||||
var encodedText = html.Encode(item.Text);
|
var encodedText = html.Encode(item.Text);
|
||||||
var option = HtmlHelper.GenerateOption(item, encodedText);
|
var option = DefaultHtmlGenerator.GenerateOption(item, encodedText);
|
||||||
builder.Append(option);
|
builder.Append(option);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
private readonly IModelMetadataProvider _metadataProvider;
|
private readonly IModelMetadataProvider _metadataProvider;
|
||||||
private readonly IUrlHelper _urlHelper;
|
private readonly IUrlHelper _urlHelper;
|
||||||
|
|
||||||
public string IdAttributeDotReplacement { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="DefaultHtmlGenerator"/> class.
|
/// Initializes a new instance of the <see cref="DefaultHtmlGenerator"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -43,6 +41,9 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
_urlHelper = urlHelper;
|
_urlHelper = urlHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public string IdAttributeDotReplacement { get; set; }
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public string Encode(string value)
|
public string Encode(string value)
|
||||||
{
|
{
|
||||||
|
|
@ -252,32 +253,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
return tagBuilder;
|
return tagBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public virtual TagBuilder GenerateOption(SelectListItem item, string encodedText)
|
|
||||||
{
|
|
||||||
var tagBuilder = new TagBuilder("option")
|
|
||||||
{
|
|
||||||
InnerHtml = encodedText,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (item.Value != null)
|
|
||||||
{
|
|
||||||
tagBuilder.Attributes["value"] = item.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.Selected)
|
|
||||||
{
|
|
||||||
tagBuilder.Attributes["selected"] = "selected";
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.Disabled)
|
|
||||||
{
|
|
||||||
tagBuilder.Attributes["disabled"] = "disabled";
|
|
||||||
}
|
|
||||||
|
|
||||||
return tagBuilder;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public virtual TagBuilder GeneratePassword(
|
public virtual TagBuilder GeneratePassword(
|
||||||
[NotNull] ViewContext viewContext,
|
[NotNull] ViewContext viewContext,
|
||||||
|
|
@ -725,6 +700,56 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
new ClientModelValidationContext(metadata, _metadataProvider)));
|
new ClientModelValidationContext(metadata, _metadataProvider)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal static string EvalString(ViewContext viewContext, string key, string format)
|
||||||
|
{
|
||||||
|
return Convert.ToString(viewContext.ViewData.Eval(key, format), CultureInfo.CurrentCulture);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <remarks>
|
||||||
|
/// Not used directly in HtmlHelper. Exposed for use in DefaultDisplayTemplates.
|
||||||
|
/// </remarks>
|
||||||
|
internal static TagBuilder GenerateOption(SelectListItem item, string encodedText)
|
||||||
|
{
|
||||||
|
var tagBuilder = new TagBuilder("option")
|
||||||
|
{
|
||||||
|
InnerHtml = encodedText,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (item.Value != null)
|
||||||
|
{
|
||||||
|
tagBuilder.Attributes["value"] = item.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.Selected)
|
||||||
|
{
|
||||||
|
tagBuilder.Attributes["selected"] = "selected";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (item.Disabled)
|
||||||
|
{
|
||||||
|
tagBuilder.Attributes["disabled"] = "disabled";
|
||||||
|
}
|
||||||
|
|
||||||
|
return tagBuilder;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static string GetFullHtmlFieldName(ViewContext viewContext, string name)
|
||||||
|
{
|
||||||
|
var fullName = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
|
||||||
|
return fullName;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static object GetModelStateValue(ViewContext viewContext, string key, Type destinationType)
|
||||||
|
{
|
||||||
|
ModelState modelState;
|
||||||
|
if (viewContext.ViewData.ModelState.TryGetValue(key, out modelState) && modelState.Value != null)
|
||||||
|
{
|
||||||
|
return modelState.Value.ConvertTo(destinationType, culture: null);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
protected virtual TagBuilder GenerateInput(
|
protected virtual TagBuilder GenerateInput(
|
||||||
[NotNull] ViewContext viewContext,
|
[NotNull] ViewContext viewContext,
|
||||||
InputType inputType,
|
InputType inputType,
|
||||||
|
|
@ -843,6 +868,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
return tagBuilder;
|
return tagBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Only render attributes if client-side validation is enabled, and then only if we've
|
||||||
|
// never rendered validation for a field with this name in this form.
|
||||||
protected virtual IDictionary<string, object> GetValidationAttributes(
|
protected virtual IDictionary<string, object> GetValidationAttributes(
|
||||||
ViewContext viewContext,
|
ViewContext viewContext,
|
||||||
ModelMetadata metadata,
|
ModelMetadata metadata,
|
||||||
|
|
@ -876,13 +903,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
return Convert.ToString(viewContext.ViewData.Eval(key), CultureInfo.CurrentCulture);
|
return Convert.ToString(viewContext.ViewData.Eval(key), CultureInfo.CurrentCulture);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only render attributes if client-side validation is enabled, and then only if we've
|
|
||||||
// never rendered validation for a field with this name in this form.
|
|
||||||
private static string EvalString(ViewContext viewContext, string key, string format)
|
|
||||||
{
|
|
||||||
return Convert.ToString(viewContext.ViewData.Eval(key, format), CultureInfo.CurrentCulture);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only need a dictionary if htmlAttributes is non-null. TagBuilder.MergeAttributes() is fine with null.
|
// Only need a dictionary if htmlAttributes is non-null. TagBuilder.MergeAttributes() is fine with null.
|
||||||
private static IDictionary<string, object> GetHtmlAttributeDictionaryOrNull(object htmlAttributes)
|
private static IDictionary<string, object> GetHtmlAttributeDictionaryOrNull(object htmlAttributes)
|
||||||
{
|
{
|
||||||
|
|
@ -899,12 +919,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
return htmlAttributeDictionary;
|
return htmlAttributeDictionary;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string GetFullHtmlFieldName(ViewContext viewContext, string name)
|
|
||||||
{
|
|
||||||
var fullName = viewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
|
|
||||||
return fullName;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string GetInputTypeString(InputType inputType)
|
private static string GetInputTypeString(InputType inputType)
|
||||||
{
|
{
|
||||||
switch (inputType)
|
switch (inputType)
|
||||||
|
|
@ -924,17 +938,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static object GetModelStateValue(ViewContext viewContext, string key, Type destinationType)
|
|
||||||
{
|
|
||||||
ModelState modelState;
|
|
||||||
if (viewContext.ViewData.ModelState.TryGetValue(key, out modelState) && modelState.Value != null)
|
|
||||||
{
|
|
||||||
return modelState.Value.ConvertTo(destinationType, culture: null);
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static IEnumerable<SelectListItem> GetSelectListItems([NotNull] ViewContext viewContext, string name)
|
private static IEnumerable<SelectListItem> GetSelectListItems([NotNull] ViewContext viewContext, string name)
|
||||||
{
|
{
|
||||||
var value = viewContext.ViewData.Eval(name);
|
var value = viewContext.ViewData.Eval(name);
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -16,12 +16,10 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
/// Initializes a new instance of the <see cref="HtmlHelper{TModel}"/> class.
|
/// Initializes a new instance of the <see cref="HtmlHelper{TModel}"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public HtmlHelper(
|
public HtmlHelper(
|
||||||
|
[NotNull] IHtmlGenerator htmlGenerator,
|
||||||
[NotNull] ICompositeViewEngine viewEngine,
|
[NotNull] ICompositeViewEngine viewEngine,
|
||||||
[NotNull] IModelMetadataProvider metadataProvider,
|
[NotNull] IModelMetadataProvider metadataProvider)
|
||||||
[NotNull] IUrlHelper urlHelper,
|
: base(htmlGenerator, viewEngine, metadataProvider)
|
||||||
[NotNull] AntiForgery antiForgeryInstance,
|
|
||||||
[NotNull] IActionBindingContextProvider actionBindingContextProvider)
|
|
||||||
: base(viewEngine, metadataProvider, urlHelper, antiForgeryInstance, actionBindingContextProvider)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,8 @@
|
||||||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
|
// 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.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.Contracts;
|
|
||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNet.Mvc.Core;
|
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
using Microsoft.AspNet.Mvc.Rendering.Expressions;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Rendering
|
namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
{
|
{
|
||||||
|
|
@ -22,6 +11,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IHtmlGenerator
|
public interface IHtmlGenerator
|
||||||
{
|
{
|
||||||
|
string IdAttributeDotReplacement { get; set; }
|
||||||
|
|
||||||
string Encode(string value);
|
string Encode(string value);
|
||||||
|
|
||||||
string Encode(object value);
|
string Encode(object value);
|
||||||
|
|
@ -101,11 +92,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
string labelText,
|
string labelText,
|
||||||
object htmlAttributes);
|
object htmlAttributes);
|
||||||
|
|
||||||
/// <remarks>
|
|
||||||
/// Not used directly in HtmlHelper. Exposed publicly for use in DefaultDisplayTemplates.
|
|
||||||
/// </remarks>
|
|
||||||
TagBuilder GenerateOption(SelectListItem item, string encodedText);
|
|
||||||
|
|
||||||
TagBuilder GeneratePassword(
|
TagBuilder GeneratePassword(
|
||||||
[NotNull] ViewContext viewContext,
|
[NotNull] ViewContext viewContext,
|
||||||
ModelMetadata metadata,
|
ModelMetadata metadata,
|
||||||
|
|
|
||||||
|
|
@ -145,15 +145,14 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
.Setup(o => o.RequestServices)
|
.Setup(o => o.RequestServices)
|
||||||
.Returns(serviceProvider.Object);
|
.Returns(serviceProvider.Object);
|
||||||
|
|
||||||
var viewContext = new ViewContext(actionContext, Mock.Of<IView>(), viewData, new StringWriter());
|
var htmlGenerator = new DefaultHtmlGenerator(
|
||||||
|
actionBindingContextProvider.Object,
|
||||||
|
GetAntiForgeryInstance(),
|
||||||
|
provider,
|
||||||
|
urlHelper);
|
||||||
|
|
||||||
// TemplateRenderer will Contextualize this transient service.
|
// TemplateRenderer will Contextualize this transient service.
|
||||||
var innerHelper = (IHtmlHelper)new HtmlHelper(
|
var innerHelper = (IHtmlHelper)new HtmlHelper(htmlGenerator, viewEngine, provider);
|
||||||
viewEngine,
|
|
||||||
provider,
|
|
||||||
urlHelper,
|
|
||||||
GetAntiForgeryInstance(),
|
|
||||||
actionBindingContextProvider.Object);
|
|
||||||
if (innerHelperWrapper != null)
|
if (innerHelperWrapper != null)
|
||||||
{
|
{
|
||||||
innerHelper = innerHelperWrapper(innerHelper);
|
innerHelper = innerHelperWrapper(innerHelper);
|
||||||
|
|
@ -162,12 +161,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
.Setup(s => s.GetService(typeof(IHtmlHelper)))
|
.Setup(s => s.GetService(typeof(IHtmlHelper)))
|
||||||
.Returns(() => innerHelper);
|
.Returns(() => innerHelper);
|
||||||
|
|
||||||
var htmlHelper = new HtmlHelper<TModel>(
|
var htmlHelper = new HtmlHelper<TModel>(htmlGenerator, viewEngine, provider);
|
||||||
viewEngine,
|
var viewContext = new ViewContext(actionContext, Mock.Of<IView>(), viewData, new StringWriter());
|
||||||
provider,
|
|
||||||
urlHelper,
|
|
||||||
GetAntiForgeryInstance(),
|
|
||||||
actionBindingContextProvider.Object);
|
|
||||||
htmlHelper.Contextualize(viewContext);
|
htmlHelper.Contextualize(viewContext);
|
||||||
|
|
||||||
return htmlHelper;
|
return htmlHelper;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue