Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/default namespaces #45

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 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,9 +837,14 @@ 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);
/// 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);
}
}

test bindNs {
Expand All @@ -859,12 +866,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(
\\<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
2 changes: 1 addition & 1 deletion src/xml.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down