DataTables / Buttons

Buttons is an extension for DataTables that adds control buttons to the table.
Other
137 stars 149 forks source link

CSV commas not escaped properly #16

Closed joe-meyer closed 9 years ago

joe-meyer commented 9 years ago

I have the following data value in one of my table cells: BUMPER-16",STL,CUST,TOW When using the html5 csv export I end up with several cells which look like

| BUMPER 16\ | STL | CR | TOW" |

DataTables commented 9 years ago

Can you link to a test case showing the issue and confirm what version of Buttons you are using please.

joe-meyer commented 9 years ago

Ya so sorry about that completely missed the contributing guidelines.

http://jsfiddle.net/50x1m4bt/

Edit: Buttons version 1.0.3

joe-meyer commented 9 years ago

in https://github.com/DataTables/Buttons/blob/master/js/buttons.html5.js#L311 changing the \\ to " seems to resolve this specifc issue. I'm far from a csv guru when it comes to the encoding rules though, not sure what other use cases that might break;

            s += boundary ?
                boundary + a[i].replace( boundary, '"'+boundary ) + boundary :
                a[i];

Update: not a complete fix... ran into an even more obscure use case: http://jsfiddle.net/50x1m4bt/

DataTables commented 9 years ago

Thanks for the JS Fiddle.

From your demo, in the CSV output I get:

"Part Description"
"BUMPER 16\",STL,CR,TOW"

That looks correct in its escaping to me. What would you have expected it to be?

joe-meyer commented 9 years ago

Apparently further research seems to indicate that there isn't really a "standard" for escaping. But Microsoft Excel uses a " for escaping, not a \. I also stumbled across a rfc memo, which apparently is a recommended implementation as opposed to a standard. See https://tools.ietf.org/html/rfc4180 In that RFC they outline escaping which aligns better with using Excel.

DataTables commented 9 years ago

Haha... So it goes. I'll look into making the escape character an customisable parameter and read through the RFC before making any changes to Buttons - thanks for the research and link!

joe-meyer commented 9 years ago

@DataTables the escape character becoming a customization parameter seems ideal. That's how PHP seems to deal with it with their fputcsv() function.

DataTables commented 9 years ago

Finally done. I've change the default to be " for the escape character, based on the information in the RFC. However, it is easy to change it as needed now using the escapeChar option.

Thanks for bringing this up.

cdaringe commented 8 years ago

Should have checked the issue tracker first! Just diagnosed this! FYI to late visitors, this affects 1.0.3, and is patched in 1.1.0+