- Changed all `GetChildContentAsync` calls to come from `TagHelperOutput` instead of `TagHelperContext`.
- Updated `ReaderAtEndOfFormTagHelper` to properly detect changes inside of a `Form`s body by calling the `Init` method.
- Add test to `RenderAtEndOfFormTagHelperTest`.

aspnet/Razor#571
This commit is contained in:
N. Taylor Mullen 2015-10-19 12:54:51 -07:00
parent 0e25470660
commit 0ae83a25e9
28 changed files with 524 additions and 377 deletions

View File

@ -158,7 +158,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// created within this scope get copied to this scope. // created within this scope get copied to this scope.
using (var link = MemoryCache.CreateLinkingScope()) using (var link = MemoryCache.CreateLinkingScope())
{ {
result = await context.GetChildContentAsync(); result = await output.GetChildContentAsync();
MemoryCache.Set(key, result, GetMemoryCacheEntryOptions(link)); MemoryCache.Set(key, result, GetMemoryCacheEntryOptions(link));
} }
@ -173,7 +173,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
} }
else else
{ {
result = await context.GetChildContentAsync(); result = await output.GetChildContentAsync();
output.Content.SetContent(result); output.Content.SetContent(result);
} }
} }

View File

@ -77,7 +77,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// </label> // </label>
if (!output.IsContentModified) if (!output.IsContentModified)
{ {
var childContent = await context.GetChildContentAsync(); var childContent = await output.GetChildContentAsync();
if (childContent.IsWhiteSpace) if (childContent.IsWhiteSpace)
{ {

View File

@ -111,7 +111,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
} }
else else
{ {
var childContent = await context.GetChildContentAsync(); var childContent = await output.GetChildContentAsync();
selected = encodedValues.Contains(childContent.GetContent()); selected = encodedValues.Contains(childContent.GetContent());
} }

View File

@ -24,6 +24,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
[ViewContext] [ViewContext]
public ViewContext ViewContext { get; set; } public ViewContext ViewContext { get; set; }
/// <inheritdoc />
public override void Init(TagHelperContext context)
{
// Push the new FormContext.
ViewContext.FormContext = new FormContext
{
CanRenderAtEndOfForm = true
};
}
/// <inheritdoc /> /// <inheritdoc />
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{ {
@ -37,13 +47,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
throw new ArgumentNullException(nameof(output)); throw new ArgumentNullException(nameof(output));
} }
// Push the new FormContext. await output.GetChildContentAsync();
ViewContext.FormContext = new FormContext
{
CanRenderAtEndOfForm = true
};
await context.GetChildContentAsync();
var formContext = ViewContext.FormContext; var formContext = ViewContext.FormContext;
if (formContext.HasEndOfFormContent) if (formContext.HasEndOfFormContent)

View File

@ -79,7 +79,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// </span> // </span>
if (!output.IsContentModified) if (!output.IsContentModified)
{ {
var childContent = await context.GetChildContentAsync(); var childContent = await output.GetChildContentAsync();
if (childContent.IsWhiteSpace) if (childContent.IsWhiteSpace)
{ {

View File

@ -1663,7 +1663,10 @@ namespace Microsoft.AspNet.Mvc.Razor
}, },
startTagHelperWritingScope: () => { }, startTagHelperWritingScope: () => { },
endTagHelperWritingScope: () => defaultTagHelperContent); endTagHelperWritingScope: () => defaultTagHelperContent);
tagHelperExecutionContext.Output = new TagHelperOutput("p", new TagHelperAttributeList()); tagHelperExecutionContext.Output = new TagHelperOutput(
tagName: "p",
attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
if (childContentRetrieved) if (childContentRetrieved)
{ {
await tagHelperExecutionContext.GetChildContentAsync(useCachedResult: true); await tagHelperExecutionContext.GetChildContentAsync(useCachedResult: true);
@ -1695,7 +1698,10 @@ namespace Microsoft.AspNet.Mvc.Razor
executeChildContentAsync: () => { return Task.FromResult(result: true); }, executeChildContentAsync: () => { return Task.FromResult(result: true); },
startTagHelperWritingScope: () => { }, startTagHelperWritingScope: () => { },
endTagHelperWritingScope: () => new DefaultTagHelperContent()); endTagHelperWritingScope: () => new DefaultTagHelperContent());
tagHelperExecutionContext.Output = new TagHelperOutput("p", new TagHelperAttributeList()); tagHelperExecutionContext.Output = new TagHelperOutput(
tagName: "p",
attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
tagHelperExecutionContext.Output.Content.AppendEncoded("Hello World!"); tagHelperExecutionContext.Output.Content.AppendEncoded("Hello World!");
// Act // Act
@ -1749,7 +1755,10 @@ namespace Microsoft.AspNet.Mvc.Razor
string postContent, string postContent,
string postElement) string postElement)
{ {
var output = new TagHelperOutput(tagName, attributes) var output = new TagHelperOutput(
tagName,
attributes,
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()))
{ {
TagMode = tagMode TagMode = tagMode
}; };

View File

@ -52,7 +52,8 @@ namespace Microsoft.AspNet.Mvc.Razor.TagHelpers
attributes: new TagHelperAttributeList attributes: new TagHelperAttributeList
{ {
{ "href", url } { "href", url }
}); },
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var urlHelperMock = new Mock<IUrlHelper>(); var urlHelperMock = new Mock<IUrlHelper>();
urlHelperMock urlHelperMock
.Setup(urlHelper => urlHelper.Content(It.IsAny<string>())) .Setup(urlHelper => urlHelper.Content(It.IsAny<string>()))
@ -63,8 +64,7 @@ namespace Microsoft.AspNet.Mvc.Razor.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act // Act
tagHelper.Process(context, tagHelperOutput); tagHelper.Process(context, tagHelperOutput);
@ -108,7 +108,8 @@ namespace Microsoft.AspNet.Mvc.Razor.TagHelpers
attributes: new TagHelperAttributeList attributes: new TagHelperAttributeList
{ {
{ "href", url } { "href", url }
}); },
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var urlHelperMock = new Mock<IUrlHelper>(); var urlHelperMock = new Mock<IUrlHelper>();
urlHelperMock urlHelperMock
.Setup(urlHelper => urlHelper.Content(It.IsAny<string>())) .Setup(urlHelper => urlHelper.Content(It.IsAny<string>()))
@ -119,8 +120,7 @@ namespace Microsoft.AspNet.Mvc.Razor.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act // Act
tagHelper.Process(context, tagHelperOutput); tagHelper.Process(context, tagHelperOutput);
@ -141,15 +141,15 @@ namespace Microsoft.AspNet.Mvc.Razor.TagHelpers
attributes: new TagHelperAttributeList attributes: new TagHelperAttributeList
{ {
{ "href", true } { "href", true }
}); },
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var tagHelper = new UrlResolutionTagHelper(urlHelper: null, htmlEncoder: null); var tagHelper = new UrlResolutionTagHelper(urlHelper: null, htmlEncoder: null);
var context = new TagHelperContext( var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act // Act
tagHelper.Process(context, tagHelperOutput); tagHelper.Process(context, tagHelperOutput);
@ -178,7 +178,8 @@ namespace Microsoft.AspNet.Mvc.Razor.TagHelpers
attributes: new TagHelperAttributeList attributes: new TagHelperAttributeList
{ {
{ "href", new HtmlString(relativeUrl) } { "href", new HtmlString(relativeUrl) }
}); },
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var urlHelperMock = new Mock<IUrlHelper>(); var urlHelperMock = new Mock<IUrlHelper>();
urlHelperMock urlHelperMock
.Setup(urlHelper => urlHelper.Content(It.IsAny<string>())) .Setup(urlHelper => urlHelper.Content(It.IsAny<string>()))
@ -189,8 +190,7 @@ namespace Microsoft.AspNet.Mvc.Razor.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act & Assert // Act & Assert
var exception = Assert.Throws<InvalidOperationException>( var exception = Assert.Throws<InvalidOperationException>(

View File

@ -37,18 +37,18 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ "asp-protocol", "http" } { "asp-protocol", "http" }
}, },
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput( var output = new TagHelperOutput(
expectedTagName, expectedTagName,
attributes: new TagHelperAttributeList attributes: new TagHelperAttributeList
{ {
{ "id", "myanchor" }, { "id", "myanchor" },
},
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
output.Content.SetContent("Something"); output.Content.SetContent("Something");
@ -94,16 +94,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
"a",
attributes: new TagHelperAttributeList(),
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var output = new TagHelperOutput(
"a",
attributes: new TagHelperAttributeList());
output.Content.SetContent(string.Empty); output.Content.SetContent(string.Empty);
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict); var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
@ -142,16 +142,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
"a",
attributes: new TagHelperAttributeList(),
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var output = new TagHelperOutput(
"a",
attributes: new TagHelperAttributeList());
output.Content.SetContent(string.Empty); output.Content.SetContent(string.Empty);
var generator = new Mock<IHtmlGenerator>(); var generator = new Mock<IHtmlGenerator>();
@ -205,7 +205,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
attributes: new TagHelperAttributeList attributes: new TagHelperAttributeList
{ {
{ "href", "http://www.contoso.com" } { "href", "http://www.contoso.com" }
}); },
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
if (propertyName == "asp-route-") if (propertyName == "asp-route-")
{ {
anchorTagHelper.RouteValues.Add("name", "value"); anchorTagHelper.RouteValues.Add("name", "value");
@ -224,8 +225,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act & Assert // Act & Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException>( var ex = await Assert.ThrowsAsync<InvalidOperationException>(
@ -251,7 +251,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
typeof(AnchorTagHelper).GetProperty(propertyName).SetValue(anchorTagHelper, "Home"); typeof(AnchorTagHelper).GetProperty(propertyName).SetValue(anchorTagHelper, "Home");
var output = new TagHelperOutput( var output = new TagHelperOutput(
"a", "a",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var expectedErrorMessage = "Cannot determine an 'href' attribute for <a>. An <a> with a specified " + var expectedErrorMessage = "Cannot determine an 'href' attribute for <a>. An <a> with a specified " +
"'asp-route' must not have an 'asp-action' or 'asp-controller' attribute."; "'asp-route' must not have an 'asp-action' or 'asp-controller' attribute.";
@ -259,8 +260,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act & Assert // Act & Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException>( var ex = await Assert.ThrowsAsync<InvalidOperationException>(

View File

@ -259,8 +259,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
object cacheResult; object cacheResult;
cache.Setup(c => c.TryGetValue(It.IsAny<string>(), out cacheResult)) cache.Setup(c => c.TryGetValue(It.IsAny<string>(), out cacheResult))
.Returns(false); .Returns(false);
var tagHelperContext = GetTagHelperContext(id, childContent); var tagHelperContext = GetTagHelperContext(id);
var tagHelperOutput = new TagHelperOutput("cache", new TagHelperAttributeList()); var tagHelperOutput = GetTagHelperOutput(
attributes: new TagHelperAttributeList(),
childContent: childContent);
var cacheTagHelper = new CacheTagHelper(cache.Object) var cacheTagHelper = new CacheTagHelper(cache.Object)
{ {
ViewContext = GetViewContext(), ViewContext = GetViewContext(),
@ -298,8 +300,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
object cacheResult; object cacheResult;
cache.Setup(c => c.TryGetValue(It.IsAny<string>(), out cacheResult)) cache.Setup(c => c.TryGetValue(It.IsAny<string>(), out cacheResult))
.Returns(false); .Returns(false);
var tagHelperContext = GetTagHelperContext(id, childContent); var tagHelperContext = GetTagHelperContext(id);
var tagHelperOutput = new TagHelperOutput("cache", new TagHelperAttributeList()); var tagHelperOutput = GetTagHelperOutput(
attributes: new TagHelperAttributeList(),
childContent: childContent);
var cacheTagHelper = new CacheTagHelper(cache.Object) var cacheTagHelper = new CacheTagHelper(cache.Object)
{ {
ViewContext = GetViewContext(), ViewContext = GetViewContext(),
@ -328,8 +332,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var id = "unique-id"; var id = "unique-id";
var childContent = "original-child-content"; var childContent = "original-child-content";
var cache = new MemoryCache(new MemoryCacheOptions()); var cache = new MemoryCache(new MemoryCacheOptions());
var tagHelperContext1 = GetTagHelperContext(id, childContent); var tagHelperContext1 = GetTagHelperContext(id);
var tagHelperOutput1 = new TagHelperOutput("cache", new TagHelperAttributeList()); var tagHelperOutput1 = GetTagHelperOutput(
attributes: new TagHelperAttributeList(),
childContent: childContent);
var cacheTagHelper1 = new CacheTagHelper(cache) var cacheTagHelper1 = new CacheTagHelper(cache)
{ {
VaryByQuery = "key1,key2", VaryByQuery = "key1,key2",
@ -348,8 +354,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
Assert.Equal(childContent, tagHelperOutput1.Content.GetContent()); Assert.Equal(childContent, tagHelperOutput1.Content.GetContent());
// Arrange - 2 // Arrange - 2
var tagHelperContext2 = GetTagHelperContext(id, "different-content"); var tagHelperContext2 = GetTagHelperContext(id);
var tagHelperOutput2 = new TagHelperOutput("cache", new TagHelperAttributeList()); var tagHelperOutput2 = GetTagHelperOutput(
attributes: new TagHelperAttributeList(),
childContent: "different-content");
var cacheTagHelper2 = new CacheTagHelper(cache) var cacheTagHelper2 = new CacheTagHelper(cache)
{ {
VaryByQuery = "key1,key2", VaryByQuery = "key1,key2",
@ -375,8 +383,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var id = "unique-id"; var id = "unique-id";
var childContent1 = "original-child-content"; var childContent1 = "original-child-content";
var cache = new MemoryCache(new MemoryCacheOptions()); var cache = new MemoryCache(new MemoryCacheOptions());
var tagHelperContext1 = GetTagHelperContext(id, childContent1); var tagHelperContext1 = GetTagHelperContext(id);
var tagHelperOutput1 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } }); var tagHelperOutput1 = GetTagHelperOutput(childContent: childContent1);
tagHelperOutput1.PreContent.Append("<cache>"); tagHelperOutput1.PreContent.Append("<cache>");
tagHelperOutput1.PostContent.SetContent("</cache>"); tagHelperOutput1.PostContent.SetContent("</cache>");
var cacheTagHelper1 = new CacheTagHelper(cache) var cacheTagHelper1 = new CacheTagHelper(cache)
@ -397,8 +405,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// Arrange - 2 // Arrange - 2
var childContent2 = "different-content"; var childContent2 = "different-content";
var tagHelperContext2 = GetTagHelperContext(id, childContent2); var tagHelperContext2 = GetTagHelperContext(id);
var tagHelperOutput2 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } }); var tagHelperOutput2 = GetTagHelperOutput(childContent: childContent2);
tagHelperOutput2.PreContent.SetContent("<cache>"); tagHelperOutput2.PreContent.SetContent("<cache>");
tagHelperOutput2.PostContent.SetContent("</cache>"); tagHelperOutput2.PostContent.SetContent("</cache>");
var cacheTagHelper2 = new CacheTagHelper(cache) var cacheTagHelper2 = new CacheTagHelper(cache)
@ -565,8 +573,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
clock.SetupGet(p => p.UtcNow) clock.SetupGet(p => p.UtcNow)
.Returns(() => currentTime); .Returns(() => currentTime);
var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object }); var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object });
var tagHelperContext1 = GetTagHelperContext(id, childContent1); var tagHelperContext1 = GetTagHelperContext(id);
var tagHelperOutput1 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } }); var tagHelperOutput1 = GetTagHelperOutput(childContent: childContent1);
tagHelperOutput1.PreContent.SetContent("<cache>"); tagHelperOutput1.PreContent.SetContent("<cache>");
tagHelperOutput1.PostContent.SetContent("</cache>"); tagHelperOutput1.PostContent.SetContent("</cache>");
var cacheTagHelper1 = new CacheTagHelper(cache) var cacheTagHelper1 = new CacheTagHelper(cache)
@ -586,8 +594,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// Arrange - 2 // Arrange - 2
var childContent2 = "different-content"; var childContent2 = "different-content";
var tagHelperContext2 = GetTagHelperContext(id, childContent2); var tagHelperContext2 = GetTagHelperContext(id);
var tagHelperOutput2 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } }); var tagHelperOutput2 = GetTagHelperOutput(childContent: childContent2);
tagHelperOutput2.PreContent.SetContent("<cache>"); tagHelperOutput2.PreContent.SetContent("<cache>");
tagHelperOutput2.PostContent.SetContent("</cache>"); tagHelperOutput2.PostContent.SetContent("</cache>");
var cacheTagHelper2 = new CacheTagHelper(cache) var cacheTagHelper2 = new CacheTagHelper(cache)
@ -618,8 +626,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
clock.SetupGet(p => p.UtcNow) clock.SetupGet(p => p.UtcNow)
.Returns(() => currentTime); .Returns(() => currentTime);
var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object }); var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object });
var tagHelperContext1 = GetTagHelperContext(id, childContent1); var tagHelperContext1 = GetTagHelperContext(id);
var tagHelperOutput1 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } }); var tagHelperOutput1 = GetTagHelperOutput(childContent: childContent1);
tagHelperOutput1.PreContent.SetContent("<cache>"); tagHelperOutput1.PreContent.SetContent("<cache>");
tagHelperOutput1.PostContent.SetContent("</cache>"); tagHelperOutput1.PostContent.SetContent("</cache>");
var cacheTagHelper1 = new CacheTagHelper(cache) var cacheTagHelper1 = new CacheTagHelper(cache)
@ -640,8 +648,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// Arrange - 2 // Arrange - 2
currentTime = currentTime.AddMinutes(5).AddSeconds(2); currentTime = currentTime.AddMinutes(5).AddSeconds(2);
var childContent2 = "different-content"; var childContent2 = "different-content";
var tagHelperContext2 = GetTagHelperContext(id, childContent2); var tagHelperContext2 = GetTagHelperContext(id);
var tagHelperOutput2 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } }); var tagHelperOutput2 = GetTagHelperOutput(childContent: childContent2);
tagHelperOutput2.PreContent.SetContent("<cache>"); tagHelperOutput2.PreContent.SetContent("<cache>");
tagHelperOutput2.PostContent.SetContent("</cache>"); tagHelperOutput2.PostContent.SetContent("</cache>");
var cacheTagHelper2 = new CacheTagHelper(cache) var cacheTagHelper2 = new CacheTagHelper(cache)
@ -671,8 +679,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
clock.SetupGet(p => p.UtcNow) clock.SetupGet(p => p.UtcNow)
.Returns(() => currentTime); .Returns(() => currentTime);
var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object }); var cache = new MemoryCache(new MemoryCacheOptions { Clock = clock.Object });
var tagHelperContext1 = GetTagHelperContext(id, childContent1); var tagHelperContext1 = GetTagHelperContext(id);
var tagHelperOutput1 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } }); var tagHelperOutput1 = GetTagHelperOutput(childContent: childContent1);
tagHelperOutput1.PreContent.SetContent("<cache>"); tagHelperOutput1.PreContent.SetContent("<cache>");
tagHelperOutput1.PostContent.SetContent("</cache>"); tagHelperOutput1.PostContent.SetContent("</cache>");
var cacheTagHelper1 = new CacheTagHelper(cache) var cacheTagHelper1 = new CacheTagHelper(cache)
@ -693,8 +701,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// Arrange - 2 // Arrange - 2
currentTime = currentTime.AddSeconds(35); currentTime = currentTime.AddSeconds(35);
var childContent2 = "different-content"; var childContent2 = "different-content";
var tagHelperContext2 = GetTagHelperContext(id, childContent2); var tagHelperContext2 = GetTagHelperContext(id);
var tagHelperOutput2 = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } }); var tagHelperOutput2 = GetTagHelperOutput(childContent: childContent2);
tagHelperOutput2.PreContent.SetContent("<cache>"); tagHelperOutput2.PreContent.SetContent("<cache>");
tagHelperOutput2.PostContent.SetContent("</cache>"); tagHelperOutput2.PostContent.SetContent("</cache>");
var cacheTagHelper2 = new CacheTagHelper(cache) var cacheTagHelper2 = new CacheTagHelper(cache)
@ -727,11 +735,14 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var tagHelperContext = new TagHelperContext( var tagHelperContext = new TagHelperContext(
allAttributes: new TagHelperAttributeList(), allAttributes: new TagHelperAttributeList(),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: id, uniqueId: id);
var tagHelperOutput = new TagHelperOutput(
"cache",
new TagHelperAttributeList { { "attr", "value" } },
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
TagHelperContent tagHelperContent; TagHelperContent tagHelperContent;
if(!cache.TryGetValue("key1", out tagHelperContent)) if (!cache.TryGetValue("key1", out tagHelperContent))
{ {
tagHelperContent = expectedContent; tagHelperContent = expectedContent;
cache.Set("key1", tagHelperContent, cacheEntryOptions); cache.Set("key1", tagHelperContent, cacheEntryOptions);
@ -739,7 +750,6 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
return Task.FromResult(tagHelperContent); return Task.FromResult(tagHelperContent);
}); });
var tagHelperOutput = new TagHelperOutput("cache", new TagHelperAttributeList { { "attr", "value" } });
tagHelperOutput.PreContent.SetContent("<cache>"); tagHelperOutput.PreContent.SetContent("<cache>");
tagHelperOutput.PostContent.SetContent("</cache>"); tagHelperOutput.PostContent.SetContent("</cache>");
var cacheTagHelper = new CacheTagHelper(cache) var cacheTagHelper = new CacheTagHelper(cache)
@ -778,14 +788,24 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
new HtmlHelperOptions()); new HtmlHelperOptions());
} }
private static TagHelperContext GetTagHelperContext( private static TagHelperContext GetTagHelperContext(string id = "testid")
string id = "testid",
string childContent = "some child content")
{ {
return new TagHelperContext( return new TagHelperContext(
allAttributes: new TagHelperAttributeList(), allAttributes: new TagHelperAttributeList(),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: id, uniqueId: id);
}
private static TagHelperOutput GetTagHelperOutput(
string tagName = "cache",
TagHelperAttributeList attributes = null,
string childContent = "some child content")
{
attributes = attributes ?? new TagHelperAttributeList { { "attr", "value" } };
return new TagHelperOutput(
tagName,
attributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();

View File

@ -80,10 +80,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Test
{ {
// Arrange // Arrange
var content = "content"; var content = "content";
var context = MakeTagHelperContext( var context = MakeTagHelperContext(attributes: new TagHelperAttributeList { { "names", namesAttribute } });
attributes: new TagHelperAttributeList { { "names", namesAttribute } }, var output = MakeTagHelperOutput("environment", childContent: content);
content: content);
var output = MakeTagHelperOutput("environment");
var hostingEnvironment = new Mock<IHostingEnvironment>(); var hostingEnvironment = new Mock<IHostingEnvironment>();
hostingEnvironment.SetupProperty(h => h.EnvironmentName); hostingEnvironment.SetupProperty(h => h.EnvironmentName);
hostingEnvironment.Object.EnvironmentName = environmentName; hostingEnvironment.Object.EnvironmentName = environmentName;
@ -108,9 +106,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Test
// Arrange // Arrange
var content = "content"; var content = "content";
var context = MakeTagHelperContext( var context = MakeTagHelperContext(
attributes: new TagHelperAttributeList { { "names", namesAttribute } }, attributes: new TagHelperAttributeList { { "names", namesAttribute } });
content: content); var output = MakeTagHelperOutput("environment", childContent: content);
var output = MakeTagHelperOutput("environment");
var hostingEnvironment = new Mock<IHostingEnvironment>(); var hostingEnvironment = new Mock<IHostingEnvironment>();
hostingEnvironment.SetupProperty(h => h.EnvironmentName); hostingEnvironment.SetupProperty(h => h.EnvironmentName);
hostingEnvironment.Object.EnvironmentName = environmentName; hostingEnvironment.Object.EnvironmentName = environmentName;
@ -127,29 +124,32 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Test
Assert.False(output.IsContentModified); Assert.False(output.IsContentModified);
} }
private TagHelperContext MakeTagHelperContext( private TagHelperContext MakeTagHelperContext(TagHelperAttributeList attributes = null)
TagHelperAttributeList attributes = null,
string content = null)
{ {
attributes = attributes ?? new TagHelperAttributeList(); attributes = attributes ?? new TagHelperAttributeList();
return new TagHelperContext( return new TagHelperContext(
attributes, attributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: Guid.NewGuid().ToString("N"), uniqueId: Guid.NewGuid().ToString("N"));
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(content);
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
} }
private TagHelperOutput MakeTagHelperOutput(string tagName, TagHelperAttributeList attributes = null) private TagHelperOutput MakeTagHelperOutput(
string tagName,
TagHelperAttributeList attributes = null,
string childContent = null)
{ {
attributes = attributes ?? new TagHelperAttributeList(); attributes = attributes ?? new TagHelperAttributeList();
return new TagHelperOutput(tagName, attributes); return new TagHelperOutput(
tagName,
attributes,
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(childContent);
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
} }
} }
} }

View File

@ -42,18 +42,18 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ "asp-antiforgery", true } { "asp-antiforgery", true }
}, },
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput( var output = new TagHelperOutput(
expectedTagName, expectedTagName,
attributes: new TagHelperAttributeList attributes: new TagHelperAttributeList
{ {
{ "id", "myform" }, { "id", "myform" },
},
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
output.PostContent.SetContent("Something"); output.PostContent.SetContent("Something");
var urlHelper = new Mock<IUrlHelper>(); var urlHelper = new Mock<IUrlHelper>();
@ -111,16 +111,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList(),
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList());
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict); var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
generator generator
.Setup(mock => mock.GenerateForm( .Setup(mock => mock.GenerateForm(
@ -162,17 +162,17 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var expectedAttribute = new TagHelperAttribute("asp-ROUTEE-NotRoute", "something");
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList(),
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var expectedAttribute = new TagHelperAttribute("asp-ROUTEE-NotRoute", "something");
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList());
output.Attributes.Add(expectedAttribute); output.Attributes.Add(expectedAttribute);
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict); var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
@ -234,16 +234,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList(),
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList());
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict); var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
generator generator
.Setup(mock => mock.GenerateForm( .Setup(mock => mock.GenerateForm(
@ -285,16 +285,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList(),
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var output = new TagHelperOutput(
"form",
attributes: new TagHelperAttributeList());
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict); var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
generator generator
.Setup(mock => mock.GenerateRouteForm( .Setup(mock => mock.GenerateRouteForm(
@ -350,22 +350,23 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
ViewContext = viewContext, ViewContext = viewContext,
}; };
var output = new TagHelperOutput("form", var output = new TagHelperOutput(
attributes: new TagHelperAttributeList tagName: "form",
{ attributes: new TagHelperAttributeList
{ "aCTiON", "my-action" }, {
}); { "aCTiON", "my-action" },
var context = new TagHelperContext( },
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test");
// Act // Act
@ -394,7 +395,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
attributes: new TagHelperAttributeList attributes: new TagHelperAttributeList
{ {
{ "action", "my-action" }, { "action", "my-action" },
}); },
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
if (propertyName == "asp-route-") if (propertyName == "asp-route-")
{ {
formTagHelper.RouteValues.Add("name", "value"); formTagHelper.RouteValues.Add("name", "value");
@ -412,8 +414,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act & Assert // Act & Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException>( var ex = await Assert.ThrowsAsync<InvalidOperationException>(
@ -435,7 +436,8 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
typeof(FormTagHelper).GetProperty(propertyName).SetValue(formTagHelper, "Home"); typeof(FormTagHelper).GetProperty(propertyName).SetValue(formTagHelper, "Home");
var output = new TagHelperOutput( var output = new TagHelperOutput(
"form", "form",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var expectedErrorMessage = "Cannot determine an 'action' attribute for <form>. A <form> with a specified " + var expectedErrorMessage = "Cannot determine an 'action' attribute for <form>. A <form> with a specified " +
"'asp-route' must not have an 'asp-action' or 'asp-controller' attribute."; "'asp-route' must not have an 'asp-action' or 'asp-controller' attribute.";
@ -443,8 +445,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act & Assert // Act & Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException>( var ex = await Assert.ThrowsAsync<InvalidOperationException>(

View File

@ -51,7 +51,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ "alt", new HtmlString("Testing") }, { "alt", new HtmlString("Testing") },
{ "src", srcOutput }, { "src", srcOutput },
}; };
var output = new TagHelperOutput("img", outputAttributes); var output = new TagHelperOutput(
"img",
outputAttributes,
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var hostingEnvironment = MakeHostingEnvironment(); var hostingEnvironment = MakeHostingEnvironment();
var viewContext = MakeViewContext(); var viewContext = MakeViewContext();
var urlHelper = new Mock<IUrlHelper>(); var urlHelper = new Mock<IUrlHelper>();
@ -273,20 +276,22 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
return new TagHelperContext( return new TagHelperContext(
attributes, attributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: Guid.NewGuid().ToString("N"), uniqueId: Guid.NewGuid().ToString("N"));
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(default(string));
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
} }
private static TagHelperOutput MakeImageTagHelperOutput(TagHelperAttributeList attributes) private static TagHelperOutput MakeImageTagHelperOutput(TagHelperAttributeList attributes)
{ {
attributes = attributes ?? new TagHelperAttributeList(); attributes = attributes ?? new TagHelperAttributeList();
return new TagHelperOutput("img", attributes); return new TagHelperOutput(
"img",
attributes,
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(default(string));
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
} }
private static IHostingEnvironment MakeHostingEnvironment() private static IHostingEnvironment MakeHostingEnvironment()

View File

@ -91,9 +91,11 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: useCachedResult => Task.FromResult<TagHelperContent>(result: null)); var output = new TagHelperOutput(
var output = new TagHelperOutput(originalTagName, outputAttributes) originalTagName,
outputAttributes,
getChildContentAsync: useCachedResult => Task.FromResult<TagHelperContent>(result: null))
{ {
TagMode = TagMode.SelfClosing, TagMode = TagMode.SelfClosing,
}; };
@ -195,18 +197,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{ {
TagMode = TagMode.StartTagOnly, TagMode = TagMode.StartTagOnly,
}; };
@ -257,18 +261,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
originalTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(originalTagName, originalAttributes)
{ {
TagMode = TagMode.SelfClosing, TagMode = TagMode.SelfClosing,
}; };
@ -354,18 +360,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var context = new TagHelperContext( var context = new TagHelperContext(
allAttributes: contextAttributes, allAttributes: contextAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{ {
TagMode = TagMode.StartTagOnly, TagMode = TagMode.StartTagOnly,
}; };
@ -453,18 +461,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var context = new TagHelperContext( var context = new TagHelperContext(
allAttributes: contextAttributes, allAttributes: contextAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{ {
TagMode = TagMode.StartTagOnly, TagMode = TagMode.StartTagOnly,
}; };
@ -549,18 +559,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var context = new TagHelperContext( var context = new TagHelperContext(
allAttributes: contextAttributes, allAttributes: contextAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{ {
TagMode = TagMode.StartTagOnly, TagMode = TagMode.StartTagOnly,
}; };
@ -656,18 +668,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var context = new TagHelperContext( var context = new TagHelperContext(
allAttributes: contextAttributes, allAttributes: contextAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{ {
TagMode = TagMode.StartTagOnly, TagMode = TagMode.StartTagOnly,
}; };
@ -774,10 +788,12 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: useCachedResult => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var output = new TagHelperOutput(expectedTagName, attributes: new TagHelperAttributeList()) var output = new TagHelperOutput(
expectedTagName,
attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()))
{ {
TagMode = TagMode.SelfClosing, TagMode = TagMode.SelfClosing,
}; };
@ -855,10 +871,12 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: useCachedResult => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var output = new TagHelperOutput(expectedTagName, attributes: new TagHelperAttributeList()) var output = new TagHelperOutput(
expectedTagName,
attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()))
{ {
TagMode = TagMode.SelfClosing, TagMode = TagMode.SelfClosing,
}; };

View File

@ -128,13 +128,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers.Internal
return new TagHelperContext( return new TagHelperContext(
attributes, attributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: Guid.NewGuid().ToString("N"), uniqueId: Guid.NewGuid().ToString("N"));
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.Append(content);
return Task.FromResult((TagHelperContent)tagHelperContent);
});
} }
} }
} }

View File

@ -194,18 +194,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var htmlAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
expectedTagName,
htmlAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.AppendEncoded(tagHelperOutputContent.OriginalChildContent); tagHelperContent.AppendEncoded(tagHelperOutputContent.OriginalChildContent);
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var htmlAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(expectedTagName, htmlAttributes);
output.PreContent.AppendEncoded(expectedPreContent); output.PreContent.AppendEncoded(expectedPreContent);
output.PostContent.AppendEncoded(expectedPostContent); output.PostContent.AppendEncoded(expectedPostContent);

View File

@ -831,29 +831,24 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
return viewContext; return viewContext;
} }
private static TagHelperContext MakeTagHelperContext( private static TagHelperContext MakeTagHelperContext(TagHelperAttributeList attributes = null)
TagHelperAttributeList attributes = null,
string content = null)
{ {
attributes = attributes ?? new TagHelperAttributeList(); attributes = attributes ?? new TagHelperAttributeList();
return new TagHelperContext( return new TagHelperContext(
attributes, attributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: Guid.NewGuid().ToString("N"), uniqueId: Guid.NewGuid().ToString("N"));
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(content);
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
} }
private static TagHelperOutput MakeTagHelperOutput(string tagName, TagHelperAttributeList attributes = null) private static TagHelperOutput MakeTagHelperOutput(string tagName, TagHelperAttributeList attributes = null)
{ {
attributes = attributes ?? new TagHelperAttributeList(); attributes = attributes ?? new TagHelperAttributeList();
return new TagHelperOutput(tagName, attributes); return new TagHelperOutput(
tagName,
attributes,
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
} }
private static IHostingEnvironment MakeHostingEnvironment() private static IHostingEnvironment MakeHostingEnvironment()

View File

@ -398,15 +398,17 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var tagHelperContext = new TagHelperContext( var tagHelperContext = new TagHelperContext(
contextAttributes, contextAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
expectedTagHelperOutput.TagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
// GetChildContentAsync should not be invoked since we are setting the content below. // GetChildContentAsync should not be invoked since we are setting the content below.
Assert.True(false); Assert.True(false);
return Task.FromResult<TagHelperContent>(null); return Task.FromResult<TagHelperContent>(null);
}); })
var output = new TagHelperOutput(expectedTagHelperOutput.TagName, originalAttributes)
{ {
TagMode = TagMode.StartTagAndEndTag TagMode = TagMode.StartTagAndEndTag
}; };
@ -467,14 +469,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var tagHelperContext = new TagHelperContext( var tagHelperContext = new TagHelperContext(
contextAttributes, contextAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
originalTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(originalContent); tagHelperContent.SetContent(originalContent);
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var output = new TagHelperOutput(originalTagName, originalAttributes)
{ {
TagMode = TagMode.StartTagAndEndTag, TagMode = TagMode.StartTagAndEndTag,
}; };
@ -528,15 +532,17 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var tagHelperContext = new TagHelperContext( var tagHelperContext = new TagHelperContext(
contextAttributes, contextAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
originalTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(originalContent); tagHelperContent.SetContent(originalContent);
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var output = new TagHelperOutput(originalTagName, originalAttributes)
{ {
TagMode = TagMode.StartTagAndEndTag, TagMode = TagMode.StartTagAndEndTag,
}; };
@ -560,7 +566,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
private static TagHelperOutput GetTagHelperOutput( private static TagHelperOutput GetTagHelperOutput(
string tagName, TagHelperAttributeList attributes, string content) string tagName, TagHelperAttributeList attributes, string content)
{ {
var tagHelperOutput = new TagHelperOutput(tagName, attributes); var tagHelperOutput = new TagHelperOutput(
tagName,
attributes,
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
tagHelperOutput.Content.SetContent(content); tagHelperOutput.Content.SetContent(content);
return tagHelperOutput; return tagHelperOutput;

View File

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering; using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Mvc.ViewFeatures;
using Microsoft.AspNet.Razor.Runtime.TagHelpers; using Microsoft.AspNet.Razor.Runtime.TagHelpers;
using Microsoft.AspNet.Razor.TagHelpers; using Microsoft.AspNet.Razor.TagHelpers;
using Xunit; using Xunit;
@ -42,19 +43,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
[Theory] [Theory]
[MemberData(nameof(RenderAtEndOfFormTagHelperData))] [MemberData(nameof(RenderAtEndOfFormTagHelperData))]
public async Task Process_AddsHiddenInputTag_FromEndOfFormContent(List<TagBuilder> tagBuilderList, string expectedOutput) public async Task Process_AddsHiddenInputTag_FromEndOfFormContent(
List<TagBuilder> tagBuilderList,
string expectedOutput)
{ {
// Arrange // Arrange
var viewContext = new ViewContext(); var viewContext = new ViewContext();
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
tagName: "form", tagName: "form",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (useCachedResult) =>
var tagHelperContext = new TagHelperContext(
Enumerable.Empty<IReadOnlyTagHelperAttribute>(),
new Dictionary<object, object>(),
"someId",
(useCachedResult) =>
{ {
Assert.True(viewContext.FormContext.CanRenderAtEndOfForm); Assert.True(viewContext.FormContext.CanRenderAtEndOfForm);
foreach (var item in tagBuilderList) foreach (var item in tagBuilderList)
@ -65,10 +63,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
return Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()); return Task.FromResult<TagHelperContent>(new DefaultTagHelperContent());
}); });
var tagHelperContext = new TagHelperContext(
Enumerable.Empty<IReadOnlyTagHelperAttribute>(),
new Dictionary<object, object>(),
"someId");
var tagHelper = new RenderAtEndOfFormTagHelper var tagHelper = new RenderAtEndOfFormTagHelper
{ {
ViewContext = viewContext ViewContext = viewContext
}; };
tagHelper.Init(tagHelperContext);
// Act // Act
await tagHelper.ProcessAsync(context: tagHelperContext, output: tagHelperOutput); await tagHelper.ProcessAsync(context: tagHelperContext, output: tagHelperOutput);
@ -77,6 +81,46 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
Assert.Equal(expectedOutput, tagHelperOutput.PostContent.GetContent()); Assert.Equal(expectedOutput, tagHelperOutput.PostContent.GetContent());
} }
[Theory]
[MemberData(nameof(RenderAtEndOfFormTagHelperData))]
public async Task Process_AddsHiddenInputTag_FromEndOfFormContent_WithCachedBody(
List<TagBuilder> tagBuilderList,
string expectedOutput)
{
// Arrange
var viewContext = new ViewContext();
var runner = new TagHelperRunner();
var tagHelperExecutionContext = new TagHelperExecutionContext(
"form",
TagMode.StartTagAndEndTag,
items: new Dictionary<object, object>(),
uniqueId: string.Empty,
executeChildContentAsync: () =>
{
foreach (var item in tagBuilderList)
{
viewContext.FormContext.EndOfFormContent.Add(item);
}
return Task.FromResult(true);
},
startTagHelperWritingScope: () => { },
endTagHelperWritingScope: () => new DefaultTagHelperContent());
// This TagHelper will pre-execute the child content forcing the body to be cached.
tagHelperExecutionContext.Add(new ChildContentInvoker());
tagHelperExecutionContext.Add(new RenderAtEndOfFormTagHelper
{
ViewContext = viewContext
});
// Act
var output = await runner.RunAsync(tagHelperExecutionContext);
// Assert
Assert.Equal(expectedOutput, output.PostContent.GetContent());
}
private static TagBuilder GetTagBuilder(string tag, string name, string type, string value, TagRenderMode mode) private static TagBuilder GetTagBuilder(string tag, string name, string type, string value, TagRenderMode mode)
{ {
var tagBuilder = new TagBuilder(tag); var tagBuilder = new TagBuilder(tag);
@ -87,5 +131,21 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
return tagBuilder; return tagBuilder;
} }
private class ChildContentInvoker : TagHelper
{
public override int Order
{
get
{
return int.MinValue;
}
}
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
await output.GetChildContentAsync();
}
}
} }
} }

View File

@ -907,13 +907,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
return new TagHelperContext( return new TagHelperContext(
attributes, attributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: Guid.NewGuid().ToString("N"), uniqueId: Guid.NewGuid().ToString("N"));
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(content);
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
} }
private static ViewContext MakeViewContext(string requestPathBase = null) private static ViewContext MakeViewContext(string requestPathBase = null)
@ -941,7 +935,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
attributes = attributes ?? new TagHelperAttributeList(); attributes = attributes ?? new TagHelperAttributeList();
return new TagHelperOutput(tagName, attributes); return new TagHelperOutput(
tagName,
attributes,
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
} }
private TagHelperLogger<ScriptTagHelper> CreateLogger() private TagHelperLogger<ScriptTagHelper> CreateLogger()

View File

@ -204,14 +204,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{ {
TagMode = TagMode.SelfClosing, TagMode = TagMode.SelfClosing,
}; };
@ -291,14 +293,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.AppendEncoded("Something"); tagHelperContent.AppendEncoded("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{ {
TagMode = TagMode.SelfClosing, TagMode = TagMode.SelfClosing,
}; };
@ -393,14 +397,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.AppendEncoded("Something"); tagHelperContent.AppendEncoded("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{ {
TagMode = TagMode.SelfClosing, TagMode = TagMode.SelfClosing,
}; };
@ -479,15 +485,17 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var tagHelperContext = new TagHelperContext( var tagHelperContext = new TagHelperContext(
contextAttributes, contextAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var output = new TagHelperOutput(expectedTagName, originalAttributes);
var metadataProvider = new EmptyModelMetadataProvider(); var metadataProvider = new EmptyModelMetadataProvider();
string model = null; string model = null;
var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(string), model); var modelExplorer = metadataProvider.GetModelExplorerForType(typeof(string), model);
@ -558,14 +566,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var tagHelperContext = new TagHelperContext( var tagHelperContext = new TagHelperContext(
contextAttributes, contextAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
tagName,
originalAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var output = new TagHelperOutput(tagName, originalAttributes);
var metadataProvider = new EmptyModelMetadataProvider(); var metadataProvider = new EmptyModelMetadataProvider();
var modelExplorer = metadataProvider.GetModelExplorerForType(modelType, model); var modelExplorer = metadataProvider.GetModelExplorerForType(modelType, model);
var modelExpression = new ModelExpression(propertyName, modelExplorer); var modelExpression = new ModelExpression(propertyName, modelExplorer);

View File

@ -334,12 +334,14 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
IEnumerable<TagHelperAttribute> expectedAttributes) IEnumerable<TagHelperAttribute> expectedAttributes)
{ {
// Arrange // Arrange
var output = new TagHelperOutput("p", attributes: new TagHelperAttributeList(outputAttributes)); var output = new TagHelperOutput(
tagName: "p",
attributes: new TagHelperAttributeList(outputAttributes),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var context = new TagHelperContext( var context = new TagHelperContext(
allAttributes, allAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: useCachedResult => Task.FromResult<TagHelperContent>(result: null));
// Act // Act
output.CopyHtmlAttribute(attributeNameToCopy, context); output.CopyHtmlAttribute(attributeNameToCopy, context);
@ -432,12 +434,14 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
IEnumerable<TagHelperAttribute> expectedAttributes) IEnumerable<TagHelperAttribute> expectedAttributes)
{ {
// Arrange // Arrange
var output = new TagHelperOutput("p", attributes: new TagHelperAttributeList()); var output = new TagHelperOutput(
tagName: "p",
attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var context = new TagHelperContext( var context = new TagHelperContext(
allAttributes, allAttributes,
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: useCachedResult => Task.FromResult<TagHelperContent>(result: null));
// Act // Act
output.CopyHtmlAttribute(attributeNameToCopy, context); output.CopyHtmlAttribute(attributeNameToCopy, context);
@ -454,20 +458,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", "p",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
var tagHelperContext = new TagHelperContext(
allAttributes: new TagHelperAttributeList
{
{ attributeName, attributeValue }
},
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.Append("Something"); tagHelperContent.Append("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var tagHelperContext = new TagHelperContext(
allAttributes: new TagHelperAttributeList
{
{ attributeName, attributeValue }
},
items: new Dictionary<object, object>(),
uniqueId: "test");
var expectedAttribute = new TagHelperAttribute(attributeName, attributeValue); var expectedAttribute = new TagHelperAttribute(attributeName, attributeValue);
// Act // Act
@ -488,6 +492,12 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
attributes: new TagHelperAttributeList() attributes: new TagHelperAttributeList()
{ {
{ attributeName, "world2" } { attributeName, "world2" }
},
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.Append("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var expectedAttribute = new TagHelperAttribute(attributeName, "world2"); var expectedAttribute = new TagHelperAttribute(attributeName, "world2");
var tagHelperContext = new TagHelperContext( var tagHelperContext = new TagHelperContext(
@ -496,13 +506,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ attributeName, "world" } { attributeName, "world" }
}, },
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.Append("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
// Act // Act
tagHelperOutput.CopyHtmlAttribute(attributeName, tagHelperContext); tagHelperOutput.CopyHtmlAttribute(attributeName, tagHelperContext);
@ -519,20 +523,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var invalidAttributeName = "hello2"; var invalidAttributeName = "hello2";
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", "p",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
var tagHelperContext = new TagHelperContext(
allAttributes: new TagHelperAttributeList
{
{ "hello", "world" }
},
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.Append("Something"); tagHelperContent.Append("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var tagHelperContext = new TagHelperContext(
allAttributes: new TagHelperAttributeList
{
{ "hello", "world" }
},
items: new Dictionary<object, object>(),
uniqueId: "test");
// Act & Assert // Act & Assert
ExceptionAssert.ThrowsArgument( ExceptionAssert.ThrowsArgument(
@ -546,12 +550,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", tagName: "p",
attributes: new TagHelperAttributeList() attributes: new TagHelperAttributeList()
{ {
{ "route-Hello", "World" }, { "route-Hello", "World" },
{ "Route-I", "Am" } { "Route-I", "Am" }
}); },
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var expectedAttribute = new TagHelperAttribute("type", "btn"); var expectedAttribute = new TagHelperAttribute("type", "btn");
tagHelperOutput.Attributes.Add(expectedAttribute); tagHelperOutput.Attributes.Add(expectedAttribute);
@ -572,12 +577,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", tagName: "p",
attributes: new TagHelperAttributeList() attributes: new TagHelperAttributeList()
{ {
{ "route-Hello", "World" }, { "route-Hello", "World" },
{ "Route-I", "Am" } { "Route-I", "Am" }
}); },
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var expectedAttribute = new TagHelperAttribute("type", "btn"); var expectedAttribute = new TagHelperAttribute("type", "btn");
tagHelperOutput.Attributes.Add(expectedAttribute); tagHelperOutput.Attributes.Add(expectedAttribute);
var attributes = tagHelperOutput.Attributes var attributes = tagHelperOutput.Attributes
@ -596,12 +602,13 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", tagName: "p",
attributes: new TagHelperAttributeList() attributes: new TagHelperAttributeList()
{ {
{ "route-Hello", "World" }, { "route-Hello", "World" },
{ "Route-I", "Am" } { "Route-I", "Am" }
}); },
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var expectedAttribute = new TagHelperAttribute("type", "btn"); var expectedAttribute = new TagHelperAttribute("type", "btn");
tagHelperOutput.Attributes.Add(expectedAttribute); tagHelperOutput.Attributes.Add(expectedAttribute);
@ -756,7 +763,10 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
TagHelperAttributeList expectedAttributes) TagHelperAttributeList expectedAttributes)
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput("p", outputAttributes); var tagHelperOutput = new TagHelperOutput(
"p",
outputAttributes,
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var tagBuilder = new TagBuilder("p"); var tagBuilder = new TagBuilder("p");
foreach (var attr in tagBuilderAttributes) foreach (var attr in tagBuilderAttributes)
@ -779,8 +789,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", tagName: "p",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var expectedAttribute = new TagHelperAttribute("type", "btn"); var expectedAttribute = new TagHelperAttribute("type", "btn");
tagHelperOutput.Attributes.Add(expectedAttribute); tagHelperOutput.Attributes.Add(expectedAttribute);
@ -800,8 +811,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", tagName: "p",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
tagHelperOutput.Attributes.Add("class", "Hello"); tagHelperOutput.Attributes.Add("class", "Hello");
var tagBuilder = new TagBuilder("p"); var tagBuilder = new TagBuilder("p");
@ -826,8 +838,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", tagName: "p",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
tagHelperOutput.Attributes.Add(originalName, "Hello"); tagHelperOutput.Attributes.Add(originalName, "Hello");
var tagBuilder = new TagBuilder("p"); var tagBuilder = new TagBuilder("p");
@ -846,8 +859,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", tagName: "p",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var tagBuilder = new TagBuilder("p"); var tagBuilder = new TagBuilder("p");
var expectedAttribute = new TagHelperAttribute("visible", "val < 3"); var expectedAttribute = new TagHelperAttribute("visible", "val < 3");
@ -866,8 +880,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", tagName: "p",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var tagBuilder = new TagBuilder("p"); var tagBuilder = new TagBuilder("p");
var expectedAttribute1 = new TagHelperAttribute("class", "btn"); var expectedAttribute1 = new TagHelperAttribute("class", "btn");
@ -891,8 +906,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", tagName: "p",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var expectedAttribute = new TagHelperAttribute("class", "btn"); var expectedAttribute = new TagHelperAttribute("class", "btn");
tagHelperOutput.Attributes.Add(expectedAttribute); tagHelperOutput.Attributes.Add(expectedAttribute);
@ -911,8 +927,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ {
// Arrange // Arrange
var tagHelperOutput = new TagHelperOutput( var tagHelperOutput = new TagHelperOutput(
"p", tagName: "p",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
var expectedOutputAttribute = new TagHelperAttribute("class", "btn"); var expectedOutputAttribute = new TagHelperAttribute("class", "btn");
tagHelperOutput.Attributes.Add(expectedOutputAttribute); tagHelperOutput.Attributes.Add(expectedOutputAttribute);

View File

@ -128,18 +128,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var htmlAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(
expectedTagName,
htmlAttributes,
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); })
var htmlAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(expectedTagName, htmlAttributes)
{ {
TagMode = TagMode.SelfClosing, TagMode = TagMode.SelfClosing,
}; };

View File

@ -47,18 +47,18 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
{ "for", modelExpression }, { "for", modelExpression },
}, },
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput( var output = new TagHelperOutput(
expectedTagName, expectedTagName,
attributes: new TagHelperAttributeList attributes: new TagHelperAttributeList
{ {
{ "id", "myvalidationmessage" } { "id", "myvalidationmessage" }
},
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
output.PreContent.SetContent(expectedPreContent); output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent); output.Content.SetContent(expectedContent);
@ -111,16 +111,16 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
var output = new TagHelperOutput(
"span",
attributes: new TagHelperAttributeList(),
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something"); tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var output = new TagHelperOutput(
"span",
attributes: new TagHelperAttributeList());
output.PreContent.SetContent(expectedPreContent); output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent); output.Content.SetContent(expectedContent);
output.PostContent.SetContent(expectedPostContent); output.PostContent.SetContent(expectedPostContent);
@ -167,20 +167,20 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
}; };
var output = new TagHelperOutput( var output = new TagHelperOutput(
"span", "span",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
output.Content.AppendEncoded(outputContent);
var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.AppendEncoded(childContent); tagHelperContent.AppendEncoded(childContent);
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
output.Content.AppendEncoded(outputContent);
var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test");
var viewContext = CreateViewContext(); var viewContext = CreateViewContext();
validationMessageTagHelper.ViewContext = viewContext; validationMessageTagHelper.ViewContext = viewContext;
@ -226,13 +226,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
}; };
var output = new TagHelperOutput( var output = new TagHelperOutput(
"span", "span",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult => getChildContentAsync: useCachedResult =>
{ {
var tagHelperContent = new DefaultTagHelperContent(); var tagHelperContent = new DefaultTagHelperContent();
@ -240,6 +234,12 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
return Task.FromResult<TagHelperContent>(tagHelperContent); return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test");
var viewContext = CreateViewContext(); var viewContext = CreateViewContext();
validationMessageTagHelper.ViewContext = viewContext; validationMessageTagHelper.ViewContext = viewContext;
@ -266,8 +266,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedContent = "original content"; var expectedContent = "original content";
var expectedPostContent = "original post-content"; var expectedPostContent = "original post-content";
var output = new TagHelperOutput( var output = new TagHelperOutput(
"span", tagName: "span",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
output.PreContent.SetContent(expectedPreContent); output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent); output.Content.SetContent(expectedContent);
output.PostContent.SetContent(expectedPostContent); output.PostContent.SetContent(expectedPostContent);
@ -276,8 +277,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var viewContext = CreateViewContext(); var viewContext = CreateViewContext();
validationMessageTagHelper.ViewContext = viewContext; validationMessageTagHelper.ViewContext = viewContext;

View File

@ -43,18 +43,18 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput( var output = new TagHelperOutput(
expectedTagName, expectedTagName,
attributes: new TagHelperAttributeList attributes: new TagHelperAttributeList
{ {
{ "class", "form-control" } { "class", "form-control" }
},
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
}); });
output.PreContent.SetContent(expectedPreContent); output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent); output.Content.SetContent(expectedContent);
@ -110,8 +110,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedContent = "original content"; var expectedContent = "original content";
var expectedPostContent = "original post-content"; var expectedPostContent = "original post-content";
var output = new TagHelperOutput( var output = new TagHelperOutput(
"div", tagName: "div",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
output.PreContent.SetContent(expectedPreContent); output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent); output.Content.SetContent(expectedContent);
output.PostContent.SetContent(expectedPostContent); output.PostContent.SetContent(expectedPostContent);
@ -122,8 +123,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act & Assert // Act & Assert
await validationSummaryTagHelper.ProcessAsync(context, output); await validationSummaryTagHelper.ProcessAsync(context, output);
@ -164,8 +164,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedPreContent = "original pre-content"; var expectedPreContent = "original pre-content";
var expectedContent = "original content"; var expectedContent = "original content";
var output = new TagHelperOutput( var output = new TagHelperOutput(
"div", tagName: "div",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
output.PreContent.SetContent(expectedPreContent); output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent); output.Content.SetContent(expectedContent);
output.PostContent.SetContent("Content of validation summary"); output.PostContent.SetContent("Content of validation summary");
@ -177,8 +178,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act // Act
await validationSummaryTagHelper.ProcessAsync(context, output); await validationSummaryTagHelper.ProcessAsync(context, output);
@ -212,8 +212,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedContent = "original content"; var expectedContent = "original content";
var expectedPostContent = "original post-content"; var expectedPostContent = "original post-content";
var output = new TagHelperOutput( var output = new TagHelperOutput(
"div", tagName: "div",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
output.PreContent.SetContent(expectedPreContent); output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent); output.Content.SetContent(expectedContent);
output.PostContent.SetContent(expectedPostContent); output.PostContent.SetContent(expectedPostContent);
@ -225,8 +226,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act // Act
await validationSummaryTagHelper.ProcessAsync(context, output); await validationSummaryTagHelper.ProcessAsync(context, output);
@ -267,8 +267,9 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
var expectedPreContent = "original pre-content"; var expectedPreContent = "original pre-content";
var expectedContent = "original content"; var expectedContent = "original content";
var output = new TagHelperOutput( var output = new TagHelperOutput(
"div", tagName: "div",
attributes: new TagHelperAttributeList()); attributes: new TagHelperAttributeList(),
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()));
output.PreContent.SetContent(expectedPreContent); output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent); output.Content.SetContent(expectedContent);
output.PostContent.SetContent("Content of validation message"); output.PostContent.SetContent("Content of validation message");
@ -280,8 +281,7 @@ namespace Microsoft.AspNet.Mvc.TagHelpers
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>( allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()), Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(), items: new Dictionary<object, object>(),
uniqueId: "test", uniqueId: "test");
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
// Act // Act
await validationSummaryTagHelper.ProcessAsync(context, output); await validationSummaryTagHelper.ProcessAsync(context, output);

View File

@ -33,7 +33,7 @@ namespace ActivatorWebSite.TagHelpers
{ {
(HtmlHelper as ICanHasViewContext)?.Contextualize(ViewContext); (HtmlHelper as ICanHasViewContext)?.Contextualize(ViewContext);
var content = await context.GetChildContentAsync(); var content = await output.GetChildContentAsync();
output.Content.SetContent(HtmlHelper.Hidden(Name, content.GetContent(HtmlEncoder))); output.Content.SetContent(HtmlHelper.Hidden(Name, content.GetContent(HtmlEncoder)));
} }
} }

View File

@ -31,7 +31,7 @@ namespace ActivatorWebSite.TagHelpers
{ {
(HtmlHelper as ICanHasViewContext)?.Contextualize(ViewContext); (HtmlHelper as ICanHasViewContext)?.Contextualize(ViewContext);
var content = await context.GetChildContentAsync(); var content = await output.GetChildContentAsync();
var repeatContent = HtmlHelper.Encode(Expression.Model.ToString()); var repeatContent = HtmlHelper.Encode(Expression.Model.ToString());
if (string.IsNullOrEmpty(repeatContent)) if (string.IsNullOrEmpty(repeatContent))

View File

@ -12,7 +12,7 @@ namespace TagHelpersWebSite.TagHelpers
{ {
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{ {
var childContent = await context.GetChildContentAsync(); var childContent = await output.GetChildContentAsync();
// Find Urls in the content and replace them with their anchor tag equivalent. // Find Urls in the content and replace them with their anchor tag equivalent.
output.Content.AppendEncoded(Regex.Replace( output.Content.AppendEncoded(Regex.Replace(

View File

@ -33,6 +33,10 @@ namespace MvcSample.Web.Components
public int Order { get; } = 0; public int Order { get; } = 0;
public void Init(TagHelperContext context)
{
}
public async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) public async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{ {
var result = await InvokeAsync(Count); var result = await InvokeAsync(Count);