From 2d26b07ff63cee999f4c561b45e635c4d503d267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20De=20Canni=C3=A8re?= Date: Wed, 15 Mar 2023 13:55:15 +0100 Subject: [PATCH] QV4: Fix formatting options for the search component of UrlObject This fix changes the way the search component of UrlObjects is formatted by passing the correct options to QUrl::query. Namely, the EncodeDelimiters flag is no longer set. This now allows for correct encoding of backslashes in search strings. Amends 6cca731f3e1304ce98f1ec18af42e3bd06001eea. Task-number: QTBUG-111014 Change-Id: Iee544dfc7ad6ba374601c8ec4690d11bf07c9b6a Reviewed-by: Ulf Hermann (cherry picked from commit af4cd4763a7e0a9b2b52de91ea07eff9be3aa9b0) Reviewed-by: Qt Cherry-pick Bot --- src/qml/jsruntime/qv4urlobject.cpp | 5 ++++- tests/auto/qml/qv4urlobject/tst_qv4urlobject.cpp | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/qml/jsruntime/qv4urlobject.cpp b/src/qml/jsruntime/qv4urlobject.cpp index 4417d327c6..637d033bd4 100644 --- a/src/qml/jsruntime/qv4urlobject.cpp +++ b/src/qml/jsruntime/qv4urlobject.cpp @@ -248,7 +248,10 @@ QString UrlObject::search() const if (auto url = QUrl(href()); !url.hasQuery() || url.query().isEmpty()) return QLatin1String(""); - return QLatin1Char('?') + url.query(QUrl::ComponentFormattingOptions(QUrl::ComponentFormattingOption::FullyEncoded)); + constexpr auto options = QUrl::ComponentFormattingOption::EncodeSpaces + | QUrl::ComponentFormattingOption::EncodeUnicode + | QUrl::ComponentFormattingOption::EncodeReserved; + return u'?' + url.query(options); } QUrl UrlObject::toQUrl() const diff --git a/tests/auto/qml/qv4urlobject/tst_qv4urlobject.cpp b/tests/auto/qml/qv4urlobject/tst_qv4urlobject.cpp index 886bfda018..b74ec3d583 100644 --- a/tests/auto/qml/qv4urlobject/tst_qv4urlobject.cpp +++ b/tests/auto/qml/qv4urlobject/tst_qv4urlobject.cpp @@ -116,6 +116,14 @@ void tst_urlobject::urlObject_search_data() << "var url = new URL(\"http://google.com/search/?a=㉽\");" "url.search" << "?a=%E3%89%BD"; + QTest::newRow("backslash") + // The JS string in the C++ source ends in 4 backslashes. + // The C++ compiler turns that into 2 backslashes. + // QV4 receives source code containing a string literal ending in two backslashes. + // The resulting JS string ends in a single backslash. + << "var url = new URL('http://google.com/search/?q=\\\\');" + "url.search" + << "?q=\\"; } void tst_urlobject::urlObject_href()