Navigation Menu

Skip to content

Commit

Permalink
Resolve StyledText img tags relative to baseUrl.
Browse files Browse the repository at this point in the history
Change-Id: I954195d52330c65e851b7c0fcdb6c8dabf29335d
Reviewed-by: Yann Bodson <yann.bodson@nokia.com>
  • Loading branch information
Andrew den Exter authored and Qt by Nokia committed Feb 7, 2012
1 parent a6eb091 commit 978550b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 9 deletions.
10 changes: 8 additions & 2 deletions src/quick/items/qquicktext.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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();
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/quick/util/qdeclarativestyledtext.cpp
Expand Up @@ -83,9 +83,10 @@ class QDeclarativeStyledTextPrivate

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)
{
}
Expand Down Expand Up @@ -117,6 +118,7 @@ class QDeclarativeStyledTextPrivate
QList<QDeclarativeStyledTextImgTag*> *imgTags;
QFont baseFont;
QStack<List> listStack;
QUrl baseUrl;
bool hasNewLine;
int nbImages;
bool updateImagePositions;
Expand Down Expand Up @@ -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))
{
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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()) {
Expand Down
2 changes: 2 additions & 0 deletions src/quick/util/qdeclarativestyledtext_p.h
Expand Up @@ -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();
Expand Down
Expand Up @@ -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);

Expand Down
83 changes: 83 additions & 0 deletions tests/auto/qtquick2/qquicktext/tst_qquicktext.cpp
Expand Up @@ -108,6 +108,8 @@ private slots:

void lineLaidOut();

void imgTagsBaseUrl_data();
void imgTagsBaseUrl();
void imgTagsAlign_data();
void imgTagsAlign();
void imgTagsMultipleImages();
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 978550b

Please sign in to comment.