[Libreoffice-commits] core.git: Branch 'distro/vector/vector-5.4' - sw/CppunitTest_sw_uiwriter.mk sw/qa sw/source

Miklos Vajna vmiklos at collabora.co.uk
Thu Mar 29 12:46:09 UTC 2018


 sw/CppunitTest_sw_uiwriter.mk           |    2 +
 sw/qa/extras/uiwriter/data/image.odt    |binary
 sw/qa/extras/uiwriter/uiwriter.cxx      |   35 +++++++++++++++++++++++++++++++-
 sw/source/filter/html/htmlflywriter.cxx |    4 ++-
 sw/source/filter/html/wrthtml.cxx       |    9 ++++++++
 sw/source/filter/html/wrthtml.hxx       |    5 +++-
 6 files changed, 52 insertions(+), 3 deletions(-)

New commits:
commit e04010881740fdba545b35a38016006617b4b9d0
Author: Miklos Vajna <vmiklos at collabora.co.uk>
Date:   Wed Mar 28 15:00:10 2018 +0200

    tdf#108122 HTML export: fix lost images on copy
    
    As in the copy part of copy&paste. The problem was that the document has
    an empty base URL during copy, and this was images were not exported at
    all.
    
    An alternative fix would be to use embedded images, but sadly Word's
    HTML import doesn't understand that markup, so use tempfiles instead.
    
    Reviewed-on: https://gerrit.libreoffice.org/52003
    Reviewed-by: Miklos Vajna <vmiklos at collabora.co.uk>
    Tested-by: Jenkins <ci at libreoffice.org>
    (cherry picked from commit 6a189b2ac72d5fb83199bdb09e41f7d088440cc9)
    
    Conflicts:
            sw/qa/extras/uiwriter/uiwriter.cxx
            sw/source/filter/html/wrthtml.hxx
    
    Change-Id: Iab8c555ac244d943c4958f24f8ac61cba4ec3aba

diff --git a/sw/CppunitTest_sw_uiwriter.mk b/sw/CppunitTest_sw_uiwriter.mk
index 372909b9cfd3..89470422c528 100644
--- a/sw/CppunitTest_sw_uiwriter.mk
+++ b/sw/CppunitTest_sw_uiwriter.mk
@@ -45,6 +45,8 @@ $(eval $(call gb_CppunitTest_set_include,sw_uiwriter,\
     -I$(SRCDIR)/sw/inc \
     -I$(SRCDIR)/sw/source/core/inc \
     -I$(SRCDIR)/sw/source/uibase/inc \
+    -I$(SRCDIR)/sw/source/filter/inc \
+    -I$(SRCDIR)/sw/source/filter/html \
     -I$(SRCDIR)/sw/qa/extras/inc \
     $$(INCLUDE) \
 ))
diff --git a/sw/qa/extras/uiwriter/data/image.odt b/sw/qa/extras/uiwriter/data/image.odt
new file mode 100644
index 000000000000..195f8836cfad
Binary files /dev/null and b/sw/qa/extras/uiwriter/data/image.odt differ
diff --git a/sw/qa/extras/uiwriter/uiwriter.cxx b/sw/qa/extras/uiwriter/uiwriter.cxx
index d73a8aa083cb..bf0950166f6b 100644
--- a/sw/qa/extras/uiwriter/uiwriter.cxx
+++ b/sw/qa/extras/uiwriter/uiwriter.cxx
@@ -106,10 +106,13 @@
 #include <editeng/unolingu.hxx>
 #include <config_features.h>
 #include <sfx2/watermarkitem.hxx>
+#include <svtools/htmlout.hxx>
+#include <test/htmltesttools.hxx>
+#include <wrthtml.hxx>
 
 static const char* const DATA_DIRECTORY = "/sw/qa/extras/uiwriter/data/";
 
-class SwUiWriterTest : public SwModelTestBase
+class SwUiWriterTest : public SwModelTestBase, public HtmlTestTools
 {
 
 public:
@@ -251,6 +254,7 @@ public:
     void testCreateDocxAnnotation();
     void testTdf107976();
     void testTdf113790();
+    void testHtmlCopyImages();
 
     CPPUNIT_TEST_SUITE(SwUiWriterTest);
     CPPUNIT_TEST(testReplaceForward);
@@ -389,6 +393,7 @@ public:
     CPPUNIT_TEST(testCreateDocxAnnotation);
     CPPUNIT_TEST(testTdf107976);
     CPPUNIT_TEST(testTdf113790);
+    CPPUNIT_TEST(testHtmlCopyImages);
     CPPUNIT_TEST_SUITE_END();
 
 private:
@@ -4987,6 +4992,34 @@ void SwUiWriterTest::testTdf113790()
     CPPUNIT_ASSERT(dynamic_cast<SwXTextDocument *>(mxComponent.get()));
 }
 
