当前位置:网站首页>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), {}))
边栏推荐
- 【FPGA教程案例12】基于vivado核的复数乘法器设计与实现
- Security xxE vulnerability recurrence (XXe Lab)
- Global and Chinese markets for fire resistant conveyor belts 2022-2028: Research Report on technology, participants, trends, market size and share
- Conditionally [jsonignore]
- asp. Core is compatible with both JWT authentication and cookies authentication
- Execution order of scripts bound to game objects
- Detailed explanation of serialization and deserialization
- [Key shake elimination] development of key shake elimination module based on FPGA
- 10 exemples les plus courants de gestion du trafic istio, que savez - vous?
- Stack and queue
猜你喜欢
Path of class file generated by idea compiling JSP page
1291_Xshell日志中增加时间戳的功能
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
C language -- structs, unions, enumerations, and custom types
Scalpel like analysis of JVM -- this article takes you to peek into the secrets of JVM
Thread sleep, thread sleep application scenarios
JVM的手术刀式剖析——一文带你窥探JVM的秘密
How to standardize the deployment of automated testing?
[Zhao Yuqiang] deploy kubernetes cluster with binary package
C form application of C (27)
随机推荐
潘多拉 IOT 开发板学习(HAL 库)—— 实验9 PWM输出实验(学习笔记)
80% of the diseases are caused by bad living habits. There are eight common bad habits, which are both physical and mental
食品行业仓储条码管理系统解决方案
Chinese brand hybrid technology: there is no best technical route, only better products
1291_Xshell日志中增加时间戳的功能
51nod 1130 n factorial length V2 (Stirling approximation)
Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
【可调延时网络】基于FPGA的可调延时网络系统verilog开发
Security xxE vulnerability recurrence (XXe Lab)
SSTI template injection explanation and real problem practice
asp. Core is compatible with both JWT authentication and cookies authentication
自动化测试的好处
Mysql数据库慢sql抓取与分析
MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
Tips for using dm8huge table
C#(二十八)之C#鼠标事件、键盘事件
Mathematical modeling regression analysis relationship between variables
Facebook and other large companies have leaked more than one billion user data, and it is time to pay attention to did
Detailed explanation of serialization and deserialization
DM8 archive log file manual switching