Add nullability to HtmlAbstractions (#22275)

Addresses #5680
This commit is contained in:
Pranav K 2020-06-02 16:27:54 -07:00 committed by GitHub
parent 8009350176
commit faf60675fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 35 additions and 35 deletions

View File

@ -2,6 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(DefaultNetCoreTargetFramework)</TargetFrameworks>
<Nullable>annotations</Nullable>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == '$(DefaultNetCoreTargetFramework)'">
<Compile Include="Microsoft.AspNetCore.Html.Abstractions.netcoreapp.cs" />

View File

@ -3,15 +3,16 @@
namespace Microsoft.AspNetCore.Html
{
[System.Diagnostics.DebuggerDisplayAttribute("{DebuggerToString()}")]
public partial class HtmlContentBuilder : Microsoft.AspNetCore.Html.IHtmlContent, Microsoft.AspNetCore.Html.IHtmlContentBuilder, Microsoft.AspNetCore.Html.IHtmlContentContainer
{
public HtmlContentBuilder() { }
public HtmlContentBuilder(System.Collections.Generic.IList<object> entries) { }
public HtmlContentBuilder(int capacity) { }
public int Count { get { throw null; } }
public Microsoft.AspNetCore.Html.IHtmlContentBuilder Append(string unencoded) { throw null; }
public Microsoft.AspNetCore.Html.IHtmlContentBuilder AppendHtml(Microsoft.AspNetCore.Html.IHtmlContent htmlContent) { throw null; }
public Microsoft.AspNetCore.Html.IHtmlContentBuilder AppendHtml(string encoded) { throw null; }
public Microsoft.AspNetCore.Html.IHtmlContentBuilder Append(string? unencoded) { throw null; }
public Microsoft.AspNetCore.Html.IHtmlContentBuilder AppendHtml(Microsoft.AspNetCore.Html.IHtmlContent? htmlContent) { throw null; }
public Microsoft.AspNetCore.Html.IHtmlContentBuilder AppendHtml(string? encoded) { throw null; }
public Microsoft.AspNetCore.Html.IHtmlContentBuilder Clear() { throw null; }
public void CopyTo(Microsoft.AspNetCore.Html.IHtmlContentBuilder destination) { }
public void MoveTo(Microsoft.AspNetCore.Html.IHtmlContentBuilder destination) { }
@ -32,7 +33,7 @@ namespace Microsoft.AspNetCore.Html
[System.Diagnostics.DebuggerDisplayAttribute("{DebuggerToString()}")]
public partial class HtmlFormattableString : Microsoft.AspNetCore.Html.IHtmlContent
{
public HtmlFormattableString(System.IFormatProvider formatProvider, string format, params object[] args) { }
public HtmlFormattableString(System.IFormatProvider? formatProvider, string format, params object[] args) { }
public HtmlFormattableString(string format, params object[] args) { }
public void WriteTo(System.IO.TextWriter writer, System.Text.Encodings.Web.HtmlEncoder encoder) { }
}
@ -40,8 +41,8 @@ namespace Microsoft.AspNetCore.Html
{
public static readonly Microsoft.AspNetCore.Html.HtmlString Empty;
public static readonly Microsoft.AspNetCore.Html.HtmlString NewLine;
public HtmlString(string value) { }
public string Value { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
public HtmlString(string? value) { }
public string? Value { [System.Runtime.CompilerServices.CompilerGeneratedAttribute] get { throw null; } }
public override string ToString() { throw null; }
public void WriteTo(System.IO.TextWriter writer, System.Text.Encodings.Web.HtmlEncoder encoder) { }
}

View File

@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Encodings.Web;
@ -11,6 +12,7 @@ namespace Microsoft.AspNetCore.Html
/// <summary>
/// An <see cref="IHtmlContentBuilder"/> implementation using an in memory list.
/// </summary>
[DebuggerDisplay("{DebuggerToString()}")]
public class HtmlContentBuilder : IHtmlContentBuilder
{
/// <summary>
@ -61,7 +63,7 @@ namespace Microsoft.AspNetCore.Html
internal IList<object> Entries { get; }
/// <inheritdoc />
public IHtmlContentBuilder Append(string unencoded)
public IHtmlContentBuilder Append(string? unencoded)
{
if (!string.IsNullOrEmpty(unencoded))
{
@ -72,7 +74,7 @@ namespace Microsoft.AspNetCore.Html
}
/// <inheritdoc />
public IHtmlContentBuilder AppendHtml(IHtmlContent htmlContent)
public IHtmlContentBuilder AppendHtml(IHtmlContent? htmlContent)
{
if (htmlContent == null)
{
@ -84,7 +86,7 @@ namespace Microsoft.AspNetCore.Html
}
/// <inheritdoc />
public IHtmlContentBuilder AppendHtml(string encoded)
public IHtmlContentBuilder AppendHtml(string? encoded)
{
if (!string.IsNullOrEmpty(encoded))
{
@ -113,13 +115,11 @@ namespace Microsoft.AspNetCore.Html
{
var entry = Entries[i];
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)
{
// Since we're copying, do a deep flatten.
entryAsContainer.CopyTo(destination);
@ -144,13 +144,11 @@ namespace Microsoft.AspNetCore.Html
{
var entry = Entries[i];
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)
{
// Since we're moving, do a deep flatten.
entryAsContainer.MoveTo(destination);
@ -197,11 +195,9 @@ namespace Microsoft.AspNetCore.Html
private string DebuggerToString()
{
using (var writer = new StringWriter())
{
WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
using var writer = new StringWriter();
WriteTo(writer, HtmlEncoder.Default);
return writer.ToString();
}
}
}

View File

@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.Html
/// <param name="formatProvider">An object that provides culture-specific formatting information.</param>
/// <param name="format">A composite format string.</param>
/// <param name="args">An array that contains objects to format.</param>
public HtmlFormattableString(IFormatProvider formatProvider, string format, params object[] args)
public HtmlFormattableString(IFormatProvider? formatProvider, string format, params object[] args)
{
if (format == null)
{
@ -94,7 +94,7 @@ namespace Microsoft.AspNetCore.Html
private readonly HtmlEncoder _encoder;
private readonly IFormatProvider _formatProvider;
private StringWriter _writer;
private StringWriter? _writer;
public EncodingFormatProvider(IFormatProvider formatProvider, HtmlEncoder encoder)
{
@ -105,7 +105,7 @@ namespace Microsoft.AspNetCore.Html
_encoder = encoder;
}
public string Format(string format, object arg, IFormatProvider formatProvider)
public string Format(string? format, object? arg, IFormatProvider? formatProvider)
{
// These are the cases we need to special case. We trust the HtmlString or IHtmlContent instance
// to do the right thing with encoding.
@ -118,7 +118,7 @@ namespace Microsoft.AspNetCore.Html
var htmlContent = arg as IHtmlContent;
if (htmlContent != null)
{
_writer = _writer ?? new StringWriter();
_writer ??= new StringWriter();
htmlContent.WriteTo(_writer, _encoder);
@ -133,7 +133,7 @@ namespace Microsoft.AspNetCore.Html
//
// First check for an ICustomFormatter - if the IFormatProvider is a CultureInfo, then it's likely
// that ICustomFormatter will be null.
var customFormatter = (ICustomFormatter)_formatProvider.GetFormat(typeof(ICustomFormatter));
var customFormatter = (ICustomFormatter?)_formatProvider.GetFormat(typeof(ICustomFormatter));
if (customFormatter != null)
{
var result = customFormatter.Format(format, arg, _formatProvider);
@ -170,7 +170,7 @@ namespace Microsoft.AspNetCore.Html
return string.Empty;
}
public object GetFormat(Type formatType)
public object? GetFormat(Type? formatType)
{
if (formatType == typeof(ICustomFormatter))
{

View File

@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Html
/// Creates a new <see cref="HtmlString"/>.
/// </summary>
/// <param name="value">The HTML encoded value.</param>
public HtmlString(string value)
public HtmlString(string? value)
{
Value = value;
}
@ -34,7 +34,7 @@ namespace Microsoft.AspNetCore.Html
/// <summary>
/// Gets the HTML encoded value.
/// </summary>
public string Value { get; }
public string? Value { get; }
/// <inheritdoc />
public void WriteTo(TextWriter writer, HtmlEncoder encoder)

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>ASP.NET Core HTML abstractions used for building HTML content.
@ -11,6 +11,7 @@ Microsoft.AspNetCore.Html.IHtmlContent</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -256,7 +256,7 @@ namespace Microsoft.Extensions.Internal
return _content.GetHashCode();
}
public override bool Equals(object obj)
public override bool Equals(object? obj)
{
var other = obj as TestHtmlContent;
if (other != null)
@ -267,9 +267,9 @@ namespace Microsoft.Extensions.Internal
return base.Equals(obj);
}
public bool Equals(TestHtmlContent other)
public bool Equals(TestHtmlContent? other)
{
return string.Equals(_content, other._content);
return other != null && string.Equals(_content, other._content);
}
}
}

View File

@ -2,6 +2,7 @@
<PropertyGroup>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>