tdf#129574 sw: fix RTF export of table of contents
The problem is that the hyperlink that starts at the start of the first
paragraph of the ToX content is written to RTF before the TOC field
itself, so the hyperlink contains the entire ToX:
{{\field{\*\fldinst HYPERLINK "#__RefHeading___Toc250984071" }{\fldrslt {\rtlch\langfe1024 \ltrch\lang1024\loch
{\field{\*\fldinst { TOC \\o "1-3" \\h \\z }}{\fldrslt {1.India\tab 1}}}}
This is because the HYPERLINK is written into m_aRun but the TOC into
m_aRunText.
Interestingly StartRun() asserts that m_aRunText should be empty, so we
don't try to change StartURL() to write the HYPERLINK into m_aRunText.
The only function that moves text from m_aRunText to m_aRun is
EndRun(), so wrap the TOC field in StartRun()/EndRun().
This breaks the export of the ToX to DOCX because a mysterious SDT is
no longer written around the field but only the field result, so only do
this for RTF.
Change-Id: I22e45c4a9c9ce6edb2b9114b4a29b2a373ec3284
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/86860
Tested-by: Jenkins
Reviewed-by: Michael Stahl <michael.stahl@cib.de>
diff --git a/sw/qa/extras/rtfexport/data/mw00_table_of_contents_templates.doc b/sw/qa/extras/rtfexport/data/mw00_table_of_contents_templates.doc
new file mode 100644
index 0000000..f0f8054
--- /dev/null
+++ b/sw/qa/extras/rtfexport/data/mw00_table_of_contents_templates.doc
Binary files differ
diff --git a/sw/qa/extras/rtfexport/rtfexport4.cxx b/sw/qa/extras/rtfexport/rtfexport4.cxx
index bbf4c60..4a95b6f 100644
--- a/sw/qa/extras/rtfexport/rtfexport4.cxx
+++ b/sw/qa/extras/rtfexport/rtfexport4.cxx
@@ -11,6 +11,7 @@
#include <com/sun/star/table/ShadowFormat.hpp>
#include <com/sun/star/text/WritingMode2.hpp>
#include <com/sun/star/text/XDocumentIndex.hpp>
#include <com/sun/star/style/ParagraphAdjust.hpp>
#include <o3tl/cppunittraitshelper.hxx>
#include <svx/swframetypes.hxx>
@@ -166,6 +167,19 @@
getProperty<sal_Int16>(getParagraph(2), "ParaLastLineAdjust")));
}
DECLARE_RTFEXPORT_TEST(testTdf129574, "mw00_table_of_contents_templates.doc")
{
uno::Reference<text::XDocumentIndexesSupplier> xIndexSupplier(mxComponent, uno::UNO_QUERY);
uno::Reference<container::XIndexAccess> xIndexes(xIndexSupplier->getDocumentIndexes());
uno::Reference<text::XDocumentIndex> xTOC(xIndexes->getByIndex(0), uno::UNO_QUERY);
CPPUNIT_ASSERT(xTOC.is());
uno::Reference<text::XTextRange> xTextRange(xTOC->getAnchor());
// table of contents contains 4 paragraphs
CPPUNIT_ASSERT_EQUAL(OUString("1.Koffice 1" SAL_NEWLINE_STRING "2.Kword 1" SAL_NEWLINE_STRING
"3.Kspread 1" SAL_NEWLINE_STRING "4.Kpresenter 1"),
xTextRange->getString());
}
DECLARE_RTFEXPORT_TEST(testCjklist34, "cjklist34.rtf")
{
sal_Int16 numFormat = getNumberingTypeOfParagraph(1);
diff --git a/sw/source/filter/ww8/ww8atr.cxx b/sw/source/filter/ww8/ww8atr.cxx
index 41a2d9c..9e8d815 100644
--- a/sw/source/filter/ww8/ww8atr.cxx
+++ b/sw/source/filter/ww8/ww8atr.cxx
@@ -2493,8 +2493,16 @@
if (!sStr.isEmpty())
{
GetExport( ).m_bInWriteTOX = true;
if (GetExport().GetExportFormat() == MSWordExportBase::ExportFormat::RTF)
{ // tdf#129574: required for RTF; doesn't work with DOCX
StartRun(nullptr, -42, true);
}
GetExport( ).OutputField( nullptr, eCode, sStr, FieldFlags::Start | FieldFlags::CmdStart |
FieldFlags::CmdEnd );
if (GetExport().GetExportFormat() == MSWordExportBase::ExportFormat::RTF)
{
EndRun(nullptr, -42, true);
}
}
}