Skip to content

Commit d9a05d3

Browse files
committed
Add nullability to HtmlAbstractions
Addresses #5680
1 parent 2bb7851 commit d9a05d3

8 files changed

+35
-35
lines changed

src/Html/Abstractions/ref/Microsoft.AspNetCore.Html.Abstractions.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<Project Sdk="Microsoft.NET.Sdk">
33
<PropertyGroup>
44
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
5+
<Nullable>annotations</Nullable>
56
</PropertyGroup>
67
<ItemGroup Condition="'$(TargetFramework)' == '$(DefaultNetCoreTargetFramework)'">
78
<Compile Include="Microsoft.AspNetCore.Html.Abstractions.netcoreapp.cs" />

src/Html/Abstractions/ref/Microsoft.AspNetCore.Html.Abstractions.netcoreapp.cs

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
namespace Microsoft.AspNetCore.Html
55
{
6+
[System.Diagnostics.DebuggerDisplayAttribute("{DebuggerToString()}")]
67
public partial class HtmlContentBuilder : Microsoft.AspNetCore.Html.IHtmlContent, Microsoft.AspNetCore.Html.IHtmlContentBuilder, Microsoft.AspNetCore.Html.IHtmlContentContainer
78
{
89
public HtmlContentBuilder() { }
910
public HtmlContentBuilder(System.Collections.Generic.IList<object> entries) { }
1011
public HtmlContentBuilder(int capacity) { }
1112
public int Count { get { throw null; } }
12-
public Microsoft.AspNetCore.Html.IHtmlContentBuilder Append(string unencoded) { throw null; }
13-
public Microsoft.AspNetCore.Html.IHtmlContentBuilder AppendHtml(Microsoft.AspNetCore.Html.IHtmlContent htmlContent) { throw null; }
14-
public Microsoft.AspNetCore.Html.IHtmlContentBuilder AppendHtml(string encoded) { throw null; }
13+
public Microsoft.AspNetCore.Html.IHtmlContentBuilder Append(string? unencoded) { throw null; }
14+
public Microsoft.AspNetCore.Html.IHtmlContentBuilder AppendHtml(Microsoft.AspNetCore.Html.IHtmlContent? htmlContent) { throw null; }
15+
public Microsoft.AspNetCore.Html.IHtmlContentBuilder AppendHtml(string? encoded) { throw null; }
1516
public Microsoft.AspNetCore.Html.IHtmlContentBuilder Clear() { throw null; }
1617
public void CopyTo(Microsoft.AspNetCore.Html.IHtmlContentBuilder destination) { }
1718
public void MoveTo(Microsoft.AspNetCore.Html.IHtmlContentBuilder destination) { }
@@ -32,16 +33,16 @@ public static partial class HtmlContentBuilderExtensions
3233
[System.Diagnostics.DebuggerDisplayAttribute("{DebuggerToString()}")]
3334
public partial class HtmlFormattableString : Microsoft.AspNetCore.Html.IHtmlContent
3435
{
35-
public HtmlFormattableString(System.IFormatProvider formatProvider, string format, params object[] args) { }
36+
public HtmlFormattableString(System.IFormatProvider? formatProvider, string format, params object[] args) { }
3637
public HtmlFormattableString(string format, params object[] args) { }
3738
public void WriteTo(System.IO.TextWriter writer, System.Text.Encodings.Web.HtmlEncoder encoder) { }
3839
}
3940
public partial class HtmlString : Microsoft.AspNetCore.Html.IHtmlContent
4041
{
4142
public static readonly Microsoft.AspNetCore.Html.HtmlString Empty;
4243
public static readonly Microsoft.AspNetCore.Html.HtmlString NewLine;
43-
public HtmlString(string value) { }
44-
public string Value { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
44+
public HtmlString(string? value) { }
45+
public string? Value { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
4546
public override string ToString() { throw null; }
4647
public void WriteTo(System.IO.TextWriter writer, System.Text.Encodings.Web.HtmlEncoder encoder) { }
4748
}

src/Html/Abstractions/src/HtmlContentBuilder.cs

+12-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Diagnostics;
67
using System.IO;
78
using System.Text.Encodings.Web;
89

@@ -11,6 +12,7 @@ namespace Microsoft.AspNetCore.Html
1112
/// <summary>
1213
/// An <see cref="IHtmlContentBuilder"/> implementation using an in memory list.
1314
/// </summary>
15+
[DebuggerDisplay("{DebuggerToString()}")]
1416
public class HtmlContentBuilder : IHtmlContentBuilder
1517
{
1618
/// <summary>
@@ -61,7 +63,7 @@ public HtmlContentBuilder(IList<object> entries)
6163
internal IList<object> Entries { get; }
6264

6365
/// <inheritdoc />
64-
public IHtmlContentBuilder Append(string unencoded)
66+
public IHtmlContentBuilder Append(string? unencoded)
6567
{
6668
if (!string.IsNullOrEmpty(unencoded))
6769
{
@@ -72,7 +74,7 @@ public IHtmlContentBuilder Append(string unencoded)
7274
}
7375

7476
/// <inheritdoc />
75-
public IHtmlContentBuilder AppendHtml(IHtmlContent htmlContent)
77+
public IHtmlContentBuilder AppendHtml(IHtmlContent? htmlContent)
7678
{
7779
if (htmlContent == null)
7880
{
@@ -84,7 +86,7 @@ public IHtmlContentBuilder AppendHtml(IHtmlContent htmlContent)
8486
}
8587

8688
/// <inheritdoc />
87-
public IHtmlContentBuilder AppendHtml(string encoded)
89+
public IHtmlContentBuilder AppendHtml(string? encoded)
8890
{
8991
if (!string.IsNullOrEmpty(encoded))
9092
{
@@ -113,13 +115,11 @@ public void CopyTo(IHtmlContentBuilder destination)
113115
{
114116
var entry = Entries[i];
115117

116-
string entryAsString;
117-
IHtmlContentContainer entryAsContainer;
118-
if ((entryAsString = entry as string) != null)
118+
if (entry is string entryAsString)
119119
{
120120
destination.Append(entryAsString);
121121
}
122-
else if ((entryAsContainer = entry as IHtmlContentContainer) != null)
122+
else if (entry is IHtmlContentContainer entryAsContainer)
123123
{
124124
// Since we're copying, do a deep flatten.
125125
entryAsContainer.CopyTo(destination);
@@ -144,13 +144,11 @@ public void MoveTo(IHtmlContentBuilder destination)
144144
{
145145
var entry = Entries[i];
146146

147-
string entryAsString;
148-
IHtmlContentContainer entryAsContainer;
149-
if ((entryAsString = entry as string) != null)
147+
if (entry is string entryAsString)
150148
{
151149
destination.Append(entryAsString);
152150
}
153-
else if ((entryAsContainer = entry as IHtmlContentContainer) != null)
151+
else if (entry is IHtmlContentContainer entryAsContainer)
154152
{
155153
// Since we're moving, do a deep flatten.
156154
entryAsContainer.MoveTo(destination);
@@ -197,11 +195,9 @@ public void WriteTo(TextWriter writer, HtmlEncoder encoder)
197195

198196
private string DebuggerToString()
199197
{
200-
using (var writer = new StringWriter())
201-
{
202-
WriteTo(writer, HtmlEncoder.Default);
203-
return writer.ToString();
204-
}
198+
using var writer = new StringWriter();
199+
WriteTo(writer, HtmlEncoder.Default);
200+
return writer.ToString();
205201
}
206202
}
207203
}

src/Html/Abstractions/src/HtmlFormattableString.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -39,7 +39,7 @@ public HtmlFormattableString(string format, params object[] args)
3939
/// <param name="formatProvider">An object that provides culture-specific formatting information.</param>
4040
/// <param name="format">A composite format string.</param>
4141
/// <param name="args">An array that contains objects to format.</param>
42-
public HtmlFormattableString(IFormatProvider formatProvider, string format, params object[] args)
42+
public HtmlFormattableString(IFormatProvider? formatProvider, string format, params object[] args)
4343
{
4444
if (format == null)
4545
{
@@ -94,7 +94,7 @@ private class EncodingFormatProvider : IFormatProvider, ICustomFormatter
9494
private readonly HtmlEncoder _encoder;
9595
private readonly IFormatProvider _formatProvider;
9696

97-
private StringWriter _writer;
97+
private StringWriter? _writer;
9898

9999
public EncodingFormatProvider(IFormatProvider formatProvider, HtmlEncoder encoder)
100100
{
@@ -105,7 +105,7 @@ public EncodingFormatProvider(IFormatProvider formatProvider, HtmlEncoder encode
105105
_encoder = encoder;
106106
}
107107

108-
public string Format(string format, object arg, IFormatProvider formatProvider)
108+
public string Format(string? format, object? arg, IFormatProvider? formatProvider)
109109
{
110110
// These are the cases we need to special case. We trust the HtmlString or IHtmlContent instance
111111
// to do the right thing with encoding.
@@ -118,7 +118,7 @@ public string Format(string format, object arg, IFormatProvider formatProvider)
118118
var htmlContent = arg as IHtmlContent;
119119
if (htmlContent != null)
120120
{
121-
_writer = _writer ?? new StringWriter();
121+
_writer ??= new StringWriter();
122122

123123
htmlContent.WriteTo(_writer, _encoder);
124124

@@ -133,7 +133,7 @@ public string Format(string format, object arg, IFormatProvider formatProvider)
133133
//
134134
// First check for an ICustomFormatter - if the IFormatProvider is a CultureInfo, then it's likely
135135
// that ICustomFormatter will be null.
136-
var customFormatter = (ICustomFormatter)_formatProvider.GetFormat(typeof(ICustomFormatter));
136+
var customFormatter = (ICustomFormatter?)_formatProvider.GetFormat(typeof(ICustomFormatter));
137137
if (customFormatter != null)
138138
{
139139
var result = customFormatter.Format(format, arg, _formatProvider);
@@ -170,7 +170,7 @@ public string Format(string format, object arg, IFormatProvider formatProvider)
170170
return string.Empty;
171171
}
172172

173-
public object GetFormat(Type formatType)
173+
public object? GetFormat(Type? formatType)
174174
{
175175
if (formatType == typeof(ICustomFormatter))
176176
{

src/Html/Abstractions/src/HtmlString.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public class HtmlString : IHtmlContent
2626
/// Creates a new <see cref="HtmlString"/>.
2727
/// </summary>
2828
/// <param name="value">The HTML encoded value.</param>
29-
public HtmlString(string value)
29+
public HtmlString(string? value)
3030
{
3131
Value = value;
3232
}
3333

3434
/// <summary>
3535
/// Gets the HTML encoded value.
3636
/// </summary>
37-
public string Value { get; }
37+
public string? Value { get; }
3838

3939
/// <inheritdoc />
4040
public void WriteTo(TextWriter writer, HtmlEncoder encoder)

src/Html/Abstractions/src/Microsoft.AspNetCore.Html.Abstractions.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>ASP.NET Core HTML abstractions used for building HTML content.
@@ -11,6 +11,7 @@ Microsoft.AspNetCore.Html.IHtmlContent</Description>
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1212
<PackageTags>aspnetcore</PackageTags>
1313
<IsPackable>false</IsPackable>
14+
<Nullable>enable</Nullable>
1415
</PropertyGroup>
1516

1617
</Project>

src/Html/Abstractions/test/HtmlContentBuilderTest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public override int GetHashCode()
256256
return _content.GetHashCode();
257257
}
258258

259-
public override bool Equals(object obj)
259+
public override bool Equals(object? obj)
260260
{
261261
var other = obj as TestHtmlContent;
262262
if (other != null)
@@ -267,9 +267,9 @@ public override bool Equals(object obj)
267267
return base.Equals(obj);
268268
}
269269

270-
public bool Equals(TestHtmlContent other)
270+
public bool Equals(TestHtmlContent? other)
271271
{
272-
return string.Equals(_content, other._content);
272+
return other != null && string.Equals(_content, other._content);
273273
}
274274
}
275275
}

src/Html/Abstractions/test/Microsoft.AspNetCore.Html.Abstractions.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
5+
<Nullable>enable</Nullable>
56
</PropertyGroup>
67

78
<ItemGroup>

0 commit comments

Comments
 (0)