Skip to content

Commit

Permalink
React to aspnet/Razor#571.
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
NTaylorMullen committed Oct 22, 2015
1 parent 0e25470 commit 0ae83a2
Show file tree
Hide file tree
Showing 28 changed files with 524 additions and 377 deletions.
4 changes: 2 additions & 2 deletions src/Microsoft.AspNet.Mvc.TagHelpers/CacheTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
// created within this scope get copied to this scope.
using (var link = MemoryCache.CreateLinkingScope())
{
result = await context.GetChildContentAsync();
result = await output.GetChildContentAsync();

MemoryCache.Set(key, result, GetMemoryCacheEntryOptions(link));
}
Expand All @@ -173,7 +173,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
}
else
{
result = await context.GetChildContentAsync();
result = await output.GetChildContentAsync();
output.Content.SetContent(result);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Mvc.TagHelpers/LabelTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
// </label>
if (!output.IsContentModified)
{
var childContent = await context.GetChildContentAsync();
var childContent = await output.GetChildContentAsync();

if (childContent.IsWhiteSpace)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.AspNet.Mvc.TagHelpers/OptionTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
}
else
{
var childContent = await context.GetChildContentAsync();
var childContent = await output.GetChildContentAsync();
selected = encodedValues.Contains(childContent.GetContent());
}

Expand Down
18 changes: 11 additions & 7 deletions src/Microsoft.AspNet.Mvc.TagHelpers/RenderAtEndOfFormTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public class RenderAtEndOfFormTagHelper : TagHelper
[ViewContext]
public ViewContext ViewContext { get; set; }

/// <inheritdoc />
public override void Init(TagHelperContext context)
{
// Push the new FormContext.
ViewContext.FormContext = new FormContext
{
CanRenderAtEndOfForm = true
};
}

/// <inheritdoc />
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
Expand All @@ -37,13 +47,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
throw new ArgumentNullException(nameof(output));
}

// Push the new FormContext.
ViewContext.FormContext = new FormContext
{
CanRenderAtEndOfForm = true
};

await context.GetChildContentAsync();
await output.GetChildContentAsync();

var formContext = ViewContext.FormContext;
if (formContext.HasEndOfFormContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
// </span>
if (!output.IsContentModified)
{
var childContent = await context.GetChildContentAsync();
var childContent = await output.GetChildContentAsync();

if (childContent.IsWhiteSpace)
{
Expand Down
15 changes: 12 additions & 3 deletions test/Microsoft.AspNet.Mvc.Razor.Test/RazorPageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,10 @@ public async Task WriteTagHelperAsync_WritesContentAppropriately(
},
startTagHelperWritingScope: () => { },
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)
{
await tagHelperExecutionContext.GetChildContentAsync(useCachedResult: true);
Expand Down Expand Up @@ -1695,7 +1698,10 @@ public async Task WriteTagHelperToAsync_WritesToSpecifiedWriter()
executeChildContentAsync: () => { return Task.FromResult(result: true); },
startTagHelperWritingScope: () => { },
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!");

// Act
Expand Down Expand Up @@ -1749,7 +1755,10 @@ private static TagHelperOutput GetTagHelperOutput(
string postContent,
string postElement)
{
var output = new TagHelperOutput(tagName, attributes)
var output = new TagHelperOutput(
tagName,
attributes,
getChildContentAsync: (_) => Task.FromResult<TagHelperContent>(new DefaultTagHelperContent()))
{
TagMode = tagMode
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public void Process_ResolvesTildeSlashValues(object url, object expectedHref)
attributes: new TagHelperAttributeList
{
{ "href", url }
});
},
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var urlHelperMock = new Mock<IUrlHelper>();
urlHelperMock
.Setup(urlHelper => urlHelper.Content(It.IsAny<string>()))
Expand All @@ -63,8 +64,7 @@ public void Process_ResolvesTildeSlashValues(object url, object expectedHref)
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
uniqueId: "test");

// Act
tagHelper.Process(context, tagHelperOutput);
Expand Down Expand Up @@ -108,7 +108,8 @@ public void Process_DoesNotResolveNonTildeSlashValues(object url)
attributes: new TagHelperAttributeList
{
{ "href", url }
});
},
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var urlHelperMock = new Mock<IUrlHelper>();
urlHelperMock
.Setup(urlHelper => urlHelper.Content(It.IsAny<string>()))
Expand All @@ -119,8 +120,7 @@ public void Process_DoesNotResolveNonTildeSlashValues(object url)
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
uniqueId: "test");

