All files / src/template/utility standardize-indentation.ts

5% Statements 1/20
0% Branches 0/16
0% Functions 0/2
5% Lines 1/20

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                                                                          2x  
function standardizeIndentation(html: string): string {
	const original_lines = html.split('\n');
	const indented_lines: string[] = [];
 
	let indentation = '';
 
	original_lines.forEach((original_line) => {
		const trimmed_line = original_line.trim();
 
		if (trimmed_line.length === 0) {
			return;
		}
 
		if (trimmed_line === '<br>' || trimmed_line === '<hr>') {
			throw new Error('Specify void elements as self-closing');
		}
 
		const has_open_tag = /^<[a-z]/.test(trimmed_line);
		const has_close_tag = /<\/[^>]+>$/.test(trimmed_line);
		const is_self_closing = /^<[a-z].*\/>$/.test(trimmed_line);
 
		if (!has_open_tag && has_close_tag && !is_self_closing) {
			indentation = indentation.slice(0, -1);
		}
 
		const indented_line = `${indentation}${trimmed_line}`;
 
		indented_lines.push(indented_line);
 
		if (has_open_tag && !has_close_tag && !is_self_closing) {
			indentation += '\t';
		}
	});
 
	return indented_lines.join('\n');
}
 
export default standardizeIndentation;