oox/source/drawingml/textbodycontext.cxx     |   11 +++++++++
 oox/source/drawingml/textrun.cxx             |    8 +++++-
 sw/qa/extras/ooxmlimport/data/tdf126426.docx |binary
 sw/qa/extras/ooxmlimport/ooxmlimport2.cxx    |   32 +++++++++++++++++++++++++++
 4 files changed, 50 insertions(+), 1 deletion(-)

New commits:
commit 5a2543daa64c901d8577dff3711a14438c46a13d
Author:     Vasily Melenchuk <[email protected]>
AuthorDate: Fri Aug 27 18:10:15 2021 +0300
Commit:     Thorsten Behrens <[email protected]>
CommitDate: Tue Sep 14 10:16:40 2021 +0200

    tdf#126426: support for hyperlinks in TextParagraphContext
    
    Usually hyperlinks are processed by TextBodyContext, but
    for grouped shape we accidentaly gone into TextParagraphContext
    It has almost all possibilities to process txbxContent,
    but not hyperlinks.
    
    Additionally some hyperlink char attributes (color and underline)
    can expand to follow up ordinal text. Additional small hack applied
    to avoid this.
    
    Unfortunately this is not a final solution: such document fails
    roundtrip and hyperlinks are lost after saving to DOCX.
    
    Change-Id: Ie954f53696bd872cb1f59cb586fb55f6cd7c73bc
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/121172
    Tested-by: Jenkins
    Reviewed-by: Thorsten Behrens <[email protected]>
    
     Conflicts:
            oox/source/drawingml/textbodycontext.cxx
            sw/qa/extras/ooxmlimport/ooxmlimport2.cxx
    
    Change-Id: I4b2c098dd81dbb9112fff038e9b989c111c5bbe7
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/121923
    Tested-by: Thorsten Behrens <[email protected]>
    Reviewed-by: Thorsten Behrens <[email protected]>

diff --git a/oox/source/drawingml/textbodycontext.cxx 
b/oox/source/drawingml/textbodycontext.cxx
index cbd1e420ee5a..444346a98de9 100644
--- a/oox/source/drawingml/textbodycontext.cxx
+++ b/oox/source/drawingml/textbodycontext.cxx
@@ -28,6 +28,7 @@
 #include <oox/drawingml/shape.hxx>
 #include <oox/token/namespaces.hxx>
 #include <oox/token/tokens.hxx>
+#include "hyperlinkcontext.hxx"
 
 #include <oox/mathml/import.hxx>
 
@@ -100,6 +101,16 @@ ContextHandlerRef TextParagraphContext::onCreateContext( 
sal_Int32 aElementToken
         case OOX_TOKEN(a14, m):
             return CreateLazyMathBufferingContext(*this, mrParagraph);
         break;
+        case W_TOKEN( hyperlink ):
+        {
+            TextRunPtr pRun = std::make_shared<TextRun>();
+            mrParagraph.addRun(pRun);
+            // parse hyperlink attributes: use HyperLinkContext for that
+            rtl::Reference<HyperLinkContext> pContext(new HyperLinkContext(
+                *this, rAttribs, 
pRun->getTextCharacterProperties().maHyperlinkPropertyMap));
+            // but create text run context because HyperLinkContext can't 
process internal w:r, w:t, etc
+            return new RegularTextRunContext(*this, pRun);
+        }
         default:
             SAL_WARN("oox", "TextParagraphContext::onCreateContext: unhandled 
element: " << getBaseToken(aElementToken));
     }
diff --git a/oox/source/drawingml/textrun.cxx b/oox/source/drawingml/textrun.cxx
index 3f66c7a852e4..a4b40ea7e9ae 100644
--- a/oox/source/drawingml/textrun.cxx
+++ b/oox/source/drawingml/textrun.cxx
@@ -69,7 +69,13 @@ sal_Int32 TextRun::insertAt(
         Any aOldFontFamily = xState->getPropertyDefault("CharFontFamily");
 
         TextCharacterProperties aTextCharacterProps( rTextCharacterStyle );
-        aTextCharacterProps.assignUsed( maTextCharacterProperties );
+
+        // If no text color specified lets anyway initialize it as default:
+        // this will help to recover after hyperlink
+        if (!aTextCharacterProps.maFillProperties.maFillColor.isUsed())
+            aTextCharacterProps.maFillProperties.moFillType = XML_solidFill;
+
+        aTextCharacterProps.assignUsed(maTextCharacterProperties);
         if ( aTextCharacterProps.moHeight.has() )
             nCharHeight = aTextCharacterProps.moHeight.get();
         else
diff --git a/sw/qa/extras/ooxmlimport/data/tdf126426.docx 
b/sw/qa/extras/ooxmlimport/data/tdf126426.docx
new file mode 100644
index 000000000000..d77051365ca2
Binary files /dev/null and b/sw/qa/extras/ooxmlimport/data/tdf126426.docx differ
diff --git a/sw/qa/extras/ooxmlimport/ooxmlimport2.cxx 
b/sw/qa/extras/ooxmlimport/ooxmlimport2.cxx
index 4b69a62859c6..acf1a0bc2626 100644
--- a/sw/qa/extras/ooxmlimport/ooxmlimport2.cxx
+++ b/sw/qa/extras/ooxmlimport/ooxmlimport2.cxx
@@ -19,6 +19,7 @@
 #include <com/sun/star/document/XEmbeddedObjectSupplier2.hpp>
 #include <com/sun/star/embed/Aspects.hpp>
 #include <com/sun/star/text/WritingMode2.hpp>
+#include <com/sun/star/text/XTextField.hpp>
 #include <com/sun/star/style/BreakType.hpp>
 #include <xmloff/odffields.hxx>
 #include <IDocumentMarkAccess.hxx>
@@ -583,6 +584,37 @@ DECLARE_OOXMLIMPORT_TEST(testTdf129912, "tdf129912.docx")
     }
 }
 
+CPPUNIT_TEST_FIXTURE(Test, testTdf126426)
+{
+    load(mpTestDocumentPath, "tdf126426.docx");
+
+    uno::Reference<container::XIndexAccess> xGroup(getShape(1), 
uno::UNO_QUERY_THROW);
+    CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xGroup->getCount());
+
+    // get second shape in group
+    uno::Reference<container::XEnumerationAccess> 
xParaEnumAccess(xGroup->getByIndex(1),
+                                                                  
uno::UNO_QUERY_THROW);
+    uno::Reference<container::XEnumeration> xParaEnum = 
xParaEnumAccess->createEnumeration();
+
+    uno::Reference<text::XTextRange> xPara(xParaEnum->nextElement(), 
uno::UNO_QUERY_THROW);
+    uno::Reference<container::XEnumerationAccess> xRunEnumAccess(xPara, 
uno::UNO_QUERY_THROW);
+
+    uno::Reference<container::XEnumeration> xRunEnum = 
xRunEnumAccess->createEnumeration();
+    {
+        // Text before: was before this bugfix
+        uno::Reference<text::XTextRange> xRun(xRunEnum->nextElement(), 
uno::UNO_QUERY_THROW);
+        CPPUNIT_ASSERT_EQUAL(OUString("Some text "), xRun->getString());
+    }
+    {
+        // Link and this content was completely missong before
+        uno::Reference<text::XTextRange> xRun(xRunEnum->nextElement(), 
uno::UNO_QUERY_THROW);
+        CPPUNIT_ASSERT_EQUAL(OUString("Link"), xRun->getString());
+        auto xURLField = getProperty<uno::Reference<text::XTextField>>(xRun, 
"TextField");
+        auto aURL = getProperty<OUString>(xURLField, "URL");
+        CPPUNIT_ASSERT_EQUAL(OUString("http://libreoffice.org/";), aURL);
+    }
+}
+
 // tests should only be added to ooxmlIMPORT *if* they fail round-tripping in 
ooxmlEXPORT
 
 CPPUNIT_PLUGIN_IMPLEMENT();

Reply via email to