From 0ae159cc40b837a6a47acaf9240dc350fe68d49d Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Tue, 20 Apr 2021 11:03:19 +0200 Subject: sw bibliography table, text generator: handle relative URLs Now that the doc model always contains absolute URLs, it is possible to decide when it does and when it doesn't make sense to convert to relative URLs. The bibliography table does a mixed approach: the document text depends on configuration (relative by default), but the underlying URL is an absolute one. This way it's always clickable and the ODT filter can save it as a relative one (again, depending on configuration). With this, all fields are meant to be clickable: biblio field vs biblio table, relative or absolute ones, with or with a (page) fragment, Writer UI vs PDF export. Change-Id: Ib6879709314fa127094faf7fa615fe57289b41f1 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/114299 Reviewed-by: Miklos Vajna Tested-by: Jenkins --- sw/source/core/tox/ToxTextGenerator.cxx | 6 +++++- sw/source/core/tox/txmsrt.cxx | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/sw/source/core/tox/ToxTextGenerator.cxx b/sw/source/core/tox/ToxTextGenerator.cxx index 95952aa06cd2..df37f294796b 100644 --- a/sw/source/core/tox/ToxTextGenerator.cxx +++ b/sw/source/core/tox/ToxTextGenerator.cxx @@ -268,7 +268,11 @@ ToxTextGenerator::GenerateText(SwDoc* pDoc, rBase.FillText( *pTOXNd, aIdx, static_cast(eField), pLayout ); if (eField == ToxAuthorityField::AUTH_FIELD_URL) { - OUString aURL(rText.subView(nStartCharStyle)); + // Get the absolute URL, the text may be a relative one. + const auto& rAuthority = static_cast(rBase); + OUString aURL = SwTOXAuthority::GetSourceURL( + rAuthority.GetText(AUTH_FIELD_URL, pLayout)); + mLinkProcessor->CloseLink(rText.getLength(), aURL, /*bRelative=*/false); } } diff --git a/sw/source/core/tox/txmsrt.cxx b/sw/source/core/tox/txmsrt.cxx index 193dc1d48a71..e0ffc1922701 100644 --- a/sw/source/core/tox/txmsrt.cxx +++ b/sw/source/core/tox/txmsrt.cxx @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -878,6 +879,36 @@ void SwTOXAuthority::FillText(SwTextNode& rNd, const SwIndex& rInsPos, sal_uInt1 if (nAuthField == AUTH_FIELD_URL) { aText = GetSourceURL(aText); + + // Convert URL to a relative one if requested. + SwDoc* pDoc = static_cast(m_rField.GetField()->GetTyp())->GetDoc(); + SwDocShell* pDocShell = pDoc->GetDocShell(); + OUString aBaseURL = pDocShell->getDocumentBaseURL(); + OUString aBaseURIScheme; + sal_Int32 nSep = aBaseURL.indexOf(':'); + if (nSep != -1) + { + aBaseURIScheme = aBaseURL.copy(0, nSep); + } + + uno::Reference xUriReferenceFactory + = uri::UriReferenceFactory::create(comphelper::getProcessComponentContext()); + uno::Reference xUriRef; + try + { + xUriRef = xUriReferenceFactory->parse(aText); + } + catch (const uno::Exception& rException) + { + SAL_WARN("sw.core", + "SwTOXAuthority::FillText: failed to parse url: " << rException.Message); + } + + bool bSaveRelFSys = officecfg::Office::Common::Save::URL::FileSystem::get(); + if (xUriRef.is() && bSaveRelFSys && xUriRef->getScheme() == aBaseURIScheme) + { + aText = INetURLObject::GetRelURL(aBaseURL, aText); + } } rNd.InsertText(aText, rInsPos); -- cgit v1.2.3