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;
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()));
if (d->doc)
d->doc->setBaseUrl(url);
+ if (d->styledText) {
+ d->textHasChanged = true;
+ qDeleteAll(d->imgTags);
+ d->imgTags.clear();
+ d->updateLayout();
+ }
emit baseUrlChanged();
}
}
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)
{
}
QList<QDeclarativeStyledTextImgTag*> *imgTags;
QFont baseFont;
QStack<List> listStack;
+ QUrl baseUrl;
bool hasNewLine;
int nbImages;
bool updateImagePositions;
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))
{
}
}
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();
}
// 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()) {
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();
QTextLayout layout;
QList<QDeclarativeStyledTextImgTag*> imgTags;
- QDeclarativeStyledText::parse(input, layout, imgTags, 0, false);
+ QDeclarativeStyledText::parse(input, layout, imgTags, QUrl(), 0, false);
QCOMPARE(layout.text(), output);
void lineLaidOut();
+ void imgTagsBaseUrl_data();
+ void imgTagsBaseUrl();
void imgTagsAlign_data();
void imgTagsAlign();
void imgTagsMultipleImages();
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");