sw/qa/filter/html/data/centered-table.xhtml | 9 ++++++++ sw/qa/filter/html/html.cxx | 19 ++++++++++++++++++ sw/source/filter/html/htmltab.cxx | 29 ++++++++++++++++++++++++++++ sw/source/filter/html/svxcss1.cxx | 16 +++++++++++++++ sw/source/filter/html/svxcss1.hxx | 2 + 5 files changed, 75 insertions(+)
New commits: commit 44c75d56cd441ad3d174db75a6e5ae672b7d2a08 Author: Miklos Vajna <vmik...@collabora.com> AuthorDate: Fri Jan 20 12:15:48 2023 +0100 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Fri Jan 20 13:40:22 2023 +0000 sw HTML import: initial CSS support on tables This is the import side of commit 6ce374140f3bd1cede959ccc8da69fdacecff191 (sw XHTML export: use CSS instead of <center> for tables, 2023-01-19), otherwise the import would not handle the new markup of the export. Change-Id: I7d18b06e10adff877dbbf1745861c65a07f807cb Reviewed-on: https://gerrit.libreoffice.org/c/core/+/145863 Reviewed-by: Miklos Vajna <vmik...@collabora.com> Tested-by: Jenkins diff --git a/sw/qa/filter/html/data/centered-table.xhtml b/sw/qa/filter/html/data/centered-table.xhtml new file mode 100644 index 000000000000..4ec88792f119 --- /dev/null +++ b/sw/qa/filter/html/data/centered-table.xhtml @@ -0,0 +1,9 @@ +<reqif-xhtml:div> + <reqif-xhtml:table width="400" cellpadding="0" cellspacing="0" style="margin-left: auto; margin-right: auto"> + <reqif-xhtml:tr> + <reqif-xhtml:td><reqif-xhtml:p>A1</reqif-xhtml:p> + </reqif-xhtml:td> + </reqif-xhtml:tr> + </reqif-xhtml:table> +<reqif-xhtml:p></reqif-xhtml:p> +</reqif-xhtml:div> diff --git a/sw/qa/filter/html/html.cxx b/sw/qa/filter/html/html.cxx index 65f67b7378d1..2b0c36508378 100644 --- a/sw/qa/filter/html/html.cxx +++ b/sw/qa/filter/html/html.cxx @@ -226,6 +226,25 @@ CPPUNIT_TEST_FIXTURE(Test, testCenteredTableCSSExport) assertXPath(pXmlDoc, "//reqif-xhtml:center", 0); assertXPath(pXmlDoc, "//reqif-xhtml:table", "style", "margin-left: auto; margin-right: auto"); } + +CPPUNIT_TEST_FIXTURE(Test, testCenteredTableCSSImport) +{ + // Given an XHTML file with a centered (with inline CSS) table, when importing that document: + setImportFilterOptions("xhtmlns=reqif-xhtml"); + setImportFilterName("HTML (StarWriter)"); + createSwDoc("centered-table.xhtml"); + + // Then make sure that the table is centered: + SwDoc* pDoc = getSwDoc(); + const SwFrameFormats& rTableFormats = *pDoc->GetTableFrameFormats(); + const SwFrameFormat* pTableFormat = rTableFormats[0]; + sal_Int16 eHoriOrient = pTableFormat->GetHoriOrient().GetHoriOrient(); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: 2 (CENTER) + // - Actual : 3 (LEFT) + // i.e. the table alignment was lost on import. + CPPUNIT_ASSERT_EQUAL(text::HoriOrientation::CENTER, eHoriOrient); +} } CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/sw/source/filter/html/htmltab.cxx b/sw/source/filter/html/htmltab.cxx index fe55cec381ac..7913cd9e85fa 100644 --- a/sw/source/filter/html/htmltab.cxx +++ b/sw/source/filter/html/htmltab.cxx @@ -4917,6 +4917,35 @@ std::shared_ptr<HTMLTable> SwHTMLParser::BuildTable(SvxAdjust eParentAdjust, else { m_xTable.reset(); + + // Parse CSS on the table. + OUString aStyle; + const HTMLOptions& rHTMLOptions = GetOptions(); + for (size_t i = rHTMLOptions.size(); i;) + { + const HTMLOption& rOption = rHTMLOptions[--i]; + if (rOption.GetToken() == HtmlOptionId::STYLE) + { + aStyle = rOption.GetString(); + } + } + if (!aStyle.isEmpty()) + { + // Have inline CSS. + SfxItemSet aItemSet(m_xDoc->GetAttrPool(), m_pCSS1Parser->GetWhichMap()); + SvxCSS1PropertyInfo aPropInfo; + if (ParseStyleOptions(aStyle, /*aId=*/OUString(), /*aClass=*/OUString(), aItemSet, + aPropInfo)) + { + if (aPropInfo.m_eLeftMarginType == SVX_CSS1_LTYPE_AUTO + && aPropInfo.m_eRightMarginType == SVX_CSS1_LTYPE_AUTO) + { + // Both left & right is set to auto: that's our center. + eParentAdjust = SvxAdjust::Center; + } + } + } + HTMLTableOptions aTableOptions(GetOptions(), eParentAdjust); if (!aTableOptions.aId.isEmpty()) diff --git a/sw/source/filter/html/svxcss1.cxx b/sw/source/filter/html/svxcss1.cxx index 256656a6df1e..9b312e0b3239 100644 --- a/sw/source/filter/html/svxcss1.cxx +++ b/sw/source/filter/html/svxcss1.cxx @@ -399,6 +399,8 @@ SvxCSS1PropertyInfo::SvxCSS1PropertyInfo( const SvxCSS1PropertyInfo& rProp ) : m_eTopType( rProp.m_eTopType ), m_eWidthType( rProp.m_eWidthType ), m_eHeightType( rProp.m_eHeightType ), + m_eLeftMarginType( rProp.m_eLeftMarginType ), + m_eRightMarginType( rProp.m_eRightMarginType ), m_eSizeType( rProp.m_eSizeType ), m_ePageBreakBefore( rProp.m_ePageBreakBefore ), m_ePageBreakAfter( rProp.m_ePageBreakAfter ) @@ -438,6 +440,8 @@ void SvxCSS1PropertyInfo::Clear() m_nLeft = m_nTop = m_nWidth = m_nHeight = 0; m_eLeftType = m_eTopType = m_eWidthType = m_eHeightType = SVX_CSS1_LTYPE_NONE; + m_eLeftMarginType = SVX_CSS1_LTYPE_NONE; + m_eRightMarginType = SVX_CSS1_LTYPE_NONE; // Feature: PrintExt m_eSizeType = SVX_CSS1_STYPE_NONE; @@ -2042,6 +2046,12 @@ static void ParseCSS1_margin_left( const CSS1Expression *pExpr, ; } + if (pExpr->GetString() == "auto") + { + rPropInfo.m_bLeftMargin = true; + rPropInfo.m_eLeftMarginType = SVX_CSS1_LTYPE_AUTO; + } + if( !bSet ) return; @@ -2099,6 +2109,12 @@ static void ParseCSS1_margin_right( const CSS1Expression *pExpr, ; } + if (pExpr->GetString() == "auto") + { + rPropInfo.m_bRightMargin = true; + rPropInfo.m_eRightMarginType = SVX_CSS1_LTYPE_AUTO; + } + if( !bSet ) return; diff --git a/sw/source/filter/html/svxcss1.hxx b/sw/source/filter/html/svxcss1.hxx index 0ca92f314ea9..30c5a7b4accb 100644 --- a/sw/source/filter/html/svxcss1.hxx +++ b/sw/source/filter/html/svxcss1.hxx @@ -136,6 +136,8 @@ public: SvxCSS1LengthType m_eLeftType, m_eTopType; SvxCSS1LengthType m_eWidthType, m_eHeightType; + SvxCSS1LengthType m_eLeftMarginType; + SvxCSS1LengthType m_eRightMarginType; SvxCSS1SizeType m_eSizeType;