Markdown: Fix images with special chars in URLs not loaded

If an image URL contains a non-ASCII character (e.g. `kartöffelchen.jpg`), the html returned by the markdown parser contains the encoded path `kart%C3%B6ffelchen.jpg`. We must decode the path before checking if the image file exists.
remotes/origin/work/devicepixelratio
M  B  4 years ago committed by Albert Astals Cid
parent 5ceeb68dfe
commit 8aa96484da
  1. 3
      autotests/data/imageUrlsWithSpecialChars.md
  2. BIN
      autotests/data/kartöffelchen.jpg
  3. 16
      autotests/markdowntest.cpp
  4. 3
      generators/markdown/converter.cpp

@ -0,0 +1,3 @@
# Test for having images with non-ASCII characters in their name
![Kartöffelchen](kartöffelchen.jpg)

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

@ -23,6 +23,7 @@ private Q_SLOTS:
void testFancyPantsEnabled();
void testFancyPantsDisabled();
void testImageSizes();
void testSpecialCharsInImageFileName();
private:
void findImages(QTextFrame *parent, QVector<QTextImageFormat> &images);
@ -121,5 +122,20 @@ void MarkdownTest::findImages(const QTextBlock &parent, QVector<QTextImageFormat
}
}
void MarkdownTest::testSpecialCharsInImageFileName()
{
Markdown::Converter converter;
QTextDocument *document = converter.convert(QStringLiteral(KDESRCDIR "data/imageUrlsWithSpecialChars.md"));
QTextFrame *parent = document->rootFrame();
QVector<QTextImageFormat> images;
findImages(parent, images);
QCOMPARE(images.size(), 1);
QVERIFY(images[0].name().endsWith(QStringLiteral("kartöffelchen.jpg")));
QVERIFY(!images[0].name().contains(QStringLiteral("kart%C3%B6ffelchen.jpg")));
}
QTEST_MAIN(MarkdownTest)
#include "markdowntest.moc"

@ -197,7 +197,8 @@ void Converter::convertImages(const QTextBlock &parent, const QDir &dir, QTextDo
cursor.setPosition(textFragment.position(), QTextCursor::MoveAnchor);
cursor.setPosition(textFragment.position() + textFragment.length(), QTextCursor::KeepAnchor);
const QString imageFilePath = QDir::cleanPath(dir.absoluteFilePath(textCharFormat.toImageFormat().name()));
const QString imageFilePath = QDir::cleanPath(dir.absoluteFilePath(QUrl::fromPercentEncoding(textCharFormat.toImageFormat().name().toUtf8())));
if (QFile::exists(imageFilePath)) {
cursor.removeSelectedText();
format.setName(imageFilePath);

Loading…
Cancel
Save