diff options
| author | Leticia Valladares <leticia.valladares.fernandez@qt.io> | 2022-06-02 11:01:03 +0200 |
|---|---|---|
| committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2022-09-13 07:16:01 +0000 |
| commit | 3571770380257c50471ce03b9448804622871375 (patch) | |
| tree | a3e5eab426731a508cd20cff707426e8bfa54119 | |
| parent | 9cd41298c115d5d82cadbef315eb876d860317aa (diff) | |
UrlObject: Add colon after scheme
This change adds a colon after scheme in methods 'setProtocol' and
'setUrl' on its protocol line. Likewise, this includes a test called
'colonAfterProtocol' to check if colons were correctly added by using
different schemes: ftp, http and https, or if colons were removed when
setting the scheme (i.e. from protocol 'ftp:', 'ftp:http:' or 'ftp:::'
to 'ftp').
Fixes: QTBUG-103746
Change-Id: I8f847bedd23e476e0ae7901a2f3f3963da3ca04d
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
(cherry picked from commit c61075d2e0049b3c92556c7221e38ef1122118b6)
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
| -rw-r--r-- | src/qml/jsruntime/qv4urlobject.cpp | 16 | ||||
| -rw-r--r-- | tests/auto/qml/qjsengine/tst_qjsengine.cpp | 2 | ||||
| -rw-r--r-- | tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp | 60 |
3 files changed, 69 insertions, 9 deletions
diff --git a/src/qml/jsruntime/qv4urlobject.cpp b/src/qml/jsruntime/qv4urlobject.cpp index c63e71ea46..0a55a1d876 100644 --- a/src/qml/jsruntime/qv4urlobject.cpp +++ b/src/qml/jsruntime/qv4urlobject.cpp @@ -167,7 +167,7 @@ void UrlObject::setUrl(const QUrl &url) d()->port.set(engine(), engine()->newString(url.port() == -1 ? QLatin1String("") : QString::number(url.port()))); - d()->protocol.set(engine(), engine()->newString(url.scheme())); + d()->protocol.set(engine(), engine()->newString(url.scheme() + QLatin1Char(':'))); d()->search.set(engine(), engine()->newString(url.query())); d()->username.set(engine(), engine()->newString(url.userName())); @@ -222,15 +222,23 @@ bool UrlObject::setPort(QString port) return true; } -bool UrlObject::setProtocol(QString protocol) +bool UrlObject::setProtocol(QString protocolOrScheme) { QUrl url = toQUrl(); - url.setScheme(protocol); + // If there is one or several ':' in the protocolOrScheme, + // everything from the first colon is removed. + + qsizetype firstColonPos = protocolOrScheme.indexOf(QLatin1Char(':')); + + if (firstColonPos != -1) + protocolOrScheme.truncate(firstColonPos); + + url.setScheme(protocolOrScheme); if (!url.isValid()) return false; - d()->protocol.set(engine(), engine()->newString(url.scheme())); + d()->protocol.set(engine(), engine()->newString(url.scheme() + QLatin1Char(':'))); d()->href.set(engine(), engine()->newString(url.toString())); updateOrigin(); diff --git a/tests/auto/qml/qjsengine/tst_qjsengine.cpp b/tests/auto/qml/qjsengine/tst_qjsengine.cpp index 4982085172..f08d247148 100644 --- a/tests/auto/qml/qjsengine/tst_qjsengine.cpp +++ b/tests/auto/qml/qjsengine/tst_qjsengine.cpp @@ -5421,7 +5421,7 @@ void tst_QJSEngine::urlObject() check(QStringLiteral("href"), url.toString()); check(QStringLiteral("origin"), QStringLiteral("http://example.com:777")); - check(QStringLiteral("protocol"), url.scheme()); + check(QStringLiteral("protocol"), url.scheme() + QLatin1Char(':')); check(QStringLiteral("username"), url.userName()); check(QStringLiteral("password"), url.password()); check(QStringLiteral("host"), url.host() + u':' + QString::number(url.port())); diff --git a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp index c68fc1019f..4beedc4e88 100644 --- a/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp +++ b/tests/auto/qml/qqmlecmascript/tst_qqmlecmascript.cpp @@ -411,6 +411,7 @@ private slots: void urlConstruction(); void urlPropertyInvalid(); void urlPropertySet(); + void colonAfterProtocol(); void urlSearchParamsConstruction(); void urlSearchParamsMethods(); void variantConversionMethod(); @@ -9629,7 +9630,7 @@ void tst_qqmlecmascript::urlConstruction() QV4::UrlObject *validUrl = ret->as<QV4::UrlObject>(); QVERIFY(validUrl != nullptr); - QCOMPARE(validUrl->protocol(), "https"); + QCOMPARE(validUrl->protocol(), "https:"); QCOMPARE(validUrl->hostname(), "example.com"); QCOMPARE(validUrl->username(), "username"); QCOMPARE(validUrl->password(), "password"); @@ -9649,7 +9650,7 @@ void tst_qqmlecmascript::urlConstruction() QV4::UrlObject *validRelativeUrl = retRel->as<QV4::UrlObject>(); QVERIFY(validRelativeUrl != nullptr); - QCOMPARE(validRelativeUrl->protocol(), "https"); + QCOMPARE(validRelativeUrl->protocol(), "https:"); QCOMPARE(validRelativeUrl->hostname(), "example.com"); QCOMPARE(validRelativeUrl->username(), "username"); QCOMPARE(validRelativeUrl->password(), "password"); @@ -9709,7 +9710,7 @@ void tst_qqmlecmascript::urlPropertySet() // protocol QVERIFY(EVALUATE("this.url.protocol = 'https';")); - QCOMPARE(url->protocol(), "https"); + QCOMPARE(url->protocol(), "https:"); QCOMPARE(url->href(), "https://localhost/a/b/c"); QCOMPARE(url->origin(), "https://localhost"); @@ -9772,7 +9773,7 @@ void tst_qqmlecmascript::urlPropertySet() "this.url.href = " "'https://username:password@example.com:1234/path/to/something?search=value#hash';")); - QCOMPARE(url->protocol(), "https"); + QCOMPARE(url->protocol(), "https:"); QCOMPARE(url->hostname(), "example.com"); QCOMPARE(url->username(), "username"); QCOMPARE(url->password(), "password"); @@ -9786,6 +9787,57 @@ void tst_qqmlecmascript::urlPropertySet() QCOMPARE(url->hash(), "#hash"); } +void tst_qqmlecmascript::colonAfterProtocol() +{ + QQmlEngine qmlengine; + + QObject *o = new QObject(&qmlengine); + + QV4::ExecutionEngine *engine = qmlengine.handle(); + QV4::Scope scope(engine); + + QV4::ScopedValue object(scope, QV4::QObjectWrapper::wrap(engine, o)); + + QV4::ScopedValue ret(scope, EVALUATE("this.url = new URL('http://localhost/a/b/c');")); + QV4::UrlObject *url = ret->as<QV4::UrlObject>(); + QVERIFY(url != nullptr); + + // https without colon + QVERIFY(EVALUATE("this.url.protocol = 'https';")); + QCOMPARE(url->protocol(), "https:"); + QCOMPARE(url->href(), "https://localhost/a/b/c"); + QCOMPARE(url->origin(), "https://localhost"); + + QV4::ScopedValue retHttps(scope, EVALUATE("this.url = new URL('https://localhost/a/b/c');")); + QV4::UrlObject *urlHttps = retHttps->as<QV4::UrlObject>(); + QVERIFY(urlHttps != nullptr); + + // ftp with a colon + QVERIFY(EVALUATE("this.url.protocol = 'ftp:';")); + QCOMPARE(urlHttps->protocol(), "ftp:"); + QCOMPARE(urlHttps->href(), "ftp://localhost/a/b/c"); + QCOMPARE(urlHttps->origin(), "ftp://localhost"); + + QV4::ScopedValue retHttp(scope, EVALUATE("this.url = new URL('http://localhost/a/b/c');")); + QV4::UrlObject *ftpHttps = retHttp->as<QV4::UrlObject>(); + QVERIFY(ftpHttps != nullptr); + + // ftp with three colons + QVERIFY(EVALUATE("this.url.protocol = 'ftp:::';")); + QCOMPARE(ftpHttps->protocol(), "ftp:"); + QCOMPARE(ftpHttps->href(), "ftp://localhost/a/b/c"); + QCOMPARE(ftpHttps->origin(), "ftp://localhost"); + + QV4::ScopedValue retWss(scope, EVALUATE("this.url = new URL('wss://localhost/a/b/c');")); + QV4::UrlObject *urlFtpHttp = retWss->as<QV4::UrlObject>(); + QVERIFY(urlFtpHttp != nullptr); + + // ftp and http with a colon inbetween + QVERIFY(EVALUATE("this.url.protocol = 'ftp:http:';")); + QCOMPARE(urlFtpHttp->protocol(), "ftp:"); + QCOMPARE(urlFtpHttp->href(), "ftp://localhost/a/b/c"); + QCOMPARE(urlFtpHttp->origin(), "ftp://localhost"); +} void tst_qqmlecmascript::urlSearchParamsConstruction() { |
