From c3102b1f7695789e86c8fc7704736074e9ea61a0 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Fri, 28 May 2021 14:51:34 +0200 Subject: Extract the entity parsing code to a static function This way we can use it from qtdeclarative to parse styled text Change-Id: Ic888a75a9700558e97b3e743d6d42fda121ddcba Reviewed-by: Allan Sandfeld Jensen --- src/gui/text/qtexthtmlparser.cpp | 54 +++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 23 deletions(-) (limited to 'src/gui/text/qtexthtmlparser.cpp') diff --git a/src/gui/text/qtexthtmlparser.cpp b/src/gui/text/qtexthtmlparser.cpp index ec9cddd9c5..025098856b 100644 --- a/src/gui/text/qtexthtmlparser.cpp +++ b/src/gui/text/qtexthtmlparser.cpp @@ -800,12 +800,38 @@ void QTextHtmlParser::parseExclamationTag() } } +QString QTextHtmlParser::parseEntity(QStringView entity) +{ + QChar resolved = resolveEntity(entity); + if (!resolved.isNull()) + return QString(resolved); + + if (entity.length() > 1 && entity.at(0) == QLatin1Char('#')) { + entity = entity.mid(1); // removing leading # + + int base = 10; + bool ok = false; + + if (entity.at(0).toLower() == QLatin1Char('x')) { // hex entity? + entity = entity.mid(1); + base = 16; + } + + uint uc = entity.toUInt(&ok, base); + if (ok) { + if (uc >= 0x80 && uc < 0x80 + (sizeof(windowsLatin1ExtendedCharacters)/sizeof(windowsLatin1ExtendedCharacters[0]))) + uc = windowsLatin1ExtendedCharacters[uc - 0x80]; + return QStringView{QChar::fromUcs4(uc)}.toString(); + } + } + return {}; +} + // parses an entity after "&", and returns it QString QTextHtmlParser::parseEntity() { const int recover = pos; int entityLen = 0; - QStringView entity; while (pos < len) { QChar c = txt.at(pos++); if (c.isSpace() || pos - recover > 9) { @@ -816,28 +842,10 @@ QString QTextHtmlParser::parseEntity() ++entityLen; } if (entityLen) { - entity = QStringView(txt).mid(recover, entityLen); - QChar resolved = resolveEntity(entity); - if (!resolved.isNull()) - return QString(resolved); - - if (entityLen > 1 && entity.at(0) == QLatin1Char('#')) { - entity = entity.mid(1); // removing leading # - - int base = 10; - bool ok = false; - - if (entity.at(0).toLower() == QLatin1Char('x')) { // hex entity? - entity = entity.mid(1); - base = 16; - } - - uint uc = entity.toUInt(&ok, base); - if (ok) { - if (uc >= 0x80 && uc < 0x80 + (sizeof(windowsLatin1ExtendedCharacters)/sizeof(windowsLatin1ExtendedCharacters[0]))) - uc = windowsLatin1ExtendedCharacters[uc - 0x80]; - return QStringView{QChar::fromUcs4(uc)}.toString(); - } + const QStringView entity = QStringView(txt).mid(recover, entityLen); + const QString parsedEntity = parseEntity(entity); + if (!parsedEntity.isNull()) { + return parsedEntity; } } error: -- cgit v1.2.3