mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-22 22:33:34 -07:00
RSS widget: Convert relative paths to absolute
In RSS widget, a relative url will cause an infinite loop loading for resource, which won't break until unselect the article explicitly format code
This commit is contained in:
parent
2d1c4fc809
commit
2cec901fde
2 changed files with 41 additions and 0 deletions
|
@ -559,6 +559,41 @@ bool RSSWidget::eventFilter(QObject *obj, QEvent *event)
|
|||
return false;
|
||||
}
|
||||
|
||||
void RSSWidget::convertRelativePathToAbsolute(QString &html, const QString &basePath) const
|
||||
{
|
||||
QRegularExpression rx;
|
||||
rx.setPatternOptions(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'/';
|
||||
QRegularExpressionMatchIterator iter = rx.globalMatch(html);
|
||||
|
||||
while (iter.hasNext())
|
||||
{
|
||||
QRegularExpressionMatch match = iter.next();
|
||||
const QString &fullMatch = match.captured(0);
|
||||
const Qstring &prefix = match.captured(1);
|
||||
const QString &scheme = match.captured(4);
|
||||
const QString &host = match.captured(5);
|
||||
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())
|
||||
normalizedBasePath = u"http:" + host;
|
||||
else if (!scheme.isEmpty())
|
||||
break; // invalid url, should never happen
|
||||
QString absolutePath = normalizedBasePath + relativePath;
|
||||
|
||||
html.replace(fullMatch,
|
||||
prefix + absolutePath + suffix);
|
||||
}
|
||||
}
|
||||
|
||||
void RSSWidget::renderArticle(const RSS::Article *article) const
|
||||
{
|
||||
Q_ASSERT(article);
|
||||
|
@ -610,6 +645,11 @@ void RSSWidget::renderArticle(const RSS::Article *article) const
|
|||
|
||||
html += u"<pre>" + description + u"</pre>";
|
||||
}
|
||||
// Supplement relative path to absolute path
|
||||
|
||||
QUrl url {article->link()};
|
||||
QString basePath = url.scheme() + u"://" + url.host();
|
||||
convertRelativePathToAbsolute(html, basePath);
|
||||
html += u"</div>";
|
||||
m_ui->textBrowser->setHtml(html);
|
||||
}
|
||||
|
|
|
@ -91,6 +91,7 @@ private slots:
|
|||
|
||||
private:
|
||||
bool eventFilter(QObject *obj, QEvent *event) override;
|
||||
void convertRelativePathToAbsolute(QString &html, const QString &basePath) const;
|
||||
void renderArticle(const RSS::Article *article) const;
|
||||
|
||||
Ui::RSSWidget *m_ui = nullptr;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue