aram123 / js-tables

Automatically exported from code.google.com/p/js-tables
0 stars 0 forks source link

double-quotes in cells break arrays #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Added line in return function to replace "" to an entity

text = text.replace(/\"\"/g,'"');

This way, if you have quotes in a cell that produce double quotes ("") in 
the CSV, you can now render the full cell, instead of the array breaking 
to random bits.

Original issue reported on code.google.com by til...@gmail.com on 23 Apr 2010 at 12:12

GoogleCodeExporter commented 9 years ago
I think what you're saying is that something like this breaks:

var csv = 'a,"b"",c",d';
// var csv = 'a,"b\",c",d'; <- this is also broken
// var csv = 'a,"b"";c",d'; <- but this works
var a = $.csv()(csv);
for (var i = 0; i < a.length; ++i) {
  var row = a[i];
  print("Number of fields: " + row.length);
  for (var j = 0; j < row.length; ++j) {
    print(row[j]);
  }
}

It outputs the following:
Number of fields: 4
a
b
c"
d

Instead I was expecting:
Number of fields: 3
a
b",c
d

If that's not what you're saying, then perhaps this is a different bug.  It's 
not clear to me from your description what exactly you're talking about.  Also 
your workaround would be clearer as a diff, but I assume you mean something 
like this:

--- jquery.csv.js.orig  2011-07-28 09:44:23.081721128 +0200
+++ jquery.csv.js   2011-07-28 11:02:46.272162478 +0200
@@ -151,6 +151,7 @@

         // The main function. Split into lines, and then call splitline repeatedly.
         return function(text) {
+            text = text.replace(/\"\"/g, '"');
             var lines = text.replace(trailing, '').split(linedre);
             for (var i=0, l=lines.length; i<l; i++) {
                 lines[i] = splitline(lines[i]);

This workaround is not a proper fix, because it can lead to things being HTML 
encoded twice.  e.g. if one of your values is "x&y" you'd have to escape that 
to "x&y", but how do you distinguish between the "&" in "x&y" and the one in 
"x"y" without another workaround?

There appears to be no preview option, so I hope this comes through OK.

Original comment by esiot...@gmail.com on 28 Jul 2011 at 9:11

GoogleCodeExporter commented 9 years ago
2nd this issue, and the fix.  If the data looks like this it fails to parse:
date,"Hits per second"
"2012-02-29 18:10",12.21
"2012-02-29 18:15",12.2
"2012-02-29 18:20",12.32
"2012-02-29 18:25",12.25
"2012-02-29 18:30",12.31

adding this line OP suggested makes the fix.  also had to add it to 
jquery.csv.min

d = d.replace(/\"\"/g,'"');

Original comment by timdietr...@gmail.com on 1 Mar 2012 at 6:14

GoogleCodeExporter commented 9 years ago
The entity encoding workaround solves the actual issue, which is related to the 
parsing of the input data. The function simply takes anything between 2 double 
quotes, and if the input contains any double quotes of its own, the function 
breaks.

Ideally, the function would be adjusted to figure out if the input data 
contains double quotes, but the workaround is fine in the meantime, IMHO.

Original comment by til...@gmail.com on 1 Mar 2012 at 7:36