Add ITagHelperComponent

Addresses https://github.com/aspnet/Mvc/issues/5728
This commit is contained in:
Jass Bagga 2017-03-24 13:53:17 -07:00 committed by GitHub
parent b4640f8bb8
commit fe6517dcdd
5 changed files with 100 additions and 34 deletions

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Razor.Runtime.TagHelpers
public class TagHelperRunner
{
/// <summary>
/// Calls the <see cref="ITagHelper.ProcessAsync"/> method on <see cref="ITagHelper"/>s.
/// Calls the <see cref="ITagHelperComponent.ProcessAsync"/> method on <see cref="ITagHelper"/>s.
/// </summary>
/// <param name="executionContext">Contains information associated with running <see cref="ITagHelper"/>s.
/// </param>

View File

@ -1,41 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Razor.TagHelpers
{
/// <summary>
/// Contract used to filter matching HTML elements.
/// Marker interface for <see cref="TagHelper"/>s.
/// </summary>
public interface ITagHelper
public interface ITagHelper : ITagHelperComponent
{
/// <summary>
/// When a set of<see cref= "ITagHelper" /> s are executed, their<see cref="Init(TagHelperContext)"/>'s
/// are first invoked in the specified <see cref="Order"/>; then their
/// <see cref="ProcessAsync(TagHelperContext, TagHelperOutput)"/>'s are invoked in the specified
/// <see cref="Order"/>. Lower values are executed first.
/// </summary>
int Order { get; }
/// <summary>
/// Initializes the <see cref="ITagHelper"/> with the given <paramref name="context"/>. Additions to
/// <see cref="TagHelperContext.Items"/> should be done within this method to ensure they're added prior to
/// executing the children.
/// </summary>
/// <param name="context">Contains information associated with the current HTML tag.</param>
/// <remarks>When more than one <see cref="ITagHelper"/> runs on the same element,
/// <see cref="M:TagHelperOutput.GetChildContentAsync"/> may be invoked prior to <see cref="ProcessAsync"/>.
/// </remarks>
void Init(TagHelperContext context);
/// <summary>
/// Asynchronously executes the <see cref="ITagHelper"/> with the given <paramref name="context"/> and
/// <paramref name="output"/>.
/// </summary>
/// <param name="context">Contains information associated with the current HTML tag.</param>
/// <param name="output">A stateful HTML element used to generate an HTML tag.</param>
/// <returns>A <see cref="Task"/> that on completion updates the <paramref name="output"/>.</returns>
Task ProcessAsync(TagHelperContext context, TagHelperOutput output);
}
}

View File

@ -0,0 +1,41 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Razor.TagHelpers
{
/// <summary>
/// Contract used to modify an HTML element.
/// </summary>
public interface ITagHelperComponent
{
/// <summary>
/// When a set of <see cref="ITagHelperComponent"/>s are executed, their <see cref="Init(TagHelperContext)"/>'s
/// are first invoked in the specified <see cref="Order"/>; then their
/// <see cref="ProcessAsync(TagHelperContext, TagHelperOutput)"/>'s are invoked in the specified
/// <see cref="Order"/>. Lower values are executed first.
/// </summary>
int Order { get; }
/// <summary>
/// Initializes the <see cref="ITagHelperComponent"/> with the given <paramref name="context"/>. Additions to
/// <see cref="TagHelperContext.Items"/> should be done within this method to ensure they're added prior to
/// executing the children.
/// </summary>
/// <param name="context">Contains information associated with the current HTML tag.</param>
/// <remarks>When more than one <see cref="ITagHelperComponent"/> runs on the same element,
/// <see cref="M:TagHelperOutput.GetChildContentAsync"/> may be invoked prior to <see cref="ProcessAsync"/>.
/// </remarks>
void Init(TagHelperContext context);
/// <summary>
/// Asynchronously executes the <see cref="ITagHelperComponent"/> with the given <paramref name="context"/> and
/// <paramref name="output"/>.
/// </summary>
/// <param name="context">Contains information associated with the current HTML tag.</param>
/// <param name="output">A stateful HTML element used to generate an HTML tag.</param>
/// <returns>A <see cref="Task"/> that on completion updates the <paramref name="output"/>.</returns>
Task ProcessAsync(TagHelperContext context, TagHelperOutput output);
}
}

View File

@ -7,15 +7,28 @@ using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Razor.TagHelpers
{
/// <summary>
/// Class used to filter matching HTML elements.
/// An abstract base class for <see cref="ITagHelper"/>.
/// </summary>
public abstract class TagHelper : ITagHelper
{
/// <inheritdoc />
/// <summary>
/// When a set of <see cref="ITagHelper"/>s are executed, their <see cref="Init(TagHelperContext)"/>'s
/// are first invoked in the specified <see cref="Order"/>; then their
/// <see cref="ProcessAsync(TagHelperContext, TagHelperOutput)"/>'s are invoked in the specified
/// <see cref="Order"/>. Lower values are executed first.
/// </summary>
/// <remarks>Default order is <c>0</c>.</remarks>
public virtual int Order { get; } = 0;
/// <inheritdoc />
/// <summary>
/// Initializes the <see cref="ITagHelper"/> with the given <paramref name="context"/>. Additions to
/// <see cref="TagHelperContext.Items"/> should be done within this method to ensure they're added prior to
/// executing the children.
/// </summary>
/// <param name="context">Contains information associated with the current HTML tag.</param>
/// <remarks>When more than one <see cref="ITagHelper"/> runs on the same element,
/// <see cref="M:TagHelperOutput.GetChildContentAsync"/> may be invoked prior to <see cref="ProcessAsync"/>.
/// </remarks>
public virtual void Init(TagHelperContext context)
{
}

View File

@ -0,0 +1,40 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks;
using Microsoft.Extensions.Internal;
namespace Microsoft.AspNetCore.Razor.TagHelpers
{
/// <summary>
/// An abstract base class for <see cref="ITagHelperComponent"/>.
/// </summary>
public abstract class TagHelperComponent : ITagHelperComponent
{
/// <inheritdoc />
/// <remarks>Default order is <c>0</c>.</remarks>
public virtual int Order => 0;
/// <inheritdoc />
public virtual void Init(TagHelperContext context)
{
}
/// <summary>
/// Synchronously executes the <see cref="ITagHelperComponent"/> with the given <paramref name="context"/> and
/// <paramref name="output"/>.
/// </summary>
/// <param name="context">Contains information associated with the current HTML tag.</param>
/// <param name="output">A stateful HTML element used to generate an HTML tag.</param>
public virtual void Process(TagHelperContext context, TagHelperOutput output)
{
}
/// <inheritdoc />
public virtual Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
Process(context, output);
return TaskCache.CompletedTask;
}
}
}