diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2022-05-10 16:15:52 +0200 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2022-05-13 11:58:03 +0200 |
commit | 2c59f66bf5c0d5982757f5c444dfea7ad504dc3a (patch) | |
tree | 90814798f5ec327c7168fe6046f115c5f65b19ab /sw | |
parent | 116338b8af38a20e21c1248cc077cfe1f0cc07c0 (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.
Conflicts:
sw/qa/extras/htmlexport/htmlexport.cxx
Change-Id: Ia0858986c3e8a3ea41adf8a24119442fe5068ee3
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134257
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Diffstat (limited to 'sw')
-rw-r--r-- | sw/CppunitTest_sw_htmlexport.mk | 1 | ||||
-rw-r--r-- | sw/qa/extras/htmlexport/htmlexport.cxx | 44 | ||||
-rw-r--r-- | sw/source/filter/html/css1atr.cxx | 13 | ||||
-rw-r--r-- | sw/source/filter/html/htmltabw.cxx | 16 | ||||
-rw-r--r-- | sw/source/filter/html/wrthtml.hxx | 5 |
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 9750555bf7f6..3ee58613903a 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> @@ -52,6 +53,7 @@ #include <docsh.hxx> #include <unotxdoc.hxx> #include <formatlinebreak.hxx> +#include <itabenum.hxx> namespace { @@ -801,9 +803,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"); } @@ -2162,6 +2164,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 150bba91ed8b..168d1cc7778a 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 968fa4efc60c..434d7e716455 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 e2821b492762..b17379c6c982 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 ); @@ -708,7 +708,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 |