Comparing version 1.0.0 to 1.0.1
@@ -60,5 +60,7 @@ var Visitor = require('./visitor'); | ||
code += ' // Create elements\n'; | ||
code += ' var ' + indent(this.declarations.join(',\n'), 4) + ';\n'; | ||
code += '\n'; | ||
if (this.declarations.length > 0) { | ||
code += ' // Create elements\n'; | ||
code += ' var ' + indent(this.declarations.join(',\n'), 4) + ';\n'; | ||
code += '\n'; | ||
} | ||
@@ -106,8 +108,12 @@ if (this.construct.length > 0) { | ||
var text = this.compileVariables(node.data, function (code) { | ||
var textParser = this.compileVariables(node.data, function (code) { | ||
return nodeName + ".nodeValue = " + code; | ||
}); | ||
this.declarations.push(nodeName + " = document.createTextNode(" + esc(text) + ")"); | ||
return nodeName; | ||
if (textParser.variables.length > 0) { | ||
this.declarations.push(nodeName + " = document.createTextNode(" + esc(textParser.rawText()) + ")"); | ||
return nodeName; | ||
} else { | ||
return "document.createTextNode(" + esc(textParser.rawText()) + ")"; | ||
} | ||
}; | ||
@@ -166,7 +172,7 @@ | ||
Object.keys(attributes).forEach(function (key) { | ||
if (key === 'id') { | ||
if (key == 'id') { | ||
var id = _this.compileVariables(attributes[key], function (code) { | ||
return nodeName + ".id = " + code; | ||
}); | ||
_this.construct.push(nodeName + ".id = " + esc(id) + ";"); | ||
_this.construct.push(nodeName + ".id = " + esc(id.rawText()) + ";"); | ||
} else { | ||
@@ -176,3 +182,6 @@ var attr = _this.compileVariables(attributes[key], function (code) { | ||
}); | ||
_this.construct.push(nodeName + ".setAttribute('" + key + "', " + esc(attr) + ");") | ||
// Skip default value for next attributes if them contains variables. | ||
if (attr.variables.length == 0 || ['src', 'href'].indexOf(key) === -1) { | ||
_this.construct.push(nodeName + ".setAttribute('" + key + "', " + esc(attr.rawText()) + ");") | ||
} | ||
} | ||
@@ -208,3 +217,3 @@ }); | ||
return textParser.rawText(); | ||
return textParser; | ||
}; | ||
@@ -214,12 +223,28 @@ | ||
if (!node.attribs.test) { | ||
throw new Error('If node must contains "key" attribute.'); | ||
throw new Error('If node must contains "test" attribute.'); | ||
} | ||
var variable = new VariableParser(node.attribs.test); | ||
var templateName = this.name + '.if' + this.uniqid('template_name'); | ||
var templateName; | ||
if (node.attribs.name) { | ||
templateName = node.attribs.name | ||
} else { | ||
templateName = this.name + '.if' + this.uniqid('template_name'); | ||
} | ||
var childrenName = 'child' + this.uniqid('children_name'); | ||
var placeholder = 'if' + this.uniqid('placeholder'); | ||
this.declarations.push(placeholder + " = document.createComment('if')"); | ||
this.subTemplates.push(new Compiler(templateName, node.children)); | ||
var placeholder, parentNode = this.lookUpOnlyOneChild(node); | ||
if (parentNode) { | ||
placeholder = parentNode.nodeName; | ||
} else { | ||
placeholder = 'if' + this.uniqid('placeholder'); | ||
this.declarations.push(placeholder + " = document.createComment('if')"); | ||
} | ||
if (node.children.length > 0) { | ||
this.subTemplates.push(new Compiler(templateName, node.children)); | ||
} | ||
this.declarations.push(childrenName + " = {}"); | ||
@@ -238,3 +263,3 @@ this.variables[variable.name] = variable; | ||
return placeholder; | ||
return parentNode ? null : placeholder; | ||
}; | ||
@@ -244,11 +269,24 @@ | ||
if (!node.attribs.each) { | ||
throw new Error('For node must contains "key" attribute.'); | ||
throw new Error('For node must contains "each" attribute.'); | ||
} | ||
var variable = new VariableParser(node.attribs.each); | ||
var templateName = this.name + '.for' + this.uniqid('template_name'); | ||
var templateName; | ||
if (node.attribs.name) { | ||
templateName = node.attribs.name | ||
} else { | ||
templateName = this.name + '.for' + this.uniqid('template_name'); | ||
} | ||
var childrenName = 'children' + this.uniqid('children_name'); | ||
var placeholder = 'for' + this.uniqid('placeholder'); | ||
this.declarations.push(placeholder + " = document.createComment('for')"); | ||
var placeholder, parentNode = this.lookUpOnlyOneChild(node); | ||
if (parentNode) { | ||
placeholder = parentNode.nodeName; | ||
} else { | ||
placeholder = 'for' + this.uniqid('placeholder'); | ||
this.declarations.push(placeholder + " = document.createComment('for')"); | ||
} | ||
this.subTemplates.push(new Compiler(templateName, node.children)); | ||
@@ -266,3 +304,3 @@ this.declarations.push(childrenName + " = {}"); | ||
return placeholder; | ||
return parentNode ? null : placeholder; | ||
}; | ||
@@ -274,3 +312,2 @@ | ||
var childrenName = 'child' + this.uniqid('children_name'); | ||
var placeholder = 'custom' + this.uniqid('placeholder'); | ||
@@ -281,3 +318,10 @@ if (node.children.length) { | ||
this.declarations.push(placeholder + " = document.createComment('" + customNodeName + "')"); | ||
var placeholder, parentNode = this.lookUpOnlyOneChild(node); | ||
if (parentNode) { | ||
placeholder = parentNode.nodeName; | ||
} else { | ||
placeholder = placeholder = 'custom' + this.uniqid('placeholder'); | ||
this.declarations.push(placeholder + " = document.createComment('" + customNodeName + "')"); | ||
} | ||
this.declarations.push(childrenName + " = {}"); | ||
@@ -294,9 +338,12 @@ this.controlStructures.push( | ||
return placeholder; | ||
return parentNode ? null : placeholder; | ||
}; | ||
Compiler.prototype.lookUpParent = function (node) { | ||
while (node = node.parent) { | ||
if (node.nodeName) { | ||
return node; | ||
Compiler.prototype.lookUpOnlyOneChild = function (node) { | ||
var parent = node.parent; | ||
if (parent) { | ||
if (parent.nodeName) { | ||
if (parent.children.length == 1 && parent.children[0] == node) { | ||
return parent; | ||
} | ||
} | ||
@@ -303,0 +350,0 @@ } |
var htmlparser = require('htmlparser2'); | ||
var drawTree = require('asciitree'); | ||
var through = require('through'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var Compiler = require('./compiler'); | ||
@@ -15,7 +18,8 @@ | ||
var compiler = new Compiler(name, dom); | ||
var code = compiler.compile(); | ||
callback(code); | ||
callback(compiler); | ||
} | ||
}, options); | ||
var parser = new htmlparser.Parser(handler); | ||
var parser = new htmlparser.Parser(handler, { | ||
recognizeSelfClosing: true | ||
}); | ||
parser.write(text); | ||
@@ -25,2 +29,28 @@ parser.done(); | ||
Monkberry.monkberrify = function (file) { | ||
if (/\.(monk|html)$/.test(file)) { | ||
var data = '', stream = through(write, end); | ||
return stream; | ||
} else { | ||
return through(); | ||
} | ||
function write(buf, enc, next) { | ||
var name = path.parse(file).name; | ||
console.log(file); | ||
var text = fs.readFileSync(file, {encoding: 'utf8'}); | ||
Monkberry(name, text, { | ||
normalizeWhitespace: true | ||
}, function (compiler) { | ||
data += compiler.compile(true); | ||
}); | ||
} | ||
function end() { | ||
stream.queue(data); | ||
stream.queue(null); | ||
} | ||
}; | ||
module.exports = Monkberry; |
@@ -5,3 +5,3 @@ var VariableParser = require('./variable'); | ||
if (!delimiters) { | ||
delimiters = '{ }'; | ||
delimiters = '{{ }}'; | ||
} | ||
@@ -44,3 +44,3 @@ | ||
TextParser.prototype.rawText = function () { | ||
return this.textParts.join(' '); | ||
return this.textParts.join(''); | ||
}; | ||
@@ -47,0 +47,0 @@ |
@@ -32,3 +32,3 @@ var | ||
continue; | ||
else if (/[a-z_]/i.test(ch)) | ||
else if (/[a-z0-9_]/i.test(ch)) | ||
push(T_NAME, ch); | ||
@@ -35,0 +35,0 @@ else if (/[\(\)]/.test(ch)) |
@@ -1,2 +0,2 @@ | ||
(function (window) { | ||
(function () { | ||
function Monkberry() { | ||
@@ -27,5 +27,7 @@ this.pool = new Pool(); | ||
for (j = childrenSize, len = data.length; j < len; j++) { | ||
var view = this.render(template, data[j]); | ||
var view = this.render(template); | ||
view.parent = parent; | ||
parent.children.push(view); | ||
view.appendTo(node); | ||
view.update(data[j]); | ||
i = push(children, view); | ||
@@ -52,5 +54,8 @@ | ||
} else if (test) { | ||
var view = this.render(template, data); | ||
var view = this.render(template); | ||
view.parent = parent; | ||
parent.children.push(view); | ||
view.appendTo(node); | ||
view.update(data); | ||
child.ref = view; | ||
@@ -62,4 +67,2 @@ var viewRemove = view.remove; | ||
}; | ||
child.ref = view; | ||
} | ||
@@ -83,2 +86,5 @@ }; | ||
view.parent = null; | ||
view.children = []; | ||
view.appendTo = function (toNode) { | ||
@@ -90,3 +96,4 @@ for (var i = 0, len = view.nodes.length; i < len; i++) { | ||
} else { | ||
throw new Error("Can not insert child view into parent view."); | ||
throw new Error("Can not insert child view into parent node." + | ||
"You need append your view first and then update."); | ||
} | ||
@@ -99,3 +106,3 @@ } else { | ||
view.root = function (toNode) { | ||
view.getDom = function (toNode) { | ||
if (view.nodes.length == 1) { | ||
@@ -113,7 +120,17 @@ return view.nodes[0]; | ||
view.remove = function () { | ||
// Remove appended nodes | ||
var i = view.nodes.length; | ||
while (i--) { | ||
console.log('Remove', view.nodes[i]); | ||
view.nodes[i].parentNode.removeChild(view.nodes[i]); | ||
} | ||
// Remove all children views | ||
i = view.children.length; | ||
while (i--) { | ||
view.children[i].remove(); | ||
} | ||
// Remove this view from parent views children. | ||
if (view.parent) { | ||
i = view.parent.children.indexOf(view); | ||
view.parent.children.splice(i, 1); | ||
} | ||
self.pool.push(name, view); | ||
@@ -175,2 +192,3 @@ }; | ||
for (var i in map) if (map.hasOwnProperty(i)) { | ||
i = parseInt(i); | ||
if (i > maximum) { | ||
@@ -201,3 +219,7 @@ maximum = i; | ||
window.monkberry = new Monkberry(); | ||
})(window); | ||
if (typeof module !== "undefined") { | ||
module.exports = new Monkberry(); | ||
} else { | ||
window.monkberry = new Monkberry(); | ||
} | ||
})(); |
{ | ||
"name": "monkberry", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "DOM Template Engine", | ||
"bin": "./bin/monkberry", | ||
"main": "lib/index.js", | ||
@@ -9,2 +10,3 @@ "scripts": { | ||
}, | ||
"browser": "./monkberry.js", | ||
"repository": { | ||
@@ -27,4 +29,6 @@ "type": "git", | ||
"asciitree": "^1.0.0", | ||
"htmlparser2": "^3.8.3" | ||
"commander": "^2.8.1", | ||
"htmlparser2": "^3.8.3", | ||
"through": "^2.3.7" | ||
} | ||
} |
142
test.js
@@ -6,10 +6,8 @@ (function (monkberry, filters, document, undefined) { | ||
var ol0 = document.createElement('ol'), | ||
for0 = document.createComment('for'), | ||
children0 = {}, | ||
if1 = document.createComment('if'), | ||
child1 = {}, | ||
text1 = document.createTextNode(" "); | ||
if0 = document.createComment('if'), | ||
child1 = {}; | ||
// Construct dom | ||
ol0.appendChild(for0); | ||
ol0.setAttribute('class', " list"); | ||
@@ -19,3 +17,6 @@ // Create setters | ||
foo: function (value) { | ||
monkberry.foreach(view, for0, children0, 'test.for0', value); | ||
monkberry.foreach(view, ol0, children0, 'test.for0', value); | ||
}, | ||
active: function (value) { | ||
ol0.setAttribute('class', value + " list"); | ||
} | ||
@@ -28,4 +29,5 @@ }; | ||
(t = data.foo) !== undefined && set.foo(t); | ||
(t = data.active) !== undefined && set.active(t); | ||
if (data.h !== undefined) | ||
monkberry.iftest(view, if1, child1, 'test.if1', data, data.h); | ||
monkberry.iftest(view, if0, child1, 'test.if1', data, data.h); | ||
} | ||
@@ -35,5 +37,35 @@ }; | ||
// Set root nodes | ||
view.nodes = [ol0, if1, text1]; | ||
view.nodes = [ol0, if0, document.createTextNode(" ")]; | ||
return view; | ||
}, | ||
"foo": function () { | ||
// Create elements | ||
var p0 = document.createElement('p'), | ||
text1 = document.createTextNode(" The foo: "), | ||
br2 = document.createElement('br'), | ||
br3 = document.createElement('br'); | ||
// Construct dom | ||
p0.appendChild(text1); | ||
p0.appendChild(br2); | ||
p0.appendChild(br3); | ||
// Create setters | ||
var set = { | ||
h: function (value) { | ||
text1.nodeValue = " The foo: " + value; | ||
} | ||
}; | ||
// Create view | ||
var view = { | ||
update: function (data) { | ||
var t; | ||
(t = data.h) !== undefined && set.h(t); | ||
} | ||
}; | ||
// Set root nodes | ||
view.nodes = [p0, document.createTextNode(" ")]; | ||
return view; | ||
}, | ||
"test.for0": function () { | ||
@@ -46,7 +78,4 @@ // Create elements | ||
child1 = {}, | ||
custom2 = document.createComment('hh'), | ||
child2 = {}, | ||
ul1 = document.createElement('ul'), | ||
if3 = document.createComment('if'), | ||
child3 = {}; | ||
if2 = document.createComment('if'), | ||
child2 = {}; | ||
@@ -56,4 +85,2 @@ // Construct dom | ||
li0.appendChild(if1); | ||
li0.appendChild(custom2); | ||
ul1.appendChild(if3); | ||
@@ -67,5 +94,4 @@ // Create view | ||
monkberry.iftest(view, if1, child1, 'test.for0.if1', data, data.value); | ||
monkberry.iftest(view, custom2, child2, 'hh', data, 1); | ||
if (data.li !== undefined) | ||
monkberry.iftest(view, if3, child3, 'test.for0.if2', data, data.li); | ||
monkberry.iftest(view, if2, child2, 'test.for0.if2', data, data.li); | ||
} | ||
@@ -75,3 +101,3 @@ }; | ||
// Set root nodes | ||
view.nodes = [li0, ul1]; | ||
view.nodes = [li0, if2]; | ||
return view; | ||
@@ -81,5 +107,3 @@ }, | ||
// Create elements | ||
var value_value0 = '', | ||
value_value1 = '', | ||
text0 = document.createTextNode(" "); | ||
var text0 = document.createTextNode(" "); | ||
@@ -89,5 +113,3 @@ // Create setters | ||
value: function (value) { | ||
value_value0 = value; | ||
value_value1 = value; | ||
text0.nodeValue = " " + value_value0 + " " + value_value1 + " "; | ||
text0.nodeValue = " " + value + " "; | ||
} | ||
@@ -109,3 +131,3 @@ }; | ||
// Create elements | ||
var text0 = document.createTextNode(" ~ ~ "); | ||
var text0 = document.createTextNode(" / "); | ||
@@ -115,3 +137,3 @@ // Create setters | ||
value: function (value) { | ||
text0.nodeValue = " ~" + value + "~ "; | ||
text0.nodeValue = " /" + value + " "; | ||
} | ||
@@ -133,13 +155,9 @@ }; | ||
// Create elements | ||
var li0 = document.createElement('li'), | ||
for0 = document.createComment('for'), | ||
var for0 = document.createComment('for'), | ||
children0 = {}; | ||
// Construct dom | ||
li0.appendChild(for0); | ||
// Create setters | ||
var set = { | ||
li: function (value) { | ||
monkberry.foreach(view, for0, children0, 'test.for0.if2.for0', value); | ||
monkberry.foreach(view, for0, children0, 'sub', value); | ||
} | ||
@@ -156,12 +174,13 @@ }; | ||
// Set root nodes | ||
view.nodes = [li0]; | ||
view.nodes = [for0]; | ||
return view; | ||
}, | ||
"test.for0.if2.for0": function () { | ||
"sub": function () { | ||
// Create elements | ||
var b0 = document.createElement('b'), | ||
text1 = document.createTextNode("subli "); | ||
var li0 = document.createElement('li'), | ||
b1 = document.createElement('b'); | ||
// Construct dom | ||
b0.appendChild(text1); | ||
b1.appendChild(document.createTextNode("Li")); | ||
li0.appendChild(b1); | ||
@@ -176,3 +195,3 @@ // Create view | ||
// Set root nodes | ||
view.nodes = [b0]; | ||
view.nodes = [li0]; | ||
return view; | ||
@@ -182,32 +201,32 @@ }, | ||
// Create elements | ||
var div0 = document.createElement('div'), | ||
custom0 = document.createComment('hh'), | ||
child0 = {}; | ||
var custom0 = document.createComment('foo'), | ||
child0 = {}, | ||
img0 = document.createElement('img'), | ||
img_value0 = '', | ||
size_value0 = '', | ||
img1 = document.createElement('img'); | ||
// Construct dom | ||
div0.appendChild(custom0); | ||
img0.setAttribute('alt', ""); | ||
img1.setAttribute('src', "http://google.com/value.png"); | ||
img1.setAttribute('alt', ""); | ||
// Create view | ||
var view = { | ||
update: function (data) { | ||
monkberry.iftest(view, custom0, child0, 'hh', data, 1); | ||
// Create setters | ||
var set = { | ||
img: function (value) { | ||
img_value0 = value; | ||
img0.setAttribute('src', "http://google.com/" + img_value0 + "-" + size_value0 + ".png"); | ||
}, | ||
size: function (value) { | ||
size_value0 = value; | ||
img0.setAttribute('src', "http://google.com/" + img_value0 + "-" + size_value0 + ".png"); | ||
} | ||
}; | ||
// Set root nodes | ||
view.nodes = [div0]; | ||
return view; | ||
}, | ||
"hh": function () { | ||
// Create elements | ||
var h20 = document.createElement('h2'), | ||
text1 = document.createTextNode("Yes"); | ||
// Construct dom | ||
h20.appendChild(text1); | ||
// Create view | ||
var view = { | ||
update: function (data) { | ||
var t; | ||
(t = data.img) !== undefined && set.img(t); | ||
(t = data.size) !== undefined && set.size(t); | ||
monkberry.iftest(view, custom0, child0, 'foo', data, 1); | ||
} | ||
@@ -217,3 +236,3 @@ }; | ||
// Set root nodes | ||
view.nodes = [h20]; | ||
view.nodes = [custom0, img0, img1]; | ||
return view; | ||
@@ -223,2 +242,1 @@ } | ||
})(monkberry, monkberry.filters, window.document, void 0); | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
85823
958
4
1
+ Addedcommander@^2.8.1
+ Addedthrough@^2.3.7
+ Addedcommander@2.20.3(transitive)
+ Addedthrough@2.3.8(transitive)