Merge pull request #5432 from bjornharrtell/rm_goog.uri

Remove use of goog.uri
This commit is contained in:
Andreas Hocevar
2016-06-09 14:20:56 +02:00
8 changed files with 98 additions and 14 deletions

View File

@@ -1,7 +1,6 @@
goog.provide('ol.source.ImageArcGISRest');
goog.require('goog.asserts');
goog.require('goog.uri.utils');
goog.require('ol');
goog.require('ol.Image');
goog.require('ol.events');
@@ -10,6 +9,7 @@ goog.require('ol.extent');
goog.require('ol.object');
goog.require('ol.proj');
goog.require('ol.source.Image');
goog.require('ol.uri');
/**
@@ -215,7 +215,7 @@ ol.source.ImageArcGISRest.prototype.getRequestUrl_ = function(extent, size, pixe
if (modifiedUrl == url) {
goog.asserts.fail('Unknown Rest Service', url);
}
return goog.uri.utils.appendParamsFromMap(modifiedUrl, params);
return ol.uri.appendParams(modifiedUrl, params);
};

View File

@@ -2,11 +2,11 @@ goog.provide('ol.source.ImageMapGuide');
goog.require('ol.events');
goog.require('ol.events.EventType');
goog.require('goog.uri.utils');
goog.require('ol.Image');
goog.require('ol.extent');
goog.require('ol.object');
goog.require('ol.source.Image');
goog.require('ol.uri');
/**
@@ -222,7 +222,7 @@ ol.source.ImageMapGuide.prototype.getUrl = function(baseUrl, params, extent, siz
'SETVIEWCENTERY': center[1]
};
ol.object.assign(baseParams, params);
return goog.uri.utils.appendParamsFromMap(baseUrl, baseParams);
return ol.uri.appendParams(baseUrl, baseParams);
};

View File

@@ -3,7 +3,6 @@
goog.provide('ol.source.ImageWMS');
goog.require('goog.asserts');
goog.require('goog.uri.utils');
goog.require('ol');
goog.require('ol.Image');
goog.require('ol.events');
@@ -15,6 +14,7 @@ goog.require('ol.source.Image');
goog.require('ol.source.wms');
goog.require('ol.source.wms.ServerType');
goog.require('ol.string');
goog.require('ol.uri');
/**
@@ -312,7 +312,7 @@ ol.source.ImageWMS.prototype.getRequestUrl_ = function(extent, size, pixelRatio,
}
params['BBOX'] = bbox.join(',');
return goog.uri.utils.appendParamsFromMap(this.url_, params);
return ol.uri.appendParams(this.url_, params);
};

View File

@@ -1,7 +1,6 @@
goog.provide('ol.source.TileArcGISRest');
goog.require('goog.asserts');
goog.require('goog.uri.utils');
goog.require('ol');
goog.require('ol.extent');
goog.require('ol.object');
@@ -10,6 +9,7 @@ goog.require('ol.proj');
goog.require('ol.size');
goog.require('ol.source.TileImage');
goog.require('ol.tilecoord');
goog.require('ol.uri');
/**
@@ -114,7 +114,7 @@ ol.source.TileArcGISRest.prototype.getRequestUrl_ = function(tileCoord, tileSize
if (modifiedUrl == url) {
goog.asserts.fail('Unknown Rest Service', url);
}
return goog.uri.utils.appendParamsFromMap(modifiedUrl, params);
return ol.uri.appendParams(modifiedUrl, params);
};

View File

@@ -5,7 +5,6 @@
goog.provide('ol.source.TileWMS');
goog.require('goog.asserts');
goog.require('goog.uri.utils');
goog.require('ol');
goog.require('ol.extent');
goog.require('ol.object');
@@ -17,7 +16,7 @@ goog.require('ol.source.wms');
goog.require('ol.source.wms.ServerType');
goog.require('ol.tilecoord');
goog.require('ol.string');
goog.require('ol.uri');
/**
* @classdesc
@@ -265,7 +264,7 @@ ol.source.TileWMS.prototype.getRequestUrl_ = function(tileCoord, tileSize, tileE
var index = ol.math.modulo(ol.tilecoord.hash(tileCoord), urls.length);
url = urls[index];
}
return goog.uri.utils.appendParamsFromMap(url, params);
return ol.uri.appendParams(url, params);
};

View File

@@ -2,7 +2,6 @@ goog.provide('ol.source.WMTS');
goog.provide('ol.source.WMTSRequestEncoding');
goog.require('goog.asserts');
goog.require('goog.uri.utils');
goog.require('ol.TileUrlFunction');
goog.require('ol.array');
goog.require('ol.extent');
@@ -10,6 +9,7 @@ goog.require('ol.object');
goog.require('ol.proj');
goog.require('ol.source.TileImage');
goog.require('ol.tilegrid.WMTS');
goog.require('ol.uri');
/**
@@ -123,7 +123,7 @@ ol.source.WMTS = function(options) {
// special template params
template = (requestEncoding == ol.source.WMTSRequestEncoding.KVP) ?
goog.uri.utils.appendParamsFromMap(template, context) :
ol.uri.appendParams(template, context) :
template.replace(/\{(\w+?)\}/g, function(m, p) {
return (p.toLowerCase() in context) ? context[p.toLowerCase()] : m;
});
@@ -147,7 +147,7 @@ ol.source.WMTS = function(options) {
ol.object.assign(localContext, dimensions);
var url = template;
if (requestEncoding == ol.source.WMTSRequestEncoding.KVP) {
url = goog.uri.utils.appendParamsFromMap(url, localContext);
url = ol.uri.appendParams(url, localContext);
} else {
url = url.replace(/\{(\w+?)\}/g, function(m, p) {
return localContext[p];

21
src/ol/uri.js Normal file
View File

@@ -0,0 +1,21 @@
goog.provide('ol.uri');
/**
* Appends query parameters to a URI.
*
* @param {string} uri The original URI, which may already have query data.
* @param {!Object} params An object where keys are URI-encoded parameter keys,
* and the values are arbitrary types or arrays.
* @return {string} The new URI.
*/
ol.uri.appendParams = function(uri, params) {
var qs = Object.keys(params).map(function(k) {
return k + '=' + encodeURIComponent(params[k]);
}).join('&');
// remove any trailing ? or &
uri = uri.replace(/[?&]$/, '')
// append ? or & depending on whether uri has existing parameters
uri = uri.indexOf('?') === -1 ? uri + '?' : uri + '&'
return uri + qs;
};

