diff options
author | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-05-19 13:33:27 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2019-05-22 14:54:26 +0200 |
commit | 90243aebb653f67a3898fff2f9feec9dca2ded5d (patch) | |
tree | e77ccce878afae772f162e844487d12218b44168 /src/widgets | |
parent | ce211b1f837ce3212074800e92dee30d95546d94 (diff) |
QTextBrowser: set base URL and document URL before loading content
setHtml() or setMarkdown() can result in attempting to load resources
such as images that are needed to render the page. Whenever the
resource has a relative URL, loading depends on the baseURL having
already been set so that a complete URL can be constructed.
QTextDocument::resource() is called to load images, and uses baseUrl().
A little later, QTextBrowserPrivate::resolveUrl() is given an URL that
already has been extended with the baseURL if known; but it has its own
logic to resolve the resource URL against currentURL, or else to treat
it as a local file path if a file exists at that location. The autotest
was relying on this fallback to the local relative file path before; but
now it tests both with a local filename in the current directory for the
source HTML and also with a fully resolved source URL containing the
complete file path.
Also made minor style improvements in tst_QTextBrowser's TestBrowser class.
Change-Id: I46a850015d0e9c5bc5f13b9e37179a9323ab1980
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/widgets')
-rw-r--r-- | src/widgets/widgets/qtextbrowser.cpp | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/widgets/widgets/qtextbrowser.cpp b/src/widgets/widgets/qtextbrowser.cpp index bb9a4fed91..91c5f62246 100644 --- a/src/widgets/widgets/qtextbrowser.cpp +++ b/src/widgets/widgets/qtextbrowser.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtWidgets module of the Qt Toolkit. @@ -327,9 +327,15 @@ void QTextBrowserPrivate::setSource(const QUrl &url) home = url; if (doSetText) { + // Setting the base URL helps QTextDocument::resource() to find resources with relative paths. + // But don't set it unless it contains the document's path, because QTextBrowserPrivate::resolveUrl() + // can already deal with local files on the filesystem in case the base URL was not set. + QUrl baseUrl = currentURL.adjusted(QUrl::RemoveFilename); + if (!baseUrl.path().isEmpty()) + q->document()->setBaseUrl(baseUrl); + q->document()->setMetaInformation(QTextDocument::DocumentUrl, currentURL.toString()); #ifndef QT_NO_TEXTHTMLPARSER q->QTextEdit::setHtml(txt); - q->document()->setMetaInformation(QTextDocument::DocumentUrl, currentURL.toString()); #else q->QTextEdit::setPlainText(txt); #endif |