From 71bcd74542e37825cb1837f162b5b2310b6ebcb9 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Tue, 22 Apr 2025 22:00:58 +0200 Subject: [PATCH] qdataurl: don't set the mime type too early If there is no actual data to decode, setting the mime type is redundant. Also only set the mime type to the default if none is specified in the URL. Change-Id: I19f7ae98c7f1f4483069f580bea028a84a719656 Reviewed-by: Thiago Macieira --- src/corelib/io/qdataurl.cpp | 4 ++-- tests/auto/corelib/io/qdataurl/tst_qdataurl.cpp | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/corelib/io/qdataurl.cpp b/src/corelib/io/qdataurl.cpp index c5ecca8fb82..2d2426e8ea5 100644 --- a/src/corelib/io/qdataurl.cpp +++ b/src/corelib/io/qdataurl.cpp @@ -20,8 +20,6 @@ Q_CORE_EXPORT bool qDecodeDataUrl(const QUrl &uri, QString &mimeType, QByteArray if (uri.scheme() != "data"_L1 || !uri.host().isEmpty()) return false; - mimeType = QStringLiteral("text/plain;charset=US-ASCII"); - // the following would have been the correct thing, but // reality often differs from the specification. People have // data: URIs with ? and # @@ -56,6 +54,8 @@ Q_CORE_EXPORT bool qDecodeDataUrl(const QUrl &uri, QString &mimeType, QByteArray if (!data.isEmpty()) mimeType = textPlain + QLatin1StringView(data.trimmed()); + else + mimeType = QStringLiteral("text/plain;charset=US-ASCII"); } return true; diff --git a/tests/auto/corelib/io/qdataurl/tst_qdataurl.cpp b/tests/auto/corelib/io/qdataurl/tst_qdataurl.cpp index 7fbf4be56b9..39d860adaf7 100644 --- a/tests/auto/corelib/io/qdataurl/tst_qdataurl.cpp +++ b/tests/auto/corelib/io/qdataurl/tst_qdataurl.cpp @@ -31,8 +31,7 @@ void tst_QDataUrl::decode_data() row("malformed-host", "data://test.com", false); row("malformed-host2", "data://text/plain;charset=ISO-8859-1", false); row("malformed-host3", "data://test.com/,", false); - row("emptyData", "data:text/plain", true, - "text/plain;charset=US-ASCII"_L1); + row("emptyData", "data:text/plain", true); row("emptyData-default-mimetype", "data:,", true, "text/plain;charset=US-ASCII"_L1, ""); row("emptyData-only-charset", "data:charset=ISO-8859-1,", true,