prop-syntaxes: add JS{{}}, print #{} errors

This commit is contained in:
jean-emmanuel 2019-04-06 13:35:36 +02:00
parent 3125cd8693
commit 7494808fb2
4 changed files with 56 additions and 2 deletions

View file

@ -2,7 +2,7 @@ var EventEmitter = require('../../events/event-emitter'),
osc = require('../../osc'),
nanoid = require('nanoid/generate'),
widgetManager = require('../../managers/widgets'),
{math} = require('../utils'),
{math, evaljs} = require('../utils'),
scopeCss = require('scope-css'),
{iconify} = require('../../ui/utils'),
resize = require('../../events/resize'),
@ -136,6 +136,7 @@ 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)
@ -593,7 +594,17 @@ class Widget extends EventEmitter {
return typeof r != 'string' ? JSON.stringify(r) : r
})
} catch (err) {}
} catch (err) {console.debug('#{} parsing error:\n' + err)}
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)
return typeof r !== 'string' ? JSON.stringify(r) : r
})
} catch (err) {console.debug('JS{{}} parsing error:\n' + err)}
for (let k in variables) {
var v = typeof variables[k] === 'string' ? variables[k] : JSON.stringify(variables[k])

View file

@ -104,6 +104,42 @@ module.exports = {
math.expression.mathWithTransform = Object.assign({}, math)
return math
})(),
evaljs: (function(){
var sandbox = document.createElement('iframe'),
loopProtect = require('loop-protect')
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) {
sandbox.contentWindow.parsers[code] = sandbox.contentWindow.Function(loopProtect(code))
return (context)=>{
for (var k in context) {
sandbox.contentWindow[k] = context[k]
}
return sandbox.contentWindow.parsers[code]()
}
}
return evaljs
})()
}