18 lines
788 B
JavaScript
18 lines
788 B
JavaScript
'use strict'
|
|
const vm = require('vm')
|
|
// WePY 2 escapes quotes in :class/:style expressions as if they were string
|
|
// contents. WXML expects an expression here, so those escapes are invalid.
|
|
// Repair only expressions that fail parsing and become valid after removing
|
|
// that extra layer. Preserve valid strings (including escaped apostrophes).
|
|
function valid(expr) {
|
|
try { new vm.Script('(' + expr + ')'); return true } catch (_) { return false }
|
|
}
|
|
module.exports = function fixTemplates(markup) {
|
|
return markup.replace(/\{\{([\s\S]*?)\}\}/g, (whole, expr) => {
|
|
if (valid(expr) || !expr.includes("\\'")) return whole
|
|
const fixed = expr.replace(/\\'/g, "'")
|
|
if (!valid(fixed)) throw new Error('Unparseable generated WXML expression')
|
|
return '{{' + fixed + '}}'
|
|
})
|
|
}
|