html-compare-rs
A Rust library for comparing HTML content with configurable comparison options. Useful for testing HTML output while ignoring differences that don't affect the rendered result.
Features
- Compare HTML strings while ignoring unimportant differences
- Configurable comparison options for whitespace, attributes, comments, and more
- Built-in test assertions macros for ergonomic testing
- Handles standard HTML parsing edge cases and malformed HTML
- Clear error messages for debugging differences
Installation
Just cargo add --dev html-compare-rs
or add this to your Cargo.toml
:
[]
= "0.1.0"
Quick Start
use ;
// Basic comparison
assert_html_eq!;
// Custom comparison options
assert_html_eq!;
Usage
Basic Comparison
The library provides both a programmatic API and test assertions:
use ;
// Using the assertion macro (recommended for tests)
assert_html_eq!;
// Using the API directly
let comparer = new;
assert!;
Configuration Options
Control how HTML is compared with HtmlCompareOptions
:
use ;
let options = HtmlCompareOptions ;
assert_html_eq!;
Built-in Presets
Common comparison configurations are available as presets:
use ;
// Relaxed comparison - ignores formatting, attributes, and order
assert_html_eq!;
// Strict comparison - only ignores whitespace
assert_html_eq!;
// Markdown comparison - ignores IDs but preserves other attributes
assert_html_eq!;
Whitespace Handling
By default, the library follows standard HTML whitespace rules:
- Multiple spaces in text content are collapsed into a single space
- Whitespace between elements is ignored when
ignore_whitespace
is true - Leading and trailing whitespace in text content is trimmed
assert_html_eq!;
assert_html_eq!;
Error Messages
When comparisons fail, detailed error messages help identify the differences:
use assert_html_eq;
// This will panic with a detailed error message:
assert_html_eq!;
// Error:
// HTML comparison failed:
// Node mismatch: Attributes mismatch...
//
// left HTML:
// <div class='test'>Content</div>
//
// right HTML:
// <div class='different'>Content</div>
//
// options: ...
License
This project is licensed under the MIT License - see the LICENSE file for details.