summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Kaganski <mike.kaganski@collabora.com>2018-01-17 12:41:41 +0300
committerMike Kaganski <mike.kaganski@collabora.com>2018-01-17 19:47:12 +0100
commitba43d4c596c9c6b8d98b749ffd7f69d4faf9826d (patch)
tree5deaeb0f80f5f942d94615064fb11cab5c99e69a
parentdb55054afb9011f7dcafcc72b222e1456fa7f187 (diff)
tdf#115013: quote columns with spaces and properly escape characters
Unit test updated Change-Id: If36c90c0ff372ce45666674d2487e6edf2536dbf Reviewed-on: https://gerrit.libreoffice.org/48038 Tested-by: Jenkins <ci@libreoffice.org> Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
-rw-r--r--sw/qa/extras/uiwriter/data/datasource.odsbin8038 -> 9596 bytes
-rw-r--r--sw/qa/extras/uiwriter/uiwriter.cxx8
-rw-r--r--sw/source/filter/ww8/ww8atr.cxx43
3 files changed, 47 insertions, 4 deletions
diff --git a/sw/qa/extras/uiwriter/data/datasource.ods b/sw/qa/extras/uiwriter/data/datasource.ods
index 076659679575..81d78440656e 100644
--- a/sw/qa/extras/uiwriter/data/datasource.ods
+++ b/sw/qa/extras/uiwriter/data/datasource.ods
Binary files differ
diff --git a/sw/qa/extras/uiwriter/uiwriter.cxx b/sw/qa/extras/uiwriter/uiwriter.cxx
index 43b492763f04..7e3a0d54e533 100644
--- a/sw/qa/extras/uiwriter/uiwriter.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter.cxx
@@ -5761,7 +5761,9 @@ void SwUiWriterTest::testTdf113481()
void SwUiWriterTest::testTdf115013()
{
- //create new writer document
+ const OUString sColumnName("Name with spaces, \"quotes\" and \\backslashes");
+
+ //create new writer document
SwDoc* pDoc = createDoc();
{
@@ -5777,7 +5779,7 @@ void SwUiWriterTest::testTdf115013()
SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
CPPUNIT_ASSERT(pWrtShell);
SwDBFieldType* pFieldType = static_cast<SwDBFieldType*>(pWrtShell->InsertFieldType(
- SwDBFieldType(pDoc, "Name", aDBData)));
+ SwDBFieldType(pDoc, sColumnName, aDBData)));
CPPUNIT_ASSERT(pFieldType);
// Insert the field into document
@@ -5800,7 +5802,7 @@ void SwUiWriterTest::testTdf115013()
CPPUNIT_ASSERT(pField);
OUString sColumn = static_cast<SwDBFieldType*>(pField->GetTyp())->GetColumnName();
// The column name must come correct after round trip
- CPPUNIT_ASSERT_EQUAL(OUString("Name"), sColumn);
+ CPPUNIT_ASSERT_EQUAL(sColumnName, sColumn);
}
CPPUNIT_TEST_SUITE_REGISTRATION(SwUiWriterTest);
diff --git a/sw/source/filter/ww8/ww8atr.cxx b/sw/source/filter/ww8/ww8atr.cxx
index 233126c05439..3ccd047b620b 100644
--- a/sw/source/filter/ww8/ww8atr.cxx
+++ b/sw/source/filter/ww8/ww8atr.cxx
@@ -2680,6 +2680,46 @@ void WW8AttributeOutput::WriteExpand( const SwField* pField )
SwWW8Writer::WriteString16( m_rWW8Export.Strm(), sExpand, false );
}
+namespace
+{
+// Escapes a token string for storing in Word formats. Its import counterpart
+// is lcl_ExtractToken in writerfilter/source/dmapper/DomainMapper_Impl.cxx
+OUString EscapeToken(const OUString& rCommand)
+{
+ bool bWasEscaped = false;
+
+ const int nBufferLen = rCommand.getLength()*1.5;
+ OUStringBuffer sResult(nBufferLen);
+ sResult.append('"'); // opening quote
+ for (sal_Int32 i = 0; i < rCommand.getLength(); ++i)
+ {
+ sal_Unicode ch = rCommand[i];
+ switch (ch)
+ {
+ case '\\':
+ case '"':
+ // Backslashes and doublequotes must be escaped
+ bWasEscaped = true;
+ sResult.append('\\');
+ break;
+ case ' ':
+ // Spaces require quotation
+ bWasEscaped = true;
+ break;
+ }
+ sResult.append(ch);
+ }
+
+ if (bWasEscaped)
+ {
+ sResult.append('"'); // closing quote
+ return sResult.makeStringAndClear();
+ }
+ // No escapement/quotation was required
+ return rCommand;
+}
+}
+
void AttributeOutputBase::TextField( const SwFormatField& rField )
{
const SwField* pField = rField.GetField();
@@ -2757,7 +2797,8 @@ void AttributeOutputBase::TextField( const SwFormatField& rField )
break;
case SwFieldIds::Database:
{
- OUString sStr = FieldString(ww::eMERGEFIELD) + static_cast<SwDBFieldType *>(pField->GetTyp())->GetColumnName() + " ";
+ OUString sStr = FieldString(ww::eMERGEFIELD)
+ + EscapeToken(static_cast<SwDBFieldType *>(pField->GetTyp())->GetColumnName()) + " ";
GetExport().OutputField(pField, ww::eMERGEFIELD, sStr);
}
break;