sw/qa/filter/html/html.cxx | 30 ++++++++++++++++++++++++++++++ sw/source/filter/html/css1atr.cxx | 11 +++++++++++ sw/source/filter/html/htmltabw.cxx | 14 ++++++++++++-- 3 files changed, 53 insertions(+), 2 deletions(-)
New commits: commit ce118776c337c598165ee86c63df2f8980bb9fe5 Author: Miklos Vajna <vmik...@collabora.com> AuthorDate: Thu Jan 19 15:46:37 2023 +0100 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Fri Jan 20 14:42:36 2023 +0000 sw XHTML export: use CSS instead of <center> for tables <center> is not valid XHTML, have to use CSS styling instead. HTML export uses <center> by default around tables where the alignment is center. Fix the problem by avoiding <center> in the XHTML case and set the left and right margin to auto, which means: If the values of margin-left and margin-right are both auto, the calculated space is evenly distributed. according to <https://developer.mozilla.org/en-US/docs/Web/CSS/margin-left>. The import will be adjusted to recognize the new markup in a follow-up change. Change-Id: I51e3507e9cde713f961b783378d66db59194a6ca Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145814 Reviewed-by: Miklos Vajna <vmik...@collabora.com> Tested-by: Jenkins (cherry picked from commit 6ce374140f3bd1cede959ccc8da69fdacecff191) Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145803 Reviewed-by: Michael Stahl <michael.st...@allotropia.de> diff --git a/sw/qa/filter/html/html.cxx b/sw/qa/filter/html/html.cxx index 537b437c4bdb..65f67b7378d1 100644 --- a/sw/qa/filter/html/html.cxx +++ b/sw/qa/filter/html/html.cxx @@ -196,6 +196,36 @@ CPPUNIT_TEST_FIXTURE(Test, testTableRowSpanInAllCells) assertXPathNoAttribute(pHtmlDoc, "//tr[1]/td[1]", "rowspan"); assertXPath(pHtmlDoc, "//tr", 1); } + +CPPUNIT_TEST_FIXTURE(Test, testCenteredTableCSSExport) +{ + // Given a document with a centered table: + createSwDoc(); + SwDoc* pDoc = getSwDoc(); + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + SwInsertTableOptions aTableOptions(SwInsertTableFlags::NONE, 0); + pWrtShell->InsertTable(aTableOptions, 1, 1); + pWrtShell->MoveTable(GotoPrevTable, fnTableStart); + SfxItemSetFixed<RES_FRMATR_BEGIN, RES_FRMATR_END - 1> aSet(pWrtShell->GetAttrPool()); + SwFormatHoriOrient aHoriOrientItem(/*nX=*/0, text::HoriOrientation::CENTER); + aSet.Put(aHoriOrientItem); + pWrtShell->SetTableAttr(aSet); + + // When exporting to XHTML: + setFilterOptions("xhtmlns=reqif-xhtml"); + save("HTML (StarWriter)"); + + // Then make sure that CSS is used to horizontally position the table: + SvMemoryStream aStream; + WrapReqifFromTempFile(aStream); + xmlDocUniquePtr pXmlDoc = parseXmlStream(&aStream); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: 0 + // - Actual : 1 + // i.e <center> was used to position the table, not CSS. + assertXPath(pXmlDoc, "//reqif-xhtml:center", 0); + assertXPath(pXmlDoc, "//reqif-xhtml:table", "style", "margin-left: auto; margin-right: auto"); +} } CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/sw/source/filter/html/css1atr.cxx b/sw/source/filter/html/css1atr.cxx index 135494df00ca..28c5cc5dea8f 100644 --- a/sw/source/filter/html/css1atr.cxx +++ b/sw/source/filter/html/css1atr.cxx @@ -2064,6 +2064,17 @@ void SwHTMLWriter::OutCSS1_TableFrameFormatOptions( const SwFrameFormat& rFrameF if( SfxItemState::SET==rItemSet.GetItemState( RES_LAYOUT_SPLIT, false, &pItem ) ) OutCSS1_SwFormatLayoutSplit( *this, *pItem ); + if (mbXHTML) + { + sal_Int16 eTabHoriOri = rFrameFormat.GetHoriOrient().GetHoriOrient(); + if (eTabHoriOri == text::HoriOrientation::CENTER) + { + // Emit XHTML's center using inline CSS. + OutCSS1_Property(sCSS1_P_margin_left, "auto", nullptr, sw::Css1Background::Table); + OutCSS1_Property(sCSS1_P_margin_right, "auto", nullptr, sw::Css1Background::Table); + } + } + if( !m_bFirstCSS1Property ) Strm().WriteChar( '\"' ); } diff --git a/sw/source/filter/html/htmltabw.cxx b/sw/source/filter/html/htmltabw.cxx index ddb8c8eb5f10..a1dee0f75333 100644 --- a/sw/source/filter/html/htmltabw.cxx +++ b/sw/source/filter/html/htmltabw.cxx @@ -1113,7 +1113,13 @@ Writer& OutHTML_SwTableNode( Writer& rWrt, SwTableNode & rNode, if( rHTMLWrt.m_bLFPossible ) rHTMLWrt.OutNewLine(); // <CENTER> in new line if( text::HoriOrientation::CENTER==eDivHoriOri ) - HTMLOutFuncs::Out_AsciiTag( rWrt.Strm(), Concat2View(rHTMLWrt.GetNamespace() + OOO_STRING_SVTOOLS_HTML_center) ); + { + if (!rHTMLWrt.mbXHTML) + { + // Not XHTML's css center: start <center>. + HTMLOutFuncs::Out_AsciiTag( rWrt.Strm(), Concat2View(rHTMLWrt.GetNamespace() + OOO_STRING_SVTOOLS_HTML_center) ); + } + } else { OStringLiteral sOut = OOO_STRING_SVTOOLS_HTML_division @@ -1166,7 +1172,11 @@ Writer& OutHTML_SwTableNode( Writer& rWrt, SwTableNode & rNode, OString aTag = text::HoriOrientation::CENTER == eDivHoriOri ? OOO_STRING_SVTOOLS_HTML_center : OOO_STRING_SVTOOLS_HTML_division; - HTMLOutFuncs::Out_AsciiTag(rWrt.Strm(), Concat2View(rHTMLWrt.GetNamespace() + aTag), false); + if (!rHTMLWrt.mbXHTML || eDivHoriOri != text::HoriOrientation::CENTER) + { + // Not XHTML's css center: end <center>. + HTMLOutFuncs::Out_AsciiTag(rWrt.Strm(), Concat2View(rHTMLWrt.GetNamespace() + aTag), false); + } rHTMLWrt.m_bLFPossible = true; }