Handle `null` entries correctly for `IsEmptyOrWhiteSpace`

- #2497

nits:
- take VS suggestions
- un-scramble `IsEmptyOrWhiteSpaceCore(...)` for readability
This commit is contained in:
Doug Bunting 2018-07-21 16:48:39 -07:00
parent 8502dd1ebe
commit 684e7855a6
No known key found for this signature in database
GPG Key ID: 888B4EB7822B32E9
2 changed files with 16 additions and 30 deletions

View File

@ -221,8 +221,7 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
return;
}
var stringValue = entry as string;
if (stringValue != null)
if (entry is string stringValue)
{
encoder.Encode(writer, stringValue);
}
@ -239,13 +238,11 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
return;
}
string entryAsString;
IHtmlContentContainer entryAsContainer;
if ((entryAsString = entry as string) != null)
if (entry is string entryAsString)
{
destination.Append(entryAsString);
}
else if ((entryAsContainer = entry as IHtmlContentContainer) != null)
else if (entry is IHtmlContentContainer entryAsContainer)
{
entryAsContainer.CopyTo(destination);
}
@ -262,13 +259,11 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
return;
}
string entryAsString;
IHtmlContentContainer entryAsContainer;
if ((entryAsString = entry as string) != null)
if (entry is string entryAsString)
{
destination.Append(entryAsString);
}
else if ((entryAsContainer = entry as IHtmlContentContainer) != null)
else if (entry is IHtmlContentContainer entryAsContainer)
{
entryAsContainer.MoveTo(destination);
}
@ -282,29 +277,19 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
{
if (entry == null)
{
return false;
return true;
}
var stringValue = entry as string;
if (stringValue != null)
if (entry is string stringValue)
{
// Do not encode the string because encoded value remains whitespace from user's POV.
if (!string.IsNullOrWhiteSpace(stringValue))
{
return false;
}
}
else
{
// Use NullHtmlEncoder to avoid treating encoded whitespace as non-whitespace e.g. "\t" as "	".
((IHtmlContent)entry).WriteTo(writer, NullHtmlEncoder.Default);
if (!writer.IsEmptyOrWhiteSpace)
{
return false;
}
return string.IsNullOrWhiteSpace(stringValue);
}
return true;
// Use NullHtmlEncoder to avoid treating encoded whitespace as non-whitespace e.g. "\t" as "	".
((IHtmlContent)entry).WriteTo(writer, NullHtmlEncoder.Default);
return writer.IsEmptyOrWhiteSpace;
}
private TagHelperContent AppendCore(object entry)
@ -329,7 +314,7 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
{
return GetContent();
}
// Overrides Write(string) to find if the content written is empty/whitespace.
private class EmptyOrWhiteSpaceWriter : TextWriter
{
@ -360,4 +345,4 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
}
}
}
}
}

View File

@ -363,6 +363,7 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
{
return new TheoryData<string>
{
null,
string.Empty,
" ",
"\n",
@ -838,4 +839,4 @@ namespace Microsoft.AspNetCore.Razor.TagHelpers
}
}
}
}
}