Commit 999893c4 authored by Ahmad Samir's avatar Ahmad Samir Committed by Volker Krause
Browse files

composer-ng: Use QString::toDouble() in fixHtmlFontSize; add unit tests

Use QString::toDouble() in fixHtmlFontSize, as actual font sizes could well be
double.

Add unit tests for fixHtmlFontSize and toCleanHtml helper methods, that
should help with porting QRegExp to QRegularExpression.
parent c9a4ec84
Pipeline #30727 passed with stage
in 48 minutes and 16 seconds
......@@ -517,4 +517,43 @@ void RichTextComposerNgTest::shouldLoadSignatureFromCommand()
QCOMPARE(richtextComposerNg.toPlainText(), expected);
}
void RichTextComposerNgTest::fixHtmlFontSizeTest()
{
MessageComposer::RichTextComposerNg richtextComposerNg;
QString str = QStringLiteral(
"<span style=\"color: green; font-size: 10pt;\">int font</span> size\n"
"<span style=\"font-size: 10.8pt; font-family: foo;\">double font</span> size\n"
"<span style=\"font-size: 15,2pt;\">invalid font</span> size, left as is\n");
richtextComposerNg.fixHtmlFontSize(str);
const QString expected(QStringLiteral(
"<span style=\"color: green; font-size:0.83em;\">int font</span> size\n"
"<span style=\"font-size:0.9em; font-family: foo;\">double font</span> size\n"
"<span style=\"font-size: 15,2pt;\">invalid font</span> size, left as is\n"));
QCOMPARE(str, expected);
}
void RichTextComposerNgTest::toCleanHtmlRegexTest()
{
MessageComposer::RichTextComposerNg richtextComposerNg;
richtextComposerNg.setHtml(QStringLiteral(
"<p style=\"-qt-paragraph-type:empty\"></p>\n"
"<p>some text</p>\n"
"<p style=\"-qt-paragraph-type:empty\"></p>\n"));
const QString result = richtextComposerNg.toCleanHtml();
const int idx = result.indexOf(QStringLiteral("<p style=\"-qt-paragraph-type:empty"));
// Remove the XHTML DOCTYPE and <style> parts as they differ
// based on the fonts on the system
const QString resultStripped = result.mid(idx);
const QString expected = QStringLiteral(
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; \">&nbsp;</p>\n"
"<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">some text</p>\n"
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; \">&nbsp;</p></body></html>");
QCOMPARE(resultStripped, expected);
}
QTEST_MAIN(RichTextComposerNgTest)
......@@ -39,6 +39,10 @@ private Q_SLOTS:
void shouldLoadSignatureFromCommand_data();
void shouldLoadSignatureFromCommand();
void fixHtmlFontSizeTest();
void toCleanHtmlRegexTest();
};
#endif // RICHTEXTCOMPOSERNGTEST_H
......@@ -28,7 +28,7 @@ public:
richTextComposerSignatures = new MessageComposer::RichTextComposerSignatures(richtextComposer, richtextComposer);
}
void fixHtmlFontSize(QString &cleanHtml);
void fixHtmlFontSize(QString &cleanHtml) const;
Q_REQUIRED_RESULT QString toCleanHtml() const;
PimCommon::AutoCorrection *autoCorrection = nullptr;
RichTextComposerNg * const richtextComposer;
......@@ -119,7 +119,7 @@ bool RichTextComposerNg::processModifyText(QKeyEvent *e)
return false;
}
void RichTextComposerNgPrivate::fixHtmlFontSize(QString &cleanHtml)
void RichTextComposerNgPrivate::fixHtmlFontSize(QString &cleanHtml) const
{
static const QString FONTSTYLEREGEX = QStringLiteral("<span style=\".*font-size:(.*)pt;.*</span>");
QRegExp styleRegex(FONTSTYLEREGEX);
......@@ -130,9 +130,9 @@ void RichTextComposerNgPrivate::fixHtmlFontSize(QString &cleanHtml)
// replace all the matching text with the new line text
bool ok = false;
const QString fontSizeStr = styleRegex.cap(1);
const int ptValue = fontSizeStr.toInt(&ok);
const double ptValue = fontSizeStr.toDouble(&ok);
if (ok) {
double emValue = static_cast<double>(ptValue) / 12;
const double emValue = ptValue / 12;
const QString emValueStr = QString::number(emValue, 'g', 2);
cleanHtml.replace(styleRegex.pos(1), QString(fontSizeStr + QLatin1String("px")).length(), emValueStr + QLatin1String("em"));
}
......@@ -378,6 +378,11 @@ QString RichTextComposerNg::toCleanHtml() const
return d->toCleanHtml();
}
void RichTextComposerNg::fixHtmlFontSize(QString &cleanHtml) const
{
d->fixHtmlFontSize(cleanHtml);
}
void RichTextComposerNg::forceAutoCorrection(bool selectedText)
{
if (document()->isEmpty()) {
......
......@@ -12,6 +12,8 @@
#include <MessageComposer/PluginEditorConvertTextInterface>
#include <KIdentityManagement/Signature>
class RichTextComposerNgTest;
namespace PimCommon {
class AutoCorrection;
}
......@@ -45,11 +47,15 @@ public:
Q_REQUIRED_RESULT virtual MessageComposer::PluginEditorConvertTextInterface::ConvertTextStatus convertPlainText(MessageComposer::TextPart *textPart);
protected:
bool processModifyText(QKeyEvent *event) override;
private:
RichTextComposerNgPrivate *const d;
friend class ::RichTextComposerNgTest; // for fixHtmlFontSize
void fixHtmlFontSize(QString &cleanHtml) const;
};
}
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment