From 3f7951c04ef474f81eda2134b67c4e4020fe39d1 Mon Sep 17 00:00:00 2001 From: Andrew Knight Date: Tue, 18 Mar 2014 00:08:49 +0200 Subject: [PATCH] Use QUrl::toLocalFile() when decoding a string URL Some percent-decoded characters may still be present in the string URL, so it is best to use QUrl::toLocalFile() to process the string properly. This also makes some drive handling "magic" obselete as QUrl already handles this. This fixes an issue whereby QML apps residing in local paths requiring percent-encoded characters (or which import local file URLs with percent-encoded characters) would fail to load, as the path was passed to the file system engine without fully decoding the URL. Change-Id: I8ec2b06f0661e0ac7cc9df79d35ec5cee211f672 Reviewed-by: Simon Hausmann --- src/qml/qml/qqmlfile.cpp | 11 +++-------- tests/auto/qml/qqmllanguage/data/{subdir}/Test.qml | 2 ++ tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 5 +++++ 3 files changed, 10 insertions(+), 8 deletions(-) create mode 100644 tests/auto/qml/qqmllanguage/data/{subdir}/Test.qml diff --git a/src/qml/qml/qqmlfile.cpp b/src/qml/qml/qqmlfile.cpp index be9b011dd..6728fa10a 100644 --- a/src/qml/qml/qqmlfile.cpp +++ b/src/qml/qml/qqmlfile.cpp @@ -666,18 +666,13 @@ QString QQmlFile::urlToLocalFileOrQrc(const QUrl& url) static QString toLocalFile(const QString &url) { - if (!url.startsWith(QLatin1String("file://"), Qt::CaseInsensitive)) + const QUrl file(url); + if (!file.isLocalFile()) return QString(); - QString file = url.mid(7); - //XXX TODO: handle windows hostnames: "//servername/path/to/file.txt" - // magic for drives on windows - if (file.length() > 2 && file.at(0) == QLatin1Char('/') && file.at(2) == QLatin1Char(':')) - file.remove(0, 1); - - return file; + return file.toLocalFile(); } /*! diff --git a/tests/auto/qml/qqmllanguage/data/{subdir}/Test.qml b/tests/auto/qml/qqmllanguage/data/{subdir}/Test.qml new file mode 100644 index 000000000..f789a905f --- /dev/null +++ b/tests/auto/qml/qqmllanguage/data/{subdir}/Test.qml @@ -0,0 +1,2 @@ +import QtQuick 2.0 +Rectangle { } diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index f48e405df..78fff5884 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -2210,6 +2210,11 @@ void tst_qqmllanguage::importsLocal_data() "Test {}" << (!qmlCheckTypes()?"TestType":"") << (!qmlCheckTypes()?"":"Test is ambiguous. Found in org/qtproject/Test/ and in subdir/"); + QTest::newRow("file URL survives percent-encoding") + << "import \"" + QUrl::fromLocalFile(QDir::currentPath() + "/{subdir}").toString() + "\"\n" + "Test {}" + << "QQuickRectangle" + << ""; } void tst_qqmllanguage::importsLocal() -- 2.34.1