mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-23 06:35:24 -07:00
Apply suggestions to move function ahead
This commit is contained in:
parent
e1c9bca999
commit
3c37670592
1 changed files with 39 additions and 40 deletions
|
@ -57,8 +57,47 @@
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
void convertRelativeUrlToAbsolute(QString &html, const QString &baseUrl);
|
void convertRelativeUrlToAbsolute(QString &html, const QString &baseUrl);
|
||||||
|
|
||||||
|
void convertRelativeUrlToAbsolute(QString &html, const QString &baseUrl)
|
||||||
|
{
|
||||||
|
const QRegularExpression rx {uR"(((<a\s+[^>]*?href|<img\s+[^>]*?src)\s*=\s*["'])((https?|ftp):)?(\/\/[^\/]*)?(\/?[^\/"].*?)(["']))"_s
|
||||||
|
, QRegularExpression::CaseInsensitiveOption};
|
||||||
|
|
||||||
|
const QString normalizedBaseUrl = baseUrl.endsWith(u'/') ? baseUrl : baseUrl + u'/';
|
||||||
|
const QUrl url {normalizedBaseUrl};
|
||||||
|
const QString defaultScheme = url.scheme();
|
||||||
|
QRegularExpressionMatchIterator iter = rx.globalMatch(html);
|
||||||
|
|
||||||
|
while (iter.hasNext())
|
||||||
|
{
|
||||||
|
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 URL
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString relativePath = match.captured(6);
|
||||||
|
if (relativePath.startsWith(u'/'))
|
||||||
|
relativePath = relativePath.mid(1);
|
||||||
|
|
||||||
|
const QString absoluteUrl = !host.isEmpty()
|
||||||
|
? QString(defaultScheme + u":" + host) : QString(normalizedBaseUrl + relativePath);
|
||||||
|
const QString fullMatch = match.captured(0);
|
||||||
|
const QString prefix = match.captured(1);
|
||||||
|
const QString suffix = match.captured(7);
|
||||||
|
|
||||||
|
html.replace(fullMatch, (prefix + absoluteUrl + suffix));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
RSSWidget::RSSWidget(IGUIApplication *app, QWidget *parent)
|
RSSWidget::RSSWidget(IGUIApplication *app, QWidget *parent)
|
||||||
: GUIApplicationComponent(app, parent)
|
: GUIApplicationComponent(app, parent)
|
||||||
, m_ui {new Ui::RSSWidget}
|
, m_ui {new Ui::RSSWidget}
|
||||||
|
@ -623,43 +662,3 @@ void RSSWidget::renderArticle(const RSS::Article *article) const
|
||||||
html += u"</div>";
|
html += u"</div>";
|
||||||
m_ui->textBrowser->setHtml(html);
|
m_ui->textBrowser->setHtml(html);
|
||||||
}
|
}
|
||||||
namespace
|
|
||||||
{
|
|
||||||
void convertRelativeUrlToAbsolute(QString &html, const QString &baseUrl)
|
|
||||||
{
|
|
||||||
const QRegularExpression rx {uR"(((<a\s+[^>]*?href|<img\s+[^>]*?src)\s*=\s*["'])((https?|ftp):)?(\/\/[^\/]*)?(\/?[^\/"].*?)(["']))"_s
|
|
||||||
, QRegularExpression::CaseInsensitiveOption};
|
|
||||||
|
|
||||||
const QString normalizedBaseUrl = baseUrl.endsWith(u'/') ? baseUrl : baseUrl + u'/';
|
|
||||||
const QUrl url {normalizedBaseUrl};
|
|
||||||
const QString defaultScheme = url.scheme();
|
|
||||||
QRegularExpressionMatchIterator iter = rx.globalMatch(html);
|
|
||||||
|
|
||||||
while (iter.hasNext())
|
|
||||||
{
|
|
||||||
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 URL
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString relativePath = match.captured(6);
|
|
||||||
if (relativePath.startsWith(u'/'))
|
|
||||||
relativePath = relativePath.mid(1);
|
|
||||||
|
|
||||||
const QString absoluteUrl = !host.isEmpty()
|
|
||||||
? QString(defaultScheme + u":" + host) : QString(normalizedBaseUrl + relativePath);
|
|
||||||
const QString fullMatch = match.captured(0);
|
|
||||||
const QString prefix = match.captured(1);
|
|
||||||
const QString suffix = match.captured(7);
|
|
||||||
|
|
||||||
html.replace(fullMatch, (prefix + absoluteUrl + suffix));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue