当前位置:网站首页>Explain in simple terms node template parsing error escape is not a function
Explain in simple terms node template parsing error escape is not a function
2022-07-06 04:08:00 【xzlAwin】
Explain profound theories in simple language node Template parsing error escape is not a function
operation
var escape = function (html) {
return String(html)
.replace(/&(?!\w+;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
}
var complie = function (str) {
console.log(str)
var tpl = str.replace(/<%=([\s\S]+?)%>/g, function (match, code) {
// escape
return "' + escape(" + code + ") + '"
}).replace(/<%-([\s\S]+?)%>/g, function (match, code) {
// Normal output
return "' + " + code + " + '"
}).replace(/<%([\s\S]+?)%>/g, function (match, code) {
return "';\n" + code + "\n tpl += '"
}).replace(/\'\n/g, '\'')
.replace(/\n\'/gm, '\'')
console.log('---------- Code replacement template ----------')
console.log(tpl)
tpl = "tpl = '" + tpl + "'"
tpl = tpl.replace(/''/g, '\'\\n\'')
tpl = ' var tpl = ""\n with (obj || {}) {\n ' + tpl + '\n }\n return tpl'
return new Function('obj', 'escape', tpl)
}
var render = function(complie, data) {
console.log('---------- Constructors ----------')
console.log(complie.toString())
console.log('---------- Operation function ----------')
return complie(data)
}
var tpl = [
'<% if (obj.user) { %>',
'<h2><%=user.name%></h2>',
'<% } else { %>',
'<h2> Anonymous users </h2>',
'<% } %>'].join('\n')
console.log('---------- Templates ----------')
console.log(tpl)
console.log(render(complie(tpl), {user: {name: 'Jackson Tian'}}))
//console.log('\n\n')
//console.log(render(complie(tpl), {}))
Abnormal information
TypeError: escape is not a function
at eval (eval at complie (F:\workspace\javascript workspace\plNode\prj8_5_4_2\src\template4.js:27:10), <anonymous>:7:21)
at render (F:\workspace\javascript workspace\plNode\prj8_5_4_2\src\template4.js:34:9)
at Object.<anonymous> (F:\workspace\javascript workspace\plNode\prj8_5_4_2\src\template4.js:49:13)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
The analysis reason
- escape It's not a function , because return new Function(‘obj’, ‘escape’, tpl) In the sentence ‘escape’ Is the function ,complie(data, escape) When calling, you need to pass escape function , Otherwise I can't find it escape function
resolvent
Method 1
- Invocation time , Pass on escape function
complie(data, escape)
Method 2
- Call yourself , Pass on escape function
complie.call(complie, data, escape)
# perhaps
complie.call(this, data, escape)
Complete code
var escape = function (html) {
return String(html)
.replace(/&(?!\w+;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
}
var complie = function (str) {
console.log(str)
var tpl = str.replace(/<%=([\s\S]+?)%>/g, function (match, code) {
// escape
return "' + escape(" + code + ") + '"
}).replace(/<%-([\s\S]+?)%>/g, function (match, code) {
// Normal output
return "' + " + code + " + '"
}).replace(/<%([\s\S]+?)%>/g, function (match, code) {
return "';\n" + code + "\n tpl += '"
}).replace(/\'\n/g, '\'')
.replace(/\n\'/gm, '\'')
console.log('---------- Code replacement template ----------')
console.log(tpl)
tpl = "tpl = '" + tpl + "'"
tpl = tpl.replace(/''/g, '\'\\n\'')
tpl = ' var tpl = ""\n with (obj || {}) {\n ' + tpl + '\n }\n return tpl'
return new Function('obj', 'escape', tpl)
}
var render = function(complie, data) {
console.log('---------- Constructors ----------')
console.log(complie.toString())
console.log('---------- Operation function ----------')
return complie(data, escape)
// Equivalent to
//return complie.call(this, data, escape)
}
var tpl = [
'<% if (obj.user) { %>',
'<h2><%=user.name%></h2>',
'<% } else { %>',
'<h2> Anonymous users </h2>',
'<% } %>'].join('\n')
console.log('---------- Templates ----------')
console.log(tpl)
console.log(render(complie(tpl), {user: {name: 'Jackson Tian'}}))
//console.log('\n\n')
//console.log(render(complie(tpl), {}))
边栏推荐
- Path of class file generated by idea compiling JSP page
- 【按鍵消抖】基於FPGA的按鍵消抖模塊開發
- Use js to complete an LRU cache
- 2/11 matrix fast power +dp+ bisection
- asp. Core is compatible with both JWT authentication and cookies authentication
- Comprehensive ability evaluation system
- ESP32_ FreeRTOS_ Arduino_ 1_ Create task
- Execution order of scripts bound to game objects
- /usr/bin/gzip: 1: ELF: not found/usr/bin/gzip: 3: : not found/usr/bin/gzip: 4: Syntax error:
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验9 PWM输出实验(学习笔记)
猜你喜欢

ESP32_ FreeRTOS_ Arduino_ 1_ Create task

《2022年中国银行业RPA供应商实力矩阵分析》研究报告正式启动

MySQL master-slave replication

Facebook等大厂超十亿用户数据遭泄露,早该关注DID了

C#(三十一)之自定义事件

Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage

阿里测试师用UI自动化测试实现元素定位

Do you know cookies, sessions, tokens?

记一次excel XXE漏洞

Mysql数据库慢sql抓取与分析
随机推荐
10 exemples les plus courants de gestion du trafic istio, que savez - vous?
有条件地 [JsonIgnore]
Yyds dry goods inventory hcie security Day11: preliminary study of firewall dual machine hot standby and vgmp concepts
MySQL transaction isolation level
User datagram protocol UDP
WPF效果第一百九十一篇之框选ListBox
MySQL about self growth
20、 EEPROM memory (AT24C02) (similar to AD)
math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)
自动化测试的好处
【按键消抖】基于FPGA的按键消抖模块开发
SSTI template injection explanation and real problem practice
Oracle ORA error message
绑定在游戏对象上的脚本的执行顺序
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
10個 Istio 流量管理 最常用的例子,你知道幾個?
The global and Chinese market of negative pressure wound therapy unit (npwtu) 2022-2028: Research Report on technology, participants, trends, market size and share
如何修改表中的字段约束条件(类型,default, null等)
颠覆你的认知?get和post请求的本质
Stc8h development (XII): I2C drive AT24C08, at24c32 series EEPROM storage