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

Add ITagHelperComponentManager #6302

Merged
merged 5 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.AspNetCore.Mvc.Razor.Compilation;
using Microsoft.AspNetCore.Mvc.Razor.Extensions;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Razor.TagHelpers;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.TagHelpers;
Expand Down Expand Up @@ -187,6 +188,9 @@ internal static void AddRazorViewEngineServices(IServiceCollection services)
services.TryAddSingleton<ITagHelperActivator, DefaultTagHelperActivator>();
services.TryAddSingleton<ITagHelperFactory, DefaultTagHelperFactory>();

// TagHelperComponents manager
services.TryAddScoped<ITagHelperComponentManager, TagHelperComponentManager>();

// Consumed by the Cache tag helper to cache results across the lifetime of the application.
services.TryAddSingleton<IMemoryCache, MemoryCache>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Razor.TagHelpers;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Microsoft.AspNetCore.Mvc.Razor.Internal
{
/// <summary>
/// The default implementation of the <see cref="ITagHelperComponentManager"/>.
/// </summary>
public class TagHelperComponentManager : ITagHelperComponentManager
{
/// <summary>
/// Creates a new <see cref="TagHelperComponentManager"/>.
/// </summary>
/// <param name="tagHelperComponents">The collection of <see cref="ITagHelperComponent"/>s.</param>
public TagHelperComponentManager(IEnumerable<ITagHelperComponent> tagHelperComponents)
{
if (tagHelperComponents == null)
{
throw new ArgumentNullException(nameof(tagHelperComponents));
}

Components = new List<ITagHelperComponent>(tagHelperComponents);
}

/// <inheritdoc />
public ICollection<ITagHelperComponent> Components { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.Collections.Generic;
using System.ComponentModel;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Logging;
Expand All @@ -18,10 +17,11 @@ public class BodyTagHelper : TagHelperComponentTagHelper
/// <summary>
/// Creates a new <see cref="BodyTagHelper"/>.
/// </summary>
/// <param name="components">The list of <see cref="ITagHelperComponent"/>.</param>
/// <param name="manager">The <see cref="ITagHelperComponentManager"/> which contains the collection
/// of <see cref="ITagHelperComponent"/>s.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public BodyTagHelper(IEnumerable<ITagHelperComponent> components, ILoggerFactory loggerFactory)
: base(components, loggerFactory)
public BodyTagHelper(ITagHelperComponentManager manager, ILoggerFactory loggerFactory)
: base(manager, loggerFactory)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.Collections.Generic;
using System.ComponentModel;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Logging;
Expand All @@ -18,10 +17,11 @@ public class HeadTagHelper : TagHelperComponentTagHelper
/// <summary>
/// Creates a new <see cref="HeadTagHelper"/>.
/// </summary>
/// <param name="components">The list of <see cref="ITagHelperComponent"/>.</param>
/// <param name="manager">The <see cref="ITagHelperComponentManager"/> which contains the collection
/// of <see cref="ITagHelperComponent"/>s.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public HeadTagHelper(IEnumerable<ITagHelperComponent> components, ILoggerFactory loggerFactory)
: base(components, loggerFactory)
public HeadTagHelper(ITagHelperComponentManager manager, ILoggerFactory loggerFactory)
: base(manager, loggerFactory)
{
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.Collections.Generic;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace Microsoft.AspNetCore.Mvc.Razor.TagHelpers
{
/// <summary>
/// An implementation of this interface provides the collection of <see cref="ITagHelperComponent"/>s
/// that will be used by <see cref="TagHelperComponentTagHelper"/>s.
/// </summary>
public interface ITagHelperComponentManager
{
/// <summary>
/// Gets the collection of <see cref="ITagHelperComponent"/>s that will be used by
/// <see cref="TagHelperComponentTagHelper"/>s.
/// </summary>
ICollection<ITagHelperComponent> Components { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,46 @@

namespace Microsoft.AspNetCore.Mvc.Razor.TagHelpers
{
/// <summary>
/// Initializes and processes the <see cref="ITagHelperComponent"/>s added to the
/// <see cref="ITagHelperComponentManager.Components"/> in the specified order.
/// </summary>
public abstract class TagHelperComponentTagHelper : TagHelper
{
private readonly ILogger _logger;

private IEnumerable<ITagHelperComponent> _components;

/// <summary>
/// Creates a new <see cref="TagHelperComponentTagHelper"/>.
/// Creates a new <see cref="TagHelperComponentTagHelper"/> and orders the
/// the collection of <see cref="ITagHelperComponent"/>s in <see cref="ITagHelperComponentManager.Components"/>.
/// </summary>
/// <param name="components">The list of <see cref="ITagHelperComponent"/>.</param>
/// <param name="manager">The <see cref="ITagHelperComponentManager"/> which contains the collection
/// of <see cref="ITagHelperComponent"/>s.</param>
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/>.</param>
public TagHelperComponentTagHelper(IEnumerable<ITagHelperComponent> components,
/// <remarks>The <see cref="ITagHelperComponentManager.Components"/> are ordered after the
/// creation of the <see cref="ITagHelperComponentManager"/> to position the <see cref="ITagHelperComponent"/>s
/// added from controllers and views correctly.</remarks>
public TagHelperComponentTagHelper(ITagHelperComponentManager manager,
ILoggerFactory loggerFactory)
{
if (components == null)
if (manager == null)
{
throw new ArgumentNullException(nameof(components));
throw new ArgumentNullException(nameof(manager));
}

if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}

_components = components;
_components = manager.Components.OrderBy(p => p.Order).ToArray();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment mentioning this ordering must be done after the manager is created (otherwise controller-added components won't be positioned correctly).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx

_logger = loggerFactory.CreateLogger(GetType());
}

/// <inheritdoc />
public override void Init(TagHelperContext context)
{
_components = _components.OrderBy(p => p.Order);
foreach (var component in _components)
{
component.Init(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ public async Task InjectsTestBodyTagHelperComponent()
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

#if GENERATE_BASELINES
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
#else
Assert.Equal(expectedContent, responseContent, ignoreLineEndingDifferences: true);
#endif
}

[Fact]
public async Task AddTestTagHelperComponent_FromController()
{
// Arrange
var url = "http://localhost/AddTagHelperComponent/AddComponent";
var request = new HttpRequestMessage(HttpMethod.Get, url);
var outputFile = "compiler/resources/RazorWebSite.AddTagHelperComponent.AddComponent.html";
var expectedContent =
await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false);

// Act
var response = await Client.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);

#if GENERATE_BASELINES
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
#else
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<head inject>
Hello from Head Tag Helper Component
<script>'This was injected!!'</script></head>
<body inject>
Hello from Body Tag Helper Component
Processed TagHelperComponent added from controller.<script>'This was injected!!'</script>Processed TagHelperComponent added from view.</body>
Loading