Skip to content

Commit a485b5b

Browse files
committed
Fix Resource template replacement.
The previous implementation did not work with template keys that are numbers, because the regex syntax was not escaped properly. Instead of building a regex for each replacement, we can use a replace callback method.
1 parent ead9f36 commit a485b5b

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Change Log
1818
* Fixed the value for `BlendFunction.ONE_MINUS_CONSTANT_COLOR`. [#7624](https://github.com/AnalyticalGraphicsInc/cesium/pull/7624)
1919
* Fixed `HeadingPitchRoll.pitch` being `NaN` when using `.fromQuaternion` do to a rounding error
2020
for pitches close to +/- 90°. [#7654](https://github.com/AnalyticalGraphicsInc/cesium/pull/7654)
21+
* Fixed an error in `Resource` when used with template replacements using numeric keys. [#7668](https://github.com/AnalyticalGraphicsInc/cesium/pull/7668)
2122

2223
### 1.55 - 2019-03-01
2324

Source/Core/Resource.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -569,15 +569,13 @@ define([
569569
// objectToQuery escapes the placeholders. Undo that.
570570
var url = uri.toString().replace(/%7B/g, '{').replace(/%7D/g, '}');
571571

572-
var template = this._templateValues;
573-
var keys = Object.keys(template);
574-
if (keys.length > 0) {
575-
for (var i = 0; i < keys.length; i++) {
576-
var key = keys[i];
577-
var value = template[key];
578-
url = url.replace(new RegExp('{' + key + '}', 'g'), encodeURIComponent(value));
579-
}
580-
}
572+
var templateValues = this._templateValues;
573+
url = url.replace(/{(.*?)}/g, function(match, key) {
574+
// use the replacement value from templateValues if there is one,
575+
// otherwise leave it unchanged
576+
return defaultValue(templateValues[key], match);
577+
});
578+
581579
if (proxy && defined(this.proxy)) {
582580
url = this.proxy.getURL(url);
583581
}

Specs/Core/Iau2006XysDataSpec.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
defineSuite([
22
'Core/Iau2006XysData',
3+
'Core/buildModuleUrl',
34
'Core/defined',
45
'Core/Iau2006XysSample',
56
'Specs/pollToPromise'
67
], function(
78
Iau2006XysData,
9+
buildModuleUrl,
810
defined,
911
Iau2006XysSample,
1012
pollToPromise) {
@@ -51,4 +53,16 @@ defineSuite([
5153
it('returns undefined after the last XYS table sample', function() {
5254
expect(xys.computeXysRadians(2442396 + 27427, 0.0)).toBeUndefined();
5355
});
56+
57+
it('allows configuring xysFileUrlTemplate', function() {
58+
xys = new Iau2006XysData({
59+
// this should be the same location as the default, but specifying the value
60+
// takes the code through a different code path.
61+
xysFileUrlTemplate: buildModuleUrl('Assets/IAU2006_XYS/IAU2006_XYS_{0}.json')
62+
});
63+
64+
return pollToPromise(function() {
65+
return defined(xys.computeXysRadians(2442398, 1234.56));
66+
});
67+
});
5468
});

Specs/Core/ResourceSpec.js

+17
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,13 @@ defineSuite([
193193

194194
it('templateValues are respected', function() {
195195
var resource = new Resource({
196+
url: 'http://test.com/tileset/{foo}/{bar}'
197+
});
198+
199+
// if template values are not provided, they are left unchanged
200+
expect(resource.url).toEqual('http://test.com/tileset/{foo}/{bar}');
201+
202+
resource = new Resource({
196203
url: 'http://test.com/tileset/{foo}/{bar}',
197204
templateValues: {
198205
foo: 'test1',
@@ -201,6 +208,16 @@ defineSuite([
201208
});
202209

203210
expect(resource.url).toEqual('http://test.com/tileset/test1/test2');
211+
212+
resource = new Resource({
213+
url: 'http://test.com/tileset/{0}/{1}',
214+
templateValues: {
215+
'0': 'test1',
216+
'1': 'test2'
217+
}
218+
});
219+
220+
expect(resource.url).toEqual('http://test.com/tileset/test1/test2');
204221
});
205222

206223
it('getDerivedResource sets correct properties', function() {

0 commit comments

Comments
 (0)