From 8c973b89eb4811a3fbb4f211a0184a1628fdaa7b Mon Sep 17 00:00:00 2001 From: Zentino <52699319+Zylsjsp@users.noreply.github.com> Date: Thu, 5 Dec 2024 23:36:23 +0800 Subject: [PATCH] Apply suggestions from code review Co-authored-by: Vladimir Golovnev --- src/gui/rss/rsswidget.cpp | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/gui/rss/rsswidget.cpp b/src/gui/rss/rsswidget.cpp index 253cc8a6e..320c9bad4 100644 --- a/src/gui/rss/rsswidget.cpp +++ b/src/gui/rss/rsswidget.cpp @@ -572,22 +572,29 @@ void RSSWidget::convertRelativePathToAbsolute(QString &html, const QString &base while (iter.hasNext()) { - QRegularExpressionMatch match = iter.next(); - const QString &fullMatch = match.captured(0); - const Qstring &prefix = match.captured(1); + const QRegularExpressionMatch match = iter.next(); const QString &scheme = match.captured(4); 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); - const QString &suffix = match.captured(7); if (relativePath.startsWith(u'/')) relativePath = relativePath.mid(1); - if (!scheme.isEmpty() && !host.isEmpty()) // already absolute path - continue; - else if (!host.isEmpty()) + + if (!host.isEmpty()) normalizedBasePath = u"http:" + host; - else if (!scheme.isEmpty()) - break; // invalid url, should never happen - QString absolutePath = normalizedBasePath + relativePath; + + const QString &fullMatch = match.captured(0); + const QString &prefix = match.captured(1); + const QString &suffix = match.captured(7); + const QString absolutePath = normalizedBasePath + relativePath; html.replace(fullMatch, prefix + absolutePath + suffix); } @@ -646,8 +653,8 @@ void RSSWidget::renderArticle(const RSS::Article *article) const } // Supplement relative path to absolute path - QUrl url {article->link()}; - QString basePath = url.scheme() + u"://" + url.host(); + const QUrl url {article->link()}; + const QString basePath = url.scheme() + u"://" + url.host(); convertRelativePathToAbsolute(html, basePath); html += u""; m_ui->textBrowser->setHtml(html);