From a5fb12bacb8c91c6641dc3ecd414372ba2878481 Mon Sep 17 00:00:00 2001 From: Carl Montanari Date: Sat, 1 Feb 2025 10:59:28 -0800 Subject: [PATCH 1/4] feat: allow null for namespace prefix --- src/Writer.zig | 12 +++++++++--- src/xml.zig | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Writer.zig b/src/Writer.zig index 1e9c292..091228a 100644 --- a/src/Writer.zig +++ b/src/Writer.zig @@ -543,7 +543,9 @@ fn attributeInternal(writer: *Writer, prefix: []const u8, name: []const u8, valu try writer.write(" "); if (prefix.len > 0) { try writer.write(prefix); - try writer.write(":"); + if(name.len > 0) { + try writer.write(":"); + } } try writer.write(name); try writer.write("=\""); @@ -836,8 +838,12 @@ test embed { /// If the writer is currently inside an element start, the namespace is /// declared immediately. Otherwise, it will be declared on the next element /// started. -pub fn bindNs(writer: *Writer, prefix: []const u8, ns: []const u8) anyerror!void { - try writer.bindNsInternal(try writer.addString(prefix), ns); +pub fn bindNs(writer: *Writer, prefix: ?[]const u8, ns: []const u8) anyerror!void { + if (prefix != null) { + try writer.bindNsInternal(try writer.addString(prefix.?), ns); + } else { + try writer.bindNsInternal(@enumFromInt(writer.strings.items.len), ns); + } } test bindNs { diff --git a/src/xml.zig b/src/xml.zig index cc25a43..b5cf376 100644 --- a/src/xml.zig +++ b/src/xml.zig @@ -529,7 +529,7 @@ pub fn GenericWriter(comptime SinkError: type) type { } /// See `Writer.bindNs`. - pub inline fn bindNs(writer: *@This(), prefix: []const u8, ns: []const u8) WriteError!void { + pub inline fn bindNs(writer: *@This(), prefix: ?[]const u8, ns: []const u8) WriteError!void { return @errorCast(writer.writer.bindNs(prefix, ns)); } From 56fc2c37d6c20128e6ee2ccb5b5f1536b22d0f87 Mon Sep 17 00:00:00 2001 From: Carl Montanari Date: Sat, 1 Feb 2025 10:59:56 -0800 Subject: [PATCH 2/4] test: add extra test for bindNs for namespace with null prefix --- src/Writer.zig | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Writer.zig b/src/Writer.zig index 091228a..92a0306 100644 --- a/src/Writer.zig +++ b/src/Writer.zig @@ -865,12 +865,16 @@ test bindNs { // declared regardless. try writer.bindNs("ex3", "http://example.com/ns3"); try writer.elementEndEmpty(); + try writer.elementStart("ex4"); + try writer.bindNs(null, "http://example.com/ex4"); + try writer.elementEndEmpty(); try writer.elementEnd(); try writer.eof(); try expectEqualStrings( \\ \\ + \\ \\ \\ , raw.items); From a8236f5c1bf28f3c5da276c99938c6d1b225bab5 Mon Sep 17 00:00:00 2001 From: Carl Montanari Date: Sat, 1 Feb 2025 11:01:31 -0800 Subject: [PATCH 3/4] doc: add to bindNs docstring annotating default namespace behavior when prefix is null --- src/Writer.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Writer.zig b/src/Writer.zig index 92a0306..7cb7486 100644 --- a/src/Writer.zig +++ b/src/Writer.zig @@ -837,7 +837,8 @@ test embed { /// /// If the writer is currently inside an element start, the namespace is /// declared immediately. Otherwise, it will be declared on the next element -/// started. +/// started. If/when `prefix` is null the namespace will be declared as a +/// default namespace (no prefix) for the element. pub fn bindNs(writer: *Writer, prefix: ?[]const u8, ns: []const u8) anyerror!void { if (prefix != null) { try writer.bindNsInternal(try writer.addString(prefix.?), ns); From b3134f2a5fb48e8d3939ee1202121cea7fdf87c3 Mon Sep 17 00:00:00 2001 From: Carl Montanari Date: Sat, 1 Feb 2025 16:57:24 -0800 Subject: [PATCH 4/4] refactor: dont use nullable argument to represent empty prefix for namespaces --- src/Writer.zig | 10 +++------- src/xml.zig | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Writer.zig b/src/Writer.zig index 7cb7486..23c6aa4 100644 --- a/src/Writer.zig +++ b/src/Writer.zig @@ -839,12 +839,8 @@ test embed { /// declared immediately. Otherwise, it will be declared on the next element /// started. If/when `prefix` is null the namespace will be declared as a /// default namespace (no prefix) for the element. -pub fn bindNs(writer: *Writer, prefix: ?[]const u8, ns: []const u8) anyerror!void { - if (prefix != null) { - try writer.bindNsInternal(try writer.addString(prefix.?), ns); - } else { - try writer.bindNsInternal(@enumFromInt(writer.strings.items.len), ns); - } +pub fn bindNs(writer: *Writer, prefix: []const u8, ns: []const u8) anyerror!void { + try writer.bindNsInternal(try writer.addString(prefix), ns); } test bindNs { @@ -867,7 +863,7 @@ test bindNs { try writer.bindNs("ex3", "http://example.com/ns3"); try writer.elementEndEmpty(); try writer.elementStart("ex4"); - try writer.bindNs(null, "http://example.com/ex4"); + try writer.bindNs("", "http://example.com/ex4"); try writer.elementEndEmpty(); try writer.elementEnd(); try writer.eof(); diff --git a/src/xml.zig b/src/xml.zig index b5cf376..cc25a43 100644 --- a/src/xml.zig +++ b/src/xml.zig @@ -529,7 +529,7 @@ pub fn GenericWriter(comptime SinkError: type) type { } /// See `Writer.bindNs`. - pub inline fn bindNs(writer: *@This(), prefix: ?[]const u8, ns: []const u8) WriteError!void { + pub inline fn bindNs(writer: *@This(), prefix: []const u8, ns: []const u8) WriteError!void { return @errorCast(writer.writer.bindNs(prefix, ns)); }