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

New commits:
commit 1ba9f7068e81fbbc59a117a63a63d399561da7e1
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Tue May 10 16:15:52 2022 +0200
Commit:     Xisco Fauli <xiscofa...@libreoffice.org>
CommitDate: Wed May 11 09:57:25 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.
    
    Change-Id: Ia0858986c3e8a3ea41adf8a24119442fe5068ee3
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134119
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins
    Signed-off-by: Xisco Fauli <xiscofa...@libreoffice.org>
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134136

diff --git a/sw/CppunitTest_sw_htmlexport.mk b/sw/CppunitTest_sw_htmlexport.mk
index 5941e906889f..038aae4eebb2 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 7f64c835ce2d..752352efe9f6 100644
--- a/sw/qa/extras/htmlexport/htmlexport.cxx
+++ b/sw/qa/extras/htmlexport/htmlexport.cxx
@@ -42,6 +42,7 @@
 #include <comphelper/processfactory.hxx>
 #include <vcl/graphicfilter.hxx>
 #include <vcl/dibtools.hxx>
+#include <editeng/brushitem.hxx>
 
 #include <swmodule.hxx>
 #include <swdll.hxx>
@@ -51,6 +52,7 @@
 #include <paratr.hxx>
 #include <docsh.hxx>
 #include <unotxdoc.hxx>
+#include <itabenum.hxx>
 
 namespace
 {
@@ -800,9 +802,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");
 }
@@ -2119,6 +2121,42 @@ CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, 
testTrailingLineBreak)
     CPPUNIT_ASSERT_EQUAL(OUString("test\n"), aActual);
 }
 
+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:
+    SwDoc* pDoc = createSwDoc();
+    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 de952e8552d8..ea888879c225 100644
--- a/sw/source/filter/html/css1atr.cxx
+++ b/sw/source/filter/html/css1atr.cxx
@@ -190,9 +190,10 @@ OString lclConvToHex(sal_uInt16 nHex)
 }
 }
 
-bool IgnorePropertyForReqIF(bool bReqIF, std::string_view rProperty, 
std::string_view rValue)
+bool IgnorePropertyForReqIF(bool bReqIF, std::string_view rProperty, 
std::string_view rValue,
+                            bool bTable)
 {
-    if (!bReqIF)
+    if (!bReqIF || bTable)
         return false;
 
     // Only allow these two keys, nothing else in ReqIF mode.
@@ -248,9 +249,10 @@ public:
 
 void SwHTMLWriter::OutCSS1_Property( const char *pProp,
                                      std::string_view sVal,
-                                     const OUString *pSVal )
+                                     const OUString *pSVal,
+                                     bool bTable )
 {
-    if (IgnorePropertyForReqIF(mbReqIF, pProp, sVal))
+    if (IgnorePropertyForReqIF(mbReqIF, pProp, sVal, bTable))
         return;
 
     OStringBuffer sOut;
@@ -3224,7 +3226,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, std::string_view(), 
&sOut,
+                                  nMode == Css1Background::Table);
 
     return rWrt;
 }
diff --git a/sw/source/filter/html/htmltabw.cxx 
b/sw/source/filter/html/htmltabw.cxx
index b35fcc91bf79..2026b4260a15 100644
--- a/sw/source/filter/html/htmltabw.cxx
+++ b/sw/source/filter/html/htmltabw.cxx
@@ -535,11 +535,14 @@ void SwHTMLWrtTable::OutTableCells( SwHTMLWriter& rWrt,
     rWrt.Strm().WriteChar( '<' ).WriteOString( 
OStringConcatenation(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 );
     }
 
@@ -703,10 +706,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 bf6806b1ae95..7dcfd99655c6 100644
--- a/sw/source/filter/html/wrthtml.hxx
+++ b/sw/source/filter/html/wrthtml.hxx
@@ -465,7 +465,7 @@ public:
                                        std::string_view rVal );
     inline void OutCSS1_Property( const char *pProp, const OUString& rVal );
     void OutCSS1_Property( const char *pProp, std::string_view pVal,
-                           const OUString *pSVal );
+                           const OUString *pSVal, bool bTable = false );
     void OutCSS1_UnitProperty( const char *pProp, tools::Long nVal );
     void OutCSS1_PixelProperty( const char *pProp, tools::Long nVal, bool 
bVert );
     void OutCSS1_SfxItemSet( const SfxItemSet& rItemSet, bool bDeep=true );
@@ -707,7 +707,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, std::string_view rProperty, 
std::string_view rValue);
+bool IgnorePropertyForReqIF(bool bReqIF, std::string_view rProperty, 
std::string_view rValue,
+                            bool bTable = false);
 
 #endif // INCLUDED_SW_SOURCE_FILTER_HTML_WRTHTML_HXX
 

Reply via email to