Skip to content
Browse files

A new option: xmlDecl -- whether to include the XML declaration <?xml…

…...; true by default
  • Loading branch information...
1 parent a5b4e2c commit cb64e4d23380b3be4d67805138fce76094b4a8f7 @ikr ikr committed
Showing with 39 additions and 24 deletions.
  1. +8 −2 data2xml.js
  2. +31 −22 test/config.js
View
10 data2xml.js
@@ -12,7 +12,7 @@
var valid = {
'omit' : true, // no element is output : ''
'empty' : true, // an empty element is output : '<element></element>'
- 'closed' : true, // a closed element is output : '<element/>'
+ 'closed' : true // a closed element is output : '<element/>'
};
var defaults = {
@@ -20,6 +20,7 @@ var defaults = {
'valProp' : '_value',
'undefined' : 'omit',
'null' : 'omit',
+ 'xmlDecl' : true
};
var xmlHeader = '<?xml version="1.0" encoding="utf-8"?>\n';
@@ -29,6 +30,11 @@ module.exports = function(opts) {
opts.attrProp = opts.attrProp || defaults.attrProp;
opts.valProp = opts.valProp || defaults.valProp;
+
+ if (typeof opts.xmlDecl === 'undefined') {
+ opts.xmlDecl = defaults.xmlDecl;
+ }
+
if ( opts['undefined'] && valid[opts['undefined']] ) {
// nothing, this is fine
}
@@ -43,7 +49,7 @@ module.exports = function(opts) {
}
return function(name, data) {
- var xml = xmlHeader;
+ var xml = opts.xmlDecl ? xmlHeader : '';
xml += makeElement(name, data, opts);
return xml;
};
View
53 test/config.js
@@ -18,29 +18,26 @@ var declaration = '<?xml version="1.0" encoding="utf-8"?>\n';
// --------------------------------------------------------------------------------------------------------------------
-var tests = [
- {
- name : 'one element structure with an xmlns',
- element : 'topelement',
- data : { '@' : { xmlns : 'http://www.appsattic.com/xml/namespace' }, second : 'value' },
- exp : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second></topelement>'
- },
- {
- name : 'complex 4 element array with some attributes',
- element : 'topelement',
- data : { item : [
- { '@' : { type : 'a' }, '#' : 'val1' },
- { '@' : { type : 'b' }, '#' : 'val2' },
- 'val3',
- { '#' : 'val4' },
- ] },
- exp : declaration + '<topelement><item type="a">val1</item><item type="b">val2</item><item>val3</item><item>val4</item></topelement>'
- },
-];
-
-var convert = data2xml({ attrProp : '@', valProp : '#' });
-
test('some simple xml with \'@\' for attributes', function (t) {
+ var convert = data2xml({ attrProp : '@', valProp : '#' }),
+
+ tests = [{
+ name : 'one element structure with an xmlns',
+ element : 'topelement',
+ data : { '@' : { xmlns : 'http://www.appsattic.com/xml/namespace' }, second : 'value' },
+ exp : declaration + '<topelement xmlns="http://www.appsattic.com/xml/namespace"><second>value</second></topelement>'
+ }, {
+ name : 'complex 4 element array with some attributes',
+ element : 'topelement',
+ data : { item : [
+ { '@' : { type : 'a' }, '#' : 'val1' },
+ { '@' : { type : 'b' }, '#' : 'val2' },
+ 'val3',
+ { '#' : 'val4' },
+ ] },
+ exp : declaration + '<topelement><item type="a">val1</item><item type="b">val2</item><item>val3</item><item>val4</item></topelement>'
+ }];
+
tests.forEach(function(test) {
var xml = convert(test.element, test.data, { attrProp : '@', valProp : '#' });
t.equal(xml, test.exp, test.name);
@@ -49,4 +46,16 @@ test('some simple xml with \'@\' for attributes', function (t) {
t.end();
});
+test('it\'s possible to omit the XML declaration', function (t) {
+ var convert = data2xml({xmlDecl: false});
+
+ t.equal(
+ convert('moo', {foo: 'bar', baz: 42}),
+ '<moo><foo>bar</foo><baz>42</baz></moo>',
+ 'must not be declared as XML'
+ );
+
+ t.end();
+});
+
// --------------------------------------------------------------------------------------------------------------------

0 comments on commit cb64e4d

Please sign in to comment.
Something went wrong with that request. Please try again.