Skip to content

Commit

Permalink
Fix sprite can't be written in relative path("sprite": "css/sprite") (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
zjp8369 committed May 22, 2022
1 parent 128e82b commit 58c0fdb
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/util/mapbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ export class RequestManager {
}

normalizeSpriteURL(url: string, format: string, extension: string, accessToken?: string): string {
const urlObject = parseUrl(url);
const urlObject = parseUrl2(url);
if (!isMapboxURL(url)) {
urlObject.path += `${format}${extension}`;
return formatUrl(urlObject);
if (urlObject.protocol === undefined) {
return `${url}${format}${extension}`;
} else {
urlObject.path += `${format}${extension}`;
return formatUrl(urlObject);
}
}
urlObject.path = `/styles/v1${urlObject.path}/sprite${format}${extension}`;
return this._makeAPIURL(urlObject, this._customAccessToken || accessToken);
Expand Down Expand Up @@ -257,6 +261,21 @@ function parseUrl(url: string): UrlObject {
};
}

const urlRe2 = /^((\w+):\/\/)?([^/?]*)(\/[^?]+)?\??(.+)?/;

function parseUrl2(url: string): UrlObject {
const parts = url.match(urlRe2);
if (!parts) {
throw new Error('Unable to parse URL object');
}
return {
protocol: parts[2],
authority: parts[3],
path: parts[4] || '/',
params: parts[5] ? parts[5].split('&') : []
};
}

function formatUrl(obj: UrlObject): string {
const params = obj.params.length ? `?${obj.params.join('&')}` : '';
return `${obj.protocol}://${obj.authority}${obj.path}${params}`;
Expand Down

0 comments on commit 58c0fdb

Please sign in to comment.