import { describe, test, expect } from 'vitest'; import { formatHtml } from './format-html'; describe('formatHtml', () => { test('indents nested block elements two spaces per level', () => { expect(formatHtml('
hi
hi
\nhello world now
')).toBe( 'hello world now
', ); }); test('void elements do not open an indent level', () => { expect(formatHtml('
x
\n x
\n contents verbatim', () => {
const src = ' keep\n this
';
expect(formatHtml(src)).toBe('\n keep\n this
\n');
});
test('is idempotent', () => {
const once = formatHtml('hi
');
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('x
')).toBe('\nx
');
});
});
// Regression coverage from code review: only was originally exempted
// from the naive '<'/'>' tag-boundary scan, which let a '>' inside a quoted
// attribute value corrupt output, and let '<'/'>' inside ';
expect(formatHtml(src)).toBe(
'\n \n\n\n \n',
);
});
test('a CSS child combinator inside ';
expect(formatHtml(src)).toBe(
'\n \n',
);
});
test('is idempotent across a quoted ">" attribute and ';
const scripted = formatHtml(scriptSrc);
expect(formatHtml(scripted)).toBe(scripted);
});
test('an unclosed is swallowed verbatim to the end of the document', () => {
expect(formatHtml('no closing tag here')).toBe(
'\n no closing tag here',
);
});
});