Use URL constructor
Co-authored-by: Tim Schaub <tschaub@users.noreply.github.com>
This commit is contained in:
@@ -99,9 +99,12 @@ export function normalizeStyleUrl(url, token) {
|
|||||||
export function normalizeSourceUrl(url, token, tokenParam) {
|
export function normalizeSourceUrl(url, token, tokenParam) {
|
||||||
const mapboxPath = getMapboxPath(url);
|
const mapboxPath = getMapboxPath(url);
|
||||||
if (!mapboxPath) {
|
if (!mapboxPath) {
|
||||||
return token && url.indexOf(token) === -1
|
if (!token) {
|
||||||
? `${url}${url.indexOf('?') === -1 ? '?' : '&'}${tokenParam}=${token}`
|
return url;
|
||||||
: url;
|
}
|
||||||
|
const urlObject = new URL(url, location.href);
|
||||||
|
urlObject.searchParams.set(tokenParam, token);
|
||||||
|
return decodeURI(urlObject.href);
|
||||||
}
|
}
|
||||||
return `https://{a-d}.tiles.mapbox.com/v4/${mapboxPath}/{z}/{x}/{y}.vector.pbf?access_token=${token}`;
|
return `https://{a-d}.tiles.mapbox.com/v4/${mapboxPath}/{z}/{x}/{y}.vector.pbf?access_token=${token}`;
|
||||||
}
|
}
|
||||||
@@ -291,12 +294,12 @@ class MapboxVectorLayer extends VectorTileLayer {
|
|||||||
if (options.accessToken) {
|
if (options.accessToken) {
|
||||||
this.accessToken = options.accessToken;
|
this.accessToken = options.accessToken;
|
||||||
} else {
|
} else {
|
||||||
const parts = options.styleUrl.split(/[?&]/);
|
const url = new URL(options.styleUrl, location.href);
|
||||||
if (parts.length > 1) {
|
// The last search parameter is the access token
|
||||||
const keyValue = parts[parts.length - 1].split('=');
|
url.searchParams.forEach((value, key) => {
|
||||||
this.accessToken = keyValue[1];
|
this.accessToken = value;
|
||||||
this.accessTokenParam_ = keyValue[0];
|
this.accessTokenParam_ = key;
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
this.fetchStyle(options.styleUrl);
|
this.fetchStyle(options.styleUrl);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ describe('ol/layer/MapboxVector', () => {
|
|||||||
});
|
});
|
||||||
it('applies correct access token from url', function () {
|
it('applies correct access token from url', function () {
|
||||||
const layer = new MapboxVectorLayer({
|
const layer = new MapboxVectorLayer({
|
||||||
styleUrl: '?key=123',
|
styleUrl: 'foo?key=123',
|
||||||
});
|
});
|
||||||
expect(layer.accessToken).to.be('123');
|
expect(layer.accessToken).to.be('123');
|
||||||
expect(layer.accessTokenParam_).to.be('key');
|
expect(layer.accessTokenParam_).to.be('key');
|
||||||
|
|||||||
Reference in New Issue
Block a user