From 385ed003de43fe38608cdf5c7365102aacbec713 Mon Sep 17 00:00:00 2001 From: bordun Date: Thu, 29 Apr 2021 13:48:43 +0200 Subject: [PATCH] widget: use balanced-match instead of regexp to parse advanced syntaxes (fixes #714) --- CHANGELOG.md | 1 + package.json | 1 + src/client/widgets/common/widget.js | 41 +++++++++++------------------ src/client/widgets/utils.js | 26 +++++++++++++++++- 4 files changed, 43 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c00ef686..5a2a21fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - bug fixes - scripting: set() not working from a slider to a pad - tabs not sending messages / triggering scripts when clicked in editing mode + - prevent hang with some syntax errors in advanced syntaxes - editor - tree: allow specifying multiple type filters diff --git a/package.json b/package.json index 67cee17f..2a340a5d 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "ansi-html": "0.0.7", "babel-eslint": "10.1.0", "babelify": "10.0.0", + "balanced-match": "1.0.0", "bonjour": "github:jean-emmanuel/bonjour", "browserify": "17.0.0", "chokidar": "3.5.1", diff --git a/src/client/widgets/common/widget.js b/src/client/widgets/common/widget.js index 47b420e3..d4c8910c 100644 --- a/src/client/widgets/common/widget.js +++ b/src/client/widgets/common/widget.js @@ -2,7 +2,7 @@ var EventEmitter = require('../../events/event-emitter'), osc = require('../../osc'), {nanoid} = require('nanoid'), widgetManager = require('../../managers/widgets'), - {urlParser} = require('../utils'), + {urlParser, balancedReplace} = require('../utils'), Vm = require('../vm'), vm = new Vm(), scopeCss = require('scope-css'), @@ -504,25 +504,17 @@ class Widget extends EventEmitter { if (typeof propValue == 'string') { - propValue = propValue.replace(/@\{[^{}]*((@\{[^{}]*\}|[^{}]*)*)\}/g, (m, nested)=>{ - // pretty regexp: - // @\{ - // [^{}]* - // (( - // @\{ - // [^{}]* - // \} - // | - // [^{}]* - // )*) - // - // \} + propValue = balancedReplace('@{', '}', propValue, (content)=>{ + + if (content.indexOf('@{') >= 0) { + + content = balancedReplace('@{', '}', content, (subcontent)=>{ + return this.resolveProp(propName, '@{' + subcontent + '}', storeLinks ? 'nested' : false, this) + }) - if (nested) { - m = m.replace(nested, this.resolveProp(propName, nested, storeLinks ? 'nested' : false, this)) } - let id = m.substr(2, m.length - 3).split('.'), + let id = content.split('.'), k, subk if (id.length > 1) { @@ -625,9 +617,11 @@ class Widget extends EventEmitter { } + return 'undefined' + }) - propValue = propValue.replace(/OSC\{([^}]+)\}/g, (m, args)=>{ + propValue = balancedReplace('OSC{', '}', propValue, (args)=>{ let [address, value, usePreArgs] = args.split(',') .map(x=>x.trim()) @@ -665,7 +659,7 @@ class Widget extends EventEmitter { }) try { - propValue = propValue.replace(/JS\{\{([\s\S]*)\}\}/g, (m, code)=>{ + propValue = balancedReplace('JS{{', '}}', propValue, (code)=>{ if (!this.parsers[code]) this.parsers[code] = vm.compile(code, defaultScope) @@ -681,12 +675,8 @@ class Widget extends EventEmitter { } try { - propValue = propValue.replace(/#\{(?:[^{}]|\{[^{}]*\})*\}/g, (m)=>{ - // one bracket nesting allowed, if we need two: #\{(?:[^{}]|\{(?:[^{}]|\{[^{}]*\})*\})*\} - - var code = m.substr(2, m.length - 3).trim() - - if (!this.parsers[code]) this.parsers[code] = vm.compile('return ' + code, defaultScope) + 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) @@ -696,6 +686,7 @@ class Widget extends EventEmitter { }) } catch (err) { + this.errorProp(propName, '#{}', err) } diff --git a/src/client/widgets/utils.js b/src/client/widgets/utils.js index d0e5f916..d96cf042 100644 --- a/src/client/widgets/utils.js +++ b/src/client/widgets/utils.js @@ -1,3 +1,5 @@ +var balanced = require('balanced-match') + module.exports = { clip: function(value,range) { @@ -113,6 +115,28 @@ module.exports = { return parser } - })() + })(), + + balancedReplace(open, close, string, replacement) { + + var b + + while (b = balanced(open, close, string)) { + + if (typeof replacement === 'function') { + + string = b.pre + replacement(b.body) + b.post + + } else { + + string = b.pre + replacement + b.post + + } + + } + + return string + + } }