sw/qa/extras/ooxmlexport/data/tdf114537_conditional-text2.doc |binary
 sw/qa/extras/ooxmlexport/ooxmlfieldexport.cxx                 |    9 +++++
 sw/source/filter/ww8/ww8par5.cxx                              |   18 +++++++++-
 3 files changed, 26 insertions(+), 1 deletion(-)

New commits:
commit f610082534745e88fd1fbfd8059948c38fb88fb6
Author:     Justin Luth <jl...@mail.com>
AuthorDate: Tue Jan 17 20:46:25 2023 -0500
Commit:     Justin Luth <jl...@mail.com>
CommitDate: Wed Jan 18 13:44:59 2023 +0000

    tdf#114537 doc import: trim switches before evaluating FIELD_IF
    
    Although this function is only used for ww8 import (and qa tests),
    it is documented as being more generic. So I decided to just trim
    at the source and not try to introduce any MS-isms into the parse function.
    Something similar will be needed for DOCX,
    but DOCX import for FIELD_IF is completely missing.
    
    Change-Id: I822b400e3e53abd953f4c382947f0e80ae62b234
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145691
    Tested-by: Jenkins
    Reviewed-by: Justin Luth <jl...@mail.com>

diff --git a/sw/qa/extras/ooxmlexport/data/tdf114537_conditional-text2.doc 
b/sw/qa/extras/ooxmlexport/data/tdf114537_conditional-text2.doc
new file mode 100644
index 000000000000..16f8076dc3c9
Binary files /dev/null and 
b/sw/qa/extras/ooxmlexport/data/tdf114537_conditional-text2.doc differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlfieldexport.cxx 
b/sw/qa/extras/ooxmlexport/ooxmlfieldexport.cxx
index 87d0878b6c0c..d20cf2338a07 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlfieldexport.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlfieldexport.cxx
@@ -818,6 +818,15 @@ CPPUNIT_TEST_FIXTURE(Test, testConditionalText)
     assertXPathContent(pXmlDoc, "/w:document/w:body/w:p/w:r[2]/w:instrText", 
OUString(aExpected));
 }
 
+CPPUNIT_TEST_FIXTURE(Test, testConditionalText2)
+{
+    loadAndReload("tdf114537_conditional-text2.doc");
+    // Load a document which has a conditional text field in it.
+    xmlDocUniquePtr pXmlDoc = parseExport("word/document.xml");
+    std::u16string_view aExpected(u" IF 1 = 1 \"test1\" \"test2\"");
+    assertXPathContent(pXmlDoc, "/w:document/w:body/w:p/w:r[3]/w:instrText", 
OUString(aExpected));
+}
+
 DECLARE_OOXMLEXPORT_TEST(testTdf142464_ampm, "tdf142464_ampm.docx")
 {
     css::uno::Reference<css::text::XTextFieldsSupplier> xTextFieldsSupplier(
diff --git a/sw/source/filter/ww8/ww8par5.cxx b/sw/source/filter/ww8/ww8par5.cxx
index 7e7cedd3f4b5..35cb2b507369 100644
--- a/sw/source/filter/ww8/ww8par5.cxx
+++ b/sw/source/filter/ww8/ww8par5.cxx
@@ -623,12 +623,28 @@ sal_uInt16 SwWW8ImplReader::End_Field()
             case ww::eIF: // IF-field
             {
                 // conditional field parameters
-                const OUString& fieldDefinition = 
m_aFieldStack.back().GetBookmarkCode();
+                OUString fieldDefinition = 
m_aFieldStack.back().GetBookmarkCode();
 
                 OUString paramCondition;
                 OUString paramTrue;
                 OUString paramFalse;
 
+                // ParseIfFieldDefinition expects: IF <some condition> "true 
result" "false result"
+                // while many fields include '\* MERGEFORMAT' after that.
+                // So first trim off the switches that are not supported anyway
+                sal_Int32 nLastIndex = fieldDefinition.lastIndexOf("\\*");
+                sal_Int32 nOtherIndex = fieldDefinition.lastIndexOf("\\#"); 
//number format
+                if (nOtherIndex > 0 && (nOtherIndex < nLastIndex || nLastIndex 
< 0))
+                    nLastIndex = nOtherIndex;
+                nOtherIndex = fieldDefinition.lastIndexOf("\\@"); //date format
+                if (nOtherIndex > 0 && (nOtherIndex < nLastIndex || nLastIndex 
< 0))
+                    nLastIndex = nOtherIndex;
+                nOtherIndex = fieldDefinition.lastIndexOf("\\!"); //locked 
result
+                if (nOtherIndex > 0 && (nOtherIndex < nLastIndex || nLastIndex 
< 0))
+                    nLastIndex = nOtherIndex;
+                if (nLastIndex > 0)
+                    fieldDefinition = fieldDefinition.copy(0, nLastIndex);
+
                 SwHiddenTextField::ParseIfFieldDefinition(fieldDefinition, 
paramCondition, paramTrue, paramFalse);
 
                 // create new field

Reply via email to