-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Reacting to Razor changes - TagHelperOutput is writing to TextWriter instead of string. #2099
Conversation
/cc: @NTaylorMullen @dougbu |
@@ -157,7 +159,7 @@ public TTagHelper CreateTagHelper<TTagHelper>() where TTagHelper : ITagHelper, n | |||
/// </remarks> | |||
public void StartWritingScope() | |||
{ | |||
StartWritingScope(new StringWriter()); | |||
StartWritingScope(new StringCollectionTextWriter(Encoding.UTF8)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dougbu do we have some app wide setting for encoding or is using UTF8 aok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not aware of a system-wide setting. but in normal use RazorTextWriter
is passed ViewContext.Writer.Encoding
when created. AFAICT that value is always Encodings.UTF8EncodingWithoutBOM
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively use the Encoding
from the current TextWriter
- StartWritingScope(new StringCollectionTextWriter(Output.Encoding));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 to @pranavkm's suggestion
⌚ |
/// </summary> | ||
/// <param name="writer">The <see cref="TextWriter"/> to which the output must be written to.</param> | ||
/// <param name="copyableTextWriter">Contains the data which needs to be written to the output.</param> | ||
public void WriteTo(TextWriter writer, ITextWriterCopyable copyableTextWriter) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as mentioned in aspnet/Razor#312, these methods must be named WriteLiteralTo
because they perform no HTML encoding.
⌚ |
…instead of string.
@@ -16,10 +15,10 @@ public class AutoLinkerTagHelper : TagHelper | |||
var childContent = await context.GetChildContentAsync(); | |||
|
|||
// Find Urls in the content and replace them with their anchor tag equivalent. | |||
output.Content = Regex.Replace( | |||
childContent, | |||
output.Content.Append(Regex.Replace( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
set
@sornaks should do |
⌚ few more things, it's close |
} | ||
else | ||
{ | ||
tagHelperContentWrapperTextWriter.Write(writer.ToString()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should also special-case the StringCollectionTextWriter
created in StartTagHelperWritingScope
. the ToString()
here loses many of the advantages of the Razor changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the declared/runtime type of writer
here? TextWriter.ToString()
is not guaranteed to return the content - there can be trouble if the writer is replaceable.
We can leave this for a follow up as this assumption was present in the old code.
⌚ |
/// <summary> | ||
/// The <see cref="TagHelperContent"/> which is wrapped. | ||
/// </summary> | ||
public TagHelperContent Content { get; set; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Advantage of having this get/set vs constructor + immutable? Are there cases where you would create the writer at one time and then the content later?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No. Currently there are no cases like that. I can make this immutable.
Updated.. |
} | ||
else | ||
{ | ||
selected = encodedValues.Contains((await context.GetChildContentAsync()).GetContent()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
two lines:
var childContent = await context.GetChildContentAsync();
selected = encodedValues.Contains(childContent.GetContent());
|
/// <summary> | ||
/// Writes an <see cref="ITextWriterCopyable"/> to the <paramref name="writer"/>. | ||
/// </summary> | ||
/// <param name="writer">The <see cref="TextWriter"/> to which the <paramref name="copyableTextWriter"/> is written.</param> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: long line
|
Checked in. Thanks guys! 284eb9a |
aspnet/Razor#296