diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-06-15 17:53:16 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-06-22 18:37:52 +0200 |
commit | 0a1e4cc7ec7548f6273befff9cdddb0bc7a58961 (patch) | |
tree | bf8b7ae725ac332fa59bd9058cc479018aca147d /src/quick/items | |
parent | 4e266103ad8b75d71fb176a2f774faf71997123d (diff) |
Do not resolve URLs when assigning them to a property
We don't know in advance if a URL is part of the source code and should
be relative to the current element, or if it is part of the application
data and should not be touched.
[ChangeLog][QtQml][Important Behavior Changes] URLs are not resolved or
intercepted anymore when assigning them to a "url" property. Instead
they are resolved and possibly intercepted when used to access an actual
resource.
Fixes: QTBUG-76879
Change-Id: Iaa2385aff2c13aa71a12e57385d9afb5dc60a073
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/quick/items')
-rw-r--r-- | src/quick/items/qquickanimatedimage.cpp | 10 | ||||
-rw-r--r-- | src/quick/items/qquickborderimage.cpp | 4 | ||||
-rw-r--r-- | src/quick/items/qquickimagebase.cpp | 8 | ||||
-rw-r--r-- | src/quick/items/qquickloader.cpp | 21 | ||||
-rw-r--r-- | src/quick/items/qquickloader_p_p.h | 1 | ||||
-rw-r--r-- | src/quick/items/qquicksprite.cpp | 13 | ||||
-rw-r--r-- | src/quick/items/qquicktext.cpp | 8 | ||||
-rw-r--r-- | src/quick/items/qquicktextedit.cpp | 4 |
8 files changed, 47 insertions, 22 deletions
diff --git a/src/quick/items/qquickanimatedimage.cpp b/src/quick/items/qquickanimatedimage.cpp index eb3385c8f8..9de48cbf5f 100644 --- a/src/quick/items/qquickanimatedimage.cpp +++ b/src/quick/items/qquickanimatedimage.cpp @@ -339,8 +339,10 @@ void QQuickAnimatedImage::load() const qreal targetDevicePixelRatio = (window() ? window()->effectiveDevicePixelRatio() : qApp->devicePixelRatio()); d->devicePixelRatio = 1.0; - QUrl loadUrl = d->url; - resolve2xLocalFile(d->url, targetDevicePixelRatio, &loadUrl, &d->devicePixelRatio); + const auto context = qmlContext(this); + QUrl loadUrl = context ? context->resolvedUrl(d->url) : d->url; + const QUrl resolvedUrl = loadUrl; + resolve2xLocalFile(resolvedUrl, targetDevicePixelRatio, &loadUrl, &d->devicePixelRatio); QString lf = QQmlFile::urlToLocalFileOrQrc(loadUrl); if (!lf.isEmpty()) { @@ -393,7 +395,9 @@ void QQuickAnimatedImage::movieRequestFinished() #endif if (!d->movie || !d->movie->isValid()) { - qmlWarning(this) << "Error Reading Animated Image File " << d->url.toString(); + const QQmlContext *context = qmlContext(this); + qmlWarning(this) << "Error Reading Animated Image File " + << (context ? context->resolvedUrl(d->url) : d->url).toString(); d->setMovie(nullptr); d->setImage(QImage()); if (d->progress != 0) { diff --git a/src/quick/items/qquickborderimage.cpp b/src/quick/items/qquickborderimage.cpp index 3679df37d1..b8828d23ba 100644 --- a/src/quick/items/qquickborderimage.cpp +++ b/src/quick/items/qquickborderimage.cpp @@ -299,7 +299,9 @@ void QQuickBorderImage::load() loadEmptyUrl(); } else { if (d->url.path().endsWith(QLatin1String("sci"))) { - QString lf = QQmlFile::urlToLocalFileOrQrc(d->url); + const QQmlContext *context = qmlContext(this); + QString lf = QQmlFile::urlToLocalFileOrQrc(context ? context->resolvedUrl(d->url) + : d->url); if (!lf.isEmpty()) { QFile file(lf); file.open(QIODevice::ReadOnly); diff --git a/src/quick/items/qquickimagebase.cpp b/src/quick/items/qquickimagebase.cpp index 0e5313db67..750b71b25e 100644 --- a/src/quick/items/qquickimagebase.cpp +++ b/src/quick/items/qquickimagebase.cpp @@ -298,8 +298,9 @@ void QQuickImageBase::loadPixmap(const QUrl &url, LoadPixmapOptions loadOptions) options |= QQuickPixmap::Cache; d->pix.clear(this); QUrl loadUrl = url; - if (const QQmlEngine *engine = qmlEngine(this)) - loadUrl = engine->interceptUrl(loadUrl, QQmlAbstractUrlInterceptor::UrlString); + const QQmlContext *context = qmlContext(this); + if (context) + loadUrl = context->resolvedUrl(url); if (loadOptions & HandleDPR) { const qreal targetDevicePixelRatio = (window() ? window()->effectiveDevicePixelRatio() : qApp->devicePixelRatio()); @@ -311,7 +312,8 @@ void QQuickImageBase::loadPixmap(const QUrl &url, LoadPixmapOptions loadOptions) if (!updatedDevicePixelRatio) { // (possible) local file: loadUrl and d->devicePixelRatio will be modified if // an "@2x" file is found. - resolve2xLocalFile(d->url, targetDevicePixelRatio, &loadUrl, &d->devicePixelRatio); + resolve2xLocalFile(context ? context->resolvedUrl(d->url) : d->url, + targetDevicePixelRatio, &loadUrl, &d->devicePixelRatio); } } diff --git a/src/quick/items/qquickloader.cpp b/src/quick/items/qquickloader.cpp index 40e0c3219a..0d47931063 100644 --- a/src/quick/items/qquickloader.cpp +++ b/src/quick/items/qquickloader.cpp @@ -439,9 +439,8 @@ void QQuickLoader::loadFromSource() } if (isComponentComplete()) { - QQmlComponent::CompilationMode mode = d->asynchronous ? QQmlComponent::Asynchronous : QQmlComponent::PreferSynchronous; if (!d->component) - d->component.setObject(new QQmlComponent(qmlEngine(this), d->source, mode, this), this); + d->createComponent(); d->load(); } } @@ -796,11 +795,8 @@ void QQuickLoader::componentComplete() Q_D(QQuickLoader); QQuickItem::componentComplete(); if (active()) { - if (d->loadingFromSource) { - QQmlComponent::CompilationMode mode = d->asynchronous ? QQmlComponent::Asynchronous : QQmlComponent::PreferSynchronous; - if (!d->component) - d->component.setObject(new QQmlComponent(qmlEngine(this), d->source, mode, this), this); - } + if (d->loadingFromSource) + d->createComponent(); d->load(); } } @@ -1027,6 +1023,17 @@ void QQuickLoaderPrivate::updateStatus() } } +void QQuickLoaderPrivate::createComponent() +{ + Q_Q(QQuickLoader); + const QQmlComponent::CompilationMode mode = asynchronous + ? QQmlComponent::Asynchronous + : QQmlComponent::PreferSynchronous; + QQmlContext *context = qmlContext(q); + component.setObject(new QQmlComponent( + context->engine(), context->resolvedUrl(source), mode, q), q); +} + #include <moc_qquickloader_p.cpp> QT_END_NAMESPACE diff --git a/src/quick/items/qquickloader_p_p.h b/src/quick/items/qquickloader_p_p.h index 39d50280c5..b178803c7d 100644 --- a/src/quick/items/qquickloader_p_p.h +++ b/src/quick/items/qquickloader_p_p.h @@ -98,6 +98,7 @@ public: QV4::ReturnedValue extractInitialPropertyValues(QQmlV4Function *args, QObject *loader, bool *error); QQuickLoader::Status computeStatus() const; void updateStatus(); + void createComponent(); qreal getImplicitWidth() const override; qreal getImplicitHeight() const override; diff --git a/src/quick/items/qquicksprite.cpp b/src/quick/items/qquicksprite.cpp index 58f3de8b7b..28ec8f41e4 100644 --- a/src/quick/items/qquicksprite.cpp +++ b/src/quick/items/qquicksprite.cpp @@ -40,6 +40,7 @@ #include "qquicksprite_p.h" #include "qquickimagebase_p.h" #include <qqml.h> +#include <QQmlContext> #include <QDebug> #include <QRandomGenerator> @@ -261,14 +262,18 @@ void QQuickSprite::startImageLoading() { m_pix.clear(this); if (!m_source.isEmpty()) { - QQmlEngine *e = qmlEngine(this); + const QQmlContext *context = qmlContext(this); + QQmlEngine *e = context ? context->engine() : nullptr; if (!e) { //If not created in QML, you must set the QObject parent to the QML element so this can work - e = qmlEngine(parent()); + context = qmlContext(parent()); + e = context ? context->engine() : nullptr; if (!e) qWarning() << "QQuickSprite: Cannot find QQmlEngine - this class is only for use in QML and may not work"; } - QUrl loadUrl = m_source; - QQuickImageBase::resolve2xLocalFile(m_source, m_devicePixelRatio, &loadUrl, &m_devicePixelRatio); + const QUrl resolvedUrl = context ? context->resolvedUrl(m_source) : m_source; + QUrl loadUrl = resolvedUrl; + QQuickImageBase::resolve2xLocalFile(resolvedUrl, m_devicePixelRatio, &loadUrl, + &m_devicePixelRatio); m_pix.load(e, loadUrl); } diff --git a/src/quick/items/qquicktext.cpp b/src/quick/items/qquicktext.cpp index bbf6e4f8b8..79665a3cda 100644 --- a/src/quick/items/qquicktext.cpp +++ b/src/quick/items/qquicktext.cpp @@ -1218,8 +1218,9 @@ void QQuickTextPrivate::setLineGeometry(QTextLine &line, qreal lineWidth, qreal image->position < line.textStart() + line.textLength()) { if (!image->pix) { - QUrl url = q->baseUrl().resolved(image->url); - image->pix = new QQuickPixmap(qmlEngine(q), url, QRect(), image->size); + const QQmlContext *context = qmlContext(q); + const QUrl url = context->resolvedUrl(q->baseUrl()).resolved(image->url); + image->pix = new QQuickPixmap(context->engine(), url, QRect(), image->size); if (image->pix->isLoading()) { image->pix->connectFinished(q, SLOT(imageDownloadFinished())); if (!extra.isAllocated() || !extra->nbActiveDownloads) @@ -1285,7 +1286,8 @@ void QQuickTextPrivate::ensureDoc() extra.value().doc = new QQuickTextDocumentWithImageResources(q); extra->doc->setPageSize(QSizeF(0, 0)); extra->doc->setDocumentMargin(0); - extra->doc->setBaseUrl(q->baseUrl()); + const QQmlContext *context = qmlContext(q); + extra->doc->setBaseUrl(context ? context->resolvedUrl(q->baseUrl()) : q->baseUrl()); qmlobject_connect(extra->doc, QQuickTextDocumentWithImageResources, SIGNAL(imagesLoaded()), q, QQuickText, SLOT(q_updateLayout())); } diff --git a/src/quick/items/qquicktextedit.cpp b/src/quick/items/qquicktextedit.cpp index 5494b73784..f5a32b8d88 100644 --- a/src/quick/items/qquicktextedit.cpp +++ b/src/quick/items/qquicktextedit.cpp @@ -1465,7 +1465,9 @@ void QQuickTextEdit::componentComplete() Q_D(QQuickTextEdit); QQuickImplicitSizeItem::componentComplete(); - d->document->setBaseUrl(baseUrl()); + const QUrl url = baseUrl(); + const QQmlContext *context = qmlContext(this); + d->document->setBaseUrl(context ? context->resolvedUrl(url) : url); #if QT_CONFIG(texthtmlparser) if (d->richText) d->control->setHtml(d->text); |