React to aspnet/Razor#268 changes

- part I of #1253 using new Razor capabilities
- update baselines to match latest code generation
- use new `CodeBuilderContext` and `TagHelperAttributeValueCodeRenderer` signatures
- test complex expressions in bound non-string attribute values
This commit is contained in:
Doug Bunting 2015-01-14 16:13:41 -08:00
parent 3dea6b11a3
commit a77d071830
12 changed files with 63 additions and 38 deletions

View File

@ -48,7 +48,8 @@
<label asp-for="YearsEmployeed" class="control-label col-md-2" /> <label asp-for="YearsEmployeed" class="control-label col-md-2" />
<div class="col-md-10"> <div class="col-md-10">
@* <select/> tag helper has ContentBehavior.Append -- items render after static options *@ @* <select/> tag helper has ContentBehavior.Append -- items render after static options *@
<select asp-for="YearsEmployeed" asp-items="(IEnumerable<SelectListItem>)ViewBag.Items" size="2" class="form-control"> @{ var @object = "multiple"; }
<select asp-for="@(Model.YearsEmployeed)" asp-items="@((IEnumerable<SelectListItem>)ViewBag.Items)" size="2" class="form-control" multiple="@(@object)">
@* Static use of "selected" attribute may cause HTML errors if in a single-selection <select/> *@ @* Static use of "selected" attribute may cause HTML errors if in a single-selection <select/> *@
@* - @NTaylorMullen thinks <option/> tag helper could tell <select/> helper not to select anything from "items" *@ @* - @NTaylorMullen thinks <option/> tag helper could tell <select/> helper not to select anything from "items" *@
@* - wouldn't help if user selected one static <option/> and expression indicated another, especially one earlier in the <select/> *@ @* - wouldn't help if user selected one static <option/> and expression indicated another, especially one earlier in the <select/> *@

View File

