diff --git a/src/client/app/widgets/common/widget.js b/src/client/app/widgets/common/widget.js index 77388f9f..9d2c71ba 100644 --- a/src/client/app/widgets/common/widget.js +++ b/src/client/app/widgets/common/widget.js @@ -136,7 +136,6 @@ class Widget extends EventEmitter { this.props = options.props this.errors = {} this.parsers = {} - this.jsparsers = {} this.parent = options.root ? widgetManager : options.parent this.parentNode = options.parentNode this.hash = nanoid('abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ', 10) @@ -418,7 +417,7 @@ class Widget extends EventEmitter { originalPropName = originalPropName || propName var variables = {}, - mathscope = context || {}, + mathscope = context || this.constructor.parsersContexts[propName] || {}, varnumber = 999 if (typeof propValue == 'string') { @@ -600,13 +599,15 @@ class Widget extends EventEmitter { try { propValue = propValue.replace(/JS\{\{([\s\S]*)\}\}/g, (m, code)=>{ - if (!this.jsparsers[code]) this.jsparsers[code] = evaljs(code) - let r = this.jsparsers[code](mathscope) + if (!this.parsers[code]) this.parsers[code] = evaljs(code) + + let r = this.parsers[code](mathscope) if (r === undefined) r = '' return typeof r !== 'string' ? JSON.stringify(r) : r + }) } catch (err) { var stackline = err.stack ? err.stack.match(/>:([0-9]+):[0-9]+/) : '', @@ -910,6 +911,8 @@ class Widget extends EventEmitter { } +Widget.parsersContexts = {} + Widget.dynamicProps = [ 'top', 'left', diff --git a/src/client/app/widgets/inputs/keys.js b/src/client/app/widgets/inputs/keys.js index 0cb84224..97eee3e2 100644 --- a/src/client/app/widgets/inputs/keys.js +++ b/src/client/app/widgets/inputs/keys.js @@ -152,6 +152,15 @@ class Keys extends Widget { } +Keys.parsersContexts.keydown = Keys.parsersContexts.keyup = { + key: 'x', + code: 'x', + ctrl: false, + alt: false, + shift: false, + super: false +} + Keys.dynamicProps = Keys.prototype.constructor.dynamicProps.concat( 'keydown', 'keyup', diff --git a/src/client/app/widgets/inputs/script.js b/src/client/app/widgets/inputs/script.js index e059f91c..75d75c18 100644 --- a/src/client/app/widgets/inputs/script.js +++ b/src/client/app/widgets/inputs/script.js @@ -89,6 +89,16 @@ class Script extends Widget { } +Script.parsersContexts.condition = { + value: '' +} + +Script.parsersContexts.script = { + value: '', + send: ()=>{}, + set: ()=>{} +} + Script.dynamicProps = Script.prototype.constructor.dynamicProps.concat( 'condition', 'script' diff --git a/src/client/app/widgets/utils.js b/src/client/app/widgets/utils.js index dd85a8a3..1ac0a2d0 100644 --- a/src/client/app/widgets/utils.js +++ b/src/client/app/widgets/utils.js @@ -111,18 +111,19 @@ module.exports = { var sandbox = document.createElement('iframe'), loopProtect = require('loop-protect') + sandbox.style.display = 'none' + sandbox.sandbox = 'allow-scripts allow-same-origin' + document.body.appendChild(sandbox) + + // init infinite loop guard loopProtect.alias = '__protect' loopProtect.hit = function(line){ throw 'Potential infinite loop found on line ' + line } - - sandbox.style.display = 'none' - sandbox.sandbox = 'allow-scripts allow-same-origin' - document.body.appendChild(sandbox) sandbox.contentWindow.parsers = {} sandbox.contentWindow.__protect = loopProtect - function evaljs(code) { + function evaljs(code, defaultContext) { sandbox.contentWindow.parsers[code] = sandbox.contentWindow.Function( loopProtect('"use strict";' + code) @@ -131,11 +132,28 @@ module.exports = { return (context)=>{ - for (var k in context) { + var ret, err, k + + // set context + for (k in context) { sandbox.contentWindow[k] = context[k] } - return sandbox.contentWindow.parsers[code]() + // evaluate + try { + ret = sandbox.contentWindow.parsers[code]() + } catch(e) { + err = e + } + + // clean context + for (k in context) { + delete sandbox.contentWindow[k] + } + + if (err) throw err + + return ret }