sw/qa/extras/htmlexport/htmlexport.cxx |   57 +++++++++++++++++++++++++++++++++
 sw/source/filter/html/wrthtml.cxx      |    3 +
 2 files changed, 59 insertions(+), 1 deletion(-)

New commits:
commit f5003b9b1b44a16e28b1c3080d11347e5b365e1e
Author:     Vasily Melenchuk <vasily.melenc...@cib.de>
AuthorDate: Fri Aug 26 16:32:57 2022 +0300
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Mon Sep 5 14:44:07 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
    Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/139409

diff --git a/sw/qa/extras/htmlexport/htmlexport.cxx 
b/sw/qa/extras/htmlexport/htmlexport.cxx
index cc30027f906d..51af126b9bb1 100644
--- a/sw/qa/extras/htmlexport/htmlexport.cxx
+++ b/sw/qa/extras/htmlexport/htmlexport.cxx
@@ -2237,6 +2237,63 @@ CPPUNIT_TEST_FIXTURE(HtmlExportTest, testImageKeepRatio)
     assertXPath(pDoc, "/html/body/p/img", "height", "auto");
 }
 
+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 df4cef472ca2..0dafeb86c8d1 100644
--- a/sw/source/filter/html/wrthtml.cxx
+++ b/sw/source/filter/html/wrthtml.cxx
@@ -1266,7 +1266,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 );
 }

Reply via email to