diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 11cc6ed7da..16263ad736 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -225,6 +225,9 @@ \value RemoveQuery The query part of the URL (following a '?' character) is removed. \value RemoveFragment + \value RemoveFilename The filename (i.e. everything after the last '/' in the path) is removed. + The trailing '/' is kept, unless StripTrailingSlash is set. + Only valid if RemovePath is not set. \value PreferLocalFile If the URL is a local file according to isLocalFile() and contains no query or fragment, a local file path is returned. \value StripTrailingSlash The trailing slash is removed if one is present. @@ -800,6 +803,12 @@ inline void QUrlPrivate::appendPassword(QString &appendTo, QUrl::FormattingOptio inline void QUrlPrivate::appendPath(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const { QString thePath = path; + if (options & QUrl::RemoveFilename) { + const int slash = path.lastIndexOf(QLatin1Char('/')); + if (slash == -1) + return; + thePath = path.left(slash+1); + } // check if we need to remove trailing slashes if ((options & QUrl::StripTrailingSlash) && !thePath.isEmpty() && thePath != QLatin1String("/") && thePath.endsWith(QLatin1Char('/'))) thePath.chop(1); @@ -3226,7 +3235,7 @@ QUrl QUrl::adjusted(QUrl::FormattingOptions options) const that.setFragment(QString()); if (options & RemovePath) { that.setPath(QString()); - } else if (options & StripTrailingSlash) { + } else if (options & (StripTrailingSlash | RemoveFilename)) { QString path; d->appendPath(path, options, QUrlPrivate::Path); that.setPath(path); diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h index cb7bcf5409..98cf760dec 100644 --- a/src/corelib/io/qurl.h +++ b/src/corelib/io/qurl.h @@ -136,7 +136,8 @@ public: RemoveFragment = 0x80, // 0x100 was a private code in Qt 4, keep unused for a while PreferLocalFile = 0x200, - StripTrailingSlash = 0x400 + StripTrailingSlash = 0x400, + RemoveFilename = 0x800 }; enum ComponentFormattingOption { diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index b97ebf11f2..698dc5ca72 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -2428,27 +2428,35 @@ void tst_QUrl::fromEncoded() void tst_QUrl::stripTrailingSlash_data() { QTest::addColumn("url"); - QTest::addColumn("expected"); + QTest::addColumn("expectedStrip"); // toString(Strip) + QTest::addColumn("expectedDir"); // toString(RemoveFilename) + QTest::addColumn("expectedDirStrip"); // toString(RemoveFilename|Strip) - QTest::newRow("subdir no slash") << "ftp://kde.org/dir/subdir" << "ftp://kde.org/dir/subdir"; - QTest::newRow("ftp no slash") << "ftp://ftp.de.kde.org/dir" << "ftp://ftp.de.kde.org/dir"; - QTest::newRow("ftp slash") << "ftp://ftp.de.kde.org/dir/" << "ftp://ftp.de.kde.org/dir"; - QTest::newRow("file slash") << "file:///dir/" << "file:///dir"; - QTest::newRow("file no slash") << "file:///dir/" << "file:///dir"; - QTest::newRow("file root") << "file:///" << "file:///"; - QTest::newRow("no path") << "remote://" << "remote://"; + QTest::newRow("subdir no slash") << "ftp://kde.org/dir/subdir" << "ftp://kde.org/dir/subdir" << "ftp://kde.org/dir/" << "ftp://kde.org/dir"; + QTest::newRow("ftp no slash") << "ftp://kde.org/dir" << "ftp://kde.org/dir" << "ftp://kde.org/" << "ftp://kde.org/"; + QTest::newRow("ftp slash") << "ftp://kde.org/dir/" << "ftp://kde.org/dir" << "ftp://kde.org/dir/" << "ftp://kde.org/dir"; + QTest::newRow("file slash") << "file:///dir/" << "file:///dir" << "file:///dir/" << "file:///dir"; + QTest::newRow("file no slash") << "file:///dir" << "file:///dir" << "file:///" << "file:///"; + QTest::newRow("file root") << "file:///" << "file:///" << "file:///" << "file:///"; + QTest::newRow("no path") << "remote://" << "remote://" << "remote://" << "remote://"; } void tst_QUrl::stripTrailingSlash() { QFETCH(QString, url); - QFETCH(QString, expected); + QFETCH(QString, expectedStrip); + QFETCH(QString, expectedDir); + QFETCH(QString, expectedDirStrip); QUrl u(url); - QCOMPARE(u.toString(QUrl::StripTrailingSlash), expected); + QCOMPARE(u.toString(QUrl::StripTrailingSlash), expectedStrip); + QCOMPARE(u.toString(QUrl::RemoveFilename), expectedDir); + QCOMPARE(u.toString(QUrl::RemoveFilename | QUrl::StripTrailingSlash), expectedDirStrip); // Same thing, using QUrl::adjusted() - QCOMPARE(u.adjusted(QUrl::StripTrailingSlash).toString(), expected); + QCOMPARE(u.adjusted(QUrl::StripTrailingSlash).toString(), expectedStrip); + QCOMPARE(u.adjusted(QUrl::RemoveFilename).toString(), expectedDir); + QCOMPARE(u.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash).toString(), expectedDirStrip); } void tst_QUrl::hosts_data()