Apply suggestions from code review

Co-authored-by: Chocobo1 <Chocobo1@users.noreply.github.com>
Co-authored-by: Vladimir Golovnev <glassez@yandex.ru>
This commit is contained in:
Zentino 2024-12-06 20:59:34 +08:00 committed by GitHub
commit 4d24395c5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -612,10 +612,10 @@ void RSSWidget::renderArticle(const RSS::Article *article) const
html += u"<pre>" + description + u"</pre>"; html += u"<pre>" + description + u"</pre>";
} }
// Supplement relative path to absolute path
// Supplement relative path to absolute path
const QUrl url {article->link()}; const QUrl url {article->link()};
const QString basePath = url.scheme() + u"://" + url.host(); const QString basePath = url.toString(QUrl::RemovePath | QUrl::RemoveQuery);
convertRelativePathToAbsolute(html, basePath); convertRelativePathToAbsolute(html, basePath);
html += u"</div>"; html += u"</div>";
m_ui->textBrowser->setHtml(html); m_ui->textBrowser->setHtml(html);
@ -623,11 +623,8 @@ void RSSWidget::renderArticle(const RSS::Article *article) const
void convertRelativePathToAbsolute(QString &html, const QString &basePath) void convertRelativePathToAbsolute(QString &html, const QString &basePath)
{ {
QRegularExpression rx; const QRegularExpression rx {uR"(((<a\s+[^>]*?href|<img\s+[^>]*?src)\s*=\s*["'])((https?|ftp):)?(\/\/[^\/]*)?(\/?[^\/"].*?)(["']))"_s
rx.setPatternOptions(QRegularExpression::CaseInsensitiveOption); , QRegularExpression::CaseInsensitiveOption};
// href regex
rx.setPattern(
uR"(((<a\s+[^>]*?href|<img\s+[^>]*?src)\s*=\s*["'])((https?|ftp):)?(\/\/[^\/]*)?(\/?[^\/"].*?)(["']))"_s);
QString normalizedBasePath = basePath.endsWith(u'/') ? basePath : basePath + u'/'; QString normalizedBasePath = basePath.endsWith(u'/') ? basePath : basePath + u'/';
QRegularExpressionMatchIterator iter = rx.globalMatch(html); QRegularExpressionMatchIterator iter = rx.globalMatch(html);
@ -635,8 +632,8 @@ void convertRelativePathToAbsolute(QString &html, const QString &basePath)
while (iter.hasNext()) while (iter.hasNext())
{ {
const QRegularExpressionMatch match = iter.next(); const QRegularExpressionMatch match = iter.next();
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 (!scheme.isEmpty())
{ {
if (host.isEmpty()) if (host.isEmpty())
@ -653,11 +650,11 @@ void convertRelativePathToAbsolute(QString &html, const QString &basePath)
if (!host.isEmpty()) if (!host.isEmpty())
normalizedBasePath = u"http:" + host; normalizedBasePath = u"http:" + host;
const QString &fullMatch = match.captured(0); const QString fullMatch = match.captured(0);
const QString &prefix = match.captured(1); const QString prefix = match.captured(1);
const QString &suffix = match.captured(7); const QString suffix = match.captured(7);
const QString absolutePath = normalizedBasePath + relativePath; const QString absolutePath = normalizedBasePath + relativePath;
html.replace(fullMatch, prefix + absolutePath + suffix); html.replace(fullMatch, (prefix + absolutePath + suffix));
} }
} }