diff --git a/packages/node_modules/@node-red/nodes/core/parsers/70-CSV.js b/packages/node_modules/@node-red/nodes/core/parsers/70-CSV.js index df73ccd1f..fa46750c3 100644 --- a/packages/node_modules/@node-red/nodes/core/parsers/70-CSV.js +++ b/packages/node_modules/@node-red/nodes/core/parsers/70-CSV.js @@ -46,9 +46,10 @@ module.exports = function(RED) { var jj = 0; // pointer into array of template items var kk = [""]; // array of data for each of the template items for (var ii = 0; ii < col.length; ii++) { + //console.log("II",ii,col[ii]); if (col[ii] === node.quo) { // if it's a quote toggle inside or outside if (ii === 0 || col[ii-1] === sep) { ff = !ff; } - else if (col[ii-1] === node.quo) { } // do nothing, "" = " in CSV world + else if (col[ii] === node.quo && col[ii+1] === node.quo) { kk[jj] += '\\'; } // do nothing, "" = " in CSV world else if (!ff && kk[jj][0] !== node.quo) { ff = !ff; } else { kk[jj] += col[ii]; } } @@ -67,6 +68,7 @@ module.exports = function(RED) { if ((kk.length === 1) && (kk[0] === "")) { node.goodtmpl = false; } else { node.goodtmpl = true; } + console.log("KK",kk) return kk; } var template = clean(node.template,','); @@ -94,7 +96,18 @@ module.exports = function(RED) { template = Object.keys(msg.payload[0]); } } - ou += template.map(v => v.indexOf(node.sep)!==-1 ? '"'+v+'"' : v).join(node.sep) + node.ret; + var t = RED.util.cloneMessage(template); + template.map(v => v.indexOf(node.sep)!==-1 ? '"'+v+'"' : v) + for (var i=0; i < t.length; i++) { + var v = t[i]; + if (v.indexOf('"') !== -1 ) { v = v.replaceAll('"','""'); } + if (v.indexOf(node.sep)!==-1 || v.indexOf('"')!==-1 ) { v = '"'+v+'"'; } + console.log("V3",v) + t[i]=v; + } + console.log("T",t) + console.log("Te",template) + ou += t.join(node.sep) + node.ret; if (node.hdrout === "once") { node.hdrSent = true; } } for (var s = 0; s < msg.payload.length; s++) { diff --git a/test/nodes/core/parsers/70-CSV_spec.js b/test/nodes/core/parsers/70-CSV_spec.js index c551a4998..243c04cc1 100644 --- a/test/nodes/core/parsers/70-CSV_spec.js +++ b/test/nodes/core/parsers/70-CSV_spec.js @@ -773,7 +773,7 @@ describe('CSV node', function() { var n2 = helper.getNode("n2"); n2.on("input", function(msg) { try { - msg.should.have.property('payload', 'a"a,b\'b\nA1,B1\nA2,B2\n'); + msg.should.have.property('payload', '"a""a",b\'b\nA1,B1\nA2,B2\n'); done(); } catch(e) { done(e); }