dewiikaaa / jquerycsvtotable

Automatically exported from code.google.com/p/jquerycsvtotable
0 stars 0 forks source link

Windows line endings (\r\n) not correctly handled #12

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Call CSVtoTable on a CSV with \r\n endings throughout, with each field 
quoted ("). The CSV must have more than 1 row.

What is the expected output? What do you see instead?
Expected: outer quotes are stripped for all columns
But I see: outer quotes are not stripped for the last column in the table.

What version of the product are you using? On what operating system?
- Windows 7 64 bit / Chrome 38.0.2125.111

Please provide any additional information below.

There is clearly attempted support to strip out \r characters, but it appears 
to be buggy. It currently has:

    var lines = data.replace('\r','').split('\n');

Which only does a single replacement. It should be global:

    var lines = data.replace(/\r/g,'').split('\n');

This is compounded by the following further bug in this line in the splitCSV 
function:

    if (thisCSV[x].replace(/"\s+$/, '"').charAt(thisCSV[x].length - 1) == '"') {

This "tidies" the string (stripping trailing whitespace after a terminating 
quote), then checks if the last character is a string. However, it uses 
thisCSV[x].length when it should probably use the length of the new, tidied 
string instead:

    if (thisCSV[x].replace(/"\s+$/, '"').slice(-1) == '"') {

NB I'm not a javascript programmer, no idea if the slice function is widely 
available. You could use a temporary variable and charAt instead.

Fixing either of these apparent bugs would address the problem. I don't know 
the code well enough to know which, if either, is safe to do. If it's safe, it 
seems wise to fix both.

Original issue reported on code.google.com by ba...@acar.org.uk on 5 Jan 2015 at 6:01

GoogleCodeExporter commented 9 years ago
Attaching a sample CSV & HTML file to illustrate.

Original comment by ba...@acar.org.uk on 5 Jan 2015 at 6:10

Attachments: