Do not strip off the fragment and query in the qfileselector

This is needed for cases where we use e.g. "file:///test.html?query#Fragment".
The fragment and query were already preserved for the qrc scheme. This
fixes it for the file scheme.

Change-Id: I5713e4a25372fdd55ac255b1c6228b4dea419244
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
This commit is contained in:
Jan Arve Sæther 2019-05-29 18:02:19 +02:00
parent 6f40331058
commit 8c7589d992
2 changed files with 19 additions and 0 deletions

View File

@ -228,7 +228,18 @@ QUrl QFileSelector::select(const QUrl &filePath) const
QString selectedPath = d->select(equivalentPath);
ret.setPath(selectedPath.remove(0, scheme.size()));
} else {
// we need to store the original query and fragment, since toLocalFile() will strip it off
QString frag;
if (ret.hasFragment())
frag = ret.fragment();
QString query;
if (ret.hasQuery())
query= ret.query();
ret = QUrl::fromLocalFile(d->select(ret.toLocalFile()));
if (!frag.isNull())
ret.setFragment(frag);
if (!query.isNull())
ret.setQuery(query);
}
return ret;
}

View File

@ -205,15 +205,23 @@ void tst_QFileSelector::urlConvenience_data()
QString test("/test");// '/' is here so dir string can also be selector string
QString custom1("custom1");
QString testWithQueryAndFragment("/test?query#Fragment");
QTest::newRow("qrc") << QUrl("qrc:///extras/test") << (QStringList() << custom1)
<< QUrl(QString("qrc:///extras/") + QLatin1Char(selectorIndicator) + custom1 + test);
QTest::newRow("qrc with query and fragment") << QUrl(QString::fromLatin1("qrc:///extras%1").arg(testWithQueryAndFragment)) << (QStringList() << custom1)
<< QUrl(QString("qrc:///extras/") + QLatin1Char(selectorIndicator) + custom1 + testWithQueryAndFragment);
QString fileBasePath = QFINDTESTDATA("extras/test");
QString fileSelectedPath = QFINDTESTDATA(QString("extras/") + QLatin1Char(selectorIndicator)
+ custom1 + QString("/test"));
QTest::newRow("file") << QUrl::fromLocalFile(fileBasePath) << (QStringList() << custom1)
<< QUrl::fromLocalFile(fileSelectedPath);
// do not strip off the query and fragment
QString strUrlWithFragment = QString("file://") + testWithQueryAndFragment;
QTest::newRow("file with query and fragment") << QUrl(strUrlWithFragment) << (QStringList()) << QUrl(strUrlWithFragment);
strUrlWithFragment = QString("file:") + testWithQueryAndFragment;
QTest::newRow("file with query and fragment too") << QUrl(strUrlWithFragment) << (QStringList()) << QUrl(strUrlWithFragment);
// http://qt-project.org/images/qtdn/sprites-combined-latest.png is chosen as a representative real world URL
// But note that this test is checking that http urls are NOT selected so it shouldn't be checked