@ -9,28 +9,20 @@
@for (var index = 0; index < Model.Count(); ++index) @for (var index = 0; index < Model.Count(); ++index)
{ {
<div class="form-group"> <div class="form-group">
@Html.LabelFor(m => m[index].Name) <label asp-for="@Model[index].Name" />
@Html.TextBoxFor(m => m[index].Name, htmlAttributes: new { disabled = "disabled", @readonly= "readonly" }) <input type="text" asp-for="@Model[index].Name" disabled="disabled" readonly="readonly" />
@*<label asp-for="[index].Name" />
<input type="text" asp-for="[index].Name" disabled="disabled" readonly="readonly" />*@
</div> </div>
<div class="form-group"> <div class="form-group">
@Html.LabelFor(m => m[index].DateOfBirth) <label asp-for="@Model[index].DateOfBirth" />
@Html.TextBoxFor(m => m[index].DateOfBirth, format: "{0:yyyy-MM-dd}", htmlAttributes: new { disabled = "disabled", @readonly = "readonly", type="date" }) <input type="date" asp-for="@Model[index].DateOfBirth" asp-format="{0:yyyy-MM-dd}" disabled="disabled" readonly="readonly" />
@*<label asp-for="[index].DateOfBirth" />
<input type="date" asp-for="[index].DateOfBirth" disabled="disabled" readonly="readonly" />*@
</div> </div>
<div class="form-group"> <div class="form-group">
@Html.LabelFor(m => m[index].YearsEmployeed) <label asp-for="@Model[index].YearsEmployeed" />
@Html.TextBoxFor(m => m[index].YearsEmployeed, htmlAttributes: new { disabled = "disabled", @readonly = "readonly", type="number" }) <input type="number" asp-for="@Model[index].YearsEmployeed" disabled="disabled" readonly="readonly" />
@*<label asp-for="[index].YearsEmployeed" />
<input type="number" asp-for="[index].YearsEmployeed" disabled="disabled" readonly="readonly" />*@
</div> </div>
<div class="form-group"> <div class="form-group">
@Html.LabelFor(m => m[index].Blurb) <label asp-for="@Model[index].Blurb" />
@Html.TextAreaFor(m => m[index].Blurb, htmlAttributes: new { disabled = "disabled", @readonly = "readonly" }) <textarea rows="4" asp-for="@Model[index].Blurb" disabled="disabled" readonly="readonly" />
@*<label asp-for="[index].Blurb" />
<textarea rows="4" asp-for="[index].Blurb" disabled="disabled" readonly="readonly" />*@
</div> </div>
<p> <p>

View File

@ -26,21 +26,28 @@ namespace Microsoft.AspNet.Mvc.Razor
/// <inheritdoc /> /// <inheritdoc />
/// <remarks>If the attribute being rendered is of the type /// <remarks>If the attribute being rendered is of the type
/// <see cref="GeneratedTagHelperAttributeContext.ModelExpressionTypeName"/> then a model expression will be /// <see cref="GeneratedTagHelperAttributeContext.ModelExpressionTypeName"/>, then a model expression will be
/// created by calling into <see cref="GeneratedTagHelperAttributeContext.CreateModelExpressionMethodName"/>. /// created by calling into <see cref="GeneratedTagHelperAttributeContext.CreateModelExpressionMethodName"/>.
/// </remarks> /// </remarks>
public override void RenderAttributeValue([NotNull] TagHelperAttributeDescriptor attributeDescriptor, public override void RenderAttributeValue([NotNull] TagHelperAttributeDescriptor attributeDescriptor,
[NotNull] CSharpCodeWriter writer, [NotNull] CSharpCodeWriter writer,
[NotNull] CodeBuilderContext codeBuilderContext, [NotNull] CodeBuilderContext codeBuilderContext,
[NotNull] Action<CSharpCodeWriter> renderAttributeValue) [NotNull] Action<CSharpCodeWriter> renderAttributeValue,
bool complexValue)
{ {
if (attributeDescriptor.TypeName.Equals(_context.ModelExpressionTypeName, StringComparison.Ordinal)) if (attributeDescriptor.TypeName.Equals(_context.ModelExpressionTypeName, StringComparison.Ordinal))
{ {
writer.WriteStartMethodInvocation(_context.CreateModelExpressionMethodName) writer
.Write(ModelLambdaVariableName) .WriteStartMethodInvocation(_context.CreateModelExpressionMethodName)
.Write(" => ") .Write(ModelLambdaVariableName)
.Write(ModelLambdaVariableName) .Write(" => ");
.Write("."); if (!complexValue)
{
writer
.Write(ModelLambdaVariableName)
.Write(".");
}
renderAttributeValue(writer); renderAttributeValue(writer);
@ -48,7 +55,12 @@ namespace Microsoft.AspNet.Mvc.Razor
} }
else else
{ {
base.RenderAttributeValue(attributeDescriptor, writer, codeBuilderContext, renderAttributeValue); base.RenderAttributeValue(
attributeDescriptor,
writer,
codeBuilderContext,
renderAttributeValue,
complexValue);
} }
} }
} }

View File

@ -18,6 +18,7 @@
</div> </div>
<div> <div>
<label class="order" for="Products">Products</label> <label class="order" for="Products">Products</label>
<select multiple="multiple" id="Products" name="Products"><option value="0">Product_0</option> <select multiple="multiple" id="Products" name="Products"><option value="0">Product_0</option>
<option value="1">Product_1</option> <option value="1">Product_1</option>
<option selected="selected" value="2">Product_2</option> <option selected="selected" value="2">Product_2</option>

View File

@ -5,6 +5,7 @@ using System;
using Microsoft.AspNet.Razor.Generator; using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Generator.Compiler; using Microsoft.AspNet.Razor.Generator.Compiler;
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp; using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;
using Microsoft.AspNet.Razor.Parser;
using Microsoft.AspNet.Razor.Parser.SyntaxTree; using Microsoft.AspNet.Razor.Parser.SyntaxTree;
using Xunit; using Xunit;
@ -149,7 +150,8 @@ MyType1
"MyClass", "MyClass",
"MyNamespace", "MyNamespace",
string.Empty, string.Empty,
shouldGenerateLinePragmas: true)); shouldGenerateLinePragmas: true),
new ParserErrorSink());
} }
} }
} }

