tdf#158023 Add \ before parentheses if they are in the <t> tag.

MSO allows you to write unpair opening or closing parentheses in the t tag.
The patch adds a backslash before parentheses, which allows you to correctly
recognize and correctly save such formulas when exporting.

Also add a test to check whether the parentheses are wrapped correctly.

Change-Id: Ib1a941858f0843742b56b8089f3c14983ba1222c
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/158794
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Tested-by: Jenkins
This commit is contained in:
Irgaliev Amin 2023-12-25 20:13:34 +04:00 committed by Mike Kaganski
parent 11fcb2814e
commit 8148dd6ed3
7 changed files with 47 additions and 4 deletions

View file

@ -602,9 +602,9 @@ OUString SmOoxmlImport::handleR()
{
XmlStream::Tag rtag = m_rStream.ensureOpeningTag( M_TOKEN( t ));
if( rtag.attribute( OOX_TOKEN( xml, space )) != "preserve" )
text.append(o3tl::trim(rtag.text));
text.append(o3tl::trim(rtag.text.replaceAll("(", "\\(").replaceAll(")", "\\)")));
else
text.append(rtag.text);
text.append(rtag.text.replaceAll("(", "\\(").replaceAll(")", "\\)"));
m_rStream.ensureClosingTag( M_TOKEN( t ));
break;
}

Binary file not shown.

View file

@ -261,7 +261,14 @@ CPPUNIT_TEST_FIXTURE(Test, testCommentsNested)
CPPUNIT_TEST_FIXTURE(Test, testMathEscape)
{
loadAndReload("math-escape.docx");
CPPUNIT_ASSERT_EQUAL(OUString("\\{ left [ right ] left ( right ) \\}"), getFormula(getRun(getParagraph(1), 1)));
CPPUNIT_ASSERT_EQUAL(OUString("\\{ left [ right ] \\( \\) \\}"), getFormula(getRun(getParagraph(1), 1)));
}
// Saving left and right for parentheses when importing not from the m:t tag (docx)
CPPUNIT_TEST_FIXTURE(Test, testTdf158023Export)
{
loadAndReload("tdf158023_export.docx");
CPPUNIT_ASSERT_EQUAL(OUString("left [ right ] left ( right ) left lbrace right rbrace"), getFormula(getRun(getParagraph(1), 1)));
}
CPPUNIT_TEST_FIXTURE(Test, testFdo51034)

Binary file not shown.

View file

@ -699,6 +699,26 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf119200)
CPPUNIT_ASSERT_EQUAL(u" size 12{ func \u2287 } {}"_ustr, getFormula(getRun(xPara, 7)));
}
// Checking a formula where the closing brackets
// come first, and then the opening ones
CPPUNIT_TEST_FIXTURE(Test, testTdf158023Import)
{
auto verify = [this]() {
auto xPara = getParagraph(1);
CPPUNIT_ASSERT_EQUAL(u"\\) sqrt {\\)2\\(} \\("_ustr, getFormula(getRun(xPara, 1)));
};
auto verifyReload = [this]() {
auto xPara = getParagraph(1);
CPPUNIT_ASSERT_EQUAL(u"\\) sqrt {\\) 2 \\(} \\("_ustr, getFormula(getRun(xPara, 1)));
};
createSwDoc("tdf158023_import.docx");
verify();
saveAndReload("Office Open XML Text");
verifyReload();
}
CPPUNIT_TEST_FIXTURE(Test, testTdf115094)
{
createSwDoc("tdf115094.docx");

View file

@ -0,0 +1,8 @@
{\rtf1
{\mmath {\*\moMath
{\md {\mdPr {\mbegChr [}{\mendChr ]}}{\me {\mr a}}}
{\md {\mdPr {\mbegChr (}{\mendChr )}}{\me {\mr b}}}
{\md {\mdPr {\mbegChr \{}{\mendChr \}}}{\me {\mr c}}}
}}
\par
}

View file

@ -376,7 +376,15 @@ CPPUNIT_TEST_FIXTURE(Test, testMathRuns)
{
loadAndReload("math-runs.rtf");
// was [](){}, i.e. first curly bracket had an incorrect position
CPPUNIT_ASSERT_EQUAL(OUString("\\{ left [ right ] left ( right ) \\}"),
CPPUNIT_ASSERT_EQUAL(OUString("\\{ left [ right ] \\( \\) \\}"),
getFormula(getRun(getParagraph(1), 1)));
}
// Saving left and right for parentheses when importing not from the m:t tag (rtf)
CPPUNIT_TEST_FIXTURE(Test, testTdf158023_rtf)
{
loadAndReload("tdf158023.rtf");
CPPUNIT_ASSERT_EQUAL(OUString("left [a right ] left (b right ) left lbrace c right rbrace"),
getFormula(getRun(getParagraph(1), 1)));
}