Skip to content

Add nullability to HtmlAbstractions #22275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand All @@ -32,16 +33,16 @@ public static partial class HtmlContentBuilderExtensions
[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) { }
}
public partial class HtmlString : Microsoft.AspNetCore.Html.IHtmlContent
{
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) { }
}
Expand Down
28 changes: 12 additions & 16 deletions src/Html/Abstractions/src/HtmlContentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Encodings.Web;

Expand All @@ -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>
Expand Down Expand Up @@ -61,7 +63,7 @@ public HtmlContentBuilder(IList<object> entries)
internal IList<object> Entries { get; }

/// <inheritdoc />
public IHtmlContentBuilder Append(string unencoded)
public IHtmlContentBuilder Append(string? unencoded)
{
if (!string.IsNullOrEmpty(unencoded))
{
Expand All @@ -72,7 +74,7 @@ public IHtmlContentBuilder Append(string unencoded)
}

/// <inheritdoc />
public IHtmlContentBuilder AppendHtml(IHtmlContent htmlContent)
public IHtmlContentBuilder AppendHtml(IHtmlContent? htmlContent)
{
if (htmlContent == null)
{
Expand All @@ -84,7 +86,7 @@ public IHtmlContentBuilder AppendHtml(IHtmlContent htmlContent)
}

/// <inheritdoc />
public IHtmlContentBuilder AppendHtml(string encoded)
public IHtmlContentBuilder AppendHtml(string? encoded)
{
if (!string.IsNullOrEmpty(encoded))
{
Expand Down Expand Up @@ -113,13 +115,11 @@ public void CopyTo(IHtmlContentBuilder destination)
{
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);
Expand All @@ -144,13 +144,11 @@ public void MoveTo(IHtmlContentBuilder destination)
{
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

booooooooooo......... My jank-mode code from the 70s should live on.

else if (entry is IHtmlContentContainer entryAsContainer)
{
// Since we're moving, do a deep flatten.
entryAsContainer.MoveTo(destination);
Expand Down Expand Up @@ -197,11 +195,9 @@ public void WriteTo(TextWriter writer, HtmlEncoder encoder)

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();
}
}
}
14 changes: 7 additions & 7 deletions src/Html/Abstractions/src/HtmlFormattableString.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,7 +39,7 @@ public HtmlFormattableString(string format, params object[] args)
/// <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)
{
Expand Down Expand Up @@ -94,7 +94,7 @@ private class EncodingFormatProvider : IFormatProvider, ICustomFormatter
private readonly HtmlEncoder _encoder;
private readonly IFormatProvider _formatProvider;

private StringWriter _writer;
private StringWriter? _writer;

public EncodingFormatProvider(IFormatProvider formatProvider, HtmlEncoder encoder)
{
Expand All @@ -105,7 +105,7 @@ public EncodingFormatProvider(IFormatProvider formatProvider, HtmlEncoder encode
_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.
Expand All @@ -118,7 +118,7 @@ public string Format(string format, object arg, IFormatProvider formatProvider)
var htmlContent = arg as IHtmlContent;
if (htmlContent != null)
{
_writer = _writer ?? new StringWriter();
_writer ??= new StringWriter();

htmlContent.WriteTo(_writer, _encoder);

Expand All @@ -133,7 +133,7 @@ public string Format(string format, object arg, IFormatProvider formatProvider)
//
// 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);
Expand Down Expand Up @@ -170,7 +170,7 @@ public string Format(string format, object arg, IFormatProvider formatProvider)
return string.Empty;
}

public object GetFormat(Type formatType)
public object? GetFormat(Type? formatType)
{
if (formatType == typeof(ICustomFormatter))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Html/Abstractions/src/HtmlString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public class HtmlString : IHtmlContent
/// 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;
}

/// <summary>
/// Gets the HTML encoded value.
/// </summary>
public string Value { get; }
public string? Value { get; }

/// <inheritdoc />
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,6 +11,7 @@ Microsoft.AspNetCore.Html.IHtmlContent</Description>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageTags>aspnetcore</PackageTags>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
6 changes: 3 additions & 3 deletions src/Html/Abstractions/test/HtmlContentBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public override int GetHashCode()
return _content.GetHashCode();
}

public override bool Equals(object obj)
public override bool Equals(object? obj)
{
var other = obj as TestHtmlContent;
if (other != null)
Expand All @@ -267,9 +267,9 @@ public override bool Equals(object obj)
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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down