+void SwUiWriterTest::testHtmlCopyImages()
+{
+    // Load a document with an image.
+    SwDoc* pDoc = createDoc("image.odt");
+
+    // Trigger the copy part of HTML copy&paste.
+    WriterRef xWrt;
+    xWrt = new SwHTMLWriter( /*rBaseURL=*/OUString() );
+    CPPUNIT_ASSERT(xWrt.is());
+
+    xWrt->bWriteClipboardDoc = true;
+    xWrt->bWriteOnlyFirstTable = false;
+    xWrt->SetShowProgress(false);
+    {
+        SvFileStream aStream(maTempFile.GetURL(), StreamMode::WRITE|StreamMode::TRUNC);
+        SwWriter aWrt(aStream, *pDoc);
+        aWrt.Write(xWrt);
+    }
+    htmlDocPtr pHtmlDoc = parseHtml(maTempFile);
+    CPPUNIT_ASSERT(pHtmlDoc);
+
+    // This failed, image was lost during HTML copy.
+    OUString aImage = getXPath(pHtmlDoc, "/html/body/p/img", "src");
+    // Also make sure that the image is not embedded (e.g. Word doesn't handle
+    // embedded images).
+    CPPUNIT_ASSERT(aImage.startsWith("file:///"));
+}
+
 CPPUNIT_TEST_SUITE_REGISTRATION(SwUiWriterTest);
 CPPUNIT_PLUGIN_IMPLEMENT();
 
diff --git a/sw/source/filter/html/htmlflywriter.cxx b/sw/source/filter/html/htmlflywriter.cxx
index 3885b87866b2..a4beb20de40d 100644
--- a/sw/source/filter/html/htmlflywriter.cxx
+++ b/sw/source/filter/html/htmlflywriter.cxx
@@ -1220,7 +1220,7 @@ Writer& OutHTML_Image( Writer& rWrt, const SwFrameFormat &rFrameFormat,
     }
 
     OUString aGraphicURL( rGraphicURL );
-    if( !rHTMLWrt.mbEmbedImages && !HTMLOutFuncs::PrivateURLToInternalImg(aGraphicURL) )
+    if( !rHTMLWrt.mbEmbedImages && !HTMLOutFuncs::PrivateURLToInternalImg(aGraphicURL) && !rHTMLWrt.mpTempBaseURL )
         aGraphicURL = URIHelper::simpleNormalizedMakeRelative( rWrt.GetBaseURL(), aGraphicURL);
 
     const SfxPoolItem* pItem;
@@ -1801,6 +1801,8 @@ static Writer& OutHTML_FrameFormatGrfNode( Writer& rWrt, const SwFrameFormat& rF
             // create a (mirrored) jpeg file
             if( rHTMLWrt.GetOrigFileName() )
                 aGraphicURL = *rHTMLWrt.GetOrigFileName();
+            else
+                aGraphicURL = rHTMLWrt.GetBaseURL();
             pGrfNd->GetGrf( true );
 
             XOutFlags nFlags = XOutFlags::UseGifIfSensible |
diff --git a/sw/source/filter/html/wrthtml.cxx b/sw/source/filter/html/wrthtml.cxx
index 4db9122288b2..ab71a1a96b22 100644
--- a/sw/source/filter/html/wrthtml.cxx
+++ b/sw/source/filter/html/wrthtml.cxx
@@ -78,6 +78,7 @@
 #include <tools/urlobj.hxx>
 #include <osl/file.hxx>
 #include <comphelper/scopeguard.hxx>
+#include <unotools/tempfile.hxx>
 
 #define MAX_INDENT_LEVEL 20
 
@@ -155,6 +156,14 @@ SwHTMLWriter::SwHTMLWriter( const OUString& rBaseURL )
     , m_bParaDotLeaders( false )
 {
     SetBaseURL(rBaseURL);
+
+    if (rBaseURL.isEmpty())
+    {
+        // Paste: set base URL to a tempfile, so images are not lost.
+        mpTempBaseURL.reset(new utl::TempFile());
+        mpTempBaseURL->EnableKillingFile();
+        SetBaseURL(mpTempBaseURL->GetURL());
+    }
 }
 
 SwHTMLWriter::~SwHTMLWriter()
diff --git a/sw/source/filter/html/wrthtml.hxx b/sw/source/filter/html/wrthtml.hxx
index e92cf9673cee..c8e953e322ff 100644
--- a/sw/source/filter/html/wrthtml.hxx
+++ b/sw/source/filter/html/wrthtml.hxx
@@ -60,6 +60,7 @@ class SwTextFootnote;
 enum class HtmlPosition;
 
 typedef std::vector<SwTextFootnote*> SwHTMLTextFootnotes;
+namespace utl { class TempFile; }
 
 extern SwAttrFnTab aHTMLAttrFnTab;
 
@@ -256,7 +257,7 @@ typedef std::set<std::unique_ptr<SwHTMLFormatInfo>,
 
 class IDocumentStylePoolAccess;
 
-class SwHTMLWriter : public Writer
+class SW_DLLPUBLIC SwHTMLWriter : public Writer
 {
     SwHTMLPosFlyFrames *m_pHTMLPosFlyFrames;
     std::unique_ptr<SwHTMLNumRuleInfo> m_pNumRuleInfo;// current numbering
@@ -390,6 +391,8 @@ public:
     /// If HTML header and footer should be written as well, or just the content itself.
     bool mbSkipHeaderFooter : 1;
     bool mbEmbedImages : 1;
+    /// Temporary base URL for paste of images.
+    std::unique_ptr<utl::TempFile> mpTempBaseURL;
     /// If XHTML markup should be written instead of HTML.
     bool mbXHTML = false;
     /// XML namespace, in case of XHTML.


More information about the Libreoffice-commits mailing list