Skip to content

Commit 234073c

Browse files
committed
Make HtmlEncodedString a suitable base for MVC's HtmlString
- #5 - provide `ToString()` implementation - add missing `null` checks in `WriteTo()`; remove extraneous one in the constructor - special-case `HtmlEncodedString` in `IHtmlContentBuilder.AppendFormat()` extension method nits: - ignore more files - add missing `null` checks in `HtmlContentBuilderExtensions` too
1 parent 736f2bc commit 234073c

File tree

4 files changed

+81
-13
lines changed

4 files changed

+81
-13
lines changed

.gitignore

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
[Bb]in/
33
TestResults/
44
.nuget/
5+
*.sln.ide/
56
_ReSharper.*/
67
packages/
78
artifacts/
89
PublishProfiles/
10+
.vs/
11+
bower_components/
12+
node_modules/
13+
**/wwwroot/lib/
14+
debugSettings.json
15+
project.lock.json
916
*.user
1017
*.suo
1118
*.cache
@@ -23,5 +30,8 @@ nuget.exe
2330
*.ncrunchsolution
2431
*.*sdf
2532
*.ipch
33+
.settings
2634
*.sln.ide
27-
project.lock.json
35+
node_modules
36+
**/[Cc]ompiler/[Rr]esources/**/*.js
37+
*launchSettings.json

src/Microsoft.AspNet.Html.Abstractions/HtmlContentBuilderExtensions.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public static IHtmlContentBuilder AppendFormat(
9999
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
100100
public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder)
101101
{
102+
if (builder == null)
103+
{
104+
throw new ArgumentNullException(nameof(builder));
105+
}
106+
102107
builder.Append(HtmlEncodedString.NewLine);
103108
return builder;
104109
}
@@ -112,6 +117,11 @@ public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder)
112117
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
113118
public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, string unencoded)
114119
{
120+
if (builder == null)
121+
{
122+
throw new ArgumentNullException(nameof(builder));
123+
}
124+
115125
builder.Append(unencoded);
116126
builder.Append(HtmlEncodedString.NewLine);
117127
return builder;
@@ -125,6 +135,11 @@ public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, s
125135
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
126136
public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, IHtmlContent content)
127137
{
138+
if (builder == null)
139+
{
140+
throw new ArgumentNullException(nameof(builder));
141+
}
142+
128143
builder.Append(content);
129144
builder.Append(HtmlEncodedString.NewLine);
130145
return builder;
@@ -139,6 +154,11 @@ public static IHtmlContentBuilder AppendLine(this IHtmlContentBuilder builder, I
139154
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
140155
public static IHtmlContentBuilder AppendHtmlLine(this IHtmlContentBuilder builder, string encoded)
141156
{
157+
if (builder == null)
158+
{
159+
throw new ArgumentNullException(nameof(builder));
160+
}
161+
142162
builder.AppendHtml(encoded);
143163
builder.Append(HtmlEncodedString.NewLine);
144164
return builder;
@@ -153,6 +173,11 @@ public static IHtmlContentBuilder AppendHtmlLine(this IHtmlContentBuilder builde
153173
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
154174
public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, string unencoded)
155175
{
176+
if (builder == null)
177+
{
178+
throw new ArgumentNullException(nameof(builder));
179+
}
180+
156181
builder.Clear();
157182
builder.Append(unencoded);
158183
return builder;
@@ -166,6 +191,11 @@ public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, s
166191
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
167192
public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, IHtmlContent content)
168193
{
194+
if (builder == null)
195+
{
196+
throw new ArgumentNullException(nameof(builder));
197+
}
198+
169199
builder.Clear();
170200
builder.Append(content);
171201
return builder;
@@ -180,6 +210,11 @@ public static IHtmlContentBuilder SetContent(this IHtmlContentBuilder builder, I
180210
/// <returns>The <see cref="IHtmlContentBuilder"/>.</returns>
181211
public static IHtmlContentBuilder SetHtmlContent(this IHtmlContentBuilder builder, string encoded)
182212
{
213+
if (builder == null)
214+
{
215+
throw new ArgumentNullException(nameof(builder));
216+
}
217+
183218
builder.Clear();
184219
builder.AppendHtml(encoded);
185220
return builder;
@@ -256,8 +291,14 @@ public EncodingFormatProvider(IFormatProvider formatProvider, HtmlEncoder encode
256291

257292
public string Format(string format, object arg, IFormatProvider formatProvider)
258293
{
259-
// This is the case we need to special case. We trust the IHtmlContent instance to do the
260-
// right thing with encoding.
294+
// These are the cases we need to special case. We trust the HtmlEncodedString or IHtmlContent instance
295+
// to do the right thing with encoding.
296+
var htmlString = arg as HtmlEncodedString;
297+
if (htmlString != null)
298+
{
299+
return htmlString.ToString();
300+
}
301+
261302
var htmlContent = arg as IHtmlContent;
262303
if (htmlContent != null)
263304
{

src/Microsoft.AspNet.Html.Abstractions/HtmlEncodedString.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Diagnostics;
65
using System.IO;
76
using System.Text.Encodings.Web;
87

98
namespace Microsoft.AspNet.Html
109
{
1110
/// <summary>
12-
/// An <see cref="IHtmlContent"/> impelementation that wraps an HTML encoded <see cref="string"/>.
11+
/// An <see cref="IHtmlContent"/> implementation that wraps an HTML encoded <see cref="string"/>.
1312
/// </summary>
14-
[DebuggerDisplay("{DebuggerToString()}")]
1513
public class HtmlEncodedString : IHtmlContent
1614
{
1715
/// <summary>
@@ -27,23 +25,29 @@ public class HtmlEncodedString : IHtmlContent
2725
/// <param name="value">The HTML encoded value.</param>
2826
public HtmlEncodedString(string value)
2927
{
30-
if (value == null)
31-
{
32-
throw new ArgumentNullException(nameof(value));
33-
}
34-
3528
_value = value;
3629
}
3730

3831
/// <inheritdoc />
3932
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
4033
{
34+
if (writer == null)
35+
{
36+
throw new ArgumentNullException(nameof(writer));
37+
}
38+
39+
if (encoder == null)
40+
{
41+
throw new ArgumentNullException(nameof(encoder));
42+
}
43+
4144
writer.Write(_value);
4245
}
4346

44-
private string DebuggerToString()
47+
/// <inheritdoc />
48+
public override string ToString()
4549
{
46-
return _value;
50+
return _value ?? string.Empty;
4751
}
4852
}
4953
}

test/Microsoft.AspNet.Html.Abstractions.Test/HtmlContentBuilderExtensionsTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ public void Builder_AppendFormat_HtmlContent()
158158
HtmlContentToString(builder));
159159
}
160160

161+
[Fact]
162+
public void Builder_AppendFormat_HtmlEncodedString()
163+
{
164+
// Arrange
165+
var builder = new TestHtmlContentBuilder();
166+
167+
// Act
168+
builder.AppendFormat("{0}!", new HtmlEncodedString("First"));
169+
170+
// Assert
171+
Assert.Equal("First!", HtmlContentToString(builder));
172+
}
173+
161174
[Fact]
162175
public void Builder_AppendFormatContent_With1Argument()
163176
{

0 commit comments

Comments
 (0)