Skip to content

Commit 9db535c

Browse files
fix formatting when there is content inside of multiple tight tags (#68)
* properly indent content inside multiple tight tags * all tests passing again
1 parent 48c3d22 commit 9db535c

File tree

1 file changed

+36
-3
lines changed

1 file changed

+36
-3
lines changed

src/html/Ast.zig

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -992,9 +992,14 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
992992
}
993993
}
994994

995+
const child_is_vertical = if (ast.child(current)) |c|
996+
(c.kind == .text or c.open.start - current.open.end > 0)
997+
else
998+
false;
995999
if (!current.self_closing and
9961000
current.kind.isElement() and
997-
!current.kind.isVoid())
1001+
!current.kind.isVoid() and
1002+
child_is_vertical)
9981003
{
9991004
indentation += 1;
10001005
}
@@ -1017,9 +1022,14 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
10171022
current,
10181023
});
10191024

1025+
const child_was_vertical = if (ast.child(current)) |c|
1026+
(c.kind == .text or c.open.start - current.open.end > 0)
1027+
else
1028+
false;
10201029
if (!current.self_closing and
10211030
current.kind.isElement() and
1022-
!current.kind.isVoid())
1031+
!current.kind.isVoid() and
1032+
child_was_vertical)
10231033
{
10241034
indentation -= 1;
10251035
}
@@ -1197,7 +1207,11 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
11971207

11981208
// if (std.mem.eql(u8, name, "path")) @breakpoint();
11991209

1200-
const attr_indent = indentation - @intFromBool(!current.kind.isVoid() and !current.self_closing);
1210+
const child_is_vertical = if (ast.child(current)) |c|
1211+
(c.kind == .text or c.open.start - current.open.end > 0)
1212+
else
1213+
false;
1214+
const attr_indent = indentation - @intFromBool(!current.kind.isVoid() and !current.self_closing and child_is_vertical);
12011215
const extra = blk: {
12021216
if (current.kind == .doctype) break :blk 1;
12031217
assert(current.kind.isElement());
@@ -1650,6 +1664,25 @@ test "newlines" {
16501664
try std.testing.expectFmt(expected, "{f}", .{ast.formatter(case)});
16511665
}
16521666

1667+
test "tight tags inner indentation" {
1668+
const case = comptime std.fmt.comptimePrint(
1669+
\\<!DOCTYPE html>
1670+
\\<html>
1671+
\\{0c}<head></head>
1672+
\\{0c}<body>
1673+
\\{0c}{0c}<div><nav><ul>
1674+
\\{0c}{0c}{0c}<li></li>
1675+
\\{0c}{0c}</ul></nav></div>
1676+
\\{0c}</body>
1677+
\\</html>
1678+
\\
1679+
, .{'\t'});
1680+
const ast = try Ast.init(std.testing.allocator, case, .html, true);
1681+
defer ast.deinit(std.testing.allocator);
1682+
1683+
try std.testing.expectFmt(case, "{f}", .{ast.formatter(case)});
1684+
}
1685+
16531686
test "bad html" {
16541687
// TODO: handle ast.errors.len != 0
16551688
if (true) return error.SkipZigTest;

0 commit comments

Comments
 (0)