Skip to content

About

Extends Verify to allow comparison of html files via AngleSharp.

Resources

Code of conduct

Stars

6 stars

Watchers

1 watching

Forks

Latest commit

 

History

1,326 Commits

Folders and files

Repository files navigation

Verify.AngleSharp

Discussions Build status NuGet Status

Extends Verify with Html verification utilities via AngleSharp.

See Milestones for release notes.

Sponsors

Entity Framework Extensions

Entity Framework Extensions is a major sponsor and is proud to contribute to the development this project.

Entity Framework Extensions

Developed using JetBrains IDEs

JetBrains logo.

NuGet

Comparer Usage

Extends Verify to allow comparison of htm and html files via AngleSharp.

Initialize

Call VerifyAngleSharpDiffing.Initialize() once at assembly load time.

Initialize takes an optional Action to control settings at a global level:

[ModuleInitializer]
public static void Init() =>
    VerifyAngleSharpDiffing.Initialize();

snippet source | anchor

Verify html

Given an existing verified file:

>
<html>
<body>
  <h1>My First Headingh1>
  <p>My first paragraph.p>
body>
html>

snippet source | anchor

And a test:

[Test]
public Task Sample()
{
    var html =
        """
        
        
          
            

My First Heading

My first paragraph.

"""; return Verify(html, "html"); }

snippet source | anchor

Note that the input html differs from the verified html, but not in a semantically significant way. Hence this test will pass.

Diff results

If the comparison fails, the resulting differences will be included in the test result displayed to the user.

For example if, in the above html,

My First Heading

changes to

First Heading

then the following will be printed in the test results:

Comparer result:
 * Node Diff
   Path: h1(0) > #text(0)
   Received: First Heading
   Verified: My First Heading

Test level settings

Settings can also be controlled for a specific test.

var settings = new VerifySettings();
settings.AngleSharpDiffingSettings(
    action =>
    {
        static FilterDecision SpanFilter(
            in ComparisonSource source,
            FilterDecision decision)
        {
            if (source.Node.NodeName == "SPAN")
            {
                return FilterDecision.Exclude;
            }

            return decision;
        }

        var options = action.AddDefaultOptions();
        options.AddFilter(SpanFilter);
    });

snippet source | anchor

Global settings

VerifyAngleSharpDiffing.Initialize(
    action =>
    {
        static FilterDecision SpanFilter(
            in ComparisonSource source,
            FilterDecision decision)
        {
            if (source.Node.NodeName == "SPAN")
            {
                return FilterDecision.Exclude;
            }

            return decision;
        }

        var options = action.AddDefaultOptions();
        options.AddFilter(SpanFilter);
    });

snippet source | anchor

Verify svg

Given an existing verified file:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
     width="300" height="200">
  <rect fill="red" width="100%" height="100%" />
  <circle fill="green" cx="150" cy="100" r="80" />
  <text fill="white"
        x="150" y="125"
        font-size="60"
        text-anchor="middle">
  SVG
  text>
svg>

snippet source | anchor

And a test:

[Test]
public Task SvgSample()
{
    var svg =
        """
        
             width="300" height="200"
             xmlns="http://www.w3.org/2000/svg">
          
          
          
                font-size="60"
                text-anchor="middle"
                fill="white">
          SVG
          
        
        """;
    return Verify(svg, "svg");
}

snippet source | anchor

Note that the input svg differs from the verified svg, but not in a semantically significant way. Hence this test will pass.

Pretty Print

Html can be pretty printed.

[Test]
public Task PrettyPrintHtml()
{
    var html = """
               
               

My First Heading

My first paragraph.

"""; return Verify(html, "html") .PrettyPrintHtml(); }

snippet source | anchor

Results in

>
<html>
  <head>head>
  <body>
    <h1>My First Headingh1>
    <p>My first paragraph.p>
  body>
html>

snippet source | anchor

To apply this to all html files use HtmlPrettyPrint.All();

Manipulate Html

Nodes can be manipulated as part of the pretty print:

[Test]
public Task PrettyPrintHtmlWithNodeManipulation()
{
    var html = """
               
               
                 
                   

My First Heading

My first paragraph.

"""; return Verify(html, "html") .PrettyPrintHtml( nodes => { foreach (var node in nodes.QuerySelectorAll("h1")) { node.Remove(); } }); }

snippet source | anchor

Results in

>
<html>
  <head>head>
  <body>
    <p>My first paragraph.p>
  body>
html>

snippet source | anchor

AngleSharp helpers

ScrubEmptyDivs

[Test]
public Task ScrubEmptyDivs()
{
    var html = """
               
               
                 
                   
My First Heading
"""; return Verify(html, "html") .PrettyPrintHtml(nodes => nodes.ScrubEmptyDivs()); }

snippet source | anchor

Results in:

>
<html>
  <head>head>
  <body>
    <div>My First Headingdiv>
  body>
html>

snippet source | anchor

ScrubAttributes

Removal

[Test]
public Task ScrubAttributes()
{
    var html = """
               
               
                 
                   
My First Heading
"""; return Verify(html, "html") .PrettyPrintHtml(nodes => nodes.ScrubAttributes("id")); }

snippet source | anchor

Results in:

>
<html>
  <head>head>
  <body>
    <div>My First Headingdiv>
  body>
html>

snippet source | anchor

Replace Value

[Test]
public Task ScrubAttributeWithNewValue()
{
    var html = """
               
               
                 
                   
My First Heading
"""; return Verify(html, "html") .PrettyPrintHtml( nodes => nodes.ScrubAttributes(x => { if (x.Name == "id") { return "new value"; } return null; })); }

snippet source | anchor

Results in:

>
<html>
  <head>head>
  <body>
    <div id="new value">My First Headingdiv>
  body>
html>

snippet source | anchor

ScrubAspCacheBusterTagHelper

[Test]
public Task ScrubAspCacheBusterTagHelper()
{
    var html = """
               
               
                 
                   
                    href="/css/site.css?v=r2K1aJs2_7mdAedOAb0OQw3K46ElgPZWqeuI"
                    rel="stylesheet"
                    />
                 
                 
                   

My Heading

"""; return Verify(html, "html") .PrettyPrintHtml(nodes => nodes.ScrubAspCacheBusterTagHelper()); }

snippet source | anchor

Results in:

>
<html>
  <head>
    <link href="/css/site.css?v={TAG_HELPER_VERSION}" rel="stylesheet">
  head>
  <body>
    <h1>My Headingh1>
  body>
html>

snippet source | anchor

ScrubBrowserLink

[Test]
public Task ScrubBrowserLink()
{
    var html = """
               
               
                 
                   
                 
                 
                   

My Heading

"""; return Verify(html, "html") .PrettyPrintHtml(nodes => nodes.ScrubBrowserLink()); }

snippet source | anchor

Results in:

>
<html>
  <head>
    <meta charset="utf-8">
  head>
  <body>
    <h1>My Headingh1>
  body>
html>

snippet source | anchor

About

Extends Verify to allow comparison of html files via AngleSharp.

Resources

Code of conduct

Stars

6 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages