summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2019-03-26 12:48:36 +0100
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2019-08-12 17:24:05 +0100
commitd45908e24292a41ff7838366b34be7340bf9fda5 (patch)
tree8ff13569a5946137794f3ec3f6eadacf1407556e
parenta1609d1603bfaeaa54f691f994c458a6a27e5229 (diff)
Support missing white-space:pre-line CSS
A mode that only preserves new lines. Change-Id: I612347b181c6e6c41dfae0cf60b22a662cba1b7e Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r--src/gui/text/qcssparser.cpp9
-rw-r--r--src/gui/text/qcssparser_p.h1
-rw-r--r--src/gui/text/qtextdocumentfragment.cpp9
-rw-r--r--src/gui/text/qtexthtmlparser.cpp9
-rw-r--r--src/gui/text/qtexthtmlparser_p.h1
-rw-r--r--tests/auto/gui/text/qcssparser/tst_qcssparser.cpp29
-rw-r--r--tests/auto/gui/text/qtextdocumentfragment/tst_qtextdocumentfragment.cpp14
7 files changed, 60 insertions, 12 deletions
diff --git a/src/gui/text/qcssparser.cpp b/src/gui/text/qcssparser.cpp
index b5489c7ed9..45f1ca596e 100644
--- a/src/gui/text/qcssparser.cpp
+++ b/src/gui/text/qcssparser.cpp
@@ -222,6 +222,7 @@ static const QCssKnownValue values[NumKnownValues - 1] = {
{ "outset", Value_Outset },
{ "overline", Value_Overline },
{ "pre", Value_Pre },
+ { "pre-line", Value_PreLine },
{ "pre-wrap", Value_PreWrap },
{ "ridge", Value_Ridge },
{ "right", Value_Right },
@@ -248,10 +249,10 @@ static const QCssKnownValue values[NumKnownValues - 1] = {
};
//Map id to strings as they appears in the 'values' array above
-static const short indexOfId[NumKnownValues] = { 0, 41, 48, 42, 49, 54, 35, 26, 70, 71, 25, 43, 5, 63, 47,
- 29, 58, 59, 27, 51, 61, 6, 10, 39, 56, 19, 13, 17, 18, 20, 21, 50, 24, 46, 67, 37, 3, 2, 40, 62, 16,
- 11, 57, 14, 32, 64, 33, 65, 55, 66, 34, 69, 8, 28, 38, 12, 36, 60, 7, 9, 4, 68, 53, 22, 23, 30, 31,
- 1, 15, 0, 52, 45, 44 };
+static const short indexOfId[NumKnownValues] = { 0, 41, 48, 42, 49, 50, 55, 35, 26, 71, 72, 25, 43, 5, 64, 48,
+ 29, 59, 60, 27, 52, 62, 6, 10, 39, 56, 19, 13, 17, 18, 20, 21, 51, 24, 46, 68, 37, 3, 2, 40, 63, 16,
+ 11, 58, 14, 32, 65, 33, 66, 56, 67, 34, 70, 8, 28, 38, 12, 36, 61, 7, 9, 4, 69, 54, 22, 23, 30, 31,
+ 1, 15, 0, 53, 45, 44 };
QString Value::toString() const
{
diff --git a/src/gui/text/qcssparser_p.h b/src/gui/text/qcssparser_p.h
index b0fa4be682..ddc46803ae 100644
--- a/src/gui/text/qcssparser_p.h
+++ b/src/gui/text/qcssparser_p.h
@@ -205,6 +205,7 @@ enum KnownValue {
Value_Normal,
Value_Pre,
Value_NoWrap,
+ Value_PreLine,
Value_PreWrap,
Value_Small,
Value_Medium,
diff --git a/src/gui/text/qtextdocumentfragment.cpp b/src/gui/text/qtextdocumentfragment.cpp
index 1905d9a1b1..8ad1300e6c 100644
--- a/src/gui/text/qtextdocumentfragment.cpp
+++ b/src/gui/text/qtextdocumentfragment.cpp
@@ -576,6 +576,9 @@ bool QTextHtmlImporter::appendNodeText()
&& ch != QChar::Nbsp
&& ch != QChar::ParagraphSeparator) {
+ if (wsm == QTextHtmlParserNode::WhiteSpacePreLine && (ch == QLatin1Char('\n') || ch == QLatin1Char('\r')))
+ compressNextWhitespace = PreserveWhiteSpace;
+
if (compressNextWhitespace == CollapseWhiteSpace)
compressNextWhitespace = RemoveWhiteSpace; // allow this one, and remove the ones coming next.
else if(compressNextWhitespace == RemoveWhiteSpace)
@@ -592,7 +595,9 @@ bool QTextHtmlImporter::appendNodeText()
}
} else if (wsm != QTextHtmlParserNode::WhiteSpacePreWrap) {
compressNextWhitespace = RemoveWhiteSpace;
- if (wsm == QTextHtmlParserNode::WhiteSpaceNoWrap)
+ if (wsm == QTextHtmlParserNode::WhiteSpacePreLine && (ch == QLatin1Char('\n') || ch == QLatin1Char('\r')))
+ { }
+ else if (wsm == QTextHtmlParserNode::WhiteSpaceNoWrap)
ch = QChar::Nbsp;
else
ch = QLatin1Char(' ');
@@ -605,6 +610,8 @@ bool QTextHtmlImporter::appendNodeText()
|| ch == QChar::ParagraphSeparator) {
if (!textToInsert.isEmpty()) {
+ if (wsm == QTextHtmlParserNode::WhiteSpacePreLine && textToInsert.at(textToInsert.length() - 1) == QChar(' '))
+ textToInsert = textToInsert.chopped(1);
cursor.insertText(textToInsert, format);
textToInsert.clear();
}
diff --git a/src/gui/text/qtexthtmlparser.cpp b/src/gui/text/qtexthtmlparser.cpp
index 1ee317d27c..43b32e7e2c 100644
--- a/src/gui/text/qtexthtmlparser.cpp
+++ b/src/gui/text/qtexthtmlparser.cpp
@@ -649,7 +649,7 @@ void QTextHtmlParser::parseTag()
parseExclamationTag();
if (nodes.last().wsm != QTextHtmlParserNode::WhiteSpacePre
&& nodes.last().wsm != QTextHtmlParserNode::WhiteSpacePreWrap
- && !textEditMode)
+ && !textEditMode)
eatSpace();
return;
}
@@ -717,7 +717,8 @@ void QTextHtmlParser::parseTag()
// in a white-space preserving environment strip off a initial newline
// since the element itself already generates a newline
if ((node->wsm == QTextHtmlParserNode::WhiteSpacePre
- || node->wsm == QTextHtmlParserNode::WhiteSpacePreWrap)
+ || node->wsm == QTextHtmlParserNode::WhiteSpacePreWrap
+ || node->wsm == QTextHtmlParserNode::WhiteSpacePreLine)
&& node->isBlock()) {
if (pos < len - 1 && txt.at(pos) == QLatin1Char('\n'))
++pos;
@@ -761,7 +762,8 @@ void QTextHtmlParser::parseCloseTag()
// in a new block for elements following the <pre>
// ...foo\n</pre><p>blah -> foo</pre><p>blah
if ((at(p).wsm == QTextHtmlParserNode::WhiteSpacePre
- || at(p).wsm == QTextHtmlParserNode::WhiteSpacePreWrap)
+ || at(p).wsm == QTextHtmlParserNode::WhiteSpacePreWrap
+ || at(p).wsm == QTextHtmlParserNode::WhiteSpacePreLine)
&& at(p).isBlock()) {
if (at(last()).text.endsWith(QLatin1Char('\n')))
nodes[last()].text.chop(1);
@@ -1278,6 +1280,7 @@ void QTextHtmlParserNode::applyCssDeclarations(const QVector<QCss::Declaration>
case QCss::Value_Pre: wsm = QTextHtmlParserNode::WhiteSpacePre; break;
case QCss::Value_NoWrap: wsm = QTextHtmlParserNode::WhiteSpaceNoWrap; break;
case QCss::Value_PreWrap: wsm = QTextHtmlParserNode::WhiteSpacePreWrap; break;
+ case QCss::Value_PreLine: wsm = QTextHtmlParserNode::WhiteSpacePreLine; break;
default: break;
}
break;
diff --git a/src/gui/text/qtexthtmlparser_p.h b/src/gui/text/qtexthtmlparser_p.h
index c174b54a61..ff5f5b4c35 100644
--- a/src/gui/text/qtexthtmlparser_p.h
+++ b/src/gui/text/qtexthtmlparser_p.h
@@ -158,6 +158,7 @@ struct QTextHtmlParserNode {
WhiteSpacePre,
WhiteSpaceNoWrap,
WhiteSpacePreWrap,
+ WhiteSpacePreLine,
WhiteSpaceModeUndefined = -1
};
diff --git a/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp b/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp
index 7dbeb13aa7..7764a716ca 100644
--- a/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp
+++ b/tests/auto/gui/text/qcssparser/tst_qcssparser.cpp
@@ -78,6 +78,8 @@ private slots:
void extractBorder();
void noTextDecoration();
void quotedAndUnquotedIdentifiers();
+ void whitespaceValues_data();
+ void whitespaceValues();
};
void tst_QCssParser::scanner_data()
@@ -1746,6 +1748,33 @@ void tst_QCssParser::quotedAndUnquotedIdentifiers()
QCOMPARE(decls.at(1).d->values.first().toString(), QLatin1String("bold"));
}
+void tst_QCssParser::whitespaceValues_data()
+{
+ QTest::addColumn<QString>("value");
+
+ QTest::newRow("normal") << "normal";
+ QTest::newRow("inherit") << "inherit";
+ QTest::newRow("nowrap") << "nowrap";
+ QTest::newRow("pre") << "pre";
+ QTest::newRow("pre-wrap") << "pre-wrap";
+ QTest::newRow("pre-line") << "pre-line";
+}
+
+void tst_QCssParser::whitespaceValues()
+{
+ QFETCH(QString, value);
+ QCss::Parser parser(QString("foo { white-space: %1 }").arg(value));
+ QCss::StyleSheet sheet;
+ QVERIFY(parser.parse(&sheet));
+
+ QCss::StyleRule rule = (!sheet.styleRules.isEmpty()) ?
+ sheet.styleRules.at(0) : *sheet.nameIndex.begin();
+ QCOMPARE(rule.declarations.size(), 1);
+
+ QCOMPARE(rule.declarations.at(0).d->property, QLatin1String("white-space"));
+ QCOMPARE(rule.declarations.at(0).d->values.first().toString(), value);
+}
+
QTEST_MAIN(tst_QCssParser)
#include "tst_qcssparser.moc"
diff --git a/tests/auto/gui/text/qtextdocumentfragment/tst_qtextdocumentfragment.cpp b/tests/auto/gui/text/qtextdocumentfragment/tst_qtextdocumentfragment.cpp
index 664ca98a3f..c5243d1535 100644
--- a/tests/auto/gui/text/qtextdocumentfragment/tst_qtextdocumentfragment.cpp
+++ b/tests/auto/gui/text/qtextdocumentfragment/tst_qtextdocumentfragment.cpp
@@ -1272,11 +1272,11 @@ void tst_QTextDocumentFragment::html_whitespace_data()
QTest::newRow("2") << QString("<span> </span><span>nowhitespacehereplease</span>")
<< QString::fromLatin1("nowhitespacehereplease");
- QTest::newRow("3") << QString("<span style=\"white-space: pre;\"> white space here </span>")
- << QString::fromLatin1(" white space here ");
+ QTest::newRow("3") << QString("<span style=\"white-space: pre;\"> white space \n\n here </span>")
+ << QString::fromLatin1(" white space \n\n here ");
- QTest::newRow("4") << QString("<span style=\"white-space: pre-wrap;\"> white space here </span>")
- << QString::fromLatin1(" white space here ");
+ QTest::newRow("4") << QString("<span style=\"white-space: pre-wrap;\"> white space \n\n here </span>")
+ << QString::fromLatin1(" white space \n\n here ");
QTest::newRow("5") << QString("<a href=\"One.html\">One</a> <a href=\"Two.html\">Two</a> <b>Three</b>\n"
"<b>Four</b>")
@@ -1291,6 +1291,12 @@ void tst_QTextDocumentFragment::html_whitespace_data()
QTest::newRow("8") << QString("<table><tr><td><i>Blah</i></td></tr></table> <i>Blub</i>")
<< QString("\nBlah\nBlub");
+ QTest::newRow("9") << QString("<span style=\"white-space: nowrap;\"> white space \n\n here </span>")
+ << QString::fromLatin1("white space here ");
+
+ QTest::newRow("10") << QString("<span style=\"white-space: pre-line;\"> white space \n\n here </span>")
+ << QString::fromLatin1("white space\n\nhere ");
+
QTest::newRow("task116492") << QString("<p>a<font=\"Times\"> b </font>c</p>")
<< QString("a b c");