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))
|
||||
{
|
||||
var encodedText = html.Encode(item.Text);
|
||||
var option = HtmlHelper.GenerateOption(item, encodedText);
|
||||
var option = DefaultHtmlGenerator.GenerateOption(item, encodedText);
|
||||
builder.Append(option);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,8 +26,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
private readonly IModelMetadataProvider _metadataProvider;
|
||||
private readonly IUrlHelper _urlHelper;
|
||||
|
||||
public string IdAttributeDotReplacement { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DefaultHtmlGenerator"/> class.
|
||||
/// </summary>
|
||||
|
|
@ -43,6 +41,9 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
_urlHelper = urlHelper;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public string IdAttributeDotReplacement { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string Encode(string value)
|
||||
{
|
||||
|
|
@ -252,32 +253,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
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 />
|
||||
public virtual TagBuilder GeneratePassword(
|
||||
[NotNull] ViewContext viewContext,
|
||||
|
|
@ -725,6 +700,56 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
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(
|
||||
[NotNull] ViewContext viewContext,
|
||||
InputType inputType,
|
||||
|
|
@ -843,6 +868,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
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(
|
||||
ViewContext viewContext,
|
||||
ModelMetadata metadata,
|
||||
|
|
@ -876,13 +903,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
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.
|
||||
private static IDictionary<string, object> GetHtmlAttributeDictionaryOrNull(object htmlAttributes)
|
||||
{
|
||||
|
|
@ -899,12 +919,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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.
|
||||
/// </summary>
|
||||
public HtmlHelper(
|
||||
[NotNull] IHtmlGenerator htmlGenerator,
|
||||
[NotNull] ICompositeViewEngine viewEngine,
|
||||
[NotNull] IModelMetadataProvider metadataProvider,
|
||||
[NotNull] IUrlHelper urlHelper,
|
||||
[NotNull] AntiForgery antiForgeryInstance,
|
||||
[NotNull] IActionBindingContextProvider actionBindingContextProvider)
|
||||
: base(viewEngine, metadataProvider, urlHelper, antiForgeryInstance, actionBindingContextProvider)
|
||||
[NotNull] IModelMetadataProvider metadataProvider)
|
||||
: base(htmlGenerator, viewEngine, metadataProvider)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,19 +1,8 @@
|
|||
// 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;
|
||||
using System.Collections;
|
||||
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.Rendering.Expressions;
|
||||
|
||||
namespace Microsoft.AspNet.Mvc.Rendering
|
||||
{
|
||||
|
|
@ -22,6 +11,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
/// </summary>
|
||||
public interface IHtmlGenerator
|
||||
{
|
||||
string IdAttributeDotReplacement { get; set; }
|
||||
|
||||
string Encode(string value);
|
||||
|
||||
string Encode(object value);
|
||||
|
|
@ -101,11 +92,6 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
string labelText,
|
||||
object htmlAttributes);
|
||||
|
||||
/// <remarks>
|
||||
/// Not used directly in HtmlHelper. Exposed publicly for use in DefaultDisplayTemplates.
|
||||
/// </remarks>
|
||||
TagBuilder GenerateOption(SelectListItem item, string encodedText);
|
||||
|
||||
TagBuilder GeneratePassword(
|
||||
[NotNull] ViewContext viewContext,
|
||||
ModelMetadata metadata,
|
||||
|
|
|
|||
|
|
@ -145,15 +145,14 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
.Setup(o => o.RequestServices)
|
||||
.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.
|
||||
var innerHelper = (IHtmlHelper)new HtmlHelper(
|
||||
viewEngine,
|
||||
provider,
|
||||
urlHelper,
|
||||
GetAntiForgeryInstance(),
|
||||
actionBindingContextProvider.Object);
|
||||
var innerHelper = (IHtmlHelper)new HtmlHelper(htmlGenerator, viewEngine, provider);
|
||||
if (innerHelperWrapper != null)
|
||||
{
|
||||
innerHelper = innerHelperWrapper(innerHelper);
|
||||
|
|
@ -162,12 +161,8 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
|||
.Setup(s => s.GetService(typeof(IHtmlHelper)))
|
||||
.Returns(() => innerHelper);
|
||||
|
||||
var htmlHelper = new HtmlHelper<TModel>(
|
||||
viewEngine,
|
||||
provider,
|
||||
urlHelper,
|
||||
GetAntiForgeryInstance(),
|
||||
actionBindingContextProvider.Object);
|
||||
var htmlHelper = new HtmlHelper<TModel>(htmlGenerator, viewEngine, provider);
|
||||
var viewContext = new ViewContext(actionContext, Mock.Of<IView>(), viewData, new StringWriter());
|
||||
htmlHelper.Contextualize(viewContext);
|
||||
|
||||
return htmlHelper;
|
||||
|
|
|
|||
Loading…
Reference in New Issue