From a562150b96b187fd92f113ea9ea9deb453ff4e5a Mon Sep 17 00:00:00 2001 From: Pieter Sheth-Voss Date: Sun, 1 Mar 2015 00:39:47 -0500 Subject: [PATCH] Use jQuery parseXML to preserve case in XML --- bits/88_write.js | 17 +++++++++-------- bits/90_utils.js | 13 +++++++++---- xlsx.js | 30 ++++++++++++++++++------------ 3 files changed, 36 insertions(+), 24 deletions(-) diff --git a/bits/88_write.js b/bits/88_write.js index 41667b8..f1411e1 100644 --- a/bits/88_write.js +++ b/bits/88_write.js @@ -1,6 +1,14 @@ function writeSync(wb, opts) { var o = opts||{}; - var z = write_zip(wb, o); + + if (typeof module != 'undefined' && typeof 'require' != 'undefined') { + style_builder = new StyleBuilder(opts); + } + else if (typeof $ != 'undefined' || typeof 'jQuery' != 'undefined') { + style_builder = new StyleBuilder(opts); + } + + var z = write_zip(wb, o); switch(o.type) { case "base64": return z.generate({type:"base64"}); case "binary": return z.generate({type:"string"}); @@ -13,13 +21,6 @@ function writeSync(wb, opts) { function writeFileSync(wb, filename, opts) { var o = opts||{}; o.type = 'file'; - if (typeof module != 'undefined' && typeof 'require' != 'undefined') { - style_builder = new StyleBuilder(opts); - } - else if (typeof $ != 'undefined' || typeof 'jQuery' != 'undefined') { - style_builder = new StyleBuilder(opts); - } - o.file = filename; switch(o.file.substr(-5).toLowerCase()) { case '.xlsm': o.bookType = 'xlsm'; break; diff --git a/bits/90_utils.js b/bits/90_utils.js index 957e61b..7b05f4a 100644 --- a/bits/90_utils.js +++ b/bits/90_utils.js @@ -205,10 +205,12 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo createElement = function(str) { return cheerio(cheerio(str, null, null, {xmlMode: true})); }; } else if (typeof jQuery !== 'undefined' || typeof $ !== 'undefined') { - createElement = function(str) { return $(str); } + createElement = function(str) { + return $($.parseXML(str).documentElement); + } //http://stackoverflow.com/a/11719466 } else { - createElement = function() { } + createElement = function() { } // this class should never have been instantiated } @@ -287,7 +289,7 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo this.$styles.find = function(q) { return this(q)} } else { - this.$styles = $(baseXml); + this.$styles = $($.parseXML(baseXml).documentElement); } @@ -539,7 +541,10 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo this.$styles.find('numFmts').remove(); } if (this.$styles.xml) { return this.$styles.xml(); } - else { return baseXmlprefix + this.$styles.html(); } + else { + var s = new XMLSerializer(); //http://stackoverflow.com/a/5744268 + return baseXmlprefix + s.serializeToString(this.$styles[0]);; + } } }.initialize(options||{}); } diff --git a/xlsx.js b/xlsx.js index 9f14dc4..b11d145 100644 --- a/xlsx.js +++ b/xlsx.js @@ -5184,7 +5184,15 @@ function readFileSync(data, opts) { function writeSync(wb, opts) { var o = opts||{}; - var z = write_zip(wb, o); + + if (typeof module != 'undefined' && typeof 'require' != 'undefined') { + style_builder = new StyleBuilder(opts); + } + else if (typeof $ != 'undefined' || typeof 'jQuery' != 'undefined') { + style_builder = new StyleBuilder(opts); + } + + var z = write_zip(wb, o); switch(o.type) { case "base64": return z.generate({type:"base64"}); case "binary": return z.generate({type:"string"}); @@ -5197,13 +5205,6 @@ function writeSync(wb, opts) { function writeFileSync(wb, filename, opts) { var o = opts||{}; o.type = 'file'; - if (typeof module != 'undefined' && typeof 'require' != 'undefined') { - style_builder = new StyleBuilder(opts); - } - else if (typeof $ != 'undefined' || typeof 'jQuery' != 'undefined') { - style_builder = new StyleBuilder(opts); - } - o.file = filename; switch(o.file.substr(-5).toLowerCase()) { case '.xlsm': o.bookType = 'xlsm'; break; @@ -5419,10 +5420,12 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo createElement = function(str) { return cheerio(cheerio(str, null, null, {xmlMode: true})); }; } else if (typeof jQuery !== 'undefined' || typeof $ !== 'undefined') { - createElement = function(str) { return $(str); } + createElement = function(str) { + return $($.parseXML(str).documentElement); + } //http://stackoverflow.com/a/11719466 } else { - createElement = function() { } + createElement = function() { } // this class should never have been instantiated } @@ -5501,7 +5504,7 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo this.$styles.find = function(q) { return this(q)} } else { - this.$styles = $(baseXml); + this.$styles = $($.parseXML(baseXml).documentElement); } @@ -5753,7 +5756,10 @@ if ((typeof 'module' != 'undefined' && typeof require != 'undefined') || (typeo this.$styles.find('numFmts').remove(); } if (this.$styles.xml) { return this.$styles.xml(); } - else { return baseXmlprefix + this.$styles.html(); } + else { + var s = new XMLSerializer(); //http://stackoverflow.com/a/5744268 + return baseXmlprefix + s.serializeToString(this.$styles[0]);; + } } }.initialize(options||{}); }