mirror of
https://bitbucket.org/smil3y/kdelibs.git
synced 2025-02-23 18:32:49 +00:00
kdecore: safety-net for KUrl::upUrl(), also append trailing slash from KUrl::directory()
relative paths are tricky Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
a051b1b284
commit
bce72b0b24
4 changed files with 40 additions and 3 deletions
|
@ -590,13 +590,23 @@ QString KUrl::directory(AdjustPathOption trailing) const
|
||||||
KUrl KUrl::upUrl() const
|
KUrl KUrl::upUrl() const
|
||||||
{
|
{
|
||||||
const QString urlpath = QUrl::path();
|
const QString urlpath = QUrl::path();
|
||||||
if (urlpath.isEmpty()) {
|
if (urlpath.isEmpty() || urlpath == QLatin1String("/")) {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const QString s_dotdotslash = QString::fromLatin1("../");
|
||||||
|
if (urlpath.count(s_dotdotslash) >= 10) {
|
||||||
|
// way too long, going to reach the path limit with that
|
||||||
|
KUrl result(*this);
|
||||||
|
result.setPath(QLatin1String("/"));
|
||||||
|
result.setQuery(QString());
|
||||||
|
result.setFragment(QString());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (QDir::isRelativePath(urlpath)) {
|
if (QDir::isRelativePath(urlpath)) {
|
||||||
KUrl result(*this);
|
KUrl result(*this);
|
||||||
QString newpath = QString::fromLatin1("../");
|
QString newpath = s_dotdotslash;
|
||||||
newpath.append(kPathDirectory(urlpath));
|
newpath.append(kPathDirectory(urlpath));
|
||||||
result.setPath(newpath);
|
result.setPath(newpath);
|
||||||
result.setQuery(QString());
|
result.setQuery(QString());
|
||||||
|
@ -604,6 +614,15 @@ KUrl KUrl::upUrl() const
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (QDir::cleanPath(urlpath).count(QLatin1Char('/')) <= 1) {
|
||||||
|
// something like /home
|
||||||
|
KUrl result(*this);
|
||||||
|
result.setPath(QLatin1String("/"));
|
||||||
|
result.setQuery(QString());
|
||||||
|
result.setFragment(QString());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (isLocalFile()) {
|
if (isLocalFile()) {
|
||||||
QString newpath;
|
QString newpath;
|
||||||
if (urlpath.endsWith(QLatin1Char('/'))) {
|
if (urlpath.endsWith(QLatin1Char('/'))) {
|
||||||
|
|
|
@ -439,7 +439,7 @@ public:
|
||||||
* <tt>file:///hallo/torben</tt> would return "hallo/". The returned string is decoded.
|
* <tt>file:///hallo/torben</tt> would return "hallo/". The returned string is decoded.
|
||||||
* QString() is returned when there is no path.
|
* QString() is returned when there is no path.
|
||||||
*/
|
*/
|
||||||
QString directory(AdjustPathOption trailing = RemoveTrailingSlash) const;
|
QString directory(AdjustPathOption trailing = AddTrailingSlash) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the directory to @p dir, leaving the filename empty.
|
* Set the directory to @p dir, leaving the filename empty.
|
||||||
|
|
|
@ -52,6 +52,10 @@ void KUrlTest::testUpUrl_data()
|
||||||
<< KUrl("ftp://ftp.kde.org/foo?bar=baz#foobar")
|
<< KUrl("ftp://ftp.kde.org/foo?bar=baz#foobar")
|
||||||
<< KUrl("ftp://ftp.kde.org/");
|
<< KUrl("ftp://ftp.kde.org/");
|
||||||
}
|
}
|
||||||
|
void KUrlTest::testUpUrl2_data()
|
||||||
|
{
|
||||||
|
testUpUrl_data();
|
||||||
|
}
|
||||||
|
|
||||||
void KUrlTest::testUpUrl()
|
void KUrlTest::testUpUrl()
|
||||||
{
|
{
|
||||||
|
@ -69,6 +73,18 @@ void KUrlTest::testUpUrl()
|
||||||
QCOMPARE(newPath[newPath.size() - 1], QChar::fromLatin1('/'));
|
QCOMPARE(newPath[newPath.size() - 1], QChar::fromLatin1('/'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void KUrlTest::testUpUrl2()
|
||||||
|
{
|
||||||
|
QFETCH(KUrl, url);
|
||||||
|
QFETCH(KUrl, url2);
|
||||||
|
|
||||||
|
KUrl copy(url);
|
||||||
|
while (copy.hasPath() && copy.path() != QLatin1String("/")) {
|
||||||
|
copy = copy.upUrl();
|
||||||
|
// qDebug() << Q_FUNC_INFO << copy.path();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void KUrlTest::testHash_data()
|
void KUrlTest::testHash_data()
|
||||||
{
|
{
|
||||||
QTest::addColumn<KUrl>("url");
|
QTest::addColumn<KUrl>("url");
|
||||||
|
|
|
@ -27,6 +27,8 @@ class KUrlTest : public QObject
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void testUpUrl_data();
|
void testUpUrl_data();
|
||||||
void testUpUrl();
|
void testUpUrl();
|
||||||
|
void testUpUrl2_data();
|
||||||
|
void testUpUrl2();
|
||||||
void testHash_data();
|
void testHash_data();
|
||||||
void testHash();
|
void testHash();
|
||||||
void testQueryAndFragment_data();
|
void testQueryAndFragment_data();
|
||||||
|
|
Loading…
Add table
Reference in a new issue