Files
site-builder/craft/src/utils/format-html.test.ts
T

42 lines
1.4 KiB
TypeScript
Raw Normal View History

import { describe, test, expect } from 'vitest';
import { formatHtml } from './format-html';
describe('formatHtml', () => {
test('indents nested block elements two spaces per level', () => {
expect(formatHtml('<div><section><p>hi</p></section></div>')).toBe(
'<div>\n <section>\n <p>hi</p>\n </section>\n</div>',
);
});
test('leaves inline tags on the same line as their text', () => {
expect(formatHtml('<p>hello <strong>world</strong> now</p>')).toBe(
'<p>hello <strong>world</strong> now</p>',
);
});
test('void elements do not open an indent level', () => {
expect(formatHtml('<div><img src="a.png"><br><p>x</p></div>')).toBe(
'<div>\n <img src="a.png">\n <br>\n <p>x</p>\n</div>',
);
});
test('preserves <pre> contents verbatim', () => {
const src = '<div><pre> keep\n this</pre></div>';
expect(formatHtml(src)).toBe('<div>\n <pre> keep\n this</pre>\n</div>');
});
test('is idempotent', () => {
const once = formatHtml('<div><section><p>hi</p></section></div>');
expect(formatHtml(once)).toBe(once);
});
test('empty and whitespace-only input round-trip to an empty string', () => {
expect(formatHtml('')).toBe('');
expect(formatHtml(' \n ')).toBe('');
});
test('unbalanced markup never produces negative indent', () => {
expect(formatHtml('</div><p>x</p>')).toBe('</div>\n<p>x</p>');
});
});