64
test/spec/ol/uri.test.js Normal file
View File

@@ -0,0 +1,64 @@
goog.provide('ol.test.uri');
describe('ol.uri.appendParams()', function() {
it('should append empty STYLES with =', function() {
var url = ol.uri.appendParams('http://example.com/foo', {
SERVICE: 'WMS',
STYLES: ''
});
expect(url).to.equal('http://example.com/foo?SERVICE=WMS&STYLES=');
});
it('should URL encode values but not names', function() {
var url = ol.uri.appendParams('http://example.com/foo', {
'k ': 'v '
});
expect(url).to.equal('http://example.com/foo?k =v%20');
});
it('should append to simple base URL', function() {
var url = ol.uri.appendParams('http://example.com/foo', {
k: 'v'
});
expect(url).to.equal('http://example.com/foo?k=v');
});
it('should append to base URL with ?', function() {
var url = ol.uri.appendParams('http://example.com/foo?', {
k: 'v'
});
expect(url).to.equal('http://example.com/foo?k=v');
});
it('should append to base URL with single existing param', function() {
var url = ol.uri.appendParams('http://example.com/foo?bar=bam', {
k: 'v'
});
expect(url).to.equal('http://example.com/foo?bar=bam&k=v');
});
it('should append to base URL with single existing param and extraneous &', function() {
var url = ol.uri.appendParams('http://example.com/foo?bar=bam&', {
k: 'v'
});
expect(url).to.equal('http://example.com/foo?bar=bam&k=v');
});
it('should append to base URL with two existing params', function() {
var url = ol.uri.appendParams('http://example.com/foo?bar=bam&baz=bat', {
k: 'v'
});
expect(url).to.equal('http://example.com/foo?bar=bam&baz=bat&k=v');
});
it('should append to base URL with three existing params last one empty', function() {
var url = ol.uri.appendParams('http://example.com/foo?bar=bam&baz=bat&bop=', {
k: 'v'
});
expect(url).to.equal('http://example.com/foo?bar=bam&baz=bat&bop=&k=v');
});
});
goog.require('ol.uri');