Add include and exclude attributes to EnvironmentTagHelper
Addresses #5671
This commit is contained in:
parent
d44c9aee1e
commit
7449ffad74
|
|
@ -11,6 +11,8 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
|
|||
/// <summary>
|
||||
/// <see cref="ITagHelper"/> implementation targeting <environment> elements that conditionally renders
|
||||
/// content based on the current value of <see cref="IHostingEnvironment.EnvironmentName"/>.
|
||||
/// If the environment is not listed in the specified <see cref="Names"/> or <see cref="Include"/>,
|
||||
/// or if it is in <see cref="Exclude"/>, the content will not be rendered.
|
||||
/// </summary>
|
||||
public class EnvironmentTagHelper : TagHelper
|
||||
{
|
||||
|
|
@ -30,6 +32,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
|
|||
|
||||
/// <summary>
|
||||
/// A comma separated list of environment names in which the content should be rendered.
|
||||
/// If the current environment is also in the <see cref="Exclude"/> list, the content will not be rendered.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The specified environment names are compared case insensitively to the current value of
|
||||
|
|
@ -37,6 +40,25 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
|
|||
/// </remarks>
|
||||
public string Names { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A comma separated list of environment names in which the content should be rendered.
|
||||
/// If the current environment is also in the <see cref="Exclude"/> list, the content will not be rendered.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The specified environment names are compared case insensitively to the current value of
|
||||
/// <see cref="IHostingEnvironment.EnvironmentName"/>.
|
||||
/// </remarks>
|
||||
public string Include { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// A comma separated list of environment names in which the content will not be rendered.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The specified environment names are compared case insensitively to the current value of
|
||||
/// <see cref="IHostingEnvironment.EnvironmentName"/>.
|
||||
/// </remarks>
|
||||
public string Exclude { get; set; }
|
||||
|
||||
protected IHostingEnvironment HostingEnvironment { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
|
|
@ -55,7 +77,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
|
|||
// Always strip the outer tag name as we never want <environment> to render
|
||||
output.TagName = null;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(Names))
|
||||
if (string.IsNullOrWhiteSpace(Names) && string.IsNullOrWhiteSpace(Include) && string.IsNullOrWhiteSpace(Exclude))
|
||||
{
|
||||
// No names specified, do nothing
|
||||
return;
|
||||
|
|
@ -68,27 +90,67 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers
|
|||
return;
|
||||
}
|
||||
|
||||
var tokenizer = new StringTokenizer(Names, NameSeparator);
|
||||
var hasEnvironments = false;
|
||||
foreach (var item in tokenizer)
|
||||
if (Exclude != null)
|
||||
{
|
||||
var environment = item.Trim();
|
||||
if (environment.HasValue && environment.Length > 0)
|
||||
var tokenizer = new StringTokenizer(Exclude, NameSeparator);
|
||||
foreach (var item in tokenizer)
|
||||
{
|
||||
hasEnvironments = true;
|
||||
if (environment.Equals(currentEnvironmentName, StringComparison.OrdinalIgnoreCase))
|
||||
var environment = item.Trim();
|
||||
if (environment.HasValue && environment.Length > 0)
|
||||
{
|
||||
// Matching environment name found, do nothing
|
||||
return;
|
||||
if (environment.Equals(currentEnvironmentName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Matching environment name found, suppress output
|
||||
output.SuppressOutput();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var hasEnvironments = false;
|
||||
if (Names != null)
|
||||
{
|
||||
var tokenizer = new StringTokenizer(Names, NameSeparator);
|
||||
foreach (var item in tokenizer)
|
||||
{
|
||||
var environment = item.Trim();
|
||||
if (environment.HasValue && environment.Length > 0)
|
||||
{
|
||||
hasEnvironments = true;
|
||||
if (environment.Equals(currentEnvironmentName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Matching environment name found, do nothing
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Include != null)
|
||||
{
|
||||
var tokenizer = new StringTokenizer(Include, NameSeparator);
|
||||
foreach (var item in tokenizer)
|
||||
{
|
||||
var environment = item.Trim();
|
||||
if (environment.HasValue && environment.Length > 0)
|
||||
{
|
||||
hasEnvironments = true;
|
||||
if (environment.Equals(currentEnvironmentName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Matching environment name found, do nothing
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (hasEnvironments)
|
||||
{
|
||||
// This instance had at least one non-empty environment specified but none of these
|
||||
// This instance had at least one non-empty environment (names or include) specified but none of these
|
||||
// environments matched the current environment. Suppress the output in this case.
|
||||
output.SuppressOutput();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,6 +66,94 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers.Test
|
|||
ShouldShowContent(namesAttribute, environmentName);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("", "", "")]
|
||||
[InlineData("", null, "Test")]
|
||||
[InlineData("Development", "", "")]
|
||||
[InlineData("", "Development, Test", "")]
|
||||
[InlineData(null, "development, TEST", "Test")]
|
||||
[InlineData("Development", "", "Test")]
|
||||
[InlineData("Development", "Test, Development", "")]
|
||||
[InlineData("Test", "DEVELOPMENT", null)]
|
||||
[InlineData("Development", "Test", "")]
|
||||
[InlineData("Development", null, "Test")]
|
||||
[InlineData("Development", "Test", "Test")]
|
||||
[InlineData("Test", "Development", "Test")]
|
||||
public void ShouldShowContent_IncludeExcludeSpecified(string namesAttribute, string includeAttribute, string excludeAttribute)
|
||||
{
|
||||
// Arrange
|
||||
var content = "content";
|
||||
var context = MakeTagHelperContext(
|
||||
attributes: new TagHelperAttributeList {
|
||||
{ "names", namesAttribute },
|
||||
{ "include", includeAttribute },
|
||||
{ "exclude", excludeAttribute },
|
||||
});
|
||||
var output = MakeTagHelperOutput("environment", childContent: content);
|
||||
var hostingEnvironment = new Mock<IHostingEnvironment>();
|
||||
hostingEnvironment.SetupProperty(h => h.EnvironmentName, "Development");
|
||||
|
||||
// Act
|
||||
var helper = new EnvironmentTagHelper(hostingEnvironment.Object)
|
||||
{
|
||||
Names = namesAttribute,
|
||||
Include = includeAttribute,
|
||||
Exclude = excludeAttribute,
|
||||
};
|
||||
helper.Process(context, output);
|
||||
|
||||
// Assert
|
||||
Assert.Null(output.TagName);
|
||||
Assert.False(output.IsContentModified);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(null, "", "Development")]
|
||||
[InlineData("", "Development", "development")]
|
||||
[InlineData("", "Test", "Development, test")]
|
||||
[InlineData("Development", "", "Development")]
|
||||
[InlineData("Test", "", "Development")]
|
||||
[InlineData("Development", "Development", "DEVELOPMENT, TEST")]
|
||||
[InlineData("Development", "Test", "Development")]
|
||||
[InlineData("Test", "Development", "Development")]
|
||||
[InlineData("Test", "Test", "Development")]
|
||||
[InlineData("", "Test", "Test")]
|
||||
[InlineData("Test", null, "Test")]
|
||||
[InlineData("Test", "Test", "Test")]
|
||||
[InlineData("", "Test", null)]
|
||||
[InlineData("Test", "", "")]
|
||||
[InlineData("Test", "Test", null)]
|
||||
public void DoesNotShowContent_IncludeExcludeSpecified(string namesAttribute, string includeAttribute, string excludeAttribute)
|
||||
{
|
||||
// Arrange
|
||||
var content = "content";
|
||||
var context = MakeTagHelperContext(
|
||||
attributes: new TagHelperAttributeList {
|
||||
{ "names", namesAttribute },
|
||||
{ "include", includeAttribute },
|
||||
{ "exclude", excludeAttribute },
|
||||
});
|
||||
var output = MakeTagHelperOutput("environment", childContent: content);
|
||||
var hostingEnvironment = new Mock<IHostingEnvironment>();
|
||||
hostingEnvironment.SetupProperty(h => h.EnvironmentName, "Development");
|
||||
|
||||
// Act
|
||||
var helper = new EnvironmentTagHelper(hostingEnvironment.Object)
|
||||
{
|
||||
Names = namesAttribute,
|
||||
Include = includeAttribute,
|
||||
Exclude = excludeAttribute,
|
||||
};
|
||||
helper.Process(context, output);
|
||||
|
||||
// Assert
|
||||
Assert.Null(output.TagName);
|
||||
Assert.Empty(output.PreContent.GetContent());
|
||||
Assert.True(output.Content.GetContent().Length == 0);
|
||||
Assert.Empty(output.PostContent.GetContent());
|
||||
Assert.True(output.IsContentModified);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("NotDevelopment", "Development")]
|
||||
[InlineData("NOTDEVELOPMENT", "Development")]
|
||||
|
|
@ -82,8 +170,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers.Test
|
|||
var context = MakeTagHelperContext(attributes: new TagHelperAttributeList { { "names", namesAttribute } });
|
||||
var output = MakeTagHelperOutput("environment", childContent: content);
|
||||
var hostingEnvironment = new Mock<IHostingEnvironment>();
|
||||
hostingEnvironment.SetupProperty(h => h.EnvironmentName);
|
||||
hostingEnvironment.Object.EnvironmentName = environmentName;
|
||||
hostingEnvironment.SetupProperty(h => h.EnvironmentName, environmentName);
|
||||
|
||||
// Act
|
||||
var helper = new EnvironmentTagHelper(hostingEnvironment.Object)
|
||||
|
|
@ -108,8 +195,7 @@ namespace Microsoft.AspNetCore.Mvc.TagHelpers.Test
|
|||
attributes: new TagHelperAttributeList { { "names", namesAttribute } });
|
||||
var output = MakeTagHelperOutput("environment", childContent: content);
|
||||
var hostingEnvironment = new Mock<IHostingEnvironment>();
|
||||
hostingEnvironment.SetupProperty(h => h.EnvironmentName);
|
||||
hostingEnvironment.Object.EnvironmentName = environmentName;
|
||||
hostingEnvironment.SetupProperty(h => h.EnvironmentName, environmentName);
|
||||
|
||||
// Act
|
||||
var helper = new EnvironmentTagHelper(hostingEnvironment.Object)
|
||||
|
|
|
|||
Loading…
Reference in New Issue