// Act
tagHelper.Process(context, tagHelperOutput);
Expand All @@ -141,15 +141,15 @@ public void Process_IgnoresNonHtmlStringOrStringValues()
attributes: new TagHelperAttributeList
{
{ "href", true }
});
},
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var tagHelper = new UrlResolutionTagHelper(urlHelper: null, htmlEncoder: null);

var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
uniqueId: "test");

// Act
tagHelper.Process(context, tagHelperOutput);
Expand Down Expand Up @@ -178,7 +178,8 @@ public void Process_ThrowsWhenEncodingNeededAndIUrlHelperActsUnexpectedly()
attributes: new TagHelperAttributeList
{
{ "href", new HtmlString(relativeUrl) }
});
},
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
var urlHelperMock = new Mock<IUrlHelper>();
urlHelperMock
.Setup(urlHelper => urlHelper.Content(It.IsAny<string>()))
Expand All @@ -189,8 +190,7 @@ public void Process_ThrowsWhenEncodingNeededAndIUrlHelperActsUnexpectedly()
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
uniqueId: "test");

// Act & Assert
var exception = Assert.Throws<InvalidOperationException>(
Expand Down
42 changes: 21 additions & 21 deletions test/Microsoft.AspNet.Mvc.TagHelpers.Test/AnchorTagHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ public async Task ProcessAsync_GeneratesExpectedOutput()
{ "asp-protocol", "http" }
},
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
uniqueId: "test");
var output = new TagHelperOutput(
expectedTagName,
attributes: new TagHelperAttributeList
{
{ "id", "myanchor" },
},
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
output.Content.SetContent("Something");

Expand Down Expand Up @@ -94,16 +94,16 @@ public async Task ProcessAsync_CallsIntoRouteLinkWithExpectedParameters()
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
uniqueId: "test");
var output = new TagHelperOutput(
"a",
attributes: new TagHelperAttributeList(),
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(
"a",
attributes: new TagHelperAttributeList());
output.Content.SetContent(string.Empty);

var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
Expand Down Expand Up @@ -142,16 +142,16 @@ public async Task ProcessAsync_CallsIntoActionLinkWithExpectedParameters()
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
uniqueId: "test");
var output = new TagHelperOutput(
"a",
attributes: new TagHelperAttributeList(),
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(
"a",
attributes: new TagHelperAttributeList());
output.Content.SetContent(string.Empty);

var generator = new Mock<IHtmlGenerator>();
Expand Down Expand Up @@ -205,7 +205,8 @@ public async Task ProcessAsync_ThrowsIfHrefConflictsWithBoundAttributes(string p
attributes: new TagHelperAttributeList
{
{ "href", "http://www.contoso.com" }
});
},
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
if (propertyName == "asp-route-")
{
anchorTagHelper.RouteValues.Add("name", "value");
Expand All @@ -224,8 +225,7 @@ public async Task ProcessAsync_ThrowsIfHrefConflictsWithBoundAttributes(string p
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
uniqueId: "test");

// Act & Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
Expand All @@ -251,16 +251,16 @@ public async Task ProcessAsync_ThrowsIfRouteAndActionOrControllerProvided(string
typeof(AnchorTagHelper).GetProperty(propertyName).SetValue(anchorTagHelper, "Home");
var output = new TagHelperOutput(
"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 " +
"'asp-route' must not have an 'asp-action' or 'asp-controller' attribute.";

var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: _ => Task.FromResult<TagHelperContent>(null));
uniqueId: "test");

// Act & Assert
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
Expand Down
Loading

0 comments on commit 0ae83a2

Please sign in to comment.