Skip to content

Add Nullable Reference Type Annotations to HtmlAgilityPack.Shared #596

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

r-Larch
Copy link

@r-Larch r-Larch commented Jun 19, 2025

This PR introduces full nullable reference type annotations to the HtmlAgilityPack.Shared project.

  • All public APIs are now annotated for nullability.
  • The existing code logic remains unchanged.
  • The null-forgiving operator (!) is used selectively where null-safety can be reasonably assumed.
  • Some nullable warnings still remain, as resolving them would require modifying the code logic.

To support nullable annotations in older target frameworks like .NET Framework and netstandard, the Nullable NuGet package is required.

I've already added this package to two projects for testing:

  • HtmlAgilityPack.NetStandard2_0
  • HtmlAgilityPack.Net35

However, I couldn't update all target projects due to missing SDKs. For the remaining ones, please run:

Install-Package Nullable

Feel free to merge or close.
But I hope we get this merged so feel free to suggest changes.


Null warnings not fixed:
...\HtmlNodeNavigator.cs(450,4,450,7): warning CS8764: Nullability of return type doesn't match overridden member (possibly because of nullability attributes).
...\HtmlNodeNavigator.cs(532,27,532,39): warning CS8764: Nullability of return type doesn't match overridden member (possibly because of nullability attributes).
...\IOLibrary.cs(21,39,21,68): warning CS8604: Possible null reference argument for parameter 'path' in 'DirectoryInfo Directory.CreateDirectory(string path)'.
...\HtmlNode.Encapsulator.cs(596,20,596,74): warning CS8603: Possible null reference return.
...\HtmlDocument.cs(381,27,381,72): warning CS8602: Dereference of a possibly null reference.
...\HtmlAttribute.cs(201,30,201,49): warning CS8602: Dereference of a possibly null reference.
...\HtmlAttribute.cs(289,24,289,36): warning CS8601: Possible null reference assignment.
...\HtmlAttribute.cs(302,13,302,23): warning CS8602: Dereference of a possibly null reference.
...\HtmlWeb.cs(1190,42,1190,52): warning CS8604: Possible null reference argument for parameter 'path1' in 'string Path.Combine(string path1, string path2)'.
...\HtmlWeb.cs(1206,46,1206,56): warning CS8604: Possible null reference argument for parameter 'path1' in 'string Path.Combine(string path1, string path2)'.
...\HtmlWeb.cs(1210,46,1210,56): warning CS8604: Possible null reference argument for parameter 'path1' in 'string Path.Combine(string path1, string path2)'.
...\HtmlNode.cs(504,9,504,19): warning CS8602: Dereference of a possibly null reference.
...\HtmlWeb.cs(1539,47,1539,50): warning CS8604: Possible null reference argument for parameter 'path' in 'DirectoryInfo Directory.CreateDirectory(string path)'.
...\HtmlNode.cs(821,11,821,38): warning CS8603: Possible null reference return.
...\HtmlDocument.cs(990,39,990,65): warning CS8601: Possible null reference assignment.
...\HtmlDocument.cs(1117,52,1117,74): warning CS8601: Possible null reference assignment.
...\HtmlNode.cs(1105,17,1105,29): warning CS8604: Possible null reference argument for parameter 'value' in 'void HtmlNode.SetName(string value)'.
...\HtmlNode.cs(1132,35,1132,55): warning CS8602: Dereference of a possibly null reference.
...\HtmlNode.cs(1135,6,1135,31): warning CS8602: Dereference of a possibly null reference.
...\HtmlWeb.cs(1705,79,1705,88): warning CS8604: Possible null reference argument for parameter 'path' in 'DateTime File.GetLastWriteTime(string path)'.
...\HtmlWeb.cs(1819,25,1819,28): warning CS8602: Dereference of a possibly null reference.
...\HtmlWeb.cs(1753,30,1753,31): warning CS0168: The variable 'e' is declared but never used
...\HtmlWeb.cs(2117,20,2117,35): warning CS8602: Dereference of a possibly null reference.
...\HtmlWeb.cs(2117,20,2117,41): warning CS8602: Dereference of a possibly null reference.
...\HtmlWeb.cs(2130,34,2130,36): warning CS8604: Possible null reference argument for parameter 'contentType' in 'bool HtmlWeb.IsHtmlContent(string contentType)'.
...\HtmlWeb.cs(2189,17,2189,33): warning CS8602: Dereference of a possibly null reference.
...\HtmlWeb.cs(2195,17,2195,22): warning CS8602: Dereference of a possibly null reference.
...\HtmlNode.cs(1375,14,1375,30): warning CS8604: Possible null reference argument for parameter 'item' in 'void List<HtmlAttribute>.Add(HtmlAttribute item)'.
...\HtmlNode.cs(2078,14,2078,26): warning CS8600: Converting null literal or possible null value to non-nullable type.
...\HtmlNode.cs(2082,11,2082,15): warning CS8602: Dereference of a possibly null reference.
...\HtmlNode.cs(2214,14,2214,26): warning CS8600: Converting null literal or possible null value to non-nullable type.
...\HtmlNode.cs(2216,31,2216,35): warning CS8604: Possible null reference argument for parameter 'localName' in 'void XmlWriter.WriteStartElement(string localName)'.
...\HtmlNode.cs(2445,13,2445,29): warning CS8600: Converting null literal or possible null value to non-nullable type.
...\HtmlNode.cs(2447,99,2447,111): warning CS8604: Possible null reference argument for parameter 'html' in 'string HtmlDocument.HtmlEncodeWithCompatibility(string html, bool backwardCompatibility = true)'.
...\HtmlNode.cs(2465,13,2465,29): warning CS8600: Converting null literal or possible null value to non-nullable type.
...\HtmlNode.cs(2792,22,2792,31): warning CS8602: Dereference of a possibly null reference.
...\HtmlNode.Encapsulator.cs(146,81,146,95): warning CS8602: Dereference of a possibly null reference.
...\HtmlNode.Encapsulator.cs(267,86,267,100): warning CS8602: Dereference of a possibly null reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant