From a418a175a933ef8ada69ee76705d82336c537f4f Mon Sep 17 00:00:00 2001 From: Jass Bagga Date: Thu, 9 Mar 2017 14:59:11 -0800 Subject: [PATCH] Add TagName to TagHelperContext Addresses #1065 --- .../TagHelpers/TagHelperExecutionContext.cs | 4 +- .../TagHelpers/TagHelperContext.cs | 39 +++++++++++++++++++ .../TagHelperExecutionContextTest.cs | 1 + .../TagHelpers/TagHelperContextTest.cs | 37 ++++++++++++++++-- 4 files changed, 76 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.AspNetCore.Razor.Runtime/Runtime/TagHelpers/TagHelperExecutionContext.cs b/src/Microsoft.AspNetCore.Razor.Runtime/Runtime/TagHelpers/TagHelperExecutionContext.cs index 469c59efb1..39b54c3396 100644 --- a/src/Microsoft.AspNetCore.Razor.Runtime/Runtime/TagHelpers/TagHelperExecutionContext.cs +++ b/src/Microsoft.AspNetCore.Razor.Runtime/Runtime/TagHelpers/TagHelperExecutionContext.cs @@ -74,7 +74,7 @@ namespace Microsoft.AspNetCore.Razor.Runtime.TagHelpers _tagHelpers = new List(); _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); } diff --git a/src/Microsoft.AspNetCore.Razor.Runtime/TagHelpers/TagHelperContext.cs b/src/Microsoft.AspNetCore.Razor.Runtime/TagHelpers/TagHelperContext.cs index d131d60369..e09a552a0b 100644 --- a/src/Microsoft.AspNetCore.Razor.Runtime/TagHelpers/TagHelperContext.cs +++ b/src/Microsoft.AspNetCore.Razor.Runtime/TagHelpers/TagHelperContext.cs @@ -13,6 +13,28 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers { private readonly TagHelperAttributeList _allAttributes; + /// + /// Instantiates a new . + /// + /// The parsed HTML tag name of the element. + /// Every attribute associated with the current HTML element. + /// Collection of items used to communicate with other s. + /// The unique identifier for the source element this + /// applies to. + public TagHelperContext( + string tagName, + TagHelperAttributeList allAttributes, + IDictionary items, + string uniqueId) : this(allAttributes, items, uniqueId) + { + if (tagName == null) + { + throw new ArgumentNullException(nameof(tagName)); + } + + TagName = tagName; + } + /// /// Instantiates a new . /// @@ -45,6 +67,11 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers UniqueId = uniqueId; } + /// + /// The parsed HTML tag name of the element. + /// + public string TagName { get; private set; } + /// /// Every attribute associated with the current HTML element. /// @@ -64,6 +91,18 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers /// public string UniqueId { get; private set; } + /// + /// Clears the and updates its state with the provided values. + /// + /// The HTML tag name to use. + /// The to use. + /// The unique id to use. + public void Reinitialize(string tagName, IDictionary items, string uniqueId) + { + TagName = tagName; + Reinitialize(items, uniqueId); + } + /// /// Clears the and updates its state with the provided values. /// diff --git a/test/Microsoft.AspNetCore.Razor.Runtime.Test/Runtime/TagHelpers/TagHelperExecutionContextTest.cs b/test/Microsoft.AspNetCore.Razor.Runtime.Test/Runtime/TagHelpers/TagHelperExecutionContextTest.cs index 75937709e5..e96be1ddb6 100644 --- a/test/Microsoft.AspNetCore.Razor.Runtime.Test/Runtime/TagHelpers/TagHelperExecutionContextTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Runtime.Test/Runtime/TagHelpers/TagHelperExecutionContextTest.cs @@ -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); diff --git a/test/Microsoft.AspNetCore.Razor.Runtime.Test/TagHelpers/TagHelperContextTest.cs b/test/Microsoft.AspNetCore.Razor.Runtime.Test/TagHelpers/TagHelperContextTest.cs index c3cf7a64a0..eb85e7ecb7 100644 --- a/test/Microsoft.AspNetCore.Razor.Runtime.Test/TagHelpers/TagHelperContextTest.cs +++ b/test/Microsoft.AspNetCore.Razor.Runtime.Test/TagHelpers/TagHelperContextTest.cs @@ -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 @@ -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 + { + { "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