Skip to content

Commit 26f4708

Browse files
authored
Merge pull request #7668 from AnalyticalGraphicsInc/fixResourceTemplating
Fix Resource template replacement.
2 parents 24d04e9 + fb82376 commit 26f4708

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-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

+10-8
Original file line numberDiff line numberDiff line change
@@ -569,15 +569,17 @@ 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));
572+
var templateValues = this._templateValues;
573+
url = url.replace(/{(.*?)}/g, function(match, key) {
574+
var replacement = templateValues[key];
575+
if (defined(replacement)) {
576+
// use the replacement value from templateValues if there is one...
577+
return encodeURIComponent(replacement);
579578
}
580-
}
579+
// otherwise leave it unchanged
580+
return match;
581+
});
582+
581583
if (proxy && defined(this.proxy)) {
582584
url = this.proxy.getURL(url);
583585
}

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

+33-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ defineSuite([
191191
expect(derived.url).toEqual('http://test.com/tileset/other_endpoint?a=5&a=7&a=1&a=2&a=4&b=6&b=3');
192192
});
193193

194-
it('templateValues are respected', function() {
194+
it('replaces templateValues in the url', function() {
195195
var resource = new Resource({
196196
url: 'http://test.com/tileset/{foo}/{bar}',
197197
templateValues: {
@@ -203,6 +203,38 @@ defineSuite([
203203
expect(resource.url).toEqual('http://test.com/tileset/test1/test2');
204204
});
205205

206+
it('replaces numeric templateValues', function() {
207+
var resource = new Resource({
208+
url: 'http://test.com/tileset/{0}/{1}',
209+
templateValues: {
210+
'0': 'test1',
211+
'1': 'test2'
212+
}
213+
});
214+
215+
expect(resource.url).toEqual('http://test.com/tileset/test1/test2');
216+
});
217+
218+
it('leaves templateValues unchanged that are not provided', function() {
219+
var resource = new Resource({
220+
url: 'http://test.com/tileset/{foo}/{bar}'
221+
});
222+
223+
expect(resource.url).toEqual('http://test.com/tileset/{foo}/{bar}');
224+
});
225+
226+
it('url encodes replacement templateValues in the url', function() {
227+
var resource = new Resource({
228+
url: 'http://test.com/tileset/{foo}/{bar}',
229+
templateValues: {
230+
foo: 'a/b',
231+
bar: 'x$y#'
232+
}
233+
});
234+
235+
expect(resource.url).toEqual('http://test.com/tileset/a%2Fb/x%24y%23');
236+
});
237+
206238
it('getDerivedResource sets correct properties', function() {
207239
var proxy = new DefaultProxy('/proxy/');
208240
var request = new Request();

0 commit comments

Comments
 (0)