QUrl: add "QUrl adjusted(options)" convenience method.

Change-Id: I5eea3e0dc7b56b88a56d813207b04661b8f05a55
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
David Faure 2013-07-06 09:52:49 +02:00 committed by The Qt Project
parent 67ec78aac1
commit 602c911820
3 changed files with 65 additions and 9 deletions

View File

@ -799,12 +799,17 @@ inline void QUrlPrivate::appendPassword(QString &appendTo, QUrl::FormattingOptio
inline void QUrlPrivate::appendPath(QString &appendTo, QUrl::FormattingOptions options, Section appendingTo) const
{
QString thePath = path;
// check if we need to remove trailing slashes
if ((options & QUrl::StripTrailingSlash) && !thePath.isEmpty() && thePath != QLatin1String("/") && thePath.endsWith(QLatin1Char('/')))
thePath.chop(1);
if (appendingTo != Path && !(options & QUrl::EncodeDelimiters)) {
if (!qt_urlRecode(appendTo, path.constData(), path.constEnd(), options, decodedPathInUrlActions))
appendTo += path;
if (!qt_urlRecode(appendTo, thePath.constData(), thePath.constEnd(), options, decodedPathInUrlActions))
appendTo += thePath;
} else {
appendToUser(appendTo, path, options, encodedPathActions, decodedPathInIsolationActions);
appendToUser(appendTo, thePath, options, encodedPathActions, decodedPathInIsolationActions);
}
}
@ -3148,12 +3153,8 @@ QString QUrl::toString(FormattingOptions options) const
url += QLatin1String("//");
}
if (!(options & QUrl::RemovePath)) {
if (!(options & QUrl::RemovePath))
d->appendPath(url, options, QUrlPrivate::FullUrl);
// check if we need to remove trailing slashes
if ((options & StripTrailingSlash) && !d->path.isEmpty() && d->path != QLatin1String("/") && url.endsWith(QLatin1Char('/')))
url.chop(1);
}
if (!(options & QUrl::RemoveQuery) && d->hasQuery()) {
url += QLatin1Char('?');
@ -3187,6 +3188,52 @@ QString QUrl::toDisplayString(FormattingOptions options) const
return toString(options | RemovePassword);
}
/*!
\since 5.2
Returns an adjusted version of the URL.
The output can be customized by passing flags with \a options.
The encoding options from QUrl::ComponentFormattingOption don't make
much sense for this method, nor does QUrl::PreferLocalFile.
This is always equivalent to QUrl(url.toString(options)).
\sa FormattingOptions, toEncoded(), toString()
*/
QUrl QUrl::adjusted(QUrl::FormattingOptions options) const
{
if (!isValid()) {
// also catches isEmpty()
return QUrl();
}
QUrl that = *this;
if (options & RemoveScheme)
that.setScheme(QString());
if ((options & RemoveAuthority) == RemoveAuthority) {
that.setAuthority(QString());
} else {
if ((options & RemoveUserInfo) == RemoveUserInfo)
that.setUserInfo(QString());
else if (options & RemovePassword)
that.setPassword(QString());
if (options & RemovePort)
that.setPort(-1);
}
if (options & RemoveQuery)
that.setQuery(QString());
if (options & RemoveFragment)
that.setFragment(QString());
if (options & RemovePath) {
that.setPath(QString());
} else if (options & StripTrailingSlash) {
QString path;
d->appendPath(path, options, QUrlPrivate::Path);
that.setPath(path);
}
return that;
}
/*!
Returns the encoded representation of the URL if it's valid;
otherwise an empty QByteArray is returned. The output can be

View File

@ -181,6 +181,7 @@ public:
QString url(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
QString toString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
QString toDisplayString(FormattingOptions options = FormattingOptions(PrettyDecoded)) const;
QUrl adjusted(FormattingOptions options) const;
QByteArray toEncoded(FormattingOptions options = FullyEncoded) const;
static QUrl fromEncoded(const QByteArray &url, ParsingMode mode = TolerantMode);

View File

@ -946,8 +946,12 @@ void tst_QUrl::toString()
QFETCH(uint, options);
QFETCH(QString, string);
QUrl::FormattingOptions opt(options);
QUrl url(urlString);
QCOMPARE(url.toString(QUrl::FormattingOptions(options)), string);
QCOMPARE(url.toString(opt), string);
QCOMPARE(url.adjusted(opt).toString(), string);
}
void tst_QUrl::toAndFromStringList_data()
@ -2426,6 +2430,7 @@ void tst_QUrl::stripTrailingSlash_data()
QTest::addColumn<QString>("url");
QTest::addColumn<QString>("expected");
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";
@ -2441,6 +2446,9 @@ void tst_QUrl::stripTrailingSlash()
QUrl u(url);
QCOMPARE(u.toString(QUrl::StripTrailingSlash), expected);
// Same thing, using QUrl::adjusted()
QCOMPARE(u.adjusted(QUrl::StripTrailingSlash).toString(), expected);
}
void tst_QUrl::hosts_data()