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" />
<div class="col-md-10">
@* <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/> *@
@* - @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/> *@

View File

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

View File

@ -26,21 +26,28 @@ namespace Microsoft.AspNet.Mvc.Razor
/// <inheritdoc />
/// <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"/>.
/// </remarks>
public override void RenderAttributeValue([NotNull] TagHelperAttributeDescriptor attributeDescriptor,
[NotNull] CSharpCodeWriter writer,
[NotNull] CodeBuilderContext codeBuilderContext,
[NotNull] Action<CSharpCodeWriter> renderAttributeValue)
[NotNull] Action<CSharpCodeWriter> renderAttributeValue,
bool complexValue)
{
if (attributeDescriptor.TypeName.Equals(_context.ModelExpressionTypeName, StringComparison.Ordinal))
{
writer.WriteStartMethodInvocation(_context.CreateModelExpressionMethodName)
.Write(ModelLambdaVariableName)
.Write(" => ")
.Write(ModelLambdaVariableName)
.Write(".");
writer
.WriteStartMethodInvocation(_context.CreateModelExpressionMethodName)
.Write(ModelLambdaVariableName)
.Write(" => ");
if (!complexValue)
{
writer
.Write(ModelLambdaVariableName)
.Write(".");
}
renderAttributeValue(writer);
@ -48,7 +55,12 @@ namespace Microsoft.AspNet.Mvc.Razor
}
else
{
base.RenderAttributeValue(attributeDescriptor, writer, codeBuilderContext, renderAttributeValue);
base.RenderAttributeValue(
attributeDescriptor,
writer,
codeBuilderContext,
renderAttributeValue,
complexValue);
}
}
}

View File

@ -18,6 +18,7 @@
</div>
<div>
<label class="order" for="Products">Products</label>
<select multiple="multiple" id="Products" name="Products"><option value="0">Product_0</option>
<option value="1">Product_1</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.Compiler;
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;
using Microsoft.AspNet.Razor.Parser;
using Microsoft.AspNet.Razor.Parser.SyntaxTree;
using Xunit;
@ -149,7 +150,8 @@ MyType1
"MyClass",
"MyNamespace",
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.Compiler;
using Microsoft.AspNet.Razor.Generator.Compiler.CSharp;
using Microsoft.AspNet.Razor.Parser;
using Microsoft.AspNet.Razor.Parser.SyntaxTree;
using Xunit;
@ -105,7 +106,8 @@ Environment.NewLine +
"MyClass",
"MyNamespace",
string.Empty,
shouldGenerateLinePragmas: true));
shouldGenerateLinePragmas: true),
new ParserErrorSink());
}
}
}

View File

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

View File

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

View File

@ -50,7 +50,11 @@
public override async Task ExecuteAsync()
{
__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
}

View File

@ -48,7 +48,11 @@ namespace Asp
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("input-test", "test");
__Microsoft_AspNet_Mvc_Razor_InputTestTagHelper = CreateTagHelper<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.Output = __tagHelperRunner.RunAsync(__tagHelperExecutionContext).Result;
WriteLiteral(__tagHelperExecutionContext.Output.GenerateStartTag());

View File

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

View File

@ -29,7 +29,8 @@
</div>
<div>
<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>
<label asp-for="OrderDate" class="order" />