sw/qa/extras/htmlexport/htmlexport.cxx | 57 +++++++++++++++++++++++++++++++++ sw/source/filter/html/wrthtml.cxx | 3 + 2 files changed, 59 insertions(+), 1 deletion(-)
New commits: commit 184a9eec62058dcbe185d74cfd40a3772d3b801d Author: Vasily Melenchuk <vasily.melenc...@cib.de> AuthorDate: Fri Aug 26 16:32:57 2022 +0300 Commit: Thorsten Behrens <thorsten.behr...@allotropia.de> CommitDate: Wed Aug 31 03:48:51 2022 +0200 tdf#114769: sw html export: better handling for relative URLs Usage of INetURLObject makes sense only in case of full URLs, but once relative one is provided it will contain invalid data. Change-Id: Icc5e191e2337a3dd9a76c660b1c7709551099a1a Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138875 Tested-by: Jenkins Reviewed-by: Thorsten Behrens <thorsten.behr...@allotropia.de> (cherry picked from commit 599da3fa69805ebf8dee4517855fd8706e19d11d) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138979 diff --git a/sw/qa/extras/htmlexport/htmlexport.cxx b/sw/qa/extras/htmlexport/htmlexport.cxx index 5e5af5e70a17..ab4d71a7d02e 100644 --- a/sw/qa/extras/htmlexport/htmlexport.cxx +++ b/sw/qa/extras/htmlexport/htmlexport.cxx @@ -2392,6 +2392,63 @@ CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testSectionDir) assertXPath(pXmlDoc, "//reqif-xhtml:div[@id='mysect']", "style", "dir: ltr"); } +CPPUNIT_TEST_FIXTURE(HtmlExportTest, testTdf114769) +{ + // Create document from scratch since relative urls to filesystem can be replaced + // by absolute during save/load + SwDoc* pDoc = createSwDoc(); + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + pWrtShell->Insert("Hyperlink1"); + pWrtShell->SplitNode(); + pWrtShell->Insert("Hyperlink2"); + pWrtShell->SplitNode(); + pWrtShell->Insert("Hyperlink3"); + pWrtShell->SplitNode(); + pWrtShell->Insert("Hyperlink4"); + pWrtShell->SplitNode(); + pWrtShell->Insert("Hyperlink5"); + pWrtShell->SplitNode(); + + // Normal external URL + uno::Reference<beans::XPropertySet> xRun(getRun(getParagraph(1), 1), uno::UNO_QUERY); + xRun->setPropertyValue("HyperLinkURL", uno::Any(OUString("http://libreoffice.org/"))); + + // Bookmark reference + xRun.set(getRun(getParagraph(2), 1), uno::UNO_QUERY); + xRun->setPropertyValue("HyperLinkURL", uno::Any(OUString("#some_bookmark"))); + + // Filesystem absolute link + xRun.set(getRun(getParagraph(3), 1), uno::UNO_QUERY); + xRun->setPropertyValue("HyperLinkURL", uno::Any(OUString("C:\\test.txt"))); + + // Filesystem relative link + xRun.set(getRun(getParagraph(4), 1), uno::UNO_QUERY); + xRun->setPropertyValue("HyperLinkURL", uno::Any(OUString("..\\..\\test.odt"))); + + // Filesystem relative link + xRun.set(getRun(getParagraph(5), 1), uno::UNO_QUERY); + xRun->setPropertyValue("HyperLinkURL", uno::Any(OUString(".\\another.odt"))); + + // Export + uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY); + uno::Sequence<beans::PropertyValue> aStoreProperties + = { comphelper::makePropertyValue("FilterName", OUString("HTML (StarWriter)")) }; + xStorable->storeToURL(maTempFile.GetURL(), aStoreProperties); + + htmlDocUniquePtr pHtmlDoc = parseHtml(maTempFile); + CPPUNIT_ASSERT(pHtmlDoc); + + CPPUNIT_ASSERT_EQUAL(OUString("http://libreoffice.org/"), + getXPath(pHtmlDoc, "/html/body/p[1]/a", "href")); + CPPUNIT_ASSERT_EQUAL(OUString("#some_bookmark"), + getXPath(pHtmlDoc, "/html/body/p[2]/a", "href")); + CPPUNIT_ASSERT_EQUAL(OUString("C:\\test.txt"), getXPath(pHtmlDoc, "/html/body/p[3]/a", "href")); + CPPUNIT_ASSERT_EQUAL(OUString("..\\..\\test.odt"), + getXPath(pHtmlDoc, "/html/body/p[4]/a", "href")); + CPPUNIT_ASSERT_EQUAL(OUString(".\\another.odt"), + getXPath(pHtmlDoc, "/html/body/p[5]/a", "href")); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/filter/html/wrthtml.cxx b/sw/source/filter/html/wrthtml.cxx index 0cede88912d3..f0a963972859 100644 --- a/sw/source/filter/html/wrthtml.cxx +++ b/sw/source/filter/html/wrthtml.cxx @@ -1334,7 +1334,8 @@ OUString SwHTMLWriter::convertHyperlinkHRefValue(const OUString& rURL) { // Link is not started from "#", so looks like external link. Encode this URL. INetURLObject aURL(sURL); - sURL = aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE); + if (!aURL.HasError()) + sURL = aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE); } return URIHelper::simpleNormalizedMakeRelative( GetBaseURL(), sURL ); }