View File

@ -5,6 +5,7 @@ using System;
using Microsoft.AspNet.Razor.Generator; using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Generator.Compiler; using Microsoft.AspNet.Razor.Generator.Compiler;
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp; using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;
using Microsoft.AspNet.Razor.Parser;
using Microsoft.AspNet.Razor.Parser.SyntaxTree; using Microsoft.AspNet.Razor.Parser.SyntaxTree;
using Xunit; using Xunit;
@ -105,7 +106,8 @@ Environment.NewLine +
"MyClass", "MyClass",
"MyNamespace", "MyNamespace",
string.Empty, string.Empty,
shouldGenerateLinePragmas: true)); shouldGenerateLinePragmas: true),
new ParserErrorSink());
} }
} }
} }

View File

@ -55,9 +55,9 @@ namespace Microsoft.AspNet.Mvc.Razor
BuildLineMapping(documentAbsoluteIndex: 139, BuildLineMapping(documentAbsoluteIndex: 139,
documentLineIndex: 4, documentLineIndex: 4,
documentCharacterIndex: 17, documentCharacterIndex: 17,
generatedAbsoluteIndex: 2058, generatedAbsoluteIndex: 2105,
generatedLineIndex: 52, generatedLineIndex: 53,
generatedCharacterIndex: 107, generatedCharacterIndex: 95,
contentLength: 3) contentLength: 3)
}; };

View File

@ -3,6 +3,7 @@
using Microsoft.AspNet.Razor.Generator; using Microsoft.AspNet.Razor.Generator;
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp; using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;
using Microsoft.AspNet.Razor.Parser;
using Microsoft.AspNet.Razor.TagHelpers; using Microsoft.AspNet.Razor.TagHelpers;
using Xunit; using Xunit;
@ -31,14 +32,16 @@ namespace Microsoft.AspNet.Mvc.Razor
rootNamespace: string.Empty, rootNamespace: string.Empty,
sourceFile: string.Empty, sourceFile: string.Empty,
shouldGenerateLinePragmas: true); shouldGenerateLinePragmas: true);
var context = new CodeBuilderContext(generatorContext); var errorSink = new ParserErrorSink();
var context = new CodeBuilderContext(generatorContext, errorSink);
// Act // Act
renderer.RenderAttributeValue(attributeDescriptor, writer, context, renderer.RenderAttributeValue(attributeDescriptor, writer, context,
(codeWriter) => (codeWriter) =>
{ {
codeWriter.Write("MyValue"); codeWriter.Write("MyValue");
}); },
complexValue: false);
// Assert // Assert
Assert.Equal(expectedValue, writer.GenerateCode()); Assert.Equal(expectedValue, writer.GenerateCode());

View File

@ -50,7 +50,11 @@
public override async Task ExecuteAsync() public override async Task ExecuteAsync()
{ {
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = CreateTagHelper<Microsoft.AspNet.Mvc.Razor.InputTestTagHelper>(); __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = CreateTagHelper<Microsoft.AspNet.Mvc.Razor.InputTestTagHelper>();
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For = CreateModelExpression(__model => __model.Now); #line 5 "TestFiles/Input/ModelExpressionTagHelper.cshtml"
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For = CreateModelExpression(__model => __model.Now);
#line default
#line hidden
} }
#pragma warning restore 1998 #pragma warning restore 1998
} }

View File

@ -48,7 +48,11 @@ namespace Asp
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("input-test", "test"); __tagHelperExecutionContext = __tagHelperScopeManager.Begin("input-test", "test");
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = CreateTagHelper<Microsoft.AspNet.Mvc.Razor.InputTestTagHelper>(); __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = CreateTagHelper<Microsoft.AspNet.Mvc.Razor.InputTestTagHelper>();
__tagHelperExecutionContext.Add(__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper); __tagHelperExecutionContext.Add(__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper);
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For = CreateModelExpression(__model => __model.Now); #line 5 "TestFiles/Input/ModelExpressionTagHelper.cshtml"
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For = CreateModelExpression(__model => __model.Now);
#line default
#line hidden
__tagHelperExecutionContext.AddTagHelperAttribute("for", __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For); __tagHelperExecutionContext.AddTagHelperAttribute("for", __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For);
__tagHelperExecutionContext.Output = __tagHelperRunner.RunAsync(__tagHelperExecutionContext).Result; __tagHelperExecutionContext.Output = __tagHelperRunner.RunAsync(__tagHelperExecutionContext).Result;
WriteLiteral(__tagHelperExecutionContext.Output.GenerateStartTag()); WriteLiteral(__tagHelperExecutionContext.Output.GenerateStartTag());

