QUrl: add RemoveFilename to UrlFormattingOptions.

This allows to find the parent directory url using
url.adjusted(QUrl::RemoveFilename).

Change-Id: I1ca433ac67e4f93080de54a9b7ab2e538509ed04
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
David Faure 2013-04-25 10:01:23 +02:00 committed by The Qt Project
parent 602c911820
commit 10023de7a8
3 changed files with 31 additions and 13 deletions

View File

@ -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);

View File

@ -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 {

View File

@ -2428,27 +2428,35 @@ void tst_QUrl::fromEncoded()
void tst_QUrl::stripTrailingSlash_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<QString>("expected");
QTest::addColumn<QString>("expectedStrip"); // toString(Strip)
QTest::addColumn<QString>("expectedDir"); // toString(RemoveFilename)
QTest::addColumn<QString>("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()