From a4bdd833a252ed4d942e4478fc820f9f2ee725fe Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Mon, 1 Mar 2021 20:57:38 +0100 Subject: tdf#140552 RTF export: fix hyperlink, in footnote, in hyperlink Regression from commit 7d42346ba77c9c4df241ea40eaf550993ca18783 (tdf#90421 RTF export: ignore hyperlinks without an URL, 2015-04-21), URLs can be nested in the footnote case, which requires a stack. Otherwise the inner URL clears "the" URL and we don't close the outer field as we believe it's empty, so it was not started. Change-Id: I9f87ddbb7e597c413bf836eb9b58beb76722361f Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111794 Tested-by: Jenkins Reviewed-by: Miklos Vajna (cherry picked from commit 5a74baa4f033f84c4bbcec869a68eef149f77161) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111778 Reviewed-by: Xisco Fauli Signed-off-by: Xisco Fauli Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111823 Reviewed-by: Michael Stahl --- sw/qa/extras/rtfexport/rtfexport3.cxx | 47 +++++++++++++++++++++++++++++ sw/source/filter/ww8/rtfattributeoutput.cxx | 12 ++++++-- sw/source/filter/ww8/rtfattributeoutput.hxx | 2 +- 3 files changed, 57 insertions(+), 4 deletions(-) diff --git a/sw/qa/extras/rtfexport/rtfexport3.cxx b/sw/qa/extras/rtfexport/rtfexport3.cxx index 7bb20cbb410e..45f84f5c5089 100644 --- a/sw/qa/extras/rtfexport/rtfexport3.cxx +++ b/sw/qa/extras/rtfexport/rtfexport3.cxx @@ -13,6 +13,7 @@ #include #include #include +#include class Test : public SwModelTestBase { @@ -236,6 +237,52 @@ DECLARE_RTFEXPORT_TEST(testTdf112520, "tdf112520.docx") getProperty(getShape(3), "AnchorType")); } +CPPUNIT_TEST_FIXTURE(Test, testNestedHyperlink) +{ + // Given a hyperlink contains a footnote which contains a hyperlink: + { + createSwDoc(); + uno::Reference xFactory(mxComponent, uno::UNO_QUERY); + uno::Reference xFootnote( + xFactory->createInstance("com.sun.star.text.Footnote"), uno::UNO_QUERY); + uno::Reference xTextDocument(mxComponent, uno::UNO_QUERY); + uno::Reference xText = xTextDocument->getText(); + uno::Reference xCursor = xText->createTextCursor(); + xText->insertString(xCursor, "a", /*bAbsorb=*/false); + xText->insertTextContent(xCursor, xFootnote, /*bAbsorb=*/false); + xText->insertString(xCursor, "b", /*bAbsorb=*/false); + xCursor->gotoStart(/*bExpand=*/false); + xCursor->gotoEnd(/*bExpand=*/true); + uno::Reference xCursorProps(xCursor, uno::UNO_QUERY); + xCursorProps->setPropertyValue("HyperLinkURL", uno::makeAny(OUString("http://body.com/"))); + uno::Reference xFootnoteText(xFootnote, uno::UNO_QUERY); + xCursor = xFootnoteText->createTextCursor(); + xFootnoteText->insertString(xCursor, "x", /*bAbsorb=*/false); + xCursor->gotoStart(/*bExpand=*/false); + xCursor->gotoEnd(/*bExpand=*/true); + xCursorProps.set(xCursor, uno::UNO_QUERY); + xCursorProps->setPropertyValue("HyperLinkURL", + uno::makeAny(OUString("http://footnote.com/"))); + } + + // When exporting to RTF: + // Without the accompanying fix in place, this test would have failed with: + // assertion failed + // - Expression: xComponent.is() + // i.e. the RTF output was not well-formed, loading failed. + reload(mpFilter, "nested-hyperlink.rtf"); + + // Then make sure both hyperlinks are have the correct URLs. + uno::Reference xParagraph = getParagraph(1); + uno::Reference xPortion = getRun(xParagraph, 1); + CPPUNIT_ASSERT_EQUAL(OUString("http://body.com/"), + getProperty(xPortion, "HyperLinkURL")); + auto xFootnote = getProperty>(getRun(xParagraph, 2), "Footnote"); + uno::Reference xFootnotePortion = getRun(getParagraphOfText(1, xFootnote), 1); + CPPUNIT_ASSERT_EQUAL(OUString("http://footnote.com/"), + getProperty(xFootnotePortion, "HyperLinkURL")); +} + DECLARE_RTFEXPORT_TEST(testTdf121623, "tdf121623.rtf") { // This was 2, multicolumn section was ignored at the table. diff --git a/sw/source/filter/ww8/rtfattributeoutput.cxx b/sw/source/filter/ww8/rtfattributeoutput.cxx index 10d38a569e92..fe7f24e474be 100644 --- a/sw/source/filter/ww8/rtfattributeoutput.cxx +++ b/sw/source/filter/ww8/rtfattributeoutput.cxx @@ -530,7 +530,7 @@ void RtfAttributeOutput::EndRuby(const SwTextNode& rNode, sal_Int32 nPos) bool RtfAttributeOutput::StartURL(const OUString& rUrl, const OUString& rTarget) { - m_sURL = rUrl; + m_aURLs.push(rUrl); // Ignore hyperlink without a URL. if (!rUrl.isEmpty()) { @@ -560,7 +560,13 @@ bool RtfAttributeOutput::StartURL(const OUString& rUrl, const OUString& rTarget) bool RtfAttributeOutput::EndURL(bool const isAtEndOfParagraph) { - if (!m_sURL.isEmpty()) + if (m_aURLs.empty()) + { + return true; + } + + const OUString& rURL = m_aURLs.top(); + if (!rURL.isEmpty()) { // UGLY: usually EndRun is called earlier, but there is an extra // call to OutAttrWithRange() when at the end of the paragraph, @@ -580,8 +586,8 @@ bool RtfAttributeOutput::EndURL(bool const isAtEndOfParagraph) // close the field group m_aRun->append('}'); } - m_sURL.clear(); } + m_aURLs.pop(); return true; } diff --git a/sw/source/filter/ww8/rtfattributeoutput.hxx b/sw/source/filter/ww8/rtfattributeoutput.hxx index 2493ce38fbb0..2a739a330f1e 100644 --- a/sw/source/filter/ww8/rtfattributeoutput.hxx +++ b/sw/source/filter/ww8/rtfattributeoutput.hxx @@ -617,7 +617,7 @@ private: std::optional m_oFillStyle; /// If we're in the process of exporting a hyperlink, then its URL. - OUString m_sURL; + std::stack m_aURLs; /// If original file had \sbauto. bool m_bParaBeforeAutoSpacing; -- cgit