prop-syntaxes: add JS{{}}, print #{} errors
This commit is contained in:
parent
3125cd8693
commit
7494808fb2
4 changed files with 56 additions and 2 deletions
|
|
@ -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])
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
})()
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue