Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 196f3ba

Browse files
author
N. Taylor Mullen
committed
Fix ModelExpression's in section directive blocks.
- Changed `SectionDirectivePass` to move non-token body nodes from the original `DirectiveIntermediateNode` to the `SectionIntermediateNode`. By doing this there's no longer dual references of `SectionIntermediateNode` bodies. - Added MVC tests for current and 1_X extensions. #1614
1 parent 8d2a9e5 commit 196f3ba

File tree

13 files changed

+291
-76
lines changed

13 files changed

+291
-76
lines changed

src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionDirectivePass.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentInte
3535
}
3636
}
3737

38-
for (; i < directive.Node.Children.Count; i++)
38+
while (i != directive.Node.Children.Count)
3939
{
40+
// Move non-token children over to the section node so we don't have double references to children nodes.
4041
section.Children.Add(directive.Node.Children[i]);
42+
directive.Node.Children.RemoveAt(i);
4143
}
4244

4345
directive.InsertAfter(section);

test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/IntegrationTests/CodeGenerationIntegrationTest.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,14 @@ public void Basic_Runtime()
109109
[Fact]
110110
public void Sections_Runtime()
111111
{
112-
var references = CreateCompilationReferences(CurrentMvcShim);
112+
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@"
113+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
114+
115+
public class InputTestTagHelper : {typeof(TagHelper).FullName}
116+
{{
117+
public ModelExpression For {{ get; set; }}
118+
}}
119+
");
113120
RunRuntimeTest(references);
114121
}
115122

@@ -351,7 +358,14 @@ public void Basic_DesignTime()
351358
[Fact]
352359
public void Sections_DesignTime()
353360
{
354-
var references = CreateCompilationReferences(CurrentMvcShim);
361+
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@"
362+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
363+
364+
public class InputTestTagHelper : {typeof(TagHelper).FullName}
365+
{{
366+
public ModelExpression For {{ get; set; }}
367+
}}
368+
");
355369
RunDesignTimeTest(references);
356370
}
357371

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
@{
1+
@model DateTime
2+
3+
@addTagHelper "InputTestTagHelper, AppCode"
4+
5+
@{
26
Layout = "_SectionTestLayout.cshtml";
37
}
48

59
<div>Some body</div>
610

711
@section Section1 {
812
<div>This is in Section 1</div>
13+
<input-test for="Date" />
914
}

test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections_DesignTime.codegen.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,20 @@ namespace AspNetCore
1111
using Microsoft.AspNetCore.Mvc;
1212
using Microsoft.AspNetCore.Mvc.Rendering;
1313
using Microsoft.AspNetCore.Mvc.ViewFeatures;
14-
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<dynamic>
14+
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<DateTime>
1515
{
16+
private global::InputTestTagHelper __InputTestTagHelper;
1617
#pragma warning disable 219
1718
private void __RazorDirectiveTokenHelpers__() {
1819
((System.Action)(() => {
20+
DateTime __typeHelper = default(DateTime);
21+
}
22+
))();
23+
((System.Action)(() => {
24+
global::System.Object __typeHelper = "InputTestTagHelper, AppCode";
25+
}
26+
))();
27+
((System.Action)(() => {
1928
global::System.Object Section1 = null;
2029
}
2130
))();
@@ -27,13 +36,19 @@ private void __RazorDirectiveTokenHelpers__() {
2736
#pragma warning disable 1998
2837
public async override global::System.Threading.Tasks.Task ExecuteAsync()
2938
{
30-
#line 1 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml"
39+
#line 5 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml"
3140

3241
Layout = "_SectionTestLayout.cshtml";
3342

3443
#line default
3544
#line hidden
3645
DefineSection("Section1", async() => {
46+
__InputTestTagHelper = CreateTagHelper<global::InputTestTagHelper>();
47+
#line 13 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml"
48+
__InputTestTagHelper.For = ModelExpressionProvider.CreateModelExpression(ViewData, __model => __model.Date);
49+
50+
#line default
51+
#line hidden
3752
}
3853
);
3954
}
@@ -47,7 +62,7 @@ private void __RazorDirectiveTokenHelpers__() {
4762
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
4863
public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
4964
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
50-
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; }
65+
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<DateTime> Html { get; private set; }
5166
}
5267
}
5368
#pragma warning restore 1591

test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections_DesignTime.ir.txt

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ Document -
88
UsingDirective - (102:4,1 [30] ) - Microsoft.AspNetCore.Mvc
99
UsingDirective - (135:5,1 [40] ) - Microsoft.AspNetCore.Mvc.Rendering
1010
UsingDirective - (178:6,1 [43] ) - Microsoft.AspNetCore.Mvc.ViewFeatures
11-
ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml - global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<dynamic> -
11+
ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml - global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<DateTime> -
12+
DefaultTagHelperRuntime -
13+
FieldDeclaration - - private - global::InputTestTagHelper - __InputTestTagHelper
1214
DesignTimeDirective -
1315
DirectiveToken - (231:7,8 [62] ) - global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TModel>
1416
DirectiveToken - (294:7,71 [4] ) - Html
@@ -23,29 +25,47 @@ Document -
2325
DirectiveToken - (617:12,14 [96] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor
2426
DirectiveToken - (729:13,14 [87] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.HeadTagHelper, Microsoft.AspNetCore.Mvc.Razor
2527
DirectiveToken - (832:14,14 [87] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.BodyTagHelper, Microsoft.AspNetCore.Mvc.Razor
26-
DirectiveToken - (86:6,9 [8] Sections.cshtml) - Section1
28+
DirectiveToken - (7:0,7 [8] Sections.cshtml) - DateTime
29+
DirectiveToken - (33:2,14 [29] Sections.cshtml) - "InputTestTagHelper, AppCode"
30+
DirectiveToken - (152:10,9 [8] Sections.cshtml) - Section1
2731
CSharpCode -
2832
IntermediateToken - - CSharp - #pragma warning disable 0414
2933
CSharpCode -
3034
IntermediateToken - - CSharp - private static System.Object __o = null;
3135
CSharpCode -
3236
IntermediateToken - - CSharp - #pragma warning restore 0414
3337
MethodDeclaration - - public async override - global::System.Threading.Tasks.Task - ExecuteAsync
34-
CSharpCode - (2:0,2 [46] Sections.cshtml)
35-
IntermediateToken - (2:0,2 [46] Sections.cshtml) - CSharp - \n Layout = "_SectionTestLayout.cshtml";\n
36-
HtmlContent - (51:3,0 [26] Sections.cshtml)
37-
IntermediateToken - (51:3,0 [2] Sections.cshtml) - Html - \n
38-
IntermediateToken - (53:4,0 [5] Sections.cshtml) - Html - <div>
39-
IntermediateToken - (58:4,5 [9] Sections.cshtml) - Html - Some body
40-
IntermediateToken - (67:4,14 [6] Sections.cshtml) - Html - </div>
41-
IntermediateToken - (73:4,20 [4] Sections.cshtml) - Html - \n\n
38+
HtmlContent - (17:1,0 [2] Sections.cshtml)
39+
IntermediateToken - (17:1,0 [2] Sections.cshtml) - Html - \n
40+
HtmlContent - (62:2,43 [4] Sections.cshtml)
41+
IntermediateToken - (62:2,43 [4] Sections.cshtml) - Html - \n\n
42+
CSharpCode - (68:4,2 [46] Sections.cshtml)
43+
IntermediateToken - (68:4,2 [46] Sections.cshtml) - CSharp - \n Layout = "_SectionTestLayout.cshtml";\n
44+
HtmlContent - (117:7,0 [26] Sections.cshtml)
45+
IntermediateToken - (117:7,0 [2] Sections.cshtml) - Html - \n
46+
IntermediateToken - (119:8,0 [5] Sections.cshtml) - Html - <div>
47+
IntermediateToken - (124:8,5 [9] Sections.cshtml) - Html - Some body
48+
IntermediateToken - (133:8,14 [6] Sections.cshtml) - Html - </div>
49+
IntermediateToken - (139:8,20 [4] Sections.cshtml) - Html - \n\n
4250
Section - - Section1
43-
HtmlContent - (96:6,19 [39] Sections.cshtml)
44-
IntermediateToken - (96:6,19 [6] Sections.cshtml) - Html - \n
45-
IntermediateToken - (102:7,4 [5] Sections.cshtml) - Html - <div>
46-
IntermediateToken - (107:7,9 [20] Sections.cshtml) - Html - This is in Section 1
47-
IntermediateToken - (127:7,29 [6] Sections.cshtml) - Html - </div>
48-
IntermediateToken - (133:7,35 [2] Sections.cshtml) - Html - \n
51+
HtmlContent - (162:10,19 [43] Sections.cshtml)
52+
IntermediateToken - (162:10,19 [6] Sections.cshtml) - Html - \n
53+
IntermediateToken - (168:11,4 [5] Sections.cshtml) - Html - <div>
54+
IntermediateToken - (173:11,9 [20] Sections.cshtml) - Html - This is in Section 1
55+
IntermediateToken - (193:11,29 [6] Sections.cshtml) - Html - </div>
56+
IntermediateToken - (199:11,35 [6] Sections.cshtml) - Html - \n
57+
TagHelper - (205:12,4 [25] Sections.cshtml) - input-test - TagMode.SelfClosing
58+
DefaultTagHelperBody -
59+
DefaultTagHelperCreate - - InputTestTagHelper
60+
DefaultTagHelperProperty - (222:12,21 [4] Sections.cshtml) - for - Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExpression InputTestTagHelper.For - HtmlAttributeValueStyle.DoubleQuotes
61+
CSharpExpression -
62+
IntermediateToken - - CSharp - ModelExpressionProvider.CreateModelExpression(ViewData, __model =>
63+
IntermediateToken - - CSharp - __model.
64+
IntermediateToken - (222:12,21 [4] Sections.cshtml) - CSharp - Date
65+
IntermediateToken - - CSharp - )
66+
DefaultTagHelperExecute -
67+
HtmlContent - (230:12,29 [2] Sections.cshtml)
68+
IntermediateToken - (230:12,29 [2] Sections.cshtml) - Html - \n
4969
Inject -
5070
Inject -
5171
Inject -
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
1-
Source Location: (86:6,9 [8] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
1+
Source Location: (7:0,7 [8] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
2+
|DateTime|
3+
Generated Location: (742:19,0 [8] )
4+
|DateTime|
5+
6+
Source Location: (33:2,14 [29] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
7+
|"InputTestTagHelper, AppCode"|
8+
Generated Location: (883:23,37 [29] )
9+
|"InputTestTagHelper, AppCode"|
10+
11+
Source Location: (152:10,9 [8] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
212
|Section1|
3-
Generated Location: (697:18,22 [8] )
13+
Generated Location: (997:27,22 [8] )
414
|Section1|
515

6-
Source Location: (2:0,2 [46] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
16+
Source Location: (68:4,2 [46] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
717
|
818
Layout = "_SectionTestLayout.cshtml";
919
|
10-
Generated Location: (1132:30,2 [46] )
20+
Generated Location: (1432:39,2 [46] )
1121
|
1222
Layout = "_SectionTestLayout.cshtml";
1323
|
1424

25+
Source Location: (222:12,21 [4] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
26+
|Date|
27+
Generated Location: (1835:47,102 [4] )
28+
|Date|
29+

test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections_Runtime.codegen.cs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#pragma checksum "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "c7635d54fa83ccf10a29be2f0a7b56d1703398a7"
1+
#pragma checksum "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "dbec91fd88a09c6a2e35b5adedb3f8ab8e3ae486"
22
// <auto-generated/>
33
#pragma warning disable 1591
44
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml))]
@@ -12,23 +12,71 @@ namespace AspNetCore
1212
using Microsoft.AspNetCore.Mvc;
1313
using Microsoft.AspNetCore.Mvc.Rendering;
1414
using Microsoft.AspNetCore.Mvc.ViewFeatures;
15-
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<dynamic>
15+
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<DateTime>
1616
{
17+
#line hidden
18+
#pragma warning disable 0169
19+
private string __tagHelperStringValueBuffer;
20+
#pragma warning restore 0169
21+
private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext;
22+
private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner();
23+
private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __backed__tagHelperScopeManager = null;
24+
private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __tagHelperScopeManager
25+
{
26+
get
27+
{
28+
if (__backed__tagHelperScopeManager == null)
29+
{
30+
__backed__tagHelperScopeManager = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager(StartTagHelperWritingScope, EndTagHelperWritingScope);
31+
}
32+
return __backed__tagHelperScopeManager;
33+
}
34+
}
35+
private global::InputTestTagHelper __InputTestTagHelper;
1736
#pragma warning disable 1998
1837
public async override global::System.Threading.Tasks.Task ExecuteAsync()
1938
{
20-
#line 1 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml"
39+
BeginContext(17, 2, true);
40+
WriteLiteral("\r\n");
41+
EndContext();
42+
BeginContext(64, 2, true);
43+
WriteLiteral("\r\n");
44+
EndContext();
45+
#line 5 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml"
2146

2247
Layout = "_SectionTestLayout.cshtml";
2348

2449
#line default
2550
#line hidden
26-
BeginContext(51, 26, true);
51+
BeginContext(117, 26, true);
2752
WriteLiteral("\r\n<div>Some body</div>\r\n\r\n");
2853
EndContext();
2954
DefineSection("Section1", async() => {
30-
BeginContext(96, 39, true);
31-
WriteLiteral("\r\n <div>This is in Section 1</div>\r\n");
55+
BeginContext(162, 43, true);
56+
WriteLiteral("\r\n <div>This is in Section 1</div>\r\n ");
57+
EndContext();
58+
BeginContext(205, 25, false);
59+
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("input-test", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, "test", async() => {
60+
}
61+
);
62+
__InputTestTagHelper = CreateTagHelper<global::InputTestTagHelper>();
63+
__tagHelperExecutionContext.Add(__InputTestTagHelper);
64+
#line 13 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml"
65+
__InputTestTagHelper.For = ModelExpressionProvider.CreateModelExpression(ViewData, __model => __model.Date);
66+
67+
#line default
68+
#line hidden
69+
__tagHelperExecutionContext.AddTagHelperAttribute("for", __InputTestTagHelper.For, global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
70+
await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
71+
if (!__tagHelperExecutionContext.Output.IsContentModified)
72+
{
73+
await __tagHelperExecutionContext.SetOutputContentAsync();
74+
}
75+
Write(__tagHelperExecutionContext.Output);
76+
__tagHelperExecutionContext = __tagHelperScopeManager.End();
77+
EndContext();
78+
BeginContext(230, 2, true);
79+
WriteLiteral("\r\n");
3280
EndContext();
3381
}
3482
);
@@ -43,7 +91,7 @@ public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_c
4391
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
4492
public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
4593
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
46-
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; }
94+
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<DateTime> Html { get; private set; }
4795
}
4896
}
4997
#pragma warning restore 1591

0 commit comments

Comments
 (0)