aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-08-16 15:52:29 +0100
committerMichael Williamson <mike@zwobble.org>2015-08-16 15:52:29 +0100
commit0363f064b05698eed9826570aad6985ef47a6322 (patch)
tree61bb0b72a3e8dbc68380ed11a5aa8d49eedbc6d3
parent79fc6da75e6bf8e64601bf6832293842fd701d31 (diff)
Support default namespace
-rw-r--r--lib/xml/writer.js10
-rw-r--r--tests/xml/writer.tests.js20
2 files changed, 23 insertions, 7 deletions
diff --git a/lib/xml/writer.js b/lib/xml/writer.js
index 66cc8de..9bf8c35 100644
--- a/lib/xml/writer.js
+++ b/lib/xml/writer.js
@@ -25,9 +25,10 @@ function writeString(root, namespaces) {
}
function mapElementName(name) {
- var longFormMatch = /^\{(.*)\}(.*)$/.exec(name);
+ var longFormMatch = /^\{(.*)\}:(.*)$/.exec(name);
if (longFormMatch) {
- return uriToPrefix[longFormMatch[1]] + ":" + longFormMatch[2];
+ var prefix = uriToPrefix[longFormMatch[1]];
+ return prefix + (prefix === "" ? "" : ":") + longFormMatch[2];
} else {
return name;
}
@@ -35,14 +36,15 @@ function writeString(root, namespaces) {
function writeDocument(root) {
var builder = xmlbuilder
- .create(root.name, {
+ .create(mapElementName(root.name), {
version: '1.0',
encoding: 'UTF-8',
standalone: true
});
_.forEach(namespaces, function(uri, prefix) {
- builder.attribute("xmlns:" + prefix, uri);
+ var key = "xmlns" + (prefix === "" ? "" : ":" + prefix);
+ builder.attribute(key, uri);
});
root.children.forEach(function(child) {
diff --git a/tests/xml/writer.tests.js b/tests/xml/writer.tests.js
index 6c414c0..7f9440f 100644
--- a/tests/xml/writer.tests.js
+++ b/tests/xml/writer.tests.js
@@ -44,21 +44,35 @@ describe('xml.writer', function() {
'<root><album>Everything in Transit</album></root>');
});
+ test('can write root element with long-form prefix when URI is namespace', function() {
+ var element = xml.element("{music}:root");
+ assertXmlString(element, {"m": "music"},
+ '<m:root xmlns:m="music"/>');
+ });
+
test('can write child elements with long-form prefix when URI is namespace', function() {
- var element = xml.element("root", {"m": "music"}, [
- xml.element("{music}album")
+ var element = xml.element("root", {}, [
+ xml.element("{music}:album")
]);
assertXmlString(element, {"m": "music"},
'<root xmlns:m="music"><m:album/></root>');
});
test('can write child elements with short-form prefix when URI is namespace', function() {
- var element = xml.element("root", {"m": "music"}, [
+ var element = xml.element("root", {}, [
xml.element("m:album")
]);
assertXmlString(element, {"m": "music"},
'<root xmlns:m="music"><m:album/></root>');
});
+
+ test('default namespace has key of empty string', function() {
+ var element = xml.element("root", {}, [
+ xml.element("{music}:album")
+ ]);
+ assertXmlString(element, {"": "music"},
+ '<root xmlns="music"><album/></root>');
+ });
});