diff options
author | Martin Jones <martin.jones@nokia.com> | 2011-12-14 15:20:25 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-12-14 09:02:24 +0100 |
commit | 22a8387aeab93fd086b297443945487677aef280 (patch) | |
tree | 518a2d8ca1bdeb0a3e827ded1688b72e16b763f2 /src/quick/util/qdeclarativestyledtext.cpp | |
parent | 5ee8eaf094b1648009b9dc410120df1b5f2fd664 (diff) |
Correctly ignore unknown tags in StyledText
Also improve tests to compare expected formatting and improve coverage.
Change-Id: I021dbdcd147dd7340a0dc1c30f4b104f22efece0
Reviewed-by: Yann Bodson <yann.bodson@nokia.com>
Diffstat (limited to 'src/quick/util/qdeclarativestyledtext.cpp')
-rw-r--r-- | src/quick/util/qdeclarativestyledtext.cpp | 83 |
1 files changed, 53 insertions, 30 deletions
diff --git a/src/quick/util/qdeclarativestyledtext.cpp b/src/quick/util/qdeclarativestyledtext.cpp index 2f79fad9a2..d2c2ef8f1b 100644 --- a/src/quick/util/qdeclarativestyledtext.cpp +++ b/src/quick/util/qdeclarativestyledtext.cpp @@ -160,6 +160,7 @@ void QDeclarativeStyledTextPrivate::parse() int textStart = 0; int textLength = 0; int rangeStart = 0; + bool formatChanged = false; const QChar *ch = text.constData(); while (!ch->isNull()) { if (*ch == lessThan) { @@ -177,26 +178,35 @@ void QDeclarativeStyledTextPrivate::parse() } } if (rangeStart != drawText.length() && formatStack.count()) { - QTextLayout::FormatRange formatRange; - formatRange.format = formatStack.top(); - formatRange.start = rangeStart; - formatRange.length = drawText.length() - rangeStart; - ranges.append(formatRange); + if (formatChanged) { + QTextLayout::FormatRange formatRange; + formatRange.format = formatStack.top(); + formatRange.start = rangeStart; + formatRange.length = drawText.length() - rangeStart; + ranges.append(formatRange); + formatChanged = false; + } else if (ranges.count()) { + ranges.last().length += drawText.length() - rangeStart; + } } rangeStart = drawText.length(); ++ch; if (*ch == slash) { ++ch; if (parseCloseTag(ch, text, drawText)) { - if (formatStack.count()) + if (formatStack.count()) { + formatChanged = true; formatStack.pop(); + } } } else { QTextCharFormat format; if (formatStack.count()) format = formatStack.top(); - if (parseTag(ch, text, drawText, format)) + if (parseTag(ch, text, drawText, format)) { + formatChanged = true; formatStack.push(format); + } } textStart = ch - text.constData() + 1; textLength = 0; @@ -215,11 +225,15 @@ void QDeclarativeStyledTextPrivate::parse() if (textLength) drawText.append(QStringRef(&text, textStart, textLength)); if (rangeStart != drawText.length() && formatStack.count()) { - QTextLayout::FormatRange formatRange; - formatRange.format = formatStack.top(); - formatRange.start = rangeStart; - formatRange.length = drawText.length() - rangeStart; - ranges.append(formatRange); + if (formatChanged) { + QTextLayout::FormatRange formatRange; + formatRange.format = formatStack.top(); + formatRange.start = rangeStart; + formatRange.length = drawText.length() - rangeStart; + ranges.append(formatRange); + } else if (ranges.count()) { + ranges.last().length += drawText.length() - rangeStart; + } } layout.setText(drawText); @@ -239,27 +253,28 @@ bool QDeclarativeStyledTextPrivate::parseTag(const QChar *&ch, const QString &te QStringRef tag(&textIn, tagStart, tagLength); const QChar char0 = tag.at(0); if (char0 == QLatin1Char('b')) { - if (tagLength == 1) + if (tagLength == 1) { format.setFontWeight(QFont::Bold); - else if (tagLength == 2 && tag.at(1) == QLatin1Char('r')) { + return true; + } else if (tagLength == 2 && tag.at(1) == QLatin1Char('r')) { textOut.append(QChar(QChar::LineSeparator)); return false; } } else if (char0 == QLatin1Char('i')) { - if (tagLength == 1) + if (tagLength == 1) { format.setFontItalic(true); + return true; + } } else if (char0 == QLatin1Char('p')) { if (tagLength == 1) { if (!hasNewLine) textOut.append(QChar::LineSeparator); } - } else if (char0 == QLatin1Char('s')) { - if (tag == QLatin1String("strong")) - format.setFontWeight(QFont::Bold); } else if (char0 == QLatin1Char('u')) { - if (tagLength == 1) + if (tagLength == 1) { format.setFontUnderline(true); - else if (tag == QLatin1String("ul")) { + return true; + } else if (tag == QLatin1String("ul")) { List listItem; listItem.level = 0; listItem.type = Unordered; @@ -274,7 +289,11 @@ bool QDeclarativeStyledTextPrivate::parseTag(const QChar *&ch, const QString &te textOut.append(QChar::LineSeparator); format.setFontPointSize(baseFont.pointSize() * scaling[level - 1]); format.setFontWeight(QFont::Bold); + return true; } + } else if (tag == QLatin1String("strong")) { + format.setFontWeight(QFont::Bold); + return true; } else if (tag == QLatin1String("ol")) { List listItem; listItem.level = 0; @@ -317,16 +336,20 @@ bool QDeclarativeStyledTextPrivate::parseTag(const QChar *&ch, const QString &te textOut += QString(2, QChar::Nbsp); } } - return true; + return false; } else if (ch->isSpace()) { // may have params. QStringRef tag(&textIn, tagStart, tagLength); if (tag == QLatin1String("font")) return parseFontAttributes(ch, textIn, format); - if (tag == QLatin1String("ol")) - return parseOrderedListAttributes(ch, textIn); - if (tag == QLatin1String("ul")) - return parseUnorderedListAttributes(ch, textIn); + if (tag == QLatin1String("ol")) { + parseOrderedListAttributes(ch, textIn); + return false; // doesn't modify format + } + if (tag == QLatin1String("ul")) { + parseUnorderedListAttributes(ch, textIn); + return false; // doesn't modify format + } if (tag == QLatin1String("a")) { return parseAnchorAttributes(ch, textIn, format); } @@ -357,7 +380,7 @@ bool QDeclarativeStyledTextPrivate::parseCloseTag(const QChar *&ch, const QStrin if (tagLength == 1) return true; else if (tag.at(1) == QLatin1Char('r') && tagLength == 2) - return true; + return false; } else if (char0 == QLatin1Char('i')) { if (tagLength == 1) return true; @@ -368,7 +391,7 @@ bool QDeclarativeStyledTextPrivate::parseCloseTag(const QChar *&ch, const QStrin if (tagLength == 1) { textOut.append(QChar::LineSeparator); hasNewLine = true; - return true; + return false; } } else if (char0 == QLatin1Char('u')) { if (tagLength == 1) @@ -379,7 +402,7 @@ bool QDeclarativeStyledTextPrivate::parseCloseTag(const QChar *&ch, const QStrin if (!listStack.count()) textOut.append(QChar::LineSeparator); } - return true; + return false; } } else if (char0 == QLatin1Char('h') && tagLength == 2) { textOut.append(QChar::LineSeparator); @@ -395,9 +418,9 @@ bool QDeclarativeStyledTextPrivate::parseCloseTag(const QChar *&ch, const QStrin if (!listStack.count()) textOut.append(QChar::LineSeparator); } - return true; + return false; } else if (tag == QLatin1String("li")) { - return true; + return false; } return false; } else if (!ch->isSpace()){ |