|
1 | 1 | function posts_and_users_stats_export_table_to_csv(table, filename) { |
2 | | - const tmpColDelim = String.fromCharCode(11), tmpRowDelim = String.fromCharCode(0), // Temporary delimiters unlikely to be typed by keyboard to avoid accidentally splitting the actual contents |
3 | | - colDelim = '","', rowDelim = '"\r\n"', // actual delimiters for CSV |
| 2 | + // Temporary delimiters unlikely to be typed by keyboard to avoid accidentally splitting the actual contents |
| 3 | + const tmpColDelim = String.fromCharCode(11), |
| 4 | + tmpRowDelim = String.fromCharCode(0), |
| 5 | + // actual delimiters for CSV |
| 6 | + colDelim = '","', |
| 7 | + rowDelim = '"\r\n"', |
| 8 | + forbiddenStartCharacters = ['+', '-', '=', '@'], |
4 | 9 | rows = table.find('tr'), |
5 | | - csv = '"' + rows.map(function (i, row) { |
6 | | - const $row = jQuery(row), $cols = $row.find('td,th'); |
7 | | - return $cols.map(function (j, col) { |
8 | | - const $col = jQuery(col), text = $col.text(); |
9 | | - return text.replace(/"/g, '""'); // escape double quotes |
10 | | - }).get().join(tmpColDelim); |
11 | | - }).get().join(tmpRowDelim).split(tmpRowDelim) |
12 | | - .join(rowDelim).split(tmpColDelim) |
| 10 | + csv = '"' + rows |
| 11 | + .map(function (i, row) { |
| 12 | + const $row = jQuery(row), |
| 13 | + $cols = $row.find('td,th'); |
| 14 | + return $cols |
| 15 | + .map(function (j, col) { |
| 16 | + const $col = jQuery(col); |
| 17 | + let text = $col.text(); |
| 18 | + // Escape double quotes and trim result. |
| 19 | + text = text.replace(/"/g, '""').trim(); |
| 20 | + // Prevent CSV injection. |
| 21 | + let startCharacter = text.substring(0, 1); |
| 22 | + if (forbiddenStartCharacters.includes(startCharacter)) { |
| 23 | + text = "'" + text; |
| 24 | + } |
| 25 | + return text; |
| 26 | + }) |
| 27 | + .get() |
| 28 | + .join(tmpColDelim); |
| 29 | + }).get() |
| 30 | + .join(tmpRowDelim) |
| 31 | + .split(tmpRowDelim) |
| 32 | + .join(rowDelim) |
| 33 | + .split(tmpColDelim) |
13 | 34 | .join(colDelim) + '"', |
14 | 35 | csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv); |
15 | 36 | jQuery(this).attr({ |
|
0 commit comments