diff options
author | Erik Verbruggen <erik.verbruggen@qt.io> | 2018-10-08 15:13:50 +0200 |
---|---|---|
committer | Erik Verbruggen <erik.verbruggen@qt.io> | 2018-10-15 10:19:22 +0000 |
commit | 67fbdadb3ba1aa9f86ae2d6ca3bff69479fb12be (patch) | |
tree | 8f3e5e56f14350c64276cdda88b6a477806fe8f6 /src/qml/jsapi | |
parent | 50be4991d205bd392d6998b6fec88bb92a9e9e9a (diff) |
Fix translation contexts for paths with drive letters on Windows
Inside method_qtTr, the filename is assumed to be a (correct) URL. When
a (normalized) path with a windows drive letter is passed to
QJSEngine::evaluate, the URL will have a scheme that is the drive
letter. We cannot correct this in method_qtTr, because at that point we
might get in files that do not come from the file system, but through
actual URLs. The place where we know for sure that the filename is a
real file name and not a URL, is in QJSEngine::evaluate. So at that
point, make sure that the filename is a valid URL.
Task-number: QTBUG-70425
Change-Id: Ia41859c4024ac46e6f8c3d96057a5dffdecd8f56
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/qml/jsapi')
-rw-r--r-- | src/qml/jsapi/qjsengine.cpp | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/src/qml/jsapi/qjsengine.cpp b/src/qml/jsapi/qjsengine.cpp index b5eadf483a..69e1436c0a 100644 --- a/src/qml/jsapi/qjsengine.cpp +++ b/src/qml/jsapi/qjsengine.cpp @@ -469,6 +469,17 @@ void QJSEngine::installExtensions(QJSEngine::Extensions extensions, const QJSVal QV4::GlobalExtensions::init(obj, extensions); } +static QUrl urlForFileName(const QString &fileName) +{ + if (!fileName.startsWith(QLatin1Char(':'))) + return QUrl::fromLocalFile(fileName); + + QUrl url; + url.setPath(fileName.mid(1)); + url.setScheme(QLatin1String("qrc")); + return url; +} + /*! Evaluates \a program, using \a lineNumber as the base line number, and returns the result of the evaluation. @@ -503,7 +514,7 @@ QJSValue QJSEngine::evaluate(const QString& program, const QString& fileName, in QV4::Scope scope(v4); QV4::ScopedValue result(scope); - QV4::Script script(v4->rootContext(), QV4::Compiler::ContextType::Global, program, fileName, lineNumber); + QV4::Script script(v4->rootContext(), QV4::Compiler::ContextType::Global, program, urlForFileName(fileName).toString(), lineNumber); script.strictMode = false; if (v4->currentStackFrame) script.strictMode = v4->currentStackFrame->v4Function->isStrict(); @@ -521,19 +532,6 @@ QJSValue QJSEngine::evaluate(const QString& program, const QString& fileName, in return retval; } -static QUrl moduleUrlForFileName(const QString &fileName) -{ - QString absolutePath = QFileInfo(fileName).canonicalFilePath(); - if (!absolutePath.startsWith(QLatin1Char(':'))) - return QUrl::fromLocalFile(absolutePath); - - absolutePath.remove(0, 1); - QUrl url; - url.setPath(absolutePath); - url.setScheme(QLatin1String("qrc")); - return url; -} - /*! Imports the module located at \a fileName and returns a module namespace object that contains all exported variables, constants and functions as properties. @@ -556,7 +554,7 @@ static QUrl moduleUrlForFileName(const QString &fileName) */ QJSValue QJSEngine::importModule(const QString &fileName) { - const QUrl url = moduleUrlForFileName(fileName); + const QUrl url = urlForFileName(QFileInfo(fileName).canonicalFilePath()); auto moduleUnit = m_v4Engine->loadModule(url); if (m_v4Engine->hasException) return QJSValue(m_v4Engine, m_v4Engine->catchException()); |