Skip to content

Commit

Permalink
feat: support default namespaces in Writer.bindNs (#45)
Browse files Browse the repository at this point in the history
* feat: allow null for namespace prefix

* test: add extra test for bindNs for namespace with null prefix

* doc: add to bindNs docstring annotating default namespace behavior when prefix is null

* refactor: dont use nullable argument to represent empty prefix for namespaces
  • Loading branch information
carlmontanari authored Feb 2, 2025
1 parent 314910d commit 31cb573
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Writer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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("=\"");
Expand Down Expand Up @@ -835,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 {
try writer.bindNsInternal(try writer.addString(prefix), ns);
}
Expand All @@ -859,12 +862,16 @@ test bindNs {
// declared regardless.
try writer.bindNs("ex3", "http://example.com/ns3");
try writer.elementEndEmpty();
try writer.elementStart("ex4");
try writer.bindNs("", "http://example.com/ex4");
try writer.elementEndEmpty();
try writer.elementEnd();
try writer.eof();

try expectEqualStrings(
\\<ex:root xmlns:ex="http://example.com" ex:a="value">
\\ <ex:element xmlns:ex2="http://example.com/ns2" ex2:a="value" xmlns:ex3="http://example.com/ns3"/>
\\ <ex4 xmlns="http://example.com/ex4"/>
\\</ex:root>
\\
, raw.items);
Expand Down

0 comments on commit 31cb573

Please sign in to comment.