View File

@ -6,6 +6,7 @@ using System.Linq;
using Microsoft.AspNet.FileSystems; using Microsoft.AspNet.FileSystems;
using Microsoft.AspNet.Razor; using Microsoft.AspNet.Razor;
using Microsoft.AspNet.Razor.Generator.Compiler; using Microsoft.AspNet.Razor.Generator.Compiler;
using Microsoft.AspNet.Razor.Parser;
using Microsoft.AspNet.Razor.Parser.SyntaxTree; using Microsoft.AspNet.Razor.Parser.SyntaxTree;
using Microsoft.AspNet.Razor.TagHelpers; using Microsoft.AspNet.Razor.TagHelpers;
using Moq; using Moq;
@ -49,10 +50,12 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
public void Compile_ReturnsFailedResultIfParseFails() public void Compile_ReturnsFailedResultIfParseFails()
{ {
// Arrange // Arrange
var errorSink = new ParserErrorSink();
errorSink.OnError(new RazorError("some message", 1, 1, 1, 1));
var generatorResult = new GeneratorResults( var generatorResult = new GeneratorResults(
new Block(new BlockBuilder { Type = BlockType.Comment }), new Block(new BlockBuilder { Type = BlockType.Comment }),
Enumerable.Empty<TagHelperDescriptor>(), Enumerable.Empty<TagHelperDescriptor>(),
new RazorError[] { new RazorError("some message", 1, 1, 1, 1) }, errorSink,
new CodeBuilderResult("", new LineMapping[0]), new CodeBuilderResult("", new LineMapping[0]),
new CodeTree()); new CodeTree());
var host = new Mock<IMvcRazorHost>(); var host = new Mock<IMvcRazorHost>();
@ -87,7 +90,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
var generatorResult = new GeneratorResults( var generatorResult = new GeneratorResults(
new Block(new BlockBuilder { Type = BlockType.Comment }), new Block(new BlockBuilder { Type = BlockType.Comment }),
Enumerable.Empty<TagHelperDescriptor>(), Enumerable.Empty<TagHelperDescriptor>(),
new RazorError[0], new ParserErrorSink(),
new CodeBuilderResult(code, new LineMapping[0]), new CodeBuilderResult(code, new LineMapping[0]),
new CodeTree()); new CodeTree());
var host = new Mock<IMvcRazorHost>(); var host = new Mock<IMvcRazorHost>();
@ -119,7 +122,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
return new GeneratorResults( return new GeneratorResults(
new Block(new BlockBuilder { Type = BlockType.Comment }), new Block(new BlockBuilder { Type = BlockType.Comment }),
Enumerable.Empty<TagHelperDescriptor>(), Enumerable.Empty<TagHelperDescriptor>(),
new RazorError[0], new ParserErrorSink(),
new CodeBuilderResult("", new LineMapping[0]), new CodeBuilderResult("", new LineMapping[0]),
new CodeTree()); new CodeTree());
} }

View File

@ -29,7 +29,8 @@
</div> </div>
<div> <div>
<label asp-for="Products" class="order" /> <label asp-for="Products" class="order" />
<select asp-for="Products" asp-items="(IEnumerable<SelectListItem>)ViewBag.Items" multiple="multiple" /> @{ var @object = "multiple"; }
<select asp-for="@Model.Products" asp-items="@((IEnumerable<SelectListItem>)ViewBag.Items)" multiple="@(@object)" />
</div> </div>
<div> <div>
<label asp-for="OrderDate" class="order" /> <label asp-for="OrderDate" class="order" />