diff --git a/src/fragment.zig b/src/fragment.zig index 080a54a..ef9bb05 100644 --- a/src/fragment.zig +++ b/src/fragment.zig @@ -95,27 +95,29 @@ fn elem(l: *Lua, w: *std.Io.Writer) Error!void { try writeProps(l, w); } - const nchildren: usize = nchildren: { - const ltype_children = l.getField(-1, "children"); - if (ltype_children == .nil) break :nchildren 0; - if (ltype_children != .table) return error.ExpectedTable; - break :nchildren l.lenRaw(-1); - }; - // -1 => children; -2 => elem + { + const nchildren: usize = nchildren: { + const ltype_children = l.getField(-1, "children"); + if (ltype_children == .nil) break :nchildren 0; + if (ltype_children != .table) return error.ExpectedTable; + break :nchildren l.lenRaw(-1); + }; + // -1 => children; -2 => elem + defer l.pop(1); // pops "children" - // empty - if (config.empty and nchildren == 0) { - if (name != null) try w.writeAll(" />"); - l.pop(1); // pops "children" + if (config.empty and nchildren == 0) { + if (name != null) try w.writeAll(" />"); - try w.flush(); - return; - } - if (name != null) try w.writeByte('>'); + try w.flush(); + return; + } + + if (name != null) try w.writeByte('>'); - // children - try writeChildren(l, w, config); - l.pop(1); + // children + if (nchildren > 0) + try writeChildren(l, w, config); + } // close if (config.close) if (name) |n| {