UNPKG

@alicd/templateparser

Version:

walle template parser

95 lines (82 loc) 3.6 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _config = require('../config'); var _config2 = _interopRequireDefault(_config); var _express = require('./express'); var _express2 = _interopRequireDefault(_express); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @license * Copyright Alibaba Group and its affiliates. All Rights Reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var tagStartReplaceRegExp = new RegExp(_config2.default.tagStartReplaceStr, 'g'); var tagEndReplaceRegExp = new RegExp(_config2.default.tagEndReplaceStr, 'g'); var compsRegexp = new RegExp('(<\\/?[\\w]+[^<>]*[\\/]?>)'); exports.default = function (str, runtimeOn) { if (!str) { return null; } str = str.replace(/\&nbsp;/g, ' ').replace(/\&nbsp;/g, ' ').replace(/\&lt;/g, '<').replace(/\&gt;/g, '>').replace(/\&amp;/g, '&').replace(/\&quot;/g, '\'').replace(tagStartReplaceRegExp, '<').replace(tagEndReplaceRegExp, '>'); // walle syntax: with `{}` syntax if (_express2.default.test(str)) { // tpl component. if (compsRegexp.test(str)) { str = _express2.default.filter(str); try { // handle the template in attributes of the node. if (!runtimeOn) { // `__walle_h__` alias of walle.h // `__walleTPSplitHolder__` walle's split holder return new Function('return __walle_h__(`__walleTPSplitHolder__' + str + '__walleTPSplitHolder__`, this)'); } // use for runtime. else { str = str.replace(/"/g, '\\"').replace(/'/g, '\\\''); return new Function('return walle.createEle(\'' + str + '\', this)'); } } catch (e) { console.error('\x1B[31m%s\x1b[0m', 'Syntax error on analysis express: `' + str + '` \n ', e); return new Function('\n console.error(\n `Syntax error on analysis express: \\`' + str + '\\` \\n ' + e.toString() + '` \n );\n '); } } // express. else { // express filter. str = _express2.default.filter(str); // handle es6 syntax for the runtime case. if (runtimeOn) { // arrow function. var arrowFnParts = str.split('=>'); if (arrowFnParts.length > 1) { arrowFnParts[0] = arrowFnParts[0].trim().replace(/[\(|\)]*/gm, ''); arrowFnParts[1] = arrowFnParts[1].trim().replace(/^{/gm, '').replace(/}$/gm, ''); str = '(function ( ' + arrowFnParts[0] + ' ) { ' + arrowFnParts[1] + ' } ).bind(this)'; } } // output function. try { var functionTemplate = void 0; if (!runtimeOn) { // alias: `if(__walleTPReplaceHolder__` => `with(` functionTemplate = 'if(__walleTPReplaceHolder____walle_c__){return ' + str + '}'; } // use for runtime. else { // alias: `if(__walleTPReplaceHolder__` => `with(` functionTemplate = 'with(__walle_c__){return ' + str + '}'; } return new Function('__walle_c__', functionTemplate); } catch (e) { console.error('\x1B[31m%s\x1b[0m', 'Syntax error on analysis express: `' + str + '` \n ', e); return new Function('\n console.error(\n `Syntax error on analysis express: \\`' + str + '\\` \\n ' + e.toString() + '` \n );\n '); } } } else { return str; } };