Fix test failures

Fixes #5794
This commit is contained in:
Pranav K 2017-02-13 12:03:10 -08:00
parent 2cdd84f437
commit 35edc299d7
5 changed files with 49 additions and 57 deletions

View File

@ -68,20 +68,20 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host
var visitor = new Visitor();
var modelType = GetModelType(irDocument, visitor);
var baseType = visitor.Class.BaseType;
var baseType = visitor.Class?.BaseType?.Replace("<TModel>", "<" + modelType + ">");
for (var i = visitor.InheritsDirectives.Count - 1; i >= 0; i--)
{
var directive = visitor.InheritsDirectives[i];
var tokens = directive.Tokens.ToArray();
if (tokens.Length >= 1)
{
baseType = tokens[0].Content;
baseType = tokens[0].Content.Replace("<TModel>", "<" + modelType + ">");
tokens[0].Content = baseType;
break;
}
}
visitor.Class.BaseType = baseType.Replace("<TModel>", "<" + modelType + ">");
visitor.Class.BaseType = baseType;
return irDocument;
}
}

View File

@ -5,11 +5,8 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
@ -30,35 +27,14 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// </summary>
public RazorPage()
{
}
/// <summary>
/// Gets the <see cref="System.Text.Encodings.Web.HtmlEncoder"/> to use when this <see cref="RazorPage"/>
/// handles non-<see cref="IHtmlContent"/> C# expressions.
/// </summary>
[RazorInject]
public HtmlEncoder HtmlEncoder { get; set; }
/// <summary>
/// Gets or sets a <see cref="DiagnosticSource.DiagnosticSource"/> instance used to instrument the page execution.
/// </summary>
[RazorInject]
public DiagnosticSource DiagnosticSource { get; set; }
/// <summary>
/// Gets the <see cref="ClaimsPrincipal"/> of the current logged in user.
/// </summary>
public virtual ClaimsPrincipal User => Context?.User;
/// <summary>
/// Gets the <see cref="ITempDataDictionary"/> from the <see cref="ViewContext"/>.
/// </summary>
/// <remarks>Returns null if <see cref="ViewContext"/> is null.</remarks>
public ITempDataDictionary TempData => ViewContext?.TempData;
protected override HtmlEncoder Encoder => HtmlEncoder;
/// <summary>
/// Format an error message about using an indexer when the tag helper property is <c>null</c>.
/// </summary>

View File

@ -5,11 +5,13 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Mvc.ViewFeatures.Internal;
@ -82,7 +84,23 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// <inheritdoc />
public IDictionary<string, RenderAsyncDelegate> PreviousSectionWriters { get; set; }
protected virtual HtmlEncoder Encoder { get; set; }
/// <summary>
/// Gets or sets a <see cref="System.Diagnostics.DiagnosticSource"/> instance used to instrument the page execution.
/// </summary>
[RazorInject]
public DiagnosticSource DiagnosticSource { get; set; }
/// <summary>
/// Gets the <see cref="System.Text.Encodings.Web.HtmlEncoder"/> to use when this <see cref="RazorPage"/>
/// handles non-<see cref="IHtmlContent"/> C# expressions.
/// </summary>
[RazorInject]
public HtmlEncoder HtmlEncoder { get; set; }
/// <summary>
/// Gets the <see cref="ClaimsPrincipal"/> of the current logged in user.
/// </summary>
public virtual ClaimsPrincipal User => Context?.User;
protected Stack<TagHelperScopeInfo> TagHelperScopes { get; } = new Stack<TagHelperScopeInfo>();
@ -143,12 +161,12 @@ namespace Microsoft.AspNetCore.Mvc.Razor
public void StartTagHelperWritingScope(HtmlEncoder encoder)
{
var buffer = new ViewBuffer(BufferScope, Path, ViewBuffer.TagHelperPageSize);
TagHelperScopes.Push(new TagHelperScopeInfo(buffer, Encoder, ViewContext.Writer));
TagHelperScopes.Push(new TagHelperScopeInfo(buffer, HtmlEncoder, ViewContext.Writer));
// If passed an HtmlEncoder, override the property.
if (encoder != null)
{
Encoder = encoder;
HtmlEncoder = encoder;
}
// We need to replace the ViewContext's Writer to ensure that all content (including content written
@ -162,6 +180,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// <returns>The buffered <see cref="TagHelperContent"/>.</returns>
public TagHelperContent EndTagHelperWritingScope()
{
if (TagHelperScopes.Count == 0)
{
throw new InvalidOperationException(Resources.RazorPage_ThereIsNoActiveWritingScopeToEnd);
}
var scopeInfo = TagHelperScopes.Pop();
// Get the content written during the current scope.
@ -169,7 +192,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
tagHelperContent.AppendHtml(scopeInfo.Buffer);
// Restore previous scope.
Encoder = scopeInfo.Encoder;
HtmlEncoder = scopeInfo.HtmlEncoder;
ViewContext.Writer = scopeInfo.Writer;
return tagHelperContent;
@ -187,6 +210,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// </remarks>
public void BeginWriteTagHelperAttribute()
{
if (_pageWriter != null)
{
throw new InvalidOperationException(Resources.RazorPage_NestingAttributeWritingScopesNotSupported);
}
_pageWriter = ViewContext.Writer;
if (_valueBuffer == null)
@ -210,6 +238,11 @@ namespace Microsoft.AspNetCore.Mvc.Razor
/// </remarks>
public string EndWriteTagHelperAttribute()
{
if (_pageWriter == null)
{
throw new InvalidOperationException(Resources.RazorPage_ThereIsNoActiveWritingScopeToEnd);
}
var content = _valueBuffer.ToString();
_valueBuffer.GetStringBuilder().Clear();
@ -290,7 +323,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
throw new ArgumentNullException(nameof(writer));
}
WriteTo(writer, Encoder, value);
WriteTo(writer, HtmlEncoder, value);
}
/// <summary>
@ -366,7 +399,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
throw new ArgumentNullException(nameof(writer));
}
WriteTo(writer, Encoder, value);
WriteTo(writer, HtmlEncoder, value);
}
private static void WriteTo(TextWriter writer, HtmlEncoder encoder, string value)
@ -643,7 +676,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor
if (TagHelperScopes.Count > 0)
{
throw new InvalidOperationException(
"Resources.FormatRazorPage_CannotFlushWhileInAWritingScope(nameof(FlushAsync), Path))");
Resources.FormatRazorPage_CannotFlushWhileInAWritingScope(nameof(FlushAsync), Path));
}
// Calls to Flush are allowed if the page does not specify a Layout or if it is executing a section in the
@ -790,13 +823,13 @@ namespace Microsoft.AspNetCore.Mvc.Razor
public TagHelperScopeInfo(ViewBuffer buffer, HtmlEncoder encoder, TextWriter writer)
{
Buffer = buffer;
Encoder = encoder;
HtmlEncoder = encoder;
Writer = writer;
}
public ViewBuffer Buffer { get; }
public HtmlEncoder Encoder { get; }
public HtmlEncoder HtmlEncoder { get; }
public TextWriter Writer { get; }
}

View File

@ -26,23 +26,6 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages
/// </summary>
public PageContext PageContext { get; set; }
/// <summary>
/// Gets or sets a <see cref="System.Diagnostics.DiagnosticSource"/> instance used to instrument the page execution.
/// </summary>
[RazorInject]
public DiagnosticSource DiagnosticSource { get; set; }
/// <summary>
/// Gets the <see cref="System.Text.Encodings.Web.HtmlEncoder"/> to use when this <see cref="RazorPage"/>
/// handles non-<see cref="IHtmlContent"/> C# expressions.
/// </summary>
[RazorInject]
public HtmlEncoder HtmlEncoder
{
get { return Encoder; }
set { Encoder = value; }
}
public PageArgumentBinder Binder
{
get

View File

@ -97,7 +97,7 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Host
var irDocument = CreateIRDocument(engine, codeDocument);
var expectedVCTHName = "AspNetCore.Generated_test.__Generated__TagCloudViewComponentTagHelper";
var expectedVCTHName = "AspNetCore.test_cshtml.__Generated__TagCloudViewComponentTagHelper";
// Act
pass.Execute(codeDocument, irDocument);
@ -179,7 +179,7 @@ public class __Generated__TagCloudViewComponentTagHelper : Microsoft.AspNetCore.
var irDocument = CreateIRDocument(engine, codeDocument);
var expectedVCTHName = "AspNetCore.Generated_test.__Generated__TagCloudViewComponentTagHelper";
var expectedVCTHName = "AspNetCore.test_cshtml.__Generated__TagCloudViewComponentTagHelper";
// Act
pass.Execute(codeDocument, irDocument);
@ -268,7 +268,7 @@ public class __Generated__TagCloudViewComponentTagHelper : Microsoft.AspNetCore.
var irDocument = CreateIRDocument(engine, codeDocument);
var expectedTagHelperName = "PTestTagHelper";
var expectedVCTHName = "AspNetCore.Generated_test.__Generated__TagCloudViewComponentTagHelper";
var expectedVCTHName = "AspNetCore.test_cshtml.__Generated__TagCloudViewComponentTagHelper";
// Act
pass.Execute(codeDocument, irDocument);