Comparing version 0.9.3 to 0.9.4
{ | ||
"name": "razorleaf", | ||
"version": "0.9.3", | ||
"version": "0.9.4", | ||
"main": "razorleaf.js", | ||
@@ -5,0 +5,0 @@ "files": ["razorleaf.js"], |
105
razorleaf.js
@@ -10,4 +10,43 @@ "use strict"; | ||
var voidTags = ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"]; | ||
var templateUtilities = "var __amp = /&/g, __quot = /\"/g, __lt = /</g, __gt = />/g, __escapeAttributeValue = function(string) { return String(string).replace(__amp, '&').replace(__quot, '"'); }, __escapeContent = function(string) { return String(string).replace(__amp, '&').replace(__lt, '<').replace(__gt, '>'); };\n"; | ||
var templateUtilities = { | ||
__amp: {value: /&/g, dependencies: []}, | ||
__quot: {value: /"/g, dependencies: []}, | ||
__lt: {value: /</g, dependencies: []}, | ||
__gt: {value: />/g, dependencies: []}, | ||
__escapeAttributeValue: { | ||
value: function(string) { | ||
return String(string).replace(__amp, "&").replace(__quot, """); | ||
}, | ||
dependencies: ["__amp", "__quot"] | ||
}, | ||
__escapeContent: { | ||
value: function(string) { | ||
return String(string).replace(__amp, "&").replace(__lt, "<").replace(__gt, ">"); | ||
}, | ||
dependencies: ["__amp", "__lt", "__gt"] | ||
} | ||
}; | ||
function createUtilities(names) { | ||
if(names.length === 0) { | ||
return ""; | ||
} | ||
var used = {}; | ||
var needed = names.slice(); | ||
while(needed.length > 0) { | ||
var name = needed.pop(); | ||
if(!used.hasOwnProperty(name)) { | ||
used[name] = templateUtilities[name].value; | ||
push.apply(needed, templateUtilities[name].dependencies); | ||
} | ||
} | ||
return "var " + Object.keys(used).map(function(name) { | ||
return name + " = " + used[name]; | ||
}).join(", ") + ";\n"; | ||
} | ||
function escapeAttributeValue(string) { | ||
@@ -27,2 +66,5 @@ return string.replace(/&/g, "&") | ||
this.unescaped = unescaped; | ||
this.interpolated = parts.some(function(part) { | ||
return typeof part !== "string"; | ||
}); | ||
} | ||
@@ -284,3 +326,3 @@ | ||
return {attributes: false, content: children.content, code: code}; | ||
return {attributes: false, content: children.content, code: code, utilities: children.utilities}; | ||
} | ||
@@ -314,3 +356,3 @@ }; | ||
var success = compileChildren(this.success); | ||
var info = {attributes: success.attributes, content: success.content}; | ||
var info = {attributes: success.attributes, content: success.content, utilities: success.utilities}; | ||
info.code = "if(" + this.condition + "\n) {\n" + success.code + "\n}"; | ||
@@ -329,2 +371,4 @@ | ||
push.apply(info.utilities, failure.utilities); | ||
info.code += " else {\n" + failure.code + "\n}"; | ||
@@ -348,3 +392,3 @@ } | ||
compile: function() { | ||
return {content: false, attributes: false, code: "__top.content += '<!DOCTYPE html>';"}; | ||
return {content: false, attributes: false, code: "__top.content += '<!DOCTYPE html>';", utilities: []}; | ||
} | ||
@@ -467,2 +511,3 @@ }; | ||
function compileStatic(element) { | ||
var utilities = []; | ||
var isVoid = voidTags.indexOf(element.name) !== -1; | ||
@@ -476,2 +521,6 @@ var startTag = "<" + element.name; | ||
if(child.type === "attribute") { | ||
if(child.value.interpolated) { | ||
utilities.push("__escapeAttributeValue"); | ||
} | ||
startTag += " " + child.name + "=\"" + child.value.toAttributeValue() + "\""; | ||
@@ -485,4 +534,9 @@ } else if(child.type === "element") { | ||
content += staticMarkup; | ||
push.apply(utilities, staticMarkup.utilities); | ||
content += staticMarkup.code; | ||
} else if(child.type === "string") { | ||
if(child.content.interpolated) { | ||
utilities.push("__escapeContent"); | ||
} | ||
content += child.content.toContent(); | ||
@@ -499,10 +553,11 @@ } else { | ||
return startTag + ">"; | ||
return {code: startTag + ">", utilities: utilities}; | ||
} | ||
return startTag + ">" + content + "</" + element.name + ">"; | ||
return {code: startTag + ">" + content + "</" + element.name + ">", utilities: utilities}; | ||
} | ||
function compileChildren(children) { | ||
var info = {attributes: false, content: false}; | ||
var info = {attributes: false, content: false, utilities: []}; | ||
info.code = children.map(function(child) { | ||
@@ -513,2 +568,6 @@ if(child.type === "attribute") { | ||
if(child.value) { | ||
if(child.value.interpolated) { | ||
info.utilities.push("__escapeAttributeValue"); | ||
} | ||
return "__top.attributes += ' " + child.name + "=\"" + child.value.toAttributeValue() + "\"';"; | ||
@@ -526,3 +585,4 @@ } else { | ||
if(staticMarkup !== null) { | ||
return "__top.content += '" + staticMarkup + "';"; | ||
push.apply(info.utilities, staticMarkup.utilities); | ||
return "__top.content += '" + staticMarkup.code + "';"; | ||
} | ||
@@ -532,5 +592,9 @@ | ||
var children = compileChildren(child.children); | ||
var compiled = "__top.content += '<" + child.name + "';\n__top = {attributes: '', content: '', next: __top};\n"; | ||
compiled += children.code; | ||
push.apply(info.utilities, children.utilities); | ||
var compiled = | ||
"__top.content += '<" + child.name + "';\n__top = {attributes: '', content: '', next: __top};\n" + | ||
children.code; | ||
var parts = []; | ||
@@ -563,2 +627,7 @@ | ||
info.content = true; | ||
if(child.content.interpolated) { | ||
info.utilities.push("__escapeContent"); | ||
} | ||
return "__top.content += '" + child.content.toContent() + "';"; | ||
@@ -577,2 +646,4 @@ } | ||
push.apply(info.utilities, childInfo.utilities); | ||
return childInfo.code; | ||
@@ -590,10 +661,12 @@ }).join("\n"); | ||
var tree = parse(template); | ||
var compiled = templateUtilities + "var __top = {attributes: null, content: '', next: null};\n"; | ||
var compiled = compileChildren(tree, options); | ||
var body = | ||
createUtilities(compiled.utilities) + | ||
"var __top = {attributes: null, content: '', next: null};\n" + | ||
compiled.code + | ||
"\n\nreturn __top.content;"; | ||
compiled += compileChildren(tree, options).code; | ||
compiled += "\n\nreturn __top.content;"; | ||
return new Function(["data"], compiled); | ||
return new Function("data", body); | ||
} | ||
module.exports.compile = compile; |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
17100
506