优化转义规则

This commit is contained in:
fofolee 2024-03-04 16:42:03 +08:00
parent fbae2864b2
commit 10d7ab200e
2 changed files with 29 additions and 21 deletions

View File

@ -156,13 +156,27 @@ export default {
};
document.addEventListener("keydown", this.ctrlCListener);
},
escapeChars(string) {
// JSON.stringify
return JSON.stringify(string)
escapeItem(item) {
// String
if (typeof item === 'object') {
try {
item = JSON.stringify(item)
} catch (_) {
item = item.toString()
}
} else {
item = item.toString()
}
// JSON.stringify
item = JSON.stringify(item)
//
.slice(1, -1)
//
//
.replace(/`|'/g, "\\$&")
//
.replace(/\{\{/g, "\\{\\{")
// .replace("$", '$$$')
return item
},
//
assignSpecialVars(cmd) {
@ -172,7 +186,7 @@ export default {
let label = val.label.slice(0, -2);
if (cmd.includes(label)) {
let replData = label === "{{usr:" ? userData : this.$root.enterData;
cmd = cmd.replace(val.match, (x) => this.escapeChars(val.repl(x, replData)));
cmd = cmd.replace(val.match, (x) => this.escapeItem(val.repl(x, replData)));
}
});
return cmd;

View File

@ -2,17 +2,11 @@
* 所有的特殊变量
*/
let escapeItem = item => {
if (typeof item === 'number') return item
item = typeof item === 'object' ? JSON.stringify(item) : item.replace(/\\/g, '\\\\')
return item.replace('$', '$$$')
}
let handlingJsonVar = (jsonVar, name, payload) => {
try {
return escapeItem(window.evalCodeInSandbox(jsonVar.slice(2, -2), {
return window.evalCodeInSandbox(jsonVar.slice(2, -2), {
[name]: payload
}))
})
} catch (e) {
return utools.showNotification(e)
}