sw/CppunitTest_sw_htmlexport.mk        |    1 
 sw/qa/extras/htmlexport/htmlexport.cxx |   47 ++++++++++++++++++++++++++++++---
 sw/source/filter/html/css1atr.cxx      |   13 +++++----
 sw/source/filter/html/htmltabw.cxx     |   16 ++++++++---
 sw/source/filter/html/wrthtml.hxx      |    5 ++-
 5 files changed, 68 insertions(+), 14 deletions(-)

New commits:
commit e14b6d2e63990c7571c0a4f0f1a767d5955055fb
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Tue May 10 16:15:52 2022 +0200
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Wed May 11 10:57:28 2022 +0200

    sw XHTML export: output table / table row background format using CSS
    
    The HTML export uses HTML elements / attributes to describe the
    background color of tables / table rows. CSS markup is used when
    exporting styles, not direct formatting.
    
    This behavior makes sense for the HTML mode, but XHTML wants to CSS
    markup whenever possible, and usage of CSS markup for the reqif mode is
    not even optional.
    
    Fix the problem by switching to CSS markup for table and table row
    backgrounds in the XHTML case -- this is OK for reqif when describing
    tables (the reqif spec only forbids detailed CSS markup for spans, not
    tables or table rows).
    
    This amends the behavior of commit
    4cd3c436923bfba281b1bf16d9785208a2119cea (sw reqif-xhtml export: limit
    values of the style attribute, 2018-04-11), which avoided invalid table
    row background markup by losing it on export.
    
    (cherry picked from commit c3c3303516c3da9372dce3f05f38f15a104e961c)
    
    Conflicts:
            sw/qa/extras/htmlexport/htmlexport.cxx
            sw/source/filter/html/css1atr.cxx
            sw/source/filter/html/wrthtml.hxx
    
    Change-Id: Ia0858986c3e8a3ea41adf8a24119442fe5068ee3

diff --git a/sw/CppunitTest_sw_htmlexport.mk b/sw/CppunitTest_sw_htmlexport.mk
index 3315566bb087..efa4a82d807f 100644
--- a/sw/CppunitTest_sw_htmlexport.mk
+++ b/sw/CppunitTest_sw_htmlexport.mk
@@ -21,6 +21,7 @@ $(eval $(call gb_CppunitTest_use_libraries,sw_htmlexport, \
     comphelper \
     cppu \
        cppuhelper \
+       editeng \
        i18nlangtag \
        msfilter \
     sal \
diff --git a/sw/qa/extras/htmlexport/htmlexport.cxx 
b/sw/qa/extras/htmlexport/htmlexport.cxx
index f55002c04a3d..5569c1205557 100644
--- a/sw/qa/extras/htmlexport/htmlexport.cxx
+++ b/sw/qa/extras/htmlexport/htmlexport.cxx
@@ -40,6 +40,9 @@
 #include <svl/eitem.hxx>
 #include <vcl/graphicfilter.hxx>
 #include <vcl/dibtools.hxx>
+#include <editeng/brushitem.hxx>
+
+#include <itabenum.hxx>
 
 namespace
 {
@@ -743,9 +746,9 @@ DECLARE_HTMLEXPORT_TEST(testReqIfTable, "reqif-table.xhtml")
     // <div> was missing, so the XHTML fragment wasn't a valid
     // xhtml.BlkStruct.class type anymore.
     assertXPath(pDoc, "/html/body/div/table/tr/th", 1);
-    // The attribute was present to contain "background" and "border", which is
-    // ignored in reqif-xhtml.
-    assertXPathNoAttribute(pDoc, "/html/body/div/table/tr/th", "style");
+    // Make sure that row background is written using CSS.
+    OUString aStyle = getXPath(pDoc, "/html/body/div/table/tr/th", "style");
+    CPPUNIT_ASSERT(aStyle.startsWith("background: "));
     // The attribute was present, which is not valid in reqif-xhtml.
     assertXPathNoAttribute(pDoc, "/html/body/div/table/tr/th", "bgcolor");
 }
@@ -2166,6 +2169,44 @@ CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, 
testLeadingTabHTML)
     assertXPathContent(pHtmlDoc, "/html/body/p", SAL_NEWLINE_STRING u"\xa0\xa0 
test");
 }
 
+CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testTableBackground)
+{
+    // Given a document with two tables: first stable has a background, second 
table has a
+    // background in its first row:
+    loadURL("private:factory/swriter", nullptr);
+    SwXTextDocument* pTextDoc = 
dynamic_cast<SwXTextDocument*>(mxComponent.get());
+    SwDoc* pDoc = pTextDoc->GetDocShell()->GetDoc();
+    SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell();
+    SwInsertTableOptions aInsertTableOptions(SwInsertTableFlags::DefaultBorder,
+                                             /*nRowsToRepeat=*/0);
+    pWrtShell->InsertTable(aInsertTableOptions, /*nRows=*/1, /*nCols=*/1);
+    pWrtShell->MoveTable(GotoPrevTable, fnTableStart);
+    SvxBrushItem aBrush(Color(0xff0000), RES_BACKGROUND);
+    pWrtShell->SetTabBackground(aBrush);
+    pWrtShell->Down(/*bSelect=*/false);
+    pWrtShell->SplitNode();
+    pWrtShell->InsertTable(aInsertTableOptions, /*nRows=*/1, /*nCols=*/1);
+    pWrtShell->MoveTable(GotoPrevTable, fnTableStart);
+    aBrush.SetColor(0x00ff00);
+    pWrtShell->SetRowBackground(aBrush);
+
+    // When exporting to reqif-xhtml:
+    ExportToReqif();
+
+    // Then make sure that CSS markup is used, not HTML one:
+    SvMemoryStream aStream;
+    HtmlExportTest::wrapFragment(maTempFile, aStream);
+    xmlDocUniquePtr pXmlDoc = parseXmlStream(&aStream);
+    // Without the accompanying fix in place, this test would have failed with:
+    // - XPath '//reqif-xhtml:table[1]' no attribute 'style' exist
+    // i.e. HTML markup was used for the table background color.
+    assertXPath(pXmlDoc, "//reqif-xhtml:table[1]", "style", "background: 
#ff0000");
+    assertXPathNoAttribute(pXmlDoc, "//reqif-xhtml:table[1]", "bgcolor");
+    assertXPath(pXmlDoc, "//reqif-xhtml:table[2]/reqif-xhtml:tr[1]", "style",
+                "background: #00ff00");
+    assertXPathNoAttribute(pXmlDoc, 
"//reqif-xhtml:table[2]/reqif-xhtml:tr[1]", "bgcolor");
+}
+
 CPPUNIT_PLUGIN_IMPLEMENT();
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/filter/html/css1atr.cxx 
b/sw/source/filter/html/css1atr.cxx
index 9d773603a1ab..b2d5aecb9e78 100644
--- a/sw/source/filter/html/css1atr.cxx
+++ b/sw/source/filter/html/css1atr.cxx
@@ -185,9 +185,10 @@ OString lclConvToHex(sal_uInt16 nHex)
 }
 }
 
-bool IgnorePropertyForReqIF(bool bReqIF, const OString& rProperty, const 
OString& rValue)
+bool IgnorePropertyForReqIF(bool bReqIF, const OString& rProperty, const 
OString& rValue,
+                            bool bTable)
 {
-    if (!bReqIF)
+    if (!bReqIF || bTable)
         return false;
 
     // Only allow these two keys, nothing else in ReqIF mode.
@@ -243,9 +244,10 @@ public:
 
 void SwHTMLWriter::OutCSS1_Property( const char *pProp,
                                      const char *pVal,
-                                     const OUString *pSVal )
+                                     const OUString *pSVal,
+                                     bool bTable )
 {
-    if (IgnorePropertyForReqIF(mbReqIF, pProp, pVal))
+    if (IgnorePropertyForReqIF(mbReqIF, pProp, pVal, bTable))
         return;
 
     OStringBuffer sOut;
@@ -3311,7 +3313,8 @@ static Writer& OutCSS1_SvxBrush( Writer& rWrt, const 
SfxPoolItem& rHt,
     }
 
     if( !sOut.isEmpty() )
-        rHTMLWrt.OutCSS1_Property( sCSS1_P_background, sOut );
+        rHTMLWrt.OutCSS1_Property(sCSS1_P_background, nullptr, &sOut,
+                                  nMode == Css1Background::Table);
 
     return rWrt;
 }
diff --git a/sw/source/filter/html/htmltabw.cxx 
b/sw/source/filter/html/htmltabw.cxx
index 1febebd9ff34..46632157af43 100644
--- a/sw/source/filter/html/htmltabw.cxx
+++ b/sw/source/filter/html/htmltabw.cxx
@@ -533,11 +533,14 @@ void SwHTMLWrtTable::OutTableCells( SwHTMLWriter& rWrt,
     rWrt.Strm().WriteChar( '<' ).WriteOString( rWrt.GetNamespace() + 
OOO_STRING_SVTOOLS_HTML_tablerow );
     if( pBrushItem )
     {
-        rWrt.OutBackground( pBrushItem, false );
+        if (!rWrt.mbXHTML)
+        {
+            rWrt.OutBackground(pBrushItem, false);
+        }
 
         rWrt.m_bTextAttr = false;
         rWrt.m_bOutOpts = true;
-        if( rWrt.m_bCfgOutStyles )
+        if (rWrt.m_bCfgOutStyles || rWrt.mbXHTML)
             OutCSS1_TableBGStyleOpt( rWrt, *pBrushItem );
     }
 
@@ -702,10 +705,15 @@ void SwHTMLWrtTable::Write( SwHTMLWriter& rWrt, sal_Int16 
eAlign,
     // output background
     if( pFrameFormat )
     {
-        rWrt.OutBackground( pFrameFormat->GetAttrSet(), false );
+        if (!rWrt.mbXHTML)
+        {
+            rWrt.OutBackground(pFrameFormat->GetAttrSet(), false);
+        }
 
-        if (rWrt.m_bCfgOutStyles)
+        if (rWrt.m_bCfgOutStyles || rWrt.mbXHTML)
+        {
             rWrt.OutCSS1_TableFrameFormatOptions( *pFrameFormat );
+        }
     }
 
     sOut.append('>');
diff --git a/sw/source/filter/html/wrthtml.hxx 
b/sw/source/filter/html/wrthtml.hxx
index cc23a82b22d3..3600124ceece 100644
--- a/sw/source/filter/html/wrthtml.hxx
+++ b/sw/source/filter/html/wrthtml.hxx
@@ -468,7 +468,7 @@ public:
                                        const OString& rVal );
     inline void OutCSS1_Property( const char *pProp, const OUString& rVal );
     void OutCSS1_Property( const char *pProp, const char *pVal,
-                           const OUString *pSVal );
+                           const OUString *pSVal, bool bTable = false );
     void OutCSS1_UnitProperty( const char *pProp, long nVal );
     void OutCSS1_PixelProperty( const char *pProp, long nVal, bool bVert );
     void OutCSS1_SfxItemSet( const SfxItemSet& rItemSet, bool bDeep=true );
@@ -710,7 +710,8 @@ Writer& OutCSS1_SvxBox( Writer& rWrt, const SfxPoolItem& 
rHt );
 OString GetCSS1_Color(const Color& rColor);
 
 /// Determines if rProperty with a given rValue has to be suppressed due to 
ReqIF mode.
-bool IgnorePropertyForReqIF(bool bReqIF, const OString& rProperty, const 
OString& rValue);
+bool IgnorePropertyForReqIF(bool bReqIF, const OString& rProperty, const 
OString& rValue,
+                            bool bTable = false);
 
 #endif // INCLUDED_SW_SOURCE_FILTER_HTML_WRTHTML_HXX
 

Reply via email to