aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/quick/items/qquicktext.cpp10
-rw-r--r--src/quick/util/qdeclarativestyledtext.cpp18
-rw-r--r--src/quick/util/qdeclarativestyledtext_p.h2
-rw-r--r--tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp2
-rw-r--r--tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp83
5 files changed, 106 insertions, 9 deletions
diff --git a/src/quick/items/qquicktext.cpp b/src/quick/items/qquicktext.cpp
index acf2d17061..14016777c1 100644
--- a/src/quick/items/qquicktext.cpp
+++ b/src/quick/items/qquicktext.cpp
@@ -298,7 +298,7 @@ void QQuickTextPrivate::updateLayout()
if (!richText) {
if (textHasChanged) {
if (styledText && !text.isEmpty()) {
- QDeclarativeStyledText::parse(text, layout, imgTags, qmlContext(q), !maximumLineCountValid);
+ QDeclarativeStyledText::parse(text, layout, imgTags, q->baseUrl(), qmlContext(q), !maximumLineCountValid);
} else {
layout.clearAdditionalFormats();
QString tmp = text;
@@ -893,7 +893,7 @@ void QQuickTextPrivate::setLineGeometry(QTextLine &line, qreal lineWidth, qreal
image->position < line.textStart() + line.textLength()) {
if (!image->pix) {
- QUrl url = qmlContext(q)->resolvedUrl(image->url);
+ QUrl url = q->baseUrl().resolved(image->url);
image->pix = new QDeclarativePixmap(qmlEngine(q), url, image->size);
if (image->pix->isLoading()) {
image->pix->connectFinished(q, SLOT(imageDownloadFinished()));
@@ -1752,6 +1752,12 @@ void QQuickText::setBaseUrl(const QUrl &url)
if (d->doc)
d->doc->setBaseUrl(url);
+ if (d->styledText) {
+ d->textHasChanged = true;
+ qDeleteAll(d->imgTags);
+ d->imgTags.clear();
+ d->updateLayout();
+ }
emit baseUrlChanged();
}
}
diff --git a/src/quick/util/qdeclarativestyledtext.cpp b/src/quick/util/qdeclarativestyledtext.cpp
index 39ea6b1a22..3023fc53dc 100644
--- a/src/quick/util/qdeclarativestyledtext.cpp
+++ b/src/quick/util/qdeclarativestyledtext.cpp
@@ -83,9 +83,10 @@ public:
QDeclarativeStyledTextPrivate(const QString &t, QTextLayout &l,
QList<QDeclarativeStyledTextImgTag*> &imgTags,
+ const QUrl &baseUrl,
QDeclarativeContext *context,
bool preloadImages)
- : text(t), layout(l), imgTags(&imgTags), baseFont(layout.font()), hasNewLine(false), nbImages(0), updateImagePositions(false)
+ : text(t), layout(l), imgTags(&imgTags), baseFont(layout.font()), baseUrl(baseUrl), hasNewLine(false), nbImages(0), updateImagePositions(false)
, preFormat(false), prependSpace(false), hasSpace(true), preloadImages(preloadImages), context(context)
{
}
@@ -117,6 +118,7 @@ public:
QList<QDeclarativeStyledTextImgTag*> *imgTags;
QFont baseFont;
QStack<List> listStack;
+ QUrl baseUrl;
bool hasNewLine;
int nbImages;
bool updateImagePositions;
@@ -155,9 +157,11 @@ const QChar QDeclarativeStyledTextPrivate::lineFeed(QLatin1Char('\n'));
const QChar QDeclarativeStyledTextPrivate::space(QLatin1Char(' '));
QDeclarativeStyledText::QDeclarativeStyledText(const QString &string, QTextLayout &layout,
- QList<QDeclarativeStyledTextImgTag*> &imgTags, QDeclarativeContext *context,
+ QList<QDeclarativeStyledTextImgTag*> &imgTags,
+ const QUrl &baseUrl,
+ QDeclarativeContext *context,
bool preloadImages)
- : d(new QDeclarativeStyledTextPrivate(string, layout, imgTags, context, preloadImages))
+ : d(new QDeclarativeStyledTextPrivate(string, layout, imgTags, baseUrl, context, preloadImages))
{
}
@@ -167,12 +171,14 @@ QDeclarativeStyledText::~QDeclarativeStyledText()
}
void QDeclarativeStyledText::parse(const QString &string, QTextLayout &layout,
- QList<QDeclarativeStyledTextImgTag*> &imgTags, QDeclarativeContext *context,
+ QList<QDeclarativeStyledTextImgTag*> &imgTags,
+ const QUrl &baseUrl,
+ QDeclarativeContext *context,
bool preloadImages)
{
if (string.isEmpty())
return;
- QDeclarativeStyledText styledText(string, layout, imgTags, context, preloadImages);
+ QDeclarativeStyledText styledText(string, layout, imgTags, baseUrl, context, preloadImages);
styledText.d->parse();
}
@@ -657,7 +663,7 @@ void QDeclarativeStyledTextPrivate::parseImageAttributes(const QChar *&ch, const
// if we don't know its size but the image is a local image,
// we load it in the pixmap cache and save its implicit size
// to avoid a relayout later on.
- QUrl url = context->resolvedUrl(image->url);
+ QUrl url = baseUrl.resolved(image->url);
if (url.isLocalFile()) {
QDeclarativePixmap *pix = new QDeclarativePixmap(context->engine(), url, image->size);
if (pix && pix->isReady()) {
diff --git a/src/quick/util/qdeclarativestyledtext_p.h b/src/quick/util/qdeclarativestyledtext_p.h
index 1c9086e7d1..aa6ae3f869 100644
--- a/src/quick/util/qdeclarativestyledtext_p.h
+++ b/src/quick/util/qdeclarativestyledtext_p.h
@@ -83,12 +83,14 @@ class Q_AUTOTEST_EXPORT QDeclarativeStyledText
public:
static void parse(const QString &string, QTextLayout &layout,
QList<QDeclarativeStyledTextImgTag*> &imgTags,
+ const QUrl &baseUrl,
QDeclarativeContext *context,
bool preloadImages);
private:
QDeclarativeStyledText(const QString &string, QTextLayout &layout,
QList<QDeclarativeStyledTextImgTag*> &imgTags,
+ const QUrl &baseUrl,
QDeclarativeContext *context,
bool preloadImages);
~QDeclarativeStyledText();
diff --git a/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp b/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp
index b4e0ba1b7a..fd0d1abd29 100644
--- a/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp
+++ b/tests/auto/qtquick2/qdeclarativestyledtext/tst_qdeclarativestyledtext.cpp
@@ -160,7 +160,7 @@ void tst_qdeclarativestyledtext::textOutput()
QTextLayout layout;
QList<QDeclarativeStyledTextImgTag*> imgTags;
- QDeclarativeStyledText::parse(input, layout, imgTags, 0, false);
+ QDeclarativeStyledText::parse(input, layout, imgTags, QUrl(), 0, false);
QCOMPARE(layout.text(), output);
diff --git a/tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp b/tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp
index 96449c00cc..8e3a1f4864 100644
--- a/tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp
+++ b/tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp
@@ -108,6 +108,8 @@ private slots:
void lineLaidOut();
+ void imgTagsBaseUrl_data();
+ void imgTagsBaseUrl();
void imgTagsAlign_data();
void imgTagsAlign();
void imgTagsMultipleImages();
@@ -1549,6 +1551,87 @@ void tst_qquicktext::lineLaidOut()
delete canvas;
}
+void tst_qquicktext::imgTagsBaseUrl_data()
+{
+ QTest::addColumn<QUrl>("src");
+ QTest::addColumn<QUrl>("baseUrl");
+ QTest::addColumn<QUrl>("contextUrl");
+ QTest::addColumn<qreal>("imgHeight");
+
+ QTest::newRow("absolute local")
+ << testFileUrl("images/heart200.png")
+ << QUrl()
+ << QUrl()
+ << 181.;
+ QTest::newRow("relative local context 1")
+ << QUrl("images/heart200.png")
+ << QUrl()
+ << testFileUrl("/app.qml")
+ << 181.;
+ QTest::newRow("relative local context 2")
+ << QUrl("heart200.png")
+ << QUrl()
+ << testFileUrl("images/app.qml")
+ << 181.;
+ QTest::newRow("relative local base 1")
+ << QUrl("images/heart200.png")
+ << testFileUrl("")
+ << testFileUrl("nonexistant/app.qml")
+ << 181.;
+ QTest::newRow("relative local base 2")
+ << QUrl("heart200.png")
+ << testFileUrl("images/")
+ << testFileUrl("nonexistant/app.qml")
+ << 181.;
+ QTest::newRow("base relative to local context")
+ << QUrl("heart200.png")
+ << testFileUrl("images/")
+ << testFileUrl("/app.qml")
+ << 181.;
+
+ QTest::newRow("absolute remote")
+ << QUrl("http://127.0.0.1:14453/images/heart200.png")
+ << QUrl()
+ << QUrl()
+ << 181.;
+ QTest::newRow("relative remote base 1")
+ << QUrl("images/heart200.png")
+ << QUrl("http://127.0.0.1:14453/")
+ << testFileUrl("nonexistant/app.qml")
+ << 181.;
+ QTest::newRow("relative remote base 2")
+ << QUrl("heart200.png")
+ << QUrl("http://127.0.0.1:14453/images/")
+ << testFileUrl("nonexistant/app.qml")
+ << 181.;
+}
+
+void tst_qquicktext::imgTagsBaseUrl()
+{
+ QFETCH(QUrl, src);
+ QFETCH(QUrl, baseUrl);
+ QFETCH(QUrl, contextUrl);
+ QFETCH(qreal, imgHeight);
+
+ TestHTTPServer server(14453);
+ server.serveDirectory(testFile(""));
+
+ QByteArray baseUrlFragment;
+ if (!baseUrl.isEmpty())
+ baseUrlFragment = "; baseUrl: \"" + baseUrl.toEncoded() + "\"";
+ QByteArray componentStr = "import QtQuick 2.0\nText { text: \"This is a test <img src=\\\"" + src.toEncoded() + "\\\">\"" + baseUrlFragment + " }";
+
+ QDeclarativeComponent component(&engine);
+ component.setData(componentStr, contextUrl);
+ QScopedPointer<QObject> object(component.create());
+ QQuickText *textObject = qobject_cast<QQuickText *>(object.data());
+ QVERIFY(textObject);
+
+ QCoreApplication::processEvents();
+
+ QTRY_COMPARE(textObject->height(), imgHeight);
+}
+
void tst_qquicktext::imgTagsAlign_data()
{
QTest::addColumn<QString>("src");