Skip to content

Commit 2d31eed

Browse files
committed
don't force newlines for elements surrounded by text
closes #132
1 parent afbc891 commit 2d31eed

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

src/html/Ast.zig

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
10001000
var indentation: u32 = 0;
10011001
var current = ast.nodes[1];
10021002
var direction: enum { enter, exit } = .enter;
1003+
var first_child = true;
10031004
var last_rbracket: u32 = 0;
10041005
var last_was_text = false;
10051006
var pre: u32 = 0;
@@ -1030,8 +1031,13 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
10301031
} else {
10311032
const vertical = if (last_was_text and current.kind != .text)
10321033
std.mem.indexOfScalar(u8, maybe_ws, '\n') != null
1033-
else
1034-
maybe_ws.len > 0;
1034+
else switch (current.kind) {
1035+
.text => if (first_child)
1036+
maybe_ws.len > 0
1037+
else
1038+
std.mem.indexOfScalar(u8, maybe_ws, '\n') != null,
1039+
else => maybe_ws.len > 0,
1040+
};
10351041

10361042
if (vertical) {
10371043
fmtlog.debug("adding a newline", .{});
@@ -1311,7 +1317,9 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
13111317
}
13121318
},
13131319
}
1320+
13141321
last_rbracket = current.open.end;
1322+
first_child = false;
13151323

13161324
if (current.next_idx != 0) {
13171325
fmtlog.debug("text next: {}", .{current.next_idx});
@@ -1334,6 +1342,7 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
13341342
));
13351343

13361344
last_rbracket = current.open.end;
1345+
first_child = false;
13371346

13381347
if (current.next_idx != 0) {
13391348
current = ast.nodes[current.next_idx];
@@ -1376,6 +1385,7 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
13761385
try w.print(">", .{});
13771386
}
13781387

1388+
first_child = false;
13791389
if (current.next_idx != 0) {
13801390
current = ast.nodes[current.next_idx];
13811391
} else {
@@ -1476,6 +1486,7 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
14761486
assert(current.kind.isElement());
14771487

14781488
if (current.self_closing or current.kind.isVoid()) {
1489+
first_child = false;
14791490
if (current.next_idx != 0) {
14801491
current = ast.nodes[current.next_idx];
14811492
} else {
@@ -1484,8 +1495,10 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
14841495
}
14851496
} else {
14861497
if (current.first_child_idx == 0) {
1498+
first_child = false;
14871499
direction = .exit;
14881500
} else {
1501+
first_child = true;
14891502
current = ast.nodes[current.first_child_idx];
14901503
}
14911504
}
@@ -1512,6 +1525,8 @@ pub fn render(ast: Ast, src: []const u8, w: *Writer) !void {
15121525
}
15131526
try w.print("</{s}>", .{name});
15141527
}
1528+
1529+
first_child = false;
15151530
if (current.next_idx != 0) {
15161531
direction = .enter;
15171532
current = ast.nodes[current.next_idx];
@@ -2505,6 +2520,24 @@ test "script formatting - single-line while loop" {
25052520
try std.testing.expectFmt(expected, "{f}", .{ast.formatter(case)});
25062521
}
25072522

2523+
test "tag in middle of text" {
2524+
const case =
2525+
\\<p>
2526+
\\ Consider using <code>superhtml fmt --check</code> in your CI!
2527+
\\</p>
2528+
;
2529+
const expected = comptime std.fmt.comptimePrint(
2530+
\\<p>
2531+
\\{0c}Consider using <code>superhtml fmt --check</code> in your CI!
2532+
\\</p>
2533+
\\
2534+
, .{'\t'});
2535+
const ast = try Ast.init(std.testing.allocator, case, .html, false);
2536+
defer ast.deinit(std.testing.allocator);
2537+
2538+
try std.testing.expectFmt(expected, "{f}", .{ast.formatter(case)});
2539+
}
2540+
25082541
// test "fuzz" {
25092542
// const Reader = std.Io.Reader;
25102543
// const generator = @import("../generator/html.zig");

0 commit comments

Comments
 (0)