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

Commit c1338a0

Browse files
author
sornaks
committed
Reacting to Razor changes for removing Generate*() method.
1 parent 489fc52 commit c1338a0

File tree

19 files changed

+687
-295
lines changed

19 files changed

+687
-295
lines changed

src/Microsoft.AspNet.Mvc.Razor.Host/MvcRazorHost.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@ public MvcRazorHost(ICodeTreeCache codeTreeCache)
9292
ScopeManagerBeginMethodName = nameof(TagHelperScopeManager.Begin),
9393
ScopeManagerEndMethodName = nameof(TagHelperScopeManager.End),
9494

95-
OutputGenerateStartTagMethodName = nameof(TagHelperOutput.GenerateStartTag),
96-
OutputGenerateContentMethodName = nameof(TagHelperOutput.GenerateContent),
97-
OutputGenerateEndTagMethodName = nameof(TagHelperOutput.GenerateEndTag),
98-
9995
// Can't use nameof because RazorPage is not accessible here.
10096
CreateTagHelperMethodName = "CreateTagHelper",
10197
StartTagHelperWritingScopeMethodName = "StartTagHelperWritingScope",
10298
EndTagHelperWritingScopeMethodName = "EndTagHelperWritingScope",
103-
HtmlEncoderPropertyName = "HtmlEncoder",
99+
100+
WriteTagHelperAsyncMethodName = "WriteTagHelperAsync",
101+
WriteTagHelperToAsyncMethodName = "WriteTagHelperToAsync",
104102
})
105103
{
106104
ResolveUrlMethodName = "Href",

src/Microsoft.AspNet.Mvc.Razor/RazorPage.cs

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Globalization;
67
using System.IO;
78
using System.Linq;
89
using System.Security.Claims;
@@ -250,25 +251,88 @@ public TagHelperContent EndTagHelperWritingScope()
250251
}
251252

252253
/// <summary>
253-
/// Writes an <see cref="ITextWriterCopyable"/> to the <see cref="Output"/>.
254+
/// Writes the content of a specified <paramref name="tagHelperExecutionContext"/>.
254255
/// </summary>
255-
/// <param name="copyableTextWriter">Contains the data to be written.</param>
256-
public void Write(ITextWriterCopyable copyableTextWriter)
256+
/// <param name="tagHelperExecutionContext">The execution context containing the content.</param>
257+
/// <returns>
258+
/// A <see cref="Task"/> that on completion writes the <paramref name="tagHelperExecutionContext"/> content.
259+
/// </returns>
260+
public async Task WriteTagHelperAsync([NotNull] TagHelperExecutionContext tagHelperExecutionContext)
257261
{
258-
WriteTo(Output, copyableTextWriter);
262+
await WriteTagHelperToAsync(Output, tagHelperExecutionContext);
259263
}
260264

261265
/// <summary>
262-
/// Writes an <see cref="ITextWriterCopyable"/> to the <paramref name="writer"/>.
266+
/// Writes the content of a specified <paramref name="tagHelperExecutionContext"/> to the specified
267+
/// <paramref name="writer"/>.
263268
/// </summary>
264-
/// <param name="writer">The <see cref="TextWriter"/> to which the
265-
/// <paramref name="copyableTextWriter"/> is written.</param>
266-
/// <param name="copyableTextWriter">Contains the data to be written.</param>
267-
public void WriteTo([NotNull] TextWriter writer, ITextWriterCopyable copyableTextWriter)
269+
/// <param name="writer">The <see cref="TextWriter"/> instance to write to.</param>
270+
/// <param name="tagHelperExecutionContext">The execution context containing the content.</param>
271+
/// <returns>
272+
/// A <see cref="Task"/> that on completion writes the <paramref name="tagHelperExecutionContext"/> content
273+
/// to the <paramref name="writer"/>.
274+
/// </returns>
275+
public async Task WriteTagHelperToAsync(
276+
[NotNull] TextWriter writer,
277+
[NotNull] TagHelperExecutionContext tagHelperExecutionContext)
268278
{
269-
if (copyableTextWriter != null)
279+
var tagHelperOutput = tagHelperExecutionContext.Output;
280+
var isTagNameNullOrWhitespace = string.IsNullOrWhiteSpace(tagHelperOutput.TagName);
281+
282+
if (!isTagNameNullOrWhitespace)
270283
{
271-
copyableTextWriter.CopyTo(writer);
284+
writer.Write('<');
285+
writer.Write(tagHelperOutput.TagName);
286+
287+
foreach (var attribute in tagHelperOutput.Attributes)
288+
{
289+
var value = HtmlEncoder.HtmlEncode(attribute.Value);
290+
writer.Write(' ');
291+
writer.Write(attribute.Key);
292+
writer.Write("=\"");
293+
writer.Write(value);
294+
writer.Write('"');
295+
}
296+
297+
if (tagHelperOutput.SelfClosing)
298+
{
299+
writer.Write(" /");
300+
}
301+
302+
writer.Write('>');
303+
}
304+
305+
if (isTagNameNullOrWhitespace || !tagHelperOutput.SelfClosing)
306+
{
307+
WriteTagHelperContentTo(writer, tagHelperOutput.PreContent);
308+
if (tagHelperOutput.IsContentModified)
309+
{
310+
WriteTagHelperContentTo(writer, tagHelperOutput.Content);
311+
}
312+
else if (tagHelperExecutionContext.ChildContentRetrieved)
313+
{
314+
var childContent = await tagHelperExecutionContext.GetChildContentAsync();
315+
WriteTagHelperContentTo(writer, childContent);
316+
}
317+
else
318+
{
319+
await tagHelperExecutionContext.ExecuteChildContentAsync();
320+
}
321+
322+
WriteTagHelperContentTo(writer, tagHelperOutput.PostContent);
323+
}
324+
325+
if (!isTagNameNullOrWhitespace && !tagHelperOutput.SelfClosing)
326+
{
327+
writer.Write(string.Format(CultureInfo.InvariantCulture, "</{0}>", tagHelperOutput.TagName));
328+
}
329+
}
330+
331+
private void WriteTagHelperContentTo(TextWriter writer, TagHelperContent content)
332+
{
333+
foreach (var entry in content)
334+
{
335+
writer.Write(entry);
272336
}
273337
}
274338

@@ -310,20 +374,7 @@ public virtual void WriteTo([NotNull] TextWriter writer, object value)
310374
}
311375
else
312376
{
313-
// This path is called when GetChildContentAsync() is called in tag helper
314-
// and content is not set.
315-
var tagHelperContent = value as TagHelperContent;
316-
if (tagHelperContent != null)
317-
{
318-
foreach (var entry in tagHelperContent)
319-
{
320-
writer.Write(entry);
321-
}
322-
}
323-
else
324-
{
325-
WriteTo(writer, value.ToString());
326-
}
377+
WriteTo(writer, value.ToString());
327378
}
328379
}
329380
}

src/Microsoft.AspNet.Mvc.TagHelpers/OptionTagHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
8585

8686
// Select this <option/> element if value attribute or content matches a selected value. Callers
8787
// encode values as-needed while executing child content. But TagHelperOutput itself
88-
// encodes attribute values later, when GenerateStartTag() is called.
88+
// encodes attribute values later, when start tag is generated.
8989
bool selected;
9090
if (Value != null)
9191
{

test/Microsoft.AspNet.Mvc.Razor.Host.Test/TestFiles/Output/Runtime/ModelExpressionTagHelper.cs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public ASPV_TestFiles_Input_ModelExpressionTagHelper_cshtml()
4242
#pragma warning disable 1998
4343
public override async Task ExecuteAsync()
4444
{
45-
__tagHelperRunner = __tagHelperRunner ?? new Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner(HtmlEncoder);
45+
__tagHelperRunner = __tagHelperRunner ?? new Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperRunner();
4646
BeginContext(120, 2, true);
4747
WriteLiteral("\r\n");
4848
EndContext();
@@ -58,22 +58,7 @@ public override async Task ExecuteAsync()
5858
#line hidden
5959
__tagHelperExecutionContext.AddTagHelperAttribute("for", __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For);
6060
__tagHelperExecutionContext.Output = __tagHelperRunner.RunAsync(__tagHelperExecutionContext).Result;
61-
WriteLiteral(__tagHelperExecutionContext.Output.GenerateStartTag());
62-
Write(__tagHelperExecutionContext.Output.GeneratePreContent());
63-
if (__tagHelperExecutionContext.Output.IsContentModified)
64-
{
65-
Write(__tagHelperExecutionContext.Output.GenerateContent());
66-
}
67-
else if (__tagHelperExecutionContext.ChildContentRetrieved)
68-
{
69-
Write(__tagHelperExecutionContext.GetChildContentAsync().Result);
70-
}
71-
else
72-
{
73-
__tagHelperExecutionContext.ExecuteChildContentAsync().Wait();
74-
}
75-
Write(__tagHelperExecutionContext.Output.GeneratePostContent());
76-
WriteLiteral(__tagHelperExecutionContext.Output.GenerateEndTag());
61+
WriteTagHelperAsync(__tagHelperExecutionContext).Wait();
7762
__tagHelperExecutionContext = __tagHelperScopeManager.End();
7863
BeginContext(146, 2, true);
7964
WriteLiteral("\r\n");
@@ -90,22 +75,7 @@ public override async Task ExecuteAsync()
9075
#line hidden
9176
__tagHelperExecutionContext.AddTagHelperAttribute("for", __Microsoft_AspNet_Mvc_Razor_InputTestTagHelper.For);
9277
__tagHelperExecutionContext.Output = __tagHelperRunner.RunAsync(__tagHelperExecutionContext).Result;
93-
WriteLiteral(__tagHelperExecutionContext.Output.GenerateStartTag());
94-
Write(__tagHelperExecutionContext.Output.GeneratePreContent());
95-
if (__tagHelperExecutionContext.Output.IsContentModified)
96-
{
97-
Write(__tagHelperExecutionContext.Output.GenerateContent());
98-
}
99-
else if (__tagHelperExecutionContext.ChildContentRetrieved)
100-
{
101-
Write(__tagHelperExecutionContext.GetChildContentAsync().Result);
102-
}
103-
else
104-
{
105-
__tagHelperExecutionContext.ExecuteChildContentAsync().Wait();
106-
}
107-
Write(__tagHelperExecutionContext.Output.GeneratePostContent());
108-
WriteLiteral(__tagHelperExecutionContext.Output.GenerateEndTag());
78+
WriteTagHelperAsync(__tagHelperExecutionContext).Wait();
10979
__tagHelperExecutionContext = __tagHelperScopeManager.End();
11080
}
11181
#pragma warning restore 1998

0 commit comments

Comments
 (0)