From ad34d788df536ef0f92fc76f39409ea6c0b3520b Mon Sep 17 00:00:00 2001 From: scott1991 Date: Thu, 11 Nov 2021 17:07:16 +0800 Subject: [PATCH] add XML Header Encoding format option add XML Header Encoding format option, xml_header has two arguments now --- jsontoxml.js | 12 +++++++++--- test.js | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/jsontoxml.js b/jsontoxml.js index 57a710d..d159141 100644 --- a/jsontoxml.js +++ b/jsontoxml.js @@ -115,13 +115,19 @@ var process_to_xml = function(node_data,options){ }; -var xml_header = function(standalone) { - var ret = [''); return ret.join(''); @@ -147,7 +153,7 @@ module.exports = function(obj,options){ if(options.xmlHeader) { // the user wants an xml header - xmlheader = xml_header(!!options.xmlHeader.standalone); + xmlheader = xml_header(!!options.xmlHeader.standalone,options.xmlHeader.encoding); } if(typeof options.docType != 'undefined') { diff --git a/test.js b/test.js index 76ad0ad..dbe1de4 100644 --- a/test.js +++ b/test.js @@ -103,3 +103,23 @@ test("creates open and close tag on empty body",function(t){ t.end() }); +var expected_with_header = '' + expected; +test("creates correct object and add a generic header",function(t){ + var result = jsonxml(input,{escape:true, xmlHeader:true}); + t.equals(result,expected_with_header,' test should have generated correct xml'); + t.end() +}); + +var expected_with_encoding_header = '' + expected; +test("creates correct object and add a header with encoding",function(t){ + var result = jsonxml(input,{escape:true, xmlHeader:{encoding:"ISO-10646-UCS-4"}}); + t.equals(result,expected_with_encoding_header,' test should have generated correct xml'); + t.end() +}); + +var expected_with_standalone_encoding_header = '' + expected; +test("creates correct object and add a header with standalone, encoding",function(t){ + var result = jsonxml(input,{escape:true, xmlHeader:{standalone:true,encoding:"ISO-10646-UCS-4"}}); + t.equals(result,expected_with_standalone_encoding_header,' test should have generated correct xml'); + t.end() +});