Apply suggestions from code review

Co-authored-by: Vladimir Golovnev <glassez@yandex.ru>
This commit is contained in:
Zentino 2024-12-05 23:36:23 +08:00 committed by GitHub
commit 8c973b89eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -572,22 +572,29 @@ void RSSWidget::convertRelativePathToAbsolute(QString &html, const QString &base
while (iter.hasNext()) while (iter.hasNext())
{ {
QRegularExpressionMatch match = iter.next(); const QRegularExpressionMatch match = iter.next();
const QString &fullMatch = match.captured(0);
const Qstring &prefix = match.captured(1);
const QString &scheme = match.captured(4); const QString &scheme = match.captured(4);
const QString &host = match.captured(5); const QString &host = match.captured(5);
if (!scheme.isEmpty())
{
if (host.isEmpty())
break; // invalid URL, should never happen
// already absolute path
continue;
}
QString relativePath = match.captured(6); QString relativePath = match.captured(6);
const QString &suffix = match.captured(7);
if (relativePath.startsWith(u'/')) if (relativePath.startsWith(u'/'))
relativePath = relativePath.mid(1); relativePath = relativePath.mid(1);
if (!scheme.isEmpty() && !host.isEmpty()) // already absolute path
continue; if (!host.isEmpty())
else if (!host.isEmpty())
normalizedBasePath = u"http:" + host; normalizedBasePath = u"http:" + host;
else if (!scheme.isEmpty())
break; // invalid url, should never happen const QString &fullMatch = match.captured(0);
QString absolutePath = normalizedBasePath + relativePath; const QString &prefix = match.captured(1);
const QString &suffix = match.captured(7);
const QString absolutePath = normalizedBasePath + relativePath;
html.replace(fullMatch, prefix + absolutePath + suffix); html.replace(fullMatch, prefix + absolutePath + suffix);
} }
@ -646,8 +653,8 @@ void RSSWidget::renderArticle(const RSS::Article *article) const
} }
// Supplement relative path to absolute path // Supplement relative path to absolute path
QUrl url {article->link()}; const QUrl url {article->link()};
QString basePath = url.scheme() + u"://" + url.host(); const QString basePath = url.scheme() + u"://" + url.host();
convertRelativePathToAbsolute(html, basePath); convertRelativePathToAbsolute(html, basePath);
html += u"</div>"; html += u"</div>";
m_ui->textBrowser->setHtml(html); m_ui->textBrowser->setHtml(html);