normalizeHtml function

String normalizeHtml(
  1. String html
)

Normalizes rendered HTML for stable snapshot comparison.

Parses the HTML string through package:html and re-serializes it. This produces consistent whitespace and attribute ordering, preventing spurious snapshot failures from insignificant formatting differences.

Trailing newline is appended if not present, ensuring consistent file endings.

Implementation

String normalizeHtml(String html) {
  final doc = html_parser.parseFragment(html);
  var normalized = doc.outerHtml;
  if (!normalized.endsWith('\n')) {
    normalized = '$normalized\n';
  }
  return normalized;
}