Fixed a bug in TagBuilder when attribute value is null

#4710
This commit is contained in:
Shahriar Gholami 2016-05-23 22:18:20 +04:30 committed by Pranav K
parent 5d72a7f747
commit 42dd4499e0
2 changed files with 21 additions and 1 deletions

View File

@ -205,7 +205,7 @@ namespace Microsoft.AspNetCore.Mvc.Rendering
writer.Write(" ");
writer.Write(key);
writer.Write("=\"");
encoder.Encode(writer, attribute.Value);
encoder.Encode(writer, attribute.Value ?? string.Empty);
writer.Write("\"");
}
}

View File

@ -113,6 +113,26 @@ namespace Microsoft.AspNetCore.Mvc.Core.Rendering
Assert.Equal(output, result);
}
[Theory]
[InlineData("attribute", "value", "<p attribute=\"HtmlEncode[[value]]\"></p>")]
[InlineData("attribute", null, "<p attribute=\"\"></p>")]
[InlineData("attribute", "", "<p attribute=\"\"></p>")]
public void WriteTo_WriteEmptyAttribute_WhenValueIsNullOrEmpty(string attributeKey, string attributeValue, string expectedOutput)
{
// Arrange
var tagBuilder = new TagBuilder("p");
// Act
tagBuilder.Attributes.Add(attributeKey, attributeValue);
// Assert
using (var writer = new StringWriter())
{
tagBuilder.WriteTo(writer, new HtmlTestEncoder());
Assert.Equal(expectedOutput, writer.ToString());
}
}
[Fact]
public void WriteTo_IncludesInnerHtml()
{