From 8188f2c0c231db18c3e10a15fc6e31d183cc48ff Mon Sep 17 00:00:00 2001 From: robin Date: Sun, 16 Aug 2026 17:57:29 +0200 Subject: [PATCH] fragment: handle nonempty elements without children --- src/fragment.zig | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) 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| { -- 2.51.2