summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiklos Vajna <vmiklos@collabora.com>2022-05-10 16:15:52 +0200
committerMiklos Vajna <vmiklos@collabora.com>2022-05-10 17:19:59 +0200
commitc3c3303516c3da9372dce3f05f38f15a104e961c (patch)
tree5193b10c8bbab3f7ef7d8e30574efd804a35fe6b
parent4cc3a144ef2ecc019da42bb9b115267a18e9559f (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. Change-Id: Ia0858986c3e8a3ea41adf8a24119442fe5068ee3 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134119 Reviewed-by: Miklos Vajna <vmiklos@collabora.com> Tested-by: Jenkins
-rw-r--r--sw/CppunitTest_sw_htmlexport.mk1
-rw-r--r--sw/qa/extras/htmlexport/htmlexport.cxx44
-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, 65 insertions, 14 deletions
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 ff9d6504bb72..639b744b05da 100644
--- a/sw/qa/extras/htmlexport/htmlexport.cxx
+++ b/sw/qa/extras/htmlexport/htmlexport.cxx
@@ -43,6 +43,7 @@
#include <vcl/graphicfilter.hxx>
#include <vcl/dibtools.hxx>
#include <o3tl/string_view.hxx>
+#include <editeng/brushitem.hxx>
#include <swmodule.hxx>
#include <swdll.hxx>
@@ -53,6 +54,7 @@
#include <docsh.hxx>
#include <unotxdoc.hxx>
#include <formatlinebreak.hxx>
+#include <itabenum.hxx>
namespace
{
@@ -803,9 +805,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");
}
@@ -2252,6 +2254,42 @@ CPPUNIT_TEST_FIXTURE(HtmlExportTest, testClearingBreak)
verify();
}
+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 41912dac4881..92bb509a2020 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;
@@ -3207,7 +3209,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 e1bb62dd0969..46507b6a31b2 100644
--- a/sw/source/filter/html/htmltabw.cxx
+++ b/sw/source/filter/html/htmltabw.cxx
@@ -529,11 +529,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 );
}
@@ -697,10 +700,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 fc3d01eecd2b..434e13825340 100644
--- a/sw/source/filter/html/wrthtml.hxx
+++ b/sw/source/filter/html/wrthtml.hxx
@@ -468,7 +468,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 );
@@ -711,7 +711,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