Skip to content

Commit aa2aa13

Browse files
committed
Quote only data with end-of-lines, tabs or double-quotes
Headers are also quoted if they contain end-of-lines or tabs. See #1244
1 parent 882fc8d commit aa2aa13

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

‎src/ExtendedTableWidget.cpp‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ void ExtendedTableWidget::copy(const bool withHeaders)
244244
result.append(fieldSepText);
245245
htmlResult.append("</th><th>");
246246
}
247-
result.append(QString("\"%1\"").arg(headerText)); // TODO: Check if we need the quotes here
247+
// SQLite allows tabs in column and table names. TODO: should we escape double quotes too?
248+
if (headerText.contains('\n') || headerText.contains('\t'))
249+
result.append(QString("\"%1\"").arg(headerText));
250+
else
251+
result.append(headerText);
248252
htmlResult.append(headerText);
249253
}
250254
result.append(rowSepText);
@@ -268,18 +272,21 @@ void ExtendedTableWidget::copy(const bool withHeaders)
268272

269273
// Table cell data
270274
QString text = data;
271-
if (text.contains('\n'))
275+
if (text.contains('\n') || text.contains('\t'))
272276
htmlResult.append("<pre>" + text.toHtmlEscaped() + "</pre>");
273277
else
274278
htmlResult.append(text.toHtmlEscaped());
275279

276-
// non-NULL data is enquoted in plain text format, whilst NULL isn't
277-
// TODO: Do we really want to do this for the non-internal buffer case? The only case where we should definitely quote the data is when there are line breaks
278-
// in the text, again for spreadsheet compatability. We also need to quote when there are tabs in the string (or replace the tabs by spaces, that's what
279-
// LibreOffice seems to be doing here).
280+
// Empty string is enquoted in plain text format, whilst NULL isn't
281+
// We also quote the data when there are line breaks in the text, again for spreadsheet compatability.
282+
// We also need to quote when there are tabs in the string (another option would be to replace the tabs by spaces, that's what
283+
// LibreOffice seems to be doing here).
280284
if (!data.isNull()) {
281-
text.replace("\"", "\"\"");
282-
result.append(QString("\"%1\"").arg(text));
285+
if (text.isEmpty() || text.contains('\n') || text.contains('\t') || text.contains('"')) {
286+
text.replace("\"", "\"\"");
287+
result.append(QString("\"%1\"").arg(text));
288+
} else
289+
result.append(text);
283290
}
284291
}
285292

0 commit comments

Comments
 (0)