escape quotes in event handler attribute content

This commit is contained in:
Lukas Bickel 2018-10-17 21:22:27 +02:00 committed by Ryan Nowak
parent efbf2c5678
commit fb6427a46a
6 changed files with 117 additions and 1 deletions

View File

@ -199,7 +199,7 @@ namespace Microsoft.AspNetCore.Blazor.Razor
// an expression.
var tokens = htmlContentNode.FindDescendantNodes<IntermediateToken>();
var content = "\"" + string.Join(string.Empty, tokens.Select(t => t.Content))+ "\"";
var content = "\"" + string.Join(string.Empty, tokens.Select(t => t.Content.Replace("\"", "\\\""))) + "\"";
return new[] { new IntermediateToken() { Content = content, Kind = TokenKind.CSharp, } };
}
else

View File

@ -2443,6 +2443,20 @@ Welcome to your new app.
CompileToAssembly(generated);
}
[Fact]
public void EventHandlerTagHelper_EscapeQuotes()
{
// Act
var generated = CompileToCSharp(@"
<input onfocus='alert(""Test"");' />
");
// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}
#endregion
}
}

View File

@ -0,0 +1,30 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Blazor;
using Microsoft.AspNetCore.Blazor.Components;
public class TestComponent : Microsoft.AspNetCore.Blazor.Components.BlazorComponent
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder builder)
{
base.BuildRenderTree(builder);
__o = Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIFocusEventArgs>("alert(\"Test\");");
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,29 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [12] ) - System
UsingDirective - (18:2,1 [32] ) - System.Collections.Generic
UsingDirective - (53:3,1 [17] ) - System.Linq
UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [33] ) - Microsoft.AspNetCore.Blazor
UsingDirective - (140:6,1 [44] ) - Microsoft.AspNetCore.Blazor.Components
ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Blazor.Components.BlazorComponent -
DesignTimeDirective -
DirectiveToken - (14:0,14 [32] ) - "*, Microsoft.AspNetCore.Blazor"
DirectiveToken - (14:0,14 [9] ) - "*, Test"
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static System.Object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
CSharpCode -
IntermediateToken - - CSharp - base.BuildRenderTree(builder);
HtmlElement - (0:0,0 [34] x:\dir\subdir\Test\TestComponent.cshtml) - input
HtmlAttribute - (16:0,16 [14] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIFocusEventArgs>(
IntermediateToken - - CSharp - "alert(\"Test\");"
IntermediateToken - - CSharp - )
HtmlContent - (34:0,34 [2] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (34:0,34 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n

View File

@ -0,0 +1,25 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Blazor;
using Microsoft.AspNetCore.Blazor.Components;
public class TestComponent : Microsoft.AspNetCore.Blazor.Components.BlazorComponent
{
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder builder)
{
base.BuildRenderTree(builder);
builder.OpenElement(0, "input");
builder.AddAttribute(1, "onfocus", Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIFocusEventArgs>("alert(\"Test\");"));
builder.CloseElement();
}
#pragma warning restore 1998
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,18 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [14] ) - System
UsingDirective - (18:2,1 [34] ) - System.Collections.Generic
UsingDirective - (53:3,1 [19] ) - System.Linq
UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [35] ) - Microsoft.AspNetCore.Blazor
UsingDirective - (140:6,1 [46] ) - Microsoft.AspNetCore.Blazor.Components
ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Blazor.Components.BlazorComponent -
MethodDeclaration - - protected override - void - BuildRenderTree
CSharpCode -
IntermediateToken - - CSharp - base.BuildRenderTree(builder);
HtmlElement - (0:0,0 [34] x:\dir\subdir\Test\TestComponent.cshtml) - input
HtmlAttribute - (16:0,16 [14] x:\dir\subdir\Test\TestComponent.cshtml) - onfocus=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Blazor.Components.BindMethods.GetEventHandlerValue<Microsoft.AspNetCore.Blazor.UIFocusEventArgs>(
IntermediateToken - - CSharp - "alert(\"Test\");"
IntermediateToken - - CSharp - )