Fix in SubstituteKeys: implementation is now robust against non-iterable tags (because they are lazy)

This commit is contained in:
Pieter Vander Vennet 2021-10-18 22:17:15 +02:00
parent be9d70d62b
commit 46e3fa84de

View file

@ -12,6 +12,8 @@ export class Utils {
public static externalDownloadFunction: (url: string, headers?: any) => Promise<any>;
private static knownKeys = ["addExtraTags", "and", "calculatedTags", "changesetmessage", "clustering", "color", "condition", "customCss", "dashArray", "defaultBackgroundId", "description", "descriptionTail", "doNotDownload", "enableAddNewPoints", "enableBackgroundLayerSelection", "enableGeolocation", "enableLayers", "enableMoreQuests", "enableSearch", "enableShareScreen", "enableUserBadge", "freeform", "hideFromOverview", "hideInAnswer", "icon", "iconOverlays", "iconSize", "id", "if", "ifnot", "isShown", "key", "language", "layers", "lockLocation", "maintainer", "mappings", "maxzoom", "maxZoom", "minNeededElements", "minzoom", "multiAnswer", "name", "or", "osmTags", "passAllFeatures", "presets", "question", "render", "roaming", "roamingRenderings", "rotation", "shortDescription", "socialImage", "source", "startLat", "startLon", "startZoom", "tagRenderings", "tags", "then", "title", "titleIcons", "type", "version", "wayHandling", "widenFactor", "width"]
private static extraKeys = ["nl", "en", "fr", "de", "pt", "es", "name", "phone", "email", "amenity", "leisure", "highway", "building", "yes", "no", "true", "false"]
private static injectedDownloads = {}
private static _download_cache = new Map<string, { promise: Promise<any>, timestamp: number }>()
static EncodeXmlValue(str) {
if (typeof str !== "string") {
@ -164,18 +166,17 @@ export class Utils {
}
public static SubstituteKeys(txt: string, tags: any) {
for (const key in tags) {
if (!tags.hasOwnProperty(key)) {
continue
const regex = /.*{([^}]*)}.*/
let match = txt.match(regex)
while (match) {
const key = match[1]
txt = txt.replace("{" + key + "}", tags[key] ?? "")
match = txt.match(regex)
}
try{
txt = txt.replace(new RegExp("{" + key + "}", "g"), tags[key] ?? "")
}catch(e){
console.error("WEIRD" , e)
throw e
}
}
txt = txt.replace(new RegExp('{.*}', "g"), "")
return txt;
}
@ -288,9 +289,6 @@ export class Utils {
return result;
}
private static injectedDownloads = {}
public static injectJsonDownloadForTests(url: string, data) {
Utils.injectedDownloads[url] = data
}
@ -325,7 +323,6 @@ export class Utils {
)
}
private static _download_cache = new Map<string, {promise: Promise<any>, timestamp: number}>()
public static async downloadJsonCached(url: string, maxCacheTimeMs: number, headers?: any): Promise<any> {
const cached = Utils._download_cache.get(url)
if (cached !== undefined) {
@ -407,34 +404,6 @@ export class Utils {
return bestColor ?? hex;
}
private static colorDiff(c0: { r: number, g: number, b: number }, c1: { r: number, g: number, b: number }) {
return Math.abs(c0.r - c1.r) + Math.abs(c0.g - c1.g) + Math.abs(c0.b - c1.b);
}
private static color(hex: string): { r: number, g: number, b: number } {
if (hex.startsWith == undefined) {
console.trace("WUT?", hex)
throw "wut?"
}
if (!hex.startsWith("#")) {
return undefined;
}
if (hex.length === 4) {
return {
r: parseInt(hex.substr(1, 1), 16),
g: parseInt(hex.substr(2, 1), 16),
b: parseInt(hex.substr(3, 1), 16),
}
}
return {
r: parseInt(hex.substr(1, 2), 16),
g: parseInt(hex.substr(3, 2), 16),
b: parseInt(hex.substr(5, 2), 16),
}
}
static sortKeys(o: any) {
const copy = {}
let keys = Object.keys(o)
@ -492,5 +461,32 @@ export class Utils {
}
return "https://osmcha.org/?filters=" + encodeURIComponent(osmcha_link)
}
private static colorDiff(c0: { r: number, g: number, b: number }, c1: { r: number, g: number, b: number }) {
return Math.abs(c0.r - c1.r) + Math.abs(c0.g - c1.g) + Math.abs(c0.b - c1.b);
}
private static color(hex: string): { r: number, g: number, b: number } {
if (hex.startsWith == undefined) {
console.trace("WUT?", hex)
throw "wut?"
}
if (!hex.startsWith("#")) {
return undefined;
}
if (hex.length === 4) {
return {
r: parseInt(hex.substr(1, 1), 16),
g: parseInt(hex.substr(2, 1), 16),
b: parseInt(hex.substr(3, 1), 16),
}
}
return {
r: parseInt(hex.substr(1, 2), 16),
g: parseInt(hex.substr(3, 2), 16),
b: parseInt(hex.substr(5, 2), 16),
}
}
}