Only do ID-check on tagrenderings for official themes

This commit is contained in:
Pieter Vander Vennet 2021-11-11 17:14:03 +01:00
parent 746273f594
commit 0018fcf1f0
17 changed files with 150 additions and 123 deletions

View file

@ -2,7 +2,7 @@ import {Utils} from "../Utils";
export default class Constants {
public static vNumber = "0.12.6";
public static vNumber = "0.12.7";
public static ImgurApiKey = '7070e7167f0a25a'
public static readonly mapillary_client_token_v3 = 'TXhLaWthQ1d4RUg0czVxaTVoRjFJZzowNDczNjUzNmIyNTQyYzI2'
public static readonly mapillary_client_token_v4 = "MLY|4441509239301885|b40ad2d3ea105435bd40c7e76993ae85"

View file

@ -133,7 +133,6 @@ export default class LayerConfig extends WithContextLoader {
const code = kv.substring(index + 1);
try {
new Function("feat", "return " + code + ";");
} catch (e) {
throw `Invalid function definition: code ${code} is invalid:${e} (at ${context})`
@ -225,9 +224,8 @@ export default class LayerConfig extends WithContextLoader {
throw "Missing ids in tagrenderings"
}
this.tagRenderings = this.ExtractLayerTagRenderings(json)
{
this.tagRenderings = this.ExtractLayerTagRenderings(json, official)
if (official) {
const emptyIds = this.tagRenderings.filter(tr => tr.id === "");
if (emptyIds.length > 0) {
@ -265,7 +263,9 @@ export default class LayerConfig extends WithContextLoader {
}
}
this.titleIcons = this.ParseTagRenderings(titleIcons, true);
this.titleIcons = this.ParseTagRenderings(titleIcons, {
readOnlyMode: true
});
this.title = this.tr("title", undefined);
this.isShown = this.tr("isShown", "yes");
@ -298,7 +298,7 @@ export default class LayerConfig extends WithContextLoader {
}
public defaultIcon(): BaseUIElement | undefined {
if(this.mapRendering === undefined || this.mapRendering === null){
if (this.mapRendering === undefined || this.mapRendering === null) {
return undefined;
}
const mapRendering = this.mapRendering.filter(r => r.location.has("point"))[0]
@ -309,7 +309,7 @@ export default class LayerConfig extends WithContextLoader {
return mapRendering.GenerateLeafletStyle(defaultTags, false, {noSize: true}).html
}
public ExtractLayerTagRenderings(json: LayerConfigJson): TagRenderingConfig[] {
public ExtractLayerTagRenderings(json: LayerConfigJson, official: boolean): TagRenderingConfig[] {
if (json.tagRenderings === undefined) {
return []
@ -342,12 +342,16 @@ export default class LayerConfig extends WithContextLoader {
throw `Error in ${this._context}.tagrenderings[${i}]: got a value which defines either \`rewrite\` or \`renderings\`, but not both. Either define both or move the \`renderings\` out of this scope`
}
const allRenderings = this.ParseTagRenderings(normalTagRenderings, false);
const allRenderings = this.ParseTagRenderings(normalTagRenderings,
{
requiresId: official
});
if (renderingsToRewrite.length === 0) {
return allRenderings
}
/* Used for left|right group creation and replacement */
function prepConfig(keyToRewrite: string, target: string, tr: TagRenderingConfigJson) {
function replaceRecursive(transl: string | any) {
@ -379,7 +383,9 @@ export default class LayerConfig extends WithContextLoader {
const textToReplace = rewriteGroup.rewrite.sourceString
const targets = rewriteGroup.rewrite.into
for (const target of targets) {
const parsedRenderings = this.ParseTagRenderings(tagRenderings, false, tr => prepConfig(textToReplace, target, tr))
const parsedRenderings = this.ParseTagRenderings(tagRenderings, {
prepConfig: tr => prepConfig(textToReplace, target, tr)
})
if (!rewriteGroups.has(target)) {
rewriteGroups.set(target, [])

View file

@ -71,6 +71,10 @@ export default class TagRenderingConfig {
this.id = json.id ?? "";
if(this.id.match(/^[a-zA-Z0-9 ()?\/=:;,_-]*$/) === null){
throw "Invalid ID in "+context+": an id can only contain [a-zA-Z0-0_-] as characters. The offending id is: "+this.id
}
this.group = json.group ?? "";
this.render = Translations.T(json.render, context + ".render");
this.question = Translations.T(json.question, context + ".question");
@ -173,6 +177,18 @@ export default class TagRenderingConfig {
throw `${context}: A question is defined, but no mappings nor freeform (key) are. The question is ${this.question.txt} at ${context}`
}
if (this.id === "questions" && this.render !== undefined) {
for (const ln in this.render.translations) {
const txt :string = this.render.translations[ln]
if(txt.indexOf("{questions}") >= 0){
continue
}
throw `${context}: The rendering for language ${ln} does not contain {questions}. This is a bug, as this rendering should include exactly this to trigger those questions to be shown!`
}
}
if (this.freeform) {
if(this.render === undefined){
throw `${context}: Detected a freeform key without rendering... Key: ${this.freeform.key} in ${context}`
@ -202,16 +218,6 @@ export default class TagRenderingConfig {
}
}
if (this.id === "questions" && this.render !== undefined) {
for (const ln in this.render.translations) {
const txt :string = this.render.translations[ln]
if(txt.indexOf("{questions}") >= 0){
continue
}
throw `${context}: The rendering for language ${ln} does not contain {questions}. This is a bug, as this rendering should include exactly this to trigger those questions to be shown!`
}
}
if (this.render && this.question && this.freeform === undefined) {

View file

@ -48,9 +48,16 @@ export default class WithContextLoader {
* A string is interpreted as a name to call
*/
public ParseTagRenderings(
tagRenderings?: (string | { builtin: string, override: any } | TagRenderingConfigJson)[],
readOnly = false,
prepConfig: ((config: TagRenderingConfigJson) => TagRenderingConfigJson) = undefined
tagRenderings: (string | { builtin: string, override: any } | TagRenderingConfigJson)[],
options?:{
/**
* Throw an error if 'question' is defined
*/
readOnlyMode?: boolean,
requiresId?: boolean
prepConfig?: ((config: TagRenderingConfigJson) => TagRenderingConfigJson)
}
): TagRenderingConfig[] {
if (tagRenderings === undefined) {
return [];
@ -58,8 +65,9 @@ export default class WithContextLoader {
const context = this._context
const renderings: TagRenderingConfig[] = []
if (prepConfig === undefined) {
prepConfig = c => c
options = options ?? {}
if (options.prepConfig === undefined) {
options.prepConfig = c => c
}
for (let i = 0; i < tagRenderings.length; i++) {
let renderingJson = tagRenderings[i]
@ -89,9 +97,16 @@ export default class WithContextLoader {
}
const patchedConfig = prepConfig(<TagRenderingConfigJson>renderingJson)
const patchedConfig = options.prepConfig(<TagRenderingConfigJson>renderingJson)
const tr = new TagRenderingConfig(patchedConfig, `${context}.tagrendering[${i}]`);
if(options.readOnlyMode && tr.question !== undefined){
throw "A question is defined for "+`${context}.tagrendering[${i}], but this is not allowed at this position - probably because this rendering is an icon, badge or label`
}
if(options.requiresId && tr.id === ""){
throw `${context}.tagrendering[${i}] has an invalid ID - make sure it is defined and not empty`
}
renderings.push(tr)
}