enforce id to be a string (#2313)

This commit is contained in:
ArnoldSmith86 2024-09-17 22:24:36 +02:00 committed by GitHub
parent 1d57f387dd
commit 9e30ec63f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 3 deletions

View File

@ -2080,6 +2080,8 @@ function jeGetContext() {
if(!jeStateNow.id)
jeJSONerror = 'No ID given.';
else if(typeof jeStateNow.id != 'string')
jeJSONerror = 'ID has to be a string.';
else if(JSON.parse(jeStateBefore).id != jeStateNow.id && widgets.has(jeStateNow.id))
jeJSONerror = `ID ${jeStateNow.id} is already in use.`;
else if(jeStateNow.parent !== undefined && jeStateNow.parent !== null && !widgets.has(jeStateNow.parent))

View File

@ -108,6 +108,8 @@ export function addWidget(widget, instance) {
async function addWidgetLocal(widget) {
if (!widget.id)
widget.id = generateUniqueWidgetID();
else
widget.id = String(widget.id);
if(widget.parent && !widgets.has(widget.parent)) {
console.error(`Refusing to add widget ${widget.id} with invalid parent ${widget.parent}.`);

View File

@ -1738,12 +1738,14 @@ export class Widget extends StateManaged {
let newState = JSON.parse(JSON.stringify(oldWidget.state));
newState.id = await compute(a.relation, null, oldWidget.get(a.property), a.value);
if(!widgets.has(newState.id)) {
if(widgets.has(newState.id)) {
problems.push(`id ${newState.id} already in use, ignored.`);
} else if(typeof newState.id != 'string' || newState.id.length == 0) {
problems.push(`id ${newState.id} is not a string or empty, ignored.`);
} else {
await updateWidgetId(newState, oldID);
for(const c in collections)
collections[c] = collections[c].map(w=>w.id==oldID ? widgets.get(newState.id) : w);
} else {
problems.push(`id ${newState.id} already in use, ignored.`);
}
}
} else {