diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2024-07-26 23:04:34 +0500 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2024-07-27 02:03:15 +0200 |
commit | 4be345abd678babcbb989db1e7a16021ad1da562 (patch) | |
tree | 3a8068019187343952b09fd3a5bb61f217ce5cb3 /sw/qa/extras/htmlexport | |
parent | 0b683547bbb22cab46e92dfd65c129630bd9ca94 (diff) |
HTML/ReqIF export: introduce RelativeOwnObjectURL filter option
When the use cases require use of absolute URLs at export (Options->
Load/Save->General, Save URLs relative to file system and Save URLs
relative to internet), the fles generated from the document itself
(e.g., images that aren't embedded into HTML/ReqIF itself) may still
need to be referenced relatively.
This introduces the export filter option, named RelativeOwnObjectURL,
that overrides the general export settings specifically for these
self-generated objects.
Change-Id: I09aeb931db5712271a40c683370316783507775a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/171083
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'sw/qa/extras/htmlexport')
-rw-r--r-- | sw/qa/extras/htmlexport/data/URLs.odt | bin | 0 -> 18377 bytes | |||
-rw-r--r-- | sw/qa/extras/htmlexport/data/external.png | bin | 0 -> 173 bytes | |||
-rw-r--r-- | sw/qa/extras/htmlexport/htmlexport.cxx | 221 |
3 files changed, 221 insertions, 0 deletions
diff --git a/sw/qa/extras/htmlexport/data/URLs.odt b/sw/qa/extras/htmlexport/data/URLs.odt Binary files differnew file mode 100644 index 000000000000..fd3593113f31 --- /dev/null +++ b/sw/qa/extras/htmlexport/data/URLs.odt diff --git a/sw/qa/extras/htmlexport/data/external.png b/sw/qa/extras/htmlexport/data/external.png Binary files differnew file mode 100644 index 000000000000..6ed2b8afc0af --- /dev/null +++ b/sw/qa/extras/htmlexport/data/external.png diff --git a/sw/qa/extras/htmlexport/htmlexport.cxx b/sw/qa/extras/htmlexport/htmlexport.cxx index a7878823f63b..b0976c5a764f 100644 --- a/sw/qa/extras/htmlexport/htmlexport.cxx +++ b/sw/qa/extras/htmlexport/htmlexport.cxx @@ -27,11 +27,14 @@ #include <com/sun/star/packages/zip/ZipFileAccess.hpp> #include <com/sun/star/view/XSelectionSupplier.hpp> +#include <officecfg/Office/Common.hxx> + #include <test/htmltesttools.hxx> #include <tools/urlobj.hxx> #include <svtools/rtfkeywd.hxx> #include <comphelper/propertyvalue.hxx> #include <comphelper/propertysequence.hxx> +#include <comphelper/scopeguard.hxx> #include <svtools/parrtf.hxx> #include <rtl/strbuf.hxx> #include <svtools/rtftoken.h> @@ -3168,6 +3171,224 @@ CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testHTML_161979) CPPUNIT_ASSERT(numNonTransparent < size.Height() * size.Width()); } +CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testReqIF_exportAbsoluteURLs_ownRelative) +{ + auto pBatch(comphelper::ConfigurationChanges::create()); + Resetter resetter([ + bInternetPreviousValue = officecfg::Office::Common::Save::URL::Internet::get(), + bFileSystemPreviousValue = officecfg::Office::Common::Save::URL::FileSystem::get(), pBatch + ]() { + officecfg::Office::Common::Save::URL::Internet::set(bInternetPreviousValue, pBatch); + officecfg::Office::Common::Save::URL::FileSystem::set(bFileSystemPreviousValue, pBatch); + return pBatch->commit(); + }); + // Set saving absolute URLs + officecfg::Office::Common::Save::URL::Internet::set(false, pBatch); + officecfg::Office::Common::Save::URL::FileSystem::set(false, pBatch); + pBatch->commit(); + + createSwDoc("URLs.odt"); + // Export to ReqIF, using absolute URLs + saveWithParams({ + comphelper::makePropertyValue(u"FilterName"_ustr, u"HTML (StarWriter)"_ustr), + comphelper::makePropertyValue(u"FilterOptions"_ustr, u"xhtmlns=reqif-xhtml"_ustr), + comphelper::makePropertyValue(u"ExportImagesAsOLE"_ustr, true), + comphelper::makePropertyValue(u"RelativeOwnObjectURL"_ustr, true), + }); + xmlDocUniquePtr pXmlDoc = WrapReqifFromTempFile(); + + // HTTP URL: must be absolute + assertXPath(pXmlDoc, "//reqif-xhtml:p[1]/reqif-xhtml:a"_ostr, "href"_ostr, + u"http://www.example.org/"_ustr); + // file URL: must be absolute + assertXPath(pXmlDoc, "//reqif-xhtml:p[2]/reqif-xhtml:a"_ostr, "href"_ostr, + createFileURL(u"NonExistingPath/NonExistingFile.html")); + // form URL: must be absolute + assertXPath(pXmlDoc, "//reqif-xhtml:form"_ostr, "action"_ostr, + u"https://www.example.org/submit"_ustr); + // linked image exported as object: generated, must be relative + OUString url = getXPath(pXmlDoc, "//reqif-xhtml:p[3]/reqif-xhtml:object"_ostr, "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".ole")); + // its original image URL: must be absolute + assertXPath(pXmlDoc, "//reqif-xhtml:p[3]/reqif-xhtml:object/reqif-xhtml:object"_ostr, + "data"_ostr, createFileURL(u"external.png")); + // embedded image exported as object: generated, must be relative + url = getXPath(pXmlDoc, "//reqif-xhtml:p[4]/reqif-xhtml:object"_ostr, "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".ole")); + // its image URL: generated, must be relative + url = getXPath(pXmlDoc, "//reqif-xhtml:p[4]/reqif-xhtml:object/reqif-xhtml:object"_ostr, + "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".png")); + // unordered list with image bullet - it gets embedded as base64 data + OUString style = getXPath(pXmlDoc, "//reqif-xhtml:ul"_ostr, "style"_ostr); + CPPUNIT_ASSERT(style.indexOf("list-style-image: url(data:image/png;base64,") != -1); + // an as-char frame, exported as a whole to an object, must be relative + url = getXPath(pXmlDoc, "//reqif-xhtml:p[5]/reqif-xhtml:object"_ostr, "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".ole")); + // its file hyperlink must be absolute + assertXPath(pXmlDoc, "//reqif-xhtml:p[5]/reqif-xhtml:object/reqif-xhtml:a"_ostr, "href"_ostr, + createFileURL(u"foo/bar")); + // its image URL: generated, must be relative + url = getXPath(pXmlDoc, + "//reqif-xhtml:p[5]/reqif-xhtml:object/reqif-xhtml:a/reqif-xhtml:object"_ostr, + "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".png")); +} + +CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testReqIF_exportRelativeURLs) +{ + CPPUNIT_ASSERT(officecfg::Office::Common::Save::URL::Internet::get()); + CPPUNIT_ASSERT(officecfg::Office::Common::Save::URL::FileSystem::get()); + + createSwDoc("URLs.odt"); + // Export to ReqIF, using relative URLs (the default) + saveWithParams({ + comphelper::makePropertyValue(u"FilterName"_ustr, u"HTML (StarWriter)"_ustr), + comphelper::makePropertyValue(u"FilterOptions"_ustr, u"xhtmlns=reqif-xhtml"_ustr), + comphelper::makePropertyValue(u"ExportImagesAsOLE"_ustr, true), + }); + xmlDocUniquePtr pXmlDoc = WrapReqifFromTempFile(); + + // HTTP URL: must be absolute + assertXPath(pXmlDoc, "//reqif-xhtml:p[1]/reqif-xhtml:a"_ostr, "href"_ostr, + u"http://www.example.org/"_ustr); + // file URL: must be relative + OUString url = getXPath(pXmlDoc, "//reqif-xhtml:p[2]/reqif-xhtml:a"_ostr, "href"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith("NonExistingPath/NonExistingFile.html")); + // form URL: must be absolute + assertXPath(pXmlDoc, "//reqif-xhtml:form"_ostr, "action"_ostr, + u"https://www.example.org/submit"_ustr); + // linked image exported as object: generated, must be relative + url = getXPath(pXmlDoc, "//reqif-xhtml:p[3]/reqif-xhtml:object"_ostr, "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".ole")); + // its original image URL: must be relative + url = getXPath(pXmlDoc, "//reqif-xhtml:p[3]/reqif-xhtml:object/reqif-xhtml:object"_ostr, + "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith("external.png")); + // embedded image exported as object: generated, must be relative + url = getXPath(pXmlDoc, "//reqif-xhtml:p[4]/reqif-xhtml:object"_ostr, "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".ole")); + // its image URL: generated, must be relative + url = getXPath(pXmlDoc, "//reqif-xhtml:p[4]/reqif-xhtml:object/reqif-xhtml:object"_ostr, + "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".png")); + // unordered list with image bullet - it gets embedded as base64 data + OUString style = getXPath(pXmlDoc, "//reqif-xhtml:ul"_ostr, "style"_ostr); + CPPUNIT_ASSERT(style.indexOf("list-style-image: url(data:image/png;base64,") != -1); + // an as-char frame, exported as a whole to an object, must be relative + url = getXPath(pXmlDoc, "//reqif-xhtml:p[5]/reqif-xhtml:object"_ostr, "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".ole")); + // its file hyperlink must be relative + url = getXPath(pXmlDoc, "//reqif-xhtml:p[5]/reqif-xhtml:object/reqif-xhtml:a"_ostr, + "href"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith("foo/bar")); + // its image URL: generated, must be relative + url = getXPath(pXmlDoc, + "//reqif-xhtml:p[5]/reqif-xhtml:object/reqif-xhtml:a/reqif-xhtml:object"_ostr, + "data"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".png")); +} + +CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testHTML_exportAbsoluteURLs_ownRelative) +{ + auto pBatch(comphelper::ConfigurationChanges::create()); + Resetter resetter([ + bInternetPreviousValue = officecfg::Office::Common::Save::URL::Internet::get(), + bFileSystemPreviousValue = officecfg::Office::Common::Save::URL::FileSystem::get(), pBatch + ]() { + officecfg::Office::Common::Save::URL::Internet::set(bInternetPreviousValue, pBatch); + officecfg::Office::Common::Save::URL::FileSystem::set(bFileSystemPreviousValue, pBatch); + return pBatch->commit(); + }); + // Set saving absolute URLs + officecfg::Office::Common::Save::URL::Internet::set(false, pBatch); + officecfg::Office::Common::Save::URL::FileSystem::set(false, pBatch); + pBatch->commit(); + + createSwDoc("URLs.odt"); + // Export to HTML, using absolute URLs + saveWithParams({ + comphelper::makePropertyValue(u"FilterName"_ustr, u"HTML (StarWriter)"_ustr), + comphelper::makePropertyValue(u"RelativeOwnObjectURL"_ustr, true), + }); + htmlDocUniquePtr pHtmlDoc = parseHtml(maTempFile); + + // HTTP URL: must be absolute + assertXPath(pHtmlDoc, "//p[1]/a"_ostr, "href"_ostr, u"http://www.example.org/"_ustr); + // file URL: must be absolute + assertXPath(pHtmlDoc, "//p[2]/a"_ostr, "href"_ostr, + createFileURL(u"NonExistingPath/NonExistingFile.html")); + // form URL: must be absolute + assertXPath(pHtmlDoc, "//form"_ostr, "action"_ostr, u"https://www.example.org/submit"_ustr); + // linked image: must be absolute + assertXPath(pHtmlDoc, "//p[3]/img"_ostr, "src"_ostr, createFileURL(u"external.png")); + // embedded image: generated, must be relative + OUString url = getXPath(pHtmlDoc, "//p[4]/img"_ostr, "src"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".png")); + // unordered list with image bullet - it gets embedded as base64 data + OUString style = getXPath(pHtmlDoc, "//ul"_ostr, "style"_ostr); + CPPUNIT_ASSERT(style.indexOf("list-style-image: url(data:image/png;base64,") != -1); + // image-in-frame file hyperlink must be absolute; FIXME: HTMLOutFuncs::Out_ImageMap + // assertXPath(pHtmlDoc, "//p[5]/map/area"_ostr, "href"_ostr, createFileURL(u"foo/bar")); + // its image URL: generated, must be relative + url = getXPath(pHtmlDoc, "//p[5]/img"_ostr, "src"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".gif")); +} + +CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testHTML_exportRelativeURLs) +{ + CPPUNIT_ASSERT(officecfg::Office::Common::Save::URL::Internet::get()); + CPPUNIT_ASSERT(officecfg::Office::Common::Save::URL::FileSystem::get()); + + createSwDoc("URLs.odt"); + // Export to HTML, using relative URLs (the default) + ExportToHTML(); + htmlDocUniquePtr pHtmlDoc = parseHtml(maTempFile); + + // HTTP URL: must be absolute + assertXPath(pHtmlDoc, "//p[1]/a"_ostr, "href"_ostr, u"http://www.example.org/"_ustr); + // file URL: must be relative + OUString url = getXPath(pHtmlDoc, "//p[2]/a"_ostr, "href"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith("NonExistingPath/NonExistingFile.html")); + // form URL: must be absolute + assertXPath(pHtmlDoc, "//form"_ostr, "action"_ostr, u"https://www.example.org/submit"_ustr); + // linked image: must be relative + url = getXPath(pHtmlDoc, "//p[3]/img"_ostr, "src"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith("external.png")); + // embedded image: generated, must be relative + url = getXPath(pHtmlDoc, "//p[4]/img"_ostr, "src"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".png")); + // unordered list with image bullet - it gets embedded as base64 data + OUString style = getXPath(pHtmlDoc, "//ul"_ostr, "style"_ostr); + CPPUNIT_ASSERT(style.indexOf("list-style-image: url(data:image/png;base64,") != -1); + // image-in-frame file hyperlink must be relative + url = getXPath(pHtmlDoc, "//p[5]/map/area"_ostr, "href"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith("foo/bar")); + // its image URL: generated, must be relative + url = getXPath(pHtmlDoc, "//p[5]/img"_ostr, "src"_ostr); + CPPUNIT_ASSERT(!url.startsWith("file:")); + CPPUNIT_ASSERT(url.endsWith(".gif")); +} + } // end of anonymous namespace CPPUNIT_PLUGIN_IMPLEMENT(); |