From e417399158199a3baa328b76d407cf507ee1ded0 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Wed, 5 May 2021 09:41:06 +0200 Subject: [PATCH] advanced syntaxes: fix nested bracket regression, remove need for 2 brackets in JS{} --- CHANGELOG.md | 2 ++ resources/docs/docs/widgets/advanced-syntaxes.md | 12 ++++++------ src/client/widgets/common/widget.js | 15 +++++++++------ src/client/widgets/utils.js | 15 ++++++++++----- 4 files changed, 27 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 36c6a4b9..97ad71c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,11 @@ - bug fixes - prevent error with empty `OSC{}` blocks + - regression breaking advanced syntax blocks containing nested brackets - advanced syntaxes - new syntax for using custom variables in properties: `VAR{name, default}` + - `JS{}` blocks don't require 2 brackets anymore (`JS{{}}` still works) - scripting - add `getVar()` and `setVar` for reading and modifying custom variables. This allows modifying properties directly from scripts. diff --git a/resources/docs/docs/widgets/advanced-syntaxes.md b/resources/docs/docs/widgets/advanced-syntaxes.md index c153ecc9..45f18d3d 100644 --- a/resources/docs/docs/widgets/advanced-syntaxes.md +++ b/resources/docs/docs/widgets/advanced-syntaxes.md @@ -48,12 +48,12 @@ Options can contain `@{}` blocks. ??? example "Array/Object default value" ```js - JS{{ + JS{ return OSC{address} || { "key a": 1, "key b", 2 } - }} + } ``` ## Custom variables: `VAR{variableName, default}` @@ -61,7 +61,7 @@ Options can contain `@{}` blocks. This syntax creates a custom variable in the widget that can be read and modified from any widget's `script` property (see [scripting](./scripting.md#getvarid-name)). Changes made to this variable will be applied automatically. -## Javascript: `JS{{ }}` +## Javascript: `JS{ }` This syntax allows defining a property using [Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript). The code will be compiled as a function and executed in a restricted context. @@ -69,7 +69,7 @@ This syntax allows defining a property using [Javascript](https://developer.mozi - javascript [strict mode](https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Strict_mode) is always enabled - `setTimeout` and `setInterval` functions are not available -A property *cannot* contain multiple `JS{{}}` blocks +A property *cannot* contain multiple `JS{}` blocks #### Available variables @@ -92,9 +92,9 @@ Parsing errors can be read in the console (++ctrl+k++). ## Javascript: `#{ }` -This is a shorthand for the `JS{{}}` syntax, the only difference being that `` is automatically prepended with a `return` statement. +This is a shorthand for the `JS{}` syntax, the only difference being that `` is automatically prepended with a `return` statement. A property *can* contain multiple `#{}` blocks. !!! note "Compilation order" - `#{}` blocks are compiled after `JS{{}}` blocks + `#{}` blocks are compiled after `JS{}` blocks diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index a28220c7..8ca1a561 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -510,11 +510,11 @@ class Widget extends EventEmitter { if (typeof propValue == 'string') { - propValue = balancedReplace('@{', '}', propValue, (content)=>{ + propValue = balancedReplace('@', '{', '}', propValue, (content)=>{ if (content.indexOf('@{') >= 0) { - content = balancedReplace('@{', '}', content, (subcontent)=>{ + content = balancedReplace('@', '{', '}', content, (subcontent)=>{ return this.resolveProp(propName, '@{' + subcontent + '}', storeLinks ? 'nested' : false, this) }) @@ -627,7 +627,7 @@ class Widget extends EventEmitter { }) - propValue = balancedReplace('VAR{', '}', propValue, (args)=>{ + propValue = balancedReplace('VAR', '{', '}', propValue, (args)=>{ if (args === '') return 'undefined' @@ -662,7 +662,7 @@ class Widget extends EventEmitter { return varname }) - propValue = balancedReplace('OSC{', '}', propValue, (args)=>{ + propValue = balancedReplace('OSC', '{', '}', propValue, (args)=>{ if (args === '') return 'undefined' @@ -704,7 +704,9 @@ class Widget extends EventEmitter { }) try { - propValue = balancedReplace('JS{{', '}}', propValue, (code)=>{ + propValue = balancedReplace('JS', '{', '}', propValue, (code)=>{ + + if (code[0] === '{' && code[code.length - 1] === '}') code = code.slice(1, code.length - 1) if (!this.parsers[code]) this.parsers[code] = vm.compile(code, defaultScope) @@ -720,7 +722,8 @@ class Widget extends EventEmitter { } try { - propValue = balancedReplace('#{', '}', propValue, (code)=>{ + propValue = balancedReplace('#', '{', '}', propValue, (code)=>{ + if (!this.parsers[code]) this.parsers[code] = vm.compile('return ' + code.trim(), defaultScope) let r = this.parsers[code](jsScope, this.parsersLocalScope) diff --git a/src/client/widgets/utils.js b/src/client/widgets/utils.js index d96cf042..47a62cd7 100644 --- a/src/client/widgets/utils.js +++ b/src/client/widgets/utils.js @@ -117,22 +117,27 @@ module.exports = { })(), - balancedReplace(open, close, string, replacement) { + balancedReplace(tag, open, close, string, replacement) { - var b + var start - while (b = balanced(open, close, string)) { + while ((start = string.indexOf(tag + open)) >= 0) { + + var substring = string.slice(start + tag.length), + b = balanced(open, close, substring) if (typeof replacement === 'function') { - string = b.pre + replacement(b.body) + b.post + substring = b.pre + replacement(b.body) + b.post } else { - string = b.pre + replacement + b.post + substring = b.pre + replacement + b.post } + string = string.slice(0, start) + substring + } return string