Compare HTML Fragments in Rust to See if They Match
I'm working on extracting NeoDoc from my static site generator to pitch the format as an option for the Atmosphere of AT Proto. Part of that process is making an explicit spec for HTML output. The goal being to avoid a Markdown type situation where different parsers have different features that produces different outputs from the same input.
Validating the output is a little tricky. The simplest way would be to just compare to strings.
is
<strong>test</strong>
the same as:
<strong>test</strong>
yes? you're good
The problem is whitespace. Take these two snippets for example:
<div>
<strong>test</strong>
</div>
and
<div>
<strong>test</strong>
</div>
The only difference in them is the
strong tag has a couple spaces
in front of it in the second sample.
Both samples produce the exact same thing in the browser. And, for testing purposes, they should be considered as matching.
But, thanks to the whitespace, the strings are different. So, the test would fail.
I've done simple testing before where I just yoinked all the spaces to collapse everything. That works great, as long as you don't need to run tests that confirm what space that should show up does. NeoDoc has some of those tests.
I'm using a rust crate called scraper to work around that. It parses HTML that can be compared more abstractly.
This is how I'm using it:
Test solution acquired. Back to parsing,
-a
Endnotes
-
This version only checks a single element. If there are multiple sibling elements at the root of the fragment and iterator needs to be set up to go through them.