summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiklos Vajna <vmiklos@collabora.com>2022-05-10 16:15:52 +0200
committerMiklos Vajna <vmiklos@collabora.com>2022-07-01 10:03:07 +0200
commit4f26bf4c1382d7c32dc61be15db92a2503628b9c (patch)
treeb49b1f31eca3ec2d069eaa6c3ff794e2472da0de
parent3dc5b937736d4dedb2e02120df12b75740460e8e (diff)
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 (cherry picked from commit e14b6d2e63990c7571c0a4f0f1a767d5955055fb) Conflicts: sw/qa/extras/htmlexport/htmlexport.cxx Change-Id: Ia0858986c3e8a3ea41adf8a24119442fe5068ee3
-rw-r--r--sw/CppunitTest_sw_htmlexport.mk1
-rw-r--r--sw/qa/extras/htmlexport/htmlexport.cxx47
-rw-r--r--sw/source/filter/html/css1atr.cxx13
-rw-r--r--sw/source/filter/html/htmltabw.cxx16
-rw-r--r--sw/source/filter/html/wrthtml.hxx5
5 files changed, 68 insertions, 14 deletions
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 ca66e36fff8e..f0792a197a52 100644
--- a/sw/qa/extras/htmlexport/htmlexport.cxx
+++ b/sw/qa/extras/htmlexport/htmlexport.cxx
@@ -39,6 +39,9 @@
#include <sot/storage.hxx>
#include <svl/eitem.hxx>
#include <vcl/dibtools.hxx>
+#include <editeng/brushitem.hxx>
+
+#include <itabenum.hxx>
namespace
{
@@ -698,9 +701,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");
}
@@ -1623,6 +1626,44 @@ CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testNestedBullets)
"second");
}
+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 ffc416f22662..66f1b6a57198 100644
--- a/sw/source/filter/html/wrthtml.hxx
+++ b/sw/source/filter/html/wrthtml.hxx
@@ -457,7 +457,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 );
@@ -698,7 +698,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