From 3791d3b0c2dab448b53720979b2375998d06e209 Mon Sep 17 00:00:00 2001 From: David Faure Date: Wed, 8 Feb 2012 22:17:03 +0100 Subject: [PATCH] Add QUrl::toDisplayString(), which is toString() without password. And fix documentation of toString() which said this was the method to use for displaying to humans, while this has never been true. Change-Id: Iff6df92e32b2517e1481d4992d80cae2d58da427 Reviewed-by: Oswald Buddenhagen Reviewed-by: Jonas Gastal Reviewed-by: Giuseppe D'Angelo Reviewed-by: Stephen Kelly --- src/corelib/io/qurl.cpp | 27 +++++++++++++++++++------ src/corelib/io/qurl.h | 1 + tests/auto/corelib/io/qurl/tst_qurl.cpp | 6 ++++++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index ecd154b1132..183e5706fa0 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -5679,9 +5679,8 @@ static QString toPrettyPercentEncoding(const QString &input, bool forFragment) } /*! - Returns the human-displayable string representation of the - URL. The output can be customized by passing flags with \a - options. + Returns a string representation of the URL. + The output can be customized by passing flags with \a options. The resulting QString can be passed back to a QUrl later on. @@ -5734,9 +5733,8 @@ QString QUrl::toString(FormattingOptions options) const } /*! - Returns the human-displayable string representation of the - URL. The output can be customized by passing flags with \a - options. + Returns a string representation of the URL. + The output can be customized by passing flags with \a options. The resulting QString can be passed back to a QUrl later on. @@ -5749,6 +5747,23 @@ QString QUrl::url(FormattingOptions options) const return toString(options); } +/*! + Returns a human-displayable string representation of the URL. + The output can be customized by passing flags with \a options. + The option RemovePassword is always enabled, since passwords + should never be shown back to users. + + The resulting QString can be passed back to a QUrl later on, + but any password that was present initially will be lost. + + \sa FormattingOptions, toEncoded(), toString() +*/ + +QString QUrl::toDisplayString(FormattingOptions options) const +{ + return toString(options | RemovePassword); +} + /*! Returns the encoded representation of the URL if it's valid; otherwise an empty QByteArray is returned. The output can be diff --git a/src/corelib/io/qurl.h b/src/corelib/io/qurl.h index 1c3390bc3d7..3e5c62cd7c7 100644 --- a/src/corelib/io/qurl.h +++ b/src/corelib/io/qurl.h @@ -102,6 +102,7 @@ public: void setUrl(const QString &url, ParsingMode mode = TolerantMode); QString url(FormattingOptions options = None) const; QString toString(FormattingOptions options = None) const; + QString toDisplayString(FormattingOptions options = None) const; bool isValid() const; diff --git a/tests/auto/corelib/io/qurl/tst_qurl.cpp b/tests/auto/corelib/io/qurl/tst_qurl.cpp index 611847852f4..e2831036df7 100644 --- a/tests/auto/corelib/io/qurl/tst_qurl.cpp +++ b/tests/auto/corelib/io/qurl/tst_qurl.cpp @@ -350,6 +350,8 @@ void tst_QUrl::setUrl() QVERIFY(url.authority().isEmpty()); QVERIFY(url.fragment().isEmpty()); QCOMPARE(url.port(), -1); + QCOMPARE(url.toString(), QString::fromLatin1("file:///")); + QCOMPARE(url.toDisplayString(), QString::fromLatin1("file:///")); } { @@ -363,6 +365,8 @@ void tst_QUrl::setUrl() QCOMPARE(url.host(), QString::fromLatin1("www.foo.bar")); QCOMPARE(url.authority(), QString::fromLatin1("www.foo.bar:80")); QCOMPARE(url.port(), 80); + QCOMPARE(url.toString(), QString::fromLatin1("http://www.foo.bar:80")); + QCOMPARE(url.toDisplayString(), QString::fromLatin1("http://www.foo.bar:80")); QUrl url2("//www1.foo.bar"); QCOMPARE(url.resolved(url2).toString(), QString::fromLatin1("http://www1.foo.bar")); @@ -379,6 +383,8 @@ void tst_QUrl::setUrl() QCOMPARE(url.host(), QString::fromLatin1("56::56:56:56:127.0.0.1")); QCOMPARE(url.authority(), QString::fromLatin1("user:pass@[56::56:56:56:127.0.0.1]:99")); QCOMPARE(url.port(), 99); + QCOMPARE(url.url(), QString::fromLatin1("http://user:pass@[56::56:56:56:127.0.0.1]:99")); + QCOMPARE(url.toDisplayString(), QString::fromLatin1("http://user@[56::56:56:56:127.0.0.1]:99")); } {