From f288ed6d4c783299a86e84e3c02304b93f5e4cad Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 3 Jul 2026 22:37:57 +1000 Subject: [PATCH] Fix NullReferenceException comparing markup with style attributes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix NullReferenceException comparing markup with style attributes BunitMarkupComparer parsed markup with a CSS-unaware HtmlParser, so an element's inline style parsed to a null ICssStyleDeclaration. AngleSharp .Diffing's StyleAttributeComparer compares style attributes semantically, so it dereferenced that null and threw the moment two style attributes differed — surfacing as an NRE instead of a diff on any HTML snapshot whose elements carry differing styles. Parse through a CSS-enabled configuration (Configuration.Default.WithCss()) so every element gets a real CSS object model and the style comparison works. AngleSharp.Css is already present transitively via bunit. Add a regression test covering two markup strings that differ only in style-attribute whitespace, which forces the comparer through the CSS parse that previously crashed. --- readme.md | 4 ++-- src/Directory.Build.props | 2 +- ...ts.StyledElement_SemanticallyEqual.verified.html | 1 + src/Tests/HtmlComparerTests.cs | 13 +++++++++++++ src/Tests/ModuleInitializer.cs | 4 +--- src/Verify.Bunit/BunitMarkupComparer.cs | 9 ++++++++- 6 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 src/Tests/HtmlComparerTests.StyledElement_SemanticallyEqual.verified.html create mode 100644 src/Tests/HtmlComparerTests.cs diff --git a/readme.md b/readme.md index 6c2eb87..ddad603 100644 --- a/readme.md +++ b/readme.md @@ -80,7 +80,7 @@ Enable at startup: public static void Initialize() => VerifyBunit.Initialize(); ``` -snippet source | anchor +snippet source | anchor This test: @@ -245,7 +245,7 @@ VerifierSettings.ScrubLinesWithReplace( HtmlPrettyPrint.All(); VerifierSettings.ScrubLinesContaining("