Skip to content

Commit 5797c49

Browse files
authored
Merge pull request #7261 from elpaso/bugfix-19195-wms-drag
[bugfix] Fix double escaping of colon in QgsMimeDataUtils
2 parents c58c998 + 941d1c0 commit 5797c49

File tree

5 files changed

+117
-3
lines changed

5 files changed

+117
-3
lines changed

src/core/qgsmimedatautils.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@ QByteArray QgsMimeDataUtils::layerTreeNodesToUriList( const QList<QgsLayerTreeNo
206206
QString QgsMimeDataUtils::encode( const QStringList &items )
207207
{
208208
QString encoded;
209+
// Do not escape colon twice
210+
QRegularExpression re( "(?<!\\\\):" );
209211
Q_FOREACH ( const QString &item, items )
210212
{
211213
QString str = item;
212214
str.replace( '\\', QLatin1String( "\\\\" ) );
213-
str.replace( ':', QLatin1String( "\\:" ) );
215+
str.replace( re, QLatin1String( "\\:" ) );
214216
encoded += str + ':';
215217
}
216218
return encoded.left( encoded.length() - 1 );

src/core/qgsmimedatautils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ class CORE_EXPORT QgsMimeDataUtils
107107
static QStringList decode( const QString &encoded );
108108
static QByteArray uriListToByteArray( const UriList &layers );
109109

110+
111+
friend class TestQgsMimeDataUtils;
112+
110113
};
111114

112115
Q_DECLARE_METATYPE( QgsMimeDataUtils::UriList )

tests/src/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ SET(TESTS
194194
testqgsmeshlayerrenderer.cpp
195195
testqgslayerdefinition.cpp
196196
testqgssqliteutils.cpp
197+
testqgsmimedatautils.cpp
197198
)
198199

199200
IF(WITH_QTWEBKIT)

tests/src/core/testqgslayerdefinition.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/***************************************************************************
2-
testqgsfilefiledownloader.cpp
2+
testqgslayerdefinition.cpp
33
--------------------------------------
44
Date : 07.06.2018
55
Copyright : (C) 2018 Alessandro Pasotti
@@ -37,7 +37,7 @@ class TestQgsLayerDefinition: public QObject
3737
void cleanup(); // will be called after every testfunction.
3838

3939
/**
40-
* test that findLayers()
40+
* test findLayers()
4141
*/
4242
void testFindLayers();
4343

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/***************************************************************************
2+
testqgsmimedatautils.cpp
3+
--------------------------------------
4+
Date : 18.06.2018
5+
Copyright : (C) 2018 Alessandro Pasotti
6+
Email : elpaso at itopen dot it
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
17+
#include "qgstest.h"
18+
#include <QObject>
19+
20+
#include <qgsmimedatautils.h>
21+
22+
const QString TEST_ENCODED_DATA( "raster:wms:A Fancy WMS From Ciriè City:crs=EPSG\\:2036&dpiMode=7&format=image/png&layers=lidar&styles=default"
23+
"&url=https\\://geoegl.msp.gouv.qc.:EPSG\\\\:2036\\:EPSG\\\\:3857:image/tiff\\:image/jpeg" );
24+
25+
class TestQgsMimeDataUtils: public QObject
26+
{
27+
Q_OBJECT
28+
public:
29+
TestQgsMimeDataUtils() = default;
30+
31+
private slots:
32+
void initTestCase(); // will be called before the first testfunction is executed.
33+
void cleanupTestCase(); // will be called after the last testfunction was executed.
34+
void init(); // will be called before each testfunction is executed.
35+
void cleanup(); // will be called after every testfunction.
36+
37+
void testEncodeDecode();
38+
39+
};
40+
41+
42+
void TestQgsMimeDataUtils::initTestCase()
43+
{
44+
QgsApplication::init();
45+
QgsApplication::initQgis();
46+
47+
}
48+
49+
void TestQgsMimeDataUtils::cleanupTestCase()
50+
{
51+
QgsApplication::exitQgis();
52+
}
53+
54+
void TestQgsMimeDataUtils::init()
55+
{
56+
}
57+
58+
59+
void TestQgsMimeDataUtils::cleanup()
60+
{
61+
}
62+
63+
void TestQgsMimeDataUtils::testEncodeDecode()
64+
{
65+
66+
QgsMimeDataUtils::Uri uri;
67+
uri.layerType = QStringLiteral( "raster" );
68+
uri.name = QStringLiteral( "A Fancy WMS From Ciriè City" );
69+
uri.providerKey = QStringLiteral( "wms" );
70+
uri.supportedCrs << QStringLiteral( "EPSG:2036" ) << QStringLiteral( "EPSG:3857" ) ;
71+
uri.supportedFormats << QStringLiteral( "image/tiff" ) << QStringLiteral( "image/jpeg" );
72+
uri.uri = QStringLiteral( "crs=EPSG:2036&dpiMode=7&format=image/png&layers=lidar&styles=default&url=https://geoegl.msp.gouv.qc." );
73+
74+
QgsMimeDataUtils::UriList uriList;
75+
uriList << uri;
76+
77+
QMimeData *mimeData = QgsMimeDataUtils::encodeUriList( uriList );
78+
79+
QgsMimeDataUtils::Uri uriDecoded( QgsMimeDataUtils::decodeUriList( mimeData )[0] );
80+
81+
QCOMPARE( uriDecoded.name, uri.name );
82+
QCOMPARE( uriDecoded.providerKey, uri.providerKey );
83+
QCOMPARE( uriDecoded.supportedFormats, uri.supportedFormats );
84+
QCOMPARE( uriDecoded.uri, uri.uri );
85+
QCOMPARE( uriDecoded.supportedCrs, uri.supportedCrs );
86+
87+
QgsMimeDataUtils::decodeUriList( mimeData );
88+
89+
// Encode representation:
90+
QString data( uri.data() );
91+
92+
QCOMPARE( data, TEST_ENCODED_DATA );
93+
94+
QStringList fragments( QgsMimeDataUtils::decode( data ) );
95+
96+
QCOMPARE( fragments[0], QString( "raster" ) );
97+
QCOMPARE( fragments[1], QString( "wms" ) );
98+
QCOMPARE( fragments[2], QString( "A Fancy WMS From Ciriè City" ) );
99+
QCOMPARE( fragments[3], QString( "crs=EPSG:2036&dpiMode=7&format=image/png&layers=lidar&styles=default&url=https://geoegl.msp.gouv.qc." ) );
100+
QCOMPARE( fragments[4], QString( "EPSG\\:2036:EPSG\\:3857" ) );
101+
102+
}
103+
104+
105+
QGSTEST_MAIN( TestQgsMimeDataUtils )
106+
#include "testqgsmimedatautils.moc"
107+
108+

0 commit comments

Comments
 (0)