Add TagName to TagHelperContext

Addresses #1065
This commit is contained in:
Jass Bagga 2017-03-09 14:59:11 -08:00 committed by GitHub
parent b56751bf2e
commit a418a175a9
4 changed files with 76 additions and 5 deletions

View File

@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Razor.Runtime.TagHelpers
_tagHelpers = new List<ITagHelper>();
_allAttributes = new TagHelperAttributeList();
Context = new TagHelperContext(_allAttributes, items, uniqueId);
Context = new TagHelperContext(tagName, _allAttributes, items, uniqueId);
Output = new TagHelperOutput(tagName, new TagHelperAttributeList(), GetChildContentAsync)
{
TagMode = tagMode
@ -242,7 +242,7 @@ namespace Microsoft.AspNetCore.Razor.Runtime.TagHelpers
_perEncoderChildContent?.Clear();
_childContent = null;
Context.Reinitialize(Items, uniqueId);
Context.Reinitialize(tagName, Items, uniqueId);
Output.Reinitialize(tagName, tagMode);
}

View File

@ -13,6 +13,28 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
{
private readonly TagHelperAttributeList _allAttributes;
/// <summary>
/// Instantiates a new <see cref="TagHelperContext"/>.
/// </summary>
/// <param name="tagName">The parsed HTML tag name of the element.</param>
/// <param name="allAttributes">Every attribute associated with the current HTML element.</param>
/// <param name="items">Collection of items used to communicate with other <see cref="ITagHelper"/>s.</param>
/// <param name="uniqueId">The unique identifier for the source element this <see cref="TagHelperContext" />
/// applies to.</param>
public TagHelperContext(
string tagName,
TagHelperAttributeList allAttributes,
IDictionary<object, object> items,
string uniqueId) : this(allAttributes, items, uniqueId)
{
if (tagName == null)
{
throw new ArgumentNullException(nameof(tagName));
}
TagName = tagName;
}
/// <summary>
/// Instantiates a new <see cref="TagHelperContext"/>.
/// </summary>
@ -45,6 +67,11 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
UniqueId = uniqueId;
}
/// <summary>
/// The parsed HTML tag name of the element.
/// </summary>
public string TagName { get; private set; }
/// <summary>
/// Every attribute associated with the current HTML element.
/// </summary>
@ -64,6 +91,18 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
/// </summary>
public string UniqueId { get; private set; }
/// <summary>
/// Clears the <see cref="TagHelperContext"/> and updates its state with the provided values.
/// </summary>
/// <param name="tagName">The HTML tag name to use.</param>
/// <param name="items">The <see cref="IDictionary{Object, Object}"/> to use.</param>
/// <param name="uniqueId">The unique id to use.</param>
public void Reinitialize(string tagName, IDictionary<object, object> items, string uniqueId)
{
TagName = tagName;
Reinitialize(items, uniqueId);
}
/// <summary>
/// Clears the <see cref="TagHelperContext"/> and updates its state with the provided values.
/// </summary>

View File

@ -138,6 +138,7 @@ namespace Microsoft.AspNetCore.Razor.Runtime.TagHelpers
// Assert
var context = executionContext.Context;
var attribute = Assert.Single(context.AllAttributes);
Assert.Equal(tagName, context.TagName);
Assert.Equal(attribute.Name, "Another attribute");
Assert.Equal(updatedUniqueId, context.UniqueId);
Assert.Same(updatedItems, context.Items);

View File

@ -13,6 +13,7 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
public void Reinitialize_AllowsContextToBeReused()
{
// Arrange
var tagName = "test";
var initialUniqueId = "123";
var expectedUniqueId = "456";
var initialItems = new Dictionary<object, object>
@ -27,19 +28,49 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
{
{ "name", "value" }
};
var context = new TagHelperContext(initialAttributes, initialItems, initialUniqueId);
var context = new TagHelperContext(
tagName,
initialAttributes,
initialItems,
initialUniqueId);
// Act
context.Reinitialize(expectedItems, expectedUniqueId);
context.Reinitialize(tagName, expectedItems, expectedUniqueId);
// Assert
Assert.Equal(tagName, context.TagName);
Assert.Same(expectedItems, context.Items);
Assert.Equal(expectedUniqueId, context.UniqueId);
Assert.Empty(context.AllAttributes);
}
[Fact]
public void Constructor_SetsProperties_AsExpected()
public void Constructor_SetsProperties_AsExpected_WithTagName()
{
// Arrange
var expectedItems = new Dictionary<object, object>
{
{ "test-entry", 1234 }
};
// Act
var context = new TagHelperContext(
tagName: "test",
allAttributes: new TagHelperAttributeList(),
items: expectedItems,
uniqueId: string.Empty);
// Assert
Assert.Equal("test", context.TagName);
Assert.NotNull(context.Items);
Assert.Same(expectedItems, context.Items);
var item = Assert.Single(context.Items);
Assert.Equal("test-entry", (string)item.Key, StringComparer.Ordinal);
Assert.Equal(1234, item.Value);
}
[Fact]
public void Constructor_SetsProperties_AsExpected_WithoutTagName()
{
// Arrange
var expectedItems = new Dictionary<object, object>