QTextDocument: improve import of DIV tags

<div>1<br/></div>2 was inserting two newlines between 1 and 2, while all
tested web browsers only insert one newline - as long as there is nothing
between the <br/> and the </div>.

This was the cause for extra newlines being inserted in KMail when
replying to HTML emails, such as those generated by gmail.

Change-Id: I5145d977701e68913264357bba22780e7cdd3f7d
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
David Faure 2017-04-10 11:05:51 +02:00
parent 45862976b7
commit c0ecfc08e3
2 changed files with 27 additions and 5 deletions

View File

@ -825,9 +825,13 @@ bool QTextHtmlImporter::closeTag()
break;
case Html_div:
if (closedNode->children.isEmpty())
if (cursor.position() > 0) {
const QChar curChar = cursor.document()->characterAt(cursor.position() - 1);
if (!closedNode->children.isEmpty() && curChar != QChar::LineSeparator) {
blockTagClosed = true;
}
}
break;
Q_FALLTHROUGH();
default:
if (closedNode->isBlock())
blockTagClosed = true;

View File

@ -134,6 +134,7 @@ private slots:
void clearResources();
void setPlainText();
void toPlainText_data();
void toPlainText();
void toRawText();
@ -2391,10 +2392,27 @@ void tst_QTextDocument::setPlainText()
QCOMPARE(doc->toPlainText(), s);
}
void tst_QTextDocument::toPlainText_data()
{
QTest::addColumn<QString>("html");
QTest::addColumn<QString>("expectedPlainText");
QTest::newRow("nbsp") << "Hello&nbsp;World" << "Hello World";
QTest::newRow("empty_div") << "<div></div>hello" << "hello";
QTest::newRow("br_and_p") << "<p>first<br></p><p>second<br></p>" << "first\n\nsecond\n";
QTest::newRow("div") << "first<div>second<br>third</div>fourth" << "first\nsecond\nthird\nfourth"; // <div> and </div> become newlines...
QTest::newRow("br_text_end_of_div") << "<div><div>first<br>moretext</div>second<br></div>" << "first\nmoretext\nsecond\n"; // ... when there is text before <div>
QTest::newRow("br_end_of_div_like_gmail") << "<div><div><div>first<br></div>second<br></div>third<br></div>" << "first\nsecond\nthird\n"; // ... and when there is text before </div>
QTest::newRow("p_and_div") << "<div><div>first<p>second</p></div>third</div>" << "first\nsecond\nthird";
}
void tst_QTextDocument::toPlainText()
{
doc->setHtml("Hello&nbsp;World");
QCOMPARE(doc->toPlainText(), QLatin1String("Hello World"));
QFETCH(QString, html);
QFETCH(QString, expectedPlainText);
doc->setHtml(html);
QCOMPARE(doc->toPlainText(), expectedPlainText);
}
void tst_QTextDocument::toRawText()