summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/gui/text/qtexthtmlparser.cpp6
-rw-r--r--src/gui/text/qtexthtmlparser_p.h4
-rw-r--r--tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp41
3 files changed, 47 insertions, 4 deletions
diff --git a/src/gui/text/qtexthtmlparser.cpp b/src/gui/text/qtexthtmlparser.cpp
index 85877bfe47..f97b8a672a 100644
--- a/src/gui/text/qtexthtmlparser.cpp
+++ b/src/gui/text/qtexthtmlparser.cpp
@@ -785,8 +785,8 @@ void QTextHtmlParser::parseCloseTag()
void QTextHtmlParser::parseExclamationTag()
{
++pos;
- if (hasPrefix(QLatin1Char('-'),1) && hasPrefix(QLatin1Char('-'),2)) {
- pos += 3;
+ if (hasPrefix(QLatin1Char('-')) && hasPrefix(QLatin1Char('-'), 1)) {
+ pos += 2;
// eat comments
int end = txt.indexOf(QLatin1String("-->"), pos);
pos = (end >= 0 ? end + 3 : len);
@@ -882,7 +882,7 @@ QString QTextHtmlParser::parseWord()
while (pos < len) {
QChar c = txt.at(pos++);
if (c == QLatin1Char('>')
- || (c == QLatin1Char('/') && hasPrefix(QLatin1Char('>'), 1))
+ || (c == QLatin1Char('/') && hasPrefix(QLatin1Char('>')))
|| c == QLatin1Char('<')
|| c == QLatin1Char('=')
|| c.isSpace()) {
diff --git a/src/gui/text/qtexthtmlparser_p.h b/src/gui/text/qtexthtmlparser_p.h
index 06bbc032a8..a96bf3244f 100644
--- a/src/gui/text/qtexthtmlparser_p.h
+++ b/src/gui/text/qtexthtmlparser_p.h
@@ -333,7 +333,9 @@ protected:
void applyAttributes(const QStringList &attributes);
void eatSpace();
inline bool hasPrefix(QChar c, int lookahead = 0) const
- {return pos + lookahead < len && txt.at(pos) == c; }
+ {
+ return pos + lookahead < len && txt.at(pos + lookahead) == c;
+ }
int margin(int i, int mar) const;
bool nodeIsChildOf(int i, QTextHTMLElements id) const;
diff --git a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp
index a38defa656..bb2f3750aa 100644
--- a/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp
+++ b/tests/auto/gui/text/qtextdocument/tst_qtextdocument.cpp
@@ -202,6 +202,9 @@ private slots:
void contentsChangeIndices_data();
void contentsChangeIndices();
+ void insertHtmlWithComments_data();
+ void insertHtmlWithComments();
+
private:
void backgroundImage_checkExpectedHtml(const QTextDocument &doc);
void buildRegExpData();
@@ -3917,6 +3920,44 @@ void tst_QTextDocument::contentsChangeIndices()
QCOMPARE(changeAdded - changeRemoved, 1);
}
+void tst_QTextDocument::insertHtmlWithComments_data()
+{
+ QTest::addColumn<QString>("html");
+ QTest::addColumn<QStringList>("expectedBlocks");
+
+ QTest::newRow("commentless") << "<p>first</p><p>second</p><p>third</p>"
+ << QStringList { "first", "second", "third" };
+ QTest::newRow("normal") << "<p>first</p><!--<p>second</p>--><p>third</p>"
+ << QStringList { "first", "third" };
+ QTest::newRow("nonClosing") << "<p>first</p><!--<p>second</p><p>third</p>"
+ << QStringList { "first" };
+ QTest::newRow("immediatelyClosing") << "<p>first</p><!----><p>second</p><p>third</p>"
+ << QStringList { "first", "second", "third" };
+ QTest::newRow("fake") << "<p>first</p><!-<p>second</p><p>third</p>"
+ << QStringList { "first", "second", "third" };
+ QTest::newRow("endingNonExistant") << "<p>first</p>--><p>second</p><p>third</p>"
+ << QStringList { "first", "-->", "second", "third" };
+}
+
+void tst_QTextDocument::insertHtmlWithComments()
+{
+ QFETCH(QString, html);
+ QFETCH(QStringList, expectedBlocks);
+
+ QTextDocument doc;
+ doc.setHtml(html);
+
+ QCOMPARE(doc.blockCount(), expectedBlocks.count());
+
+ QStringList blockContent;
+ auto currentBlock = doc.begin();
+ while (currentBlock != doc.end()) {
+ blockContent.append(currentBlock.text());
+ currentBlock = currentBlock.next();
+ }
+
+ QCOMPARE(blockContent, expectedBlocks);
+}
QTEST_MAIN(tst_QTextDocument)
#include "tst_qtextdocument.moc"