sw HTML export, field shadings: give text portion background priority

If the user explicitly gives a background color to the field portion,
then respect that, don't overwrite with the field shading (the UI does
the same).

Change-Id: I7c35618f82a37ef1dd16c03b82651268767813af
Reviewed-on: https://gerrit.libreoffice.org/69127
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
diff --git a/sw/qa/extras/htmlexport/data/field-shade.odt b/sw/qa/extras/htmlexport/data/field-shade.odt
index 38debcb..2533be6 100644
--- a/sw/qa/extras/htmlexport/data/field-shade.odt
+++ b/sw/qa/extras/htmlexport/data/field-shade.odt
Binary files differ
diff --git a/sw/qa/extras/htmlexport/htmlexport.cxx b/sw/qa/extras/htmlexport/htmlexport.cxx
index 170213e..1f016f5 100644
--- a/sw/qa/extras/htmlexport/htmlexport.cxx
+++ b/sw/qa/extras/htmlexport/htmlexport.cxx
@@ -678,6 +678,12 @@ DECLARE_HTMLEXPORT_TEST(testFieldShade, "field-shade.odt")
    // Without the accompanying fix in place, this test would have failed with 'Expected: 1; Actual:
    // 0', i.e. shading for the field was lost.
    assertXPath(pDoc, "/html/body/p[1]/span", "style", "background: #c0c0c0");

    // Check that field shading is written only in case there is no user-defined span background.
    assertXPath(pDoc, "/html/body/p[2]/span", "style", "background: #ff0000");
    // Without the accompanying fix in place, this test would have failed with 'Expected: 0; Actual:
    // 1', i.e there was an inner span hiding the wanted background color.
    assertXPath(pDoc, "/html/body/p[2]/span/span", 0);
}

CPPUNIT_PLUGIN_IMPLEMENT();
diff --git a/sw/source/filter/html/htmlatr.cxx b/sw/source/filter/html/htmlatr.cxx
index 50a7562..d73f915 100644
--- a/sw/source/filter/html/htmlatr.cxx
+++ b/sw/source/filter/html/htmlatr.cxx
@@ -1931,6 +1931,7 @@ void HTMLEndPosLst::OutStartAttrs( SwHTMLWriter& rHWrt, sal_Int32 nPos,
                pContext = nullptr; // one time only
            }
            Out( aHTMLAttrFnTab, *pPos->GetItem(), rHWrt );
            rHWrt.maStartedAttributes[pPos->GetItem()->Which()]++;
            rHWrt.m_nCSS1Script = nCSS1Script;
        }
    }
@@ -1981,6 +1982,7 @@ void HTMLEndPosLst::OutEndAttrs( SwHTMLWriter& rHWrt, sal_Int32 nPos,
            if( !bSkipOut )
            {
                Out( aHTMLAttrFnTab, *pPos->GetItem(), rHWrt );
                rHWrt.maStartedAttributes[pPos->GetItem()->Which()]--;
            }
            RemoveItem_( i );
        }
diff --git a/sw/source/filter/html/htmlfldw.cxx b/sw/source/filter/html/htmlfldw.cxx
index e70e856..0ad1b55 100644
--- a/sw/source/filter/html/htmlfldw.cxx
+++ b/sw/source/filter/html/htmlfldw.cxx
@@ -539,7 +539,16 @@ Writer& OutHTML_SwFormatField( Writer& rWrt, const SfxPoolItem& rHt )
        if( pTextField )
        {
            SwHTMLWriter& rHTMLWrt = static_cast<SwHTMLWriter&>(rWrt);
            if (SwViewOption::IsFieldShadings())
            bool bFieldShadings = SwViewOption::IsFieldShadings();
            if (bFieldShadings)
            {
                // If there is a text portion background started already, that should have priority.
                auto it = rHTMLWrt.maStartedAttributes.find(RES_CHRATR_BACKGROUND);
                if (it != rHTMLWrt.maStartedAttributes.end())
                    bFieldShadings = it->second <= 0;
            }

            if (bFieldShadings)
            {
                OStringBuffer sOut;
                sOut.append("<" + rHTMLWrt.GetNamespace() + OOO_STRING_SVTOOLS_HTML_span);
@@ -556,7 +565,7 @@ Writer& OutHTML_SwFormatField( Writer& rWrt, const SfxPoolItem& rHt )
            OutHTML_SwField( rWrt, pField, pTextField->GetTextNode(),
                             pTextField->GetStart()  );

            if (SwViewOption::IsFieldShadings())
            if (bFieldShadings)
                HTMLOutFuncs::Out_AsciiTag(
                    rWrt.Strm(), rHTMLWrt.GetNamespace() + OOO_STRING_SVTOOLS_HTML_span, false);
        }
diff --git a/sw/source/filter/html/wrthtml.hxx b/sw/source/filter/html/wrthtml.hxx
index 9c96d86..b92b95f 100644
--- a/sw/source/filter/html/wrthtml.hxx
+++ b/sw/source/filter/html/wrthtml.hxx
@@ -22,6 +22,7 @@
#include <memory>
#include <vector>
#include <set>
#include <map>

#include <com/sun/star/container/XIndexContainer.hpp>
#include <com/sun/star/form/XForm.hpp>
@@ -398,6 +399,9 @@ public:
    bool m_bParaDotLeaders : 1;       // for TOC dot leaders
    // 25

    /// Tracks which text portion attributes are currently open: a which id -> open count map.
    std::map<sal_uInt16, int> maStartedAttributes;

    explicit SwHTMLWriter( const OUString& rBaseURL );
    virtual ~SwHTMLWriter() override;