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

Commit

Permalink
Add ITagHelperComponent
Browse files Browse the repository at this point in the history
Addresses aspnet/Mvc#5728
  • Loading branch information
jbagga authored Mar 24, 2017
1 parent b4640f8 commit fe6517d
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
32 changes: 2 additions & 30 deletions src/Microsoft.AspNetCore.Razor.Runtime/TagHelpers/ITagHelper.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
19 changes: 16 additions & 3 deletions src/Microsoft.AspNetCore.Razor.Runtime/TagHelpers/TagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,28 @@
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)
{
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit fe6517d

Please sign in to comment.