Comparing version 3.2.1 to 3.4.0
'use strict'; | ||
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })(); | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
@@ -35,2 +35,13 @@ Object.defineProperty(exports, "__esModule", { | ||
ast.SpreadAttributeNode.prototype.compile = function (figure, nodeName) { | ||
var _this2 = this; | ||
figure.root.addFunction('__spread', (0, _sourceNode.sourceNode)(null, ['function (node, attr) {\n', ' for (var property in attr) if (attr.hasOwnProperty(property)) {\n', ' if (property in ' + (0, _utils.esc)((0, _utils.arrayToObject)(plainAttributes)) + ') {\n', ' node[property] = attr[property];\n', ' } else {\n', ' node.setAttribute(property, attr[property]);\n', ' }\n', ' }\n', '}'])); | ||
var attr = this.identifier.name; | ||
figure.addUpdater(this.loc, [attr], function () { | ||
return (0, _sourceNode.sourceNode)(_this2.loc, [' __spread(' + nodeName + ', ' + attr + ')']); | ||
}); | ||
}; | ||
function attr(loc, nodeName, attrName, value) { | ||
@@ -59,2 +70,7 @@ if (plainAttributes.indexOf(attrName) != -1) { | ||
defaults.push(node.compile()); | ||
} else if (node.type == 'ExpressionStatement' && node.expression.type == 'LogicalExpression' && node.expression.operator == '||') { | ||
// Add as default right side of "||" expression if there are no variables. | ||
if ((0, _variable.collectVariables)(node.expression.right) == 0) { | ||
defaults.push(node.expression.right.compile()); | ||
} | ||
} | ||
@@ -68,2 +84,3 @@ }; | ||
expr = extract(this.body[0]); | ||
pushDefaults(this.body[0]); | ||
} else if (this.body.length >= 2) { | ||
@@ -70,0 +87,0 @@ |
'use strict'; | ||
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })(); | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
@@ -5,0 +5,0 @@ Object.defineProperty(exports, "__esModule", { |
'use strict'; | ||
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })(); | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
@@ -27,5 +27,6 @@ Object.defineProperty(exports, "__esModule", { | ||
var customData = []; | ||
var defaultData = []; | ||
var hasUpdater = false; | ||
var variables = []; | ||
// Collect info about variables and attributes. | ||
var _iteratorNormalCompletion = true; | ||
@@ -36,14 +37,40 @@ var _didIteratorError = false; | ||
try { | ||
for (var _iterator = this.attributes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _loop = function _loop() { | ||
var attr = _step.value; | ||
var _attr$compileToExpres = attr.compileToExpression(); | ||
if (attr.type == 'SpreadAttribute') { | ||
var _attr$compileToExpres2 = _slicedToArray(_attr$compileToExpres, 1); | ||
figure.addUpdater(_this.loc, [attr.identifier.name], function () { | ||
return (0, _sourceNode.sourceNode)(_this.loc, [' monkberry.insert(view, ' + placeholder + ', ' + childName + ', \'' + templateName + '\', ' + attr.identifier.name + ', true)']); | ||
}); | ||
hasUpdater = true; | ||
} else { | ||
(function () { | ||
// TODO: Add support for default value in attributes attr={{ value || 'default' }}. | ||
var expr = _attr$compileToExpres2[0]; | ||
var _attr$compileToExpres = attr.compileToExpression(); | ||
variables = variables.concat((0, _variable.collectVariables)(expr)); | ||
var _attr$compileToExpres2 = _slicedToArray(_attr$compileToExpres, 1); | ||
customData.push((0, _sourceNode.sourceNode)(this.loc, ["'", attr.name, "': ", expr.compile()])); | ||
var expr = _attr$compileToExpres2[0]; | ||
var variables = (0, _variable.collectVariables)(expr); | ||
var data = (0, _sourceNode.sourceNode)(_this.loc, ['\'' + attr.name + '\': ' + expr.compile()]); | ||
if (variables.length == 0) { | ||
defaultData.push(data); | ||
} else { | ||
figure.addUpdater(_this.loc, variables, function () { | ||
return (0, _sourceNode.sourceNode)(_this.loc, [' monkberry.insert(view, ' + placeholder + ', ' + childName + ', \'' + templateName + '\', {' + data + '}, true)']); | ||
}); | ||
hasUpdater = true; | ||
} | ||
})(); | ||
} | ||
}; | ||
for (var _iterator = this.attributes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
_loop(); | ||
} | ||
@@ -65,16 +92,14 @@ } catch (err) { | ||
variables = (0, _utils.unique)(variables); | ||
if (!hasUpdater || defaultData.length > 0) { | ||
var data = '{}'; | ||
if (variables.length == 0) { | ||
if (defaultData.length > 0) { | ||
data = '{' + defaultData.join(', ') + '}'; | ||
} | ||
figure.renderActions.push((0, _sourceNode.sourceNode)(this.loc, [" ", "monkberry.insert(view, ", placeholder, ", ", childName, ", ", '\'' + templateName + '\', ', "{", customData.join(', '), "}, ", "true", ");"])); | ||
} else { | ||
figure.addUpdater(this.loc, variables, function () { | ||
return (0, _sourceNode.sourceNode)(_this.loc, [" ", "monkberry.insert(view, ", placeholder, ", ", childName, ", ", '\'' + templateName + '\', ', "{", customData.join(', '), "}, ", "true", ")"]); | ||
}); | ||
figure.renderActions.push((0, _sourceNode.sourceNode)(this.loc, [' monkberry.insert(view, ' + placeholder + ', ' + childName + ', \'' + templateName + '\', ' + data + ', true);'])); | ||
} | ||
if (this.body.length > 0) { | ||
figure.subFigures.push((0, _figure.createFigure)(templateName, this.body)); | ||
figure.subFigures.push(figure.createFigure(templateName, this.body)); | ||
} | ||
@@ -88,6 +113,4 @@ | ||
var _figure = require('../../figure'); | ||
var _variable = require('../expression/variable'); | ||
var _utils = require('../../utils'); |
@@ -13,4 +13,13 @@ 'use strict'; | ||
figure.declarations.push((0, _sourceNode.sourceNode)(null, ["var ", this.nodeName, " = document.createTextNode('');"])); | ||
var defaultValue = '\'\''; | ||
if (this.expression.type == 'LogicalExpression' && this.expression.operator == '||') { | ||
// Add as default right side of "||" expression if there are no variables. | ||
if ((0, _variable.collectVariables)(this.expression.right) == 0) { | ||
defaultValue = this.expression.right.compile(); | ||
} | ||
} | ||
figure.declarations.push((0, _sourceNode.sourceNode)(null, 'var ' + this.nodeName + ' = document.createTextNode(' + defaultValue + ');')); | ||
var variables = (0, _variable.collectVariables)(this.expression); | ||
@@ -30,3 +39,3 @@ | ||
ast.FilterExpressionNode.prototype.compile = function () { | ||
var sn = (0, _sourceNode.sourceNode)(this.loc, ['filters.', this.callee.compile(), '(']); | ||
var sn = (0, _sourceNode.sourceNode)(this.loc, ['__filters.', this.callee.compile(), '(']); | ||
@@ -33,0 +42,0 @@ for (var i = 0; i < this.arguments.length; i++) { |
@@ -37,3 +37,3 @@ 'use strict'; | ||
if (this.body.length > 0) { | ||
figure.subFigures.push((0, _figure.createFigure)(templateName, this.body)); | ||
figure.subFigures.push(figure.createFigure(templateName, this.body)); | ||
} | ||
@@ -72,6 +72,4 @@ | ||
var _figure = require('../figure'); | ||
var _variable = require('./expression/variable'); | ||
var _utils = require('../utils'); |
@@ -59,4 +59,2 @@ 'use strict'; | ||
var _figure = require('../figure'); | ||
var _variable = require('./expression/variable'); | ||
@@ -73,3 +71,3 @@ | ||
function compileBody(figure, loc, templateName, childName, body, variablesOfExpression) { | ||
figure.subFigures.push((0, _figure.createFigure)(templateName, body)); | ||
figure.subFigures.push(figure.createFigure(templateName, body)); | ||
@@ -76,0 +74,0 @@ var variablesOfBody = (0, _variable.collectVariables)(body); |
'use strict'; | ||
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })(); | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -18,3 +18,3 @@ Object.defineProperty(exports, "__esModule", { | ||
var Updater = exports.Updater = (function () { | ||
var Updater = exports.Updater = function () { | ||
function Updater(variables) { | ||
@@ -108,5 +108,5 @@ _classCallCheck(this, Updater); | ||
return Updater; | ||
})(); | ||
}(); | ||
var Complex = (function () { | ||
var Complex = function () { | ||
function Complex(params) { | ||
@@ -147,2 +147,2 @@ _classCallCheck(this, Complex); | ||
return Complex; | ||
})(); | ||
}(); |
'use strict'; | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -9,3 +9,2 @@ Object.defineProperty(exports, "__esModule", { | ||
exports.Figure = undefined; | ||
exports.createFigure = createFigure; | ||
@@ -20,17 +19,11 @@ var _sourceNode = require('./compiler/sourceNode'); | ||
function createFigure(name, nodes) { | ||
var figure = new Figure(name); | ||
figure.children = (0, _utils.map)(nodes, function (node) { | ||
return node.compile(figure); | ||
}); | ||
return figure; | ||
} | ||
var Figure = exports.Figure = (function () { | ||
function Figure(name) { | ||
var Figure = exports.Figure = function () { | ||
function Figure(name, root) { | ||
_classCallCheck(this, Figure); | ||
this.name = name; | ||
this.root = root; | ||
this.uniqCounters = {}; | ||
this.children = []; | ||
this.functions = {}; | ||
this.declarations = []; | ||
@@ -47,2 +40,11 @@ this.construct = []; | ||
_createClass(Figure, [{ | ||
key: 'createFigure', | ||
value: function createFigure(name, nodes) { | ||
var figure = new Figure(name, this.root); | ||
figure.children = (0, _utils.map)(nodes, function (node) { | ||
return node.compile(figure); | ||
}); | ||
return figure; | ||
} | ||
}, { | ||
key: 'compile', | ||
@@ -88,2 +90,17 @@ value: function compile() { | ||
}, { | ||
key: 'compileFunctions', | ||
value: function compileFunctions() { | ||
var _this = this; | ||
if (Object.keys(this.functions).length > 0) { | ||
var defn = []; | ||
Object.keys(this.functions).forEach(function (key) { | ||
defn.push((0, _sourceNode.sourceNode)(null, key + ' = ' + _this.functions[key])); | ||
}); | ||
return (0, _sourceNode.sourceNode)(null, 'var ').add((0, _sourceNode.join)(defn, ',\n')).add(';\n'); | ||
} else { | ||
return (0, _sourceNode.sourceNode)(null, ''); | ||
} | ||
} | ||
}, { | ||
key: 'compileDeclarations', | ||
@@ -101,3 +118,3 @@ value: function compileDeclarations() { | ||
value: function compileComplexUpdaters() { | ||
var _this = this; | ||
var _this2 = this; | ||
@@ -107,3 +124,3 @@ var parts = []; | ||
Object.keys(this.complexUpdaters).forEach(function (key) { | ||
parts.push((0, _sourceNode.join)([' ', key, ': ', _this.complexUpdaters[key].compile()])); | ||
parts.push((0, _sourceNode.join)([' ', key, ': ', _this2.complexUpdaters[key].compile()])); | ||
}); | ||
@@ -116,3 +133,3 @@ | ||
value: function compileUpdaters() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
@@ -122,3 +139,3 @@ var parts = []; | ||
Object.keys(this.updaters).forEach(function (key) { | ||
parts.push((0, _sourceNode.join)([' ', key, ': ', _this2.updaters[key].compile()])); | ||
parts.push((0, _sourceNode.join)([' ', key, ': ', _this3.updaters[key].compile()])); | ||
}); | ||
@@ -160,2 +177,9 @@ | ||
}, { | ||
key: 'addFunction', | ||
value: function addFunction(name, source) { | ||
if (!(name in this.functions)) { | ||
this.functions[name] = source; | ||
} | ||
} | ||
}, { | ||
key: 'addUpdater', | ||
@@ -226,2 +250,2 @@ value: function addUpdater(loc, variables, callback) { | ||
return Figure; | ||
})(); | ||
}(); |
114
lib/graph.js
@@ -15,59 +15,63 @@ 'use strict'; | ||
function drawGraph(ast) { | ||
return (0, _asciitree2.default)(ast.body[0], function (node) { | ||
if (node.type) { | ||
switch (node.type) { | ||
case 'BinaryExpression': | ||
return '( ' + node.operator + ' )'; | ||
case 'Identifier': | ||
return node.name; | ||
case 'Literal': | ||
return node.value.toString(); | ||
case 'Accessor': | ||
return '.' + node.name; | ||
case 'Element': | ||
return '<' + node.name + '>'; | ||
case 'Text': | ||
return '"' + node.text.replace(/[\s]+/g, '') + '"'; | ||
default: | ||
return node.type; | ||
return ast.body.map(function (root) { | ||
return (0, _asciitree2.default)(root, function (node) { | ||
if (node.type) { | ||
switch (node.type) { | ||
case 'BinaryExpression': | ||
return '( ' + node.operator + ' )'; | ||
case 'Identifier': | ||
return node.name; | ||
case 'Literal': | ||
return node.value.toString(); | ||
case 'Accessor': | ||
return '.' + node.name; | ||
case 'Element': | ||
return '<' + node.name + '>'; | ||
case 'Text': | ||
return '"' + node.text.replace(/[\s]+/g, '') + '"'; | ||
default: | ||
return node.type; | ||
} | ||
} else { | ||
return JSON.stringify(node); | ||
} | ||
} else { | ||
return JSON.stringify(node); | ||
} | ||
}, function (node) { | ||
if (node instanceof Object) { | ||
switch (node.type) { | ||
case 'BinaryExpression': | ||
return [node.left, node.right]; | ||
case 'Identifier': | ||
return []; | ||
case 'Literal': | ||
return []; | ||
case 'Accessor': | ||
return []; | ||
case 'Element': | ||
return node.body.concat(node.attributes); | ||
case 'Text': | ||
return []; | ||
case 'ObjectExpression': | ||
return []; | ||
case undefined: | ||
return []; | ||
default: | ||
return Object.keys(node).filter(function (key) { | ||
return ['type', 'loc'].indexOf(key) == -1; | ||
}).map(function (key) { | ||
if (node[key]) { | ||
return [].concat(node[key]); | ||
} else { | ||
return []; | ||
} | ||
}).reduce(function (a, b) { | ||
return a.concat(b); | ||
}); | ||
}, function (node) { | ||
if (node instanceof Object) { | ||
switch (node.type) { | ||
case 'BinaryExpression': | ||
return [node.left, node.right]; | ||
case 'Identifier': | ||
return []; | ||
case 'Literal': | ||
return []; | ||
case 'Accessor': | ||
return []; | ||
case 'Element': | ||
return node.body.concat(node.attributes); | ||
case 'Text': | ||
return []; | ||
case 'ObjectExpression': | ||
return []; | ||
case undefined: | ||
return []; | ||
default: | ||
return Object.keys(node).filter(function (key) { | ||
return ['type', 'loc'].indexOf(key) == -1; | ||
}).map(function (key) { | ||
if (node[key]) { | ||
return [].concat(node[key]); | ||
} else { | ||
return []; | ||
} | ||
}).reduce(function (a, b) { | ||
return a.concat(b); | ||
}); | ||
} | ||
} else { | ||
return []; | ||
} | ||
} else { | ||
return []; | ||
} | ||
}); | ||
}); | ||
}).filter(function (tree) { | ||
return tree != '""'; | ||
}).join('\n\n'); | ||
} |
'use strict'; | ||
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })(); | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
@@ -16,2 +16,4 @@ Object.defineProperty(exports, "__esModule", { | ||
var _root = require('./figure/root'); | ||
var _visitor = require('./visitor'); | ||
@@ -55,2 +57,10 @@ | ||
var _comment = require('./compiler/comment'); | ||
var _comment2 = _interopRequireDefault(_comment); | ||
var _import = require('./compiler/import'); | ||
var _import2 = _interopRequireDefault(_import); | ||
var _if = require('./compiler/if'); | ||
@@ -64,4 +74,10 @@ | ||
var _unsafe = require('./compiler/unsafe'); | ||
var _unsafe2 = _interopRequireDefault(_unsafe); | ||
var _whitespace = require('./optimize/whitespace'); | ||
var _graph = require('./graph'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -71,3 +87,3 @@ | ||
var Compiler = exports.Compiler = (function () { | ||
var Compiler = exports.Compiler = function () { | ||
function Compiler() { | ||
@@ -107,4 +123,7 @@ _classCallCheck(this, Compiler); | ||
(0, _text2.default)(parser.ast); | ||
(0, _comment2.default)(parser.ast); | ||
(0, _import2.default)(parser.ast); | ||
(0, _if2.default)(parser.ast); | ||
(0, _for2.default)(parser.ast); | ||
(0, _unsafe2.default)(parser.ast); | ||
(0, _visitor.visitor)(parser.ast); | ||
@@ -128,2 +147,3 @@ }); | ||
var root = new _root.Root(); | ||
var figures = (0, _sourceNode.sourceNode)(null, ''); | ||
@@ -154,3 +174,4 @@ | ||
var figure = new _figure.Figure(this.getTemplateName(name)); | ||
var figure = new _figure.Figure(this.getTemplateName(name), root); | ||
if (asLibrary) { | ||
@@ -182,5 +203,5 @@ figure.perceivedAsLibrary = true; | ||
if (asModule) { | ||
output.add('module.exports = function (monkberry, document) {\n').add('var filters = monkberry.filters;\n').add('return {\n').add(figures.join(',\n')).add('};\n').add('};\n'); | ||
output.add('module.exports = function (monkberry, document) {\n').add('var __filters = monkberry.filters;\n').add(root.compile()).add('return {\n').add(figures.join(',\n')).add('};\n').add('};\n'); | ||
} else { | ||
output.add('(function (monkberry, filters, document, undefined) {\n').add('monkberry.mount({\n').add(figures.join(',\n')).add('\n});\n').add('})(monkberry, monkberry.filters, window.document, void 0);\n'); | ||
output.add('(function (monkberry, document) {\n').add('var __filters = monkberry.filters;\n').add(root.compile()).add('monkberry.mount({\n').add(figures.join(',\n')).add('\n});\n').add('})(monkberry, window.document);\n'); | ||
} | ||
@@ -191,2 +212,34 @@ | ||
}, { | ||
key: 'drawAstTree', | ||
value: function drawAstTree() { | ||
var _this3 = this; | ||
this.enhanceParsers(); | ||
if (this.sources.length > 0) { | ||
var _sources$ = _slicedToArray(this.sources[0], 3); | ||
var name = _sources$[0]; | ||
var code = _sources$[1]; | ||
var parserType = _sources$[2]; | ||
if (parserType in this.parsers) { | ||
var parser = this.parsers[parserType]; | ||
var ast = parser.parse(code, name); | ||
// Transforms | ||
Object.keys(this.transforms).forEach(function (key) { | ||
return _this3.transforms[key](ast, parser); | ||
}); | ||
return (0, _graph.drawGraph)(ast); | ||
} else { | ||
throw new Error('Unknown parser type: ' + parserType + '.'); | ||
} | ||
} else { | ||
throw new Error('No sources.'); | ||
} | ||
} | ||
}, { | ||
key: 'getTemplateName', | ||
@@ -199,2 +252,2 @@ value: function getTemplateName(name) { | ||
return Compiler; | ||
})(); | ||
}(); |
@@ -12,2 +12,3 @@ 'use strict'; | ||
exports.lookUpOnlyOneChild = lookUpOnlyOneChild; | ||
exports.arrayToObject = arrayToObject; | ||
function esc(str) { | ||
@@ -57,2 +58,12 @@ return JSON.stringify(str); | ||
return null; | ||
} | ||
function arrayToObject(array) { | ||
var value = arguments.length <= 1 || arguments[1] === undefined ? 1 : arguments[1]; | ||
var obj = {}; | ||
for (var i = 0; i < array.length; i++) { | ||
obj[array[i]] = value; | ||
} | ||
return obj; | ||
} |
@@ -20,2 +20,6 @@ "use strict"; | ||
ast.CommentNode.prototype.visit = function (callback) { | ||
callback(this); | ||
}; | ||
ast.ElementNode.prototype.visit = function (callback) { | ||
@@ -43,2 +47,7 @@ callback(this); | ||
ast.SpreadAttributeNode.prototype.visit = function (callback) { | ||
callback(this); | ||
this.identifier.visit(callback); | ||
}; | ||
ast.ExpressionStatementNode.prototype.visit = function (callback) { | ||
@@ -49,2 +58,6 @@ callback(this); | ||
ast.ImportStatementNode.prototype.visit = function (callback) { | ||
callback(this); | ||
}; | ||
ast.IfStatementNode.prototype.visit = function (callback) { | ||
@@ -76,2 +89,6 @@ callback(this); | ||
ast.UnsafeStatementNode.prototype.visit = function (callback) { | ||
callback(this); | ||
}; | ||
ast.FilterExpressionNode.prototype.visit = function (callback) { | ||
@@ -78,0 +95,0 @@ callback(this); |
@@ -248,9 +248,11 @@ (function (document) { | ||
Monkberry.View.prototype.insertBefore = function (toNode) { | ||
for (var i = 0, len = this.nodes.length; i < len; i++) { | ||
if (toNode.parentNode) { | ||
if (toNode.parentNode) { | ||
for (var i = 0, len = this.nodes.length; i < len; i++) { | ||
toNode.parentNode.insertBefore(this.nodes[i], toNode); | ||
} else { | ||
throw new Error("Can not insert child view into parent node." + | ||
"You need append your view first and then update."); | ||
} | ||
} else { | ||
throw new Error( | ||
"Can not insert child view into parent node. " + | ||
"You need append your view first and then update." | ||
); | ||
} | ||
@@ -257,0 +259,0 @@ }; |
{ | ||
"name": "monkberry", | ||
"version": "3.2.1", | ||
"version": "3.4.0", | ||
"description": "JavaScript DOM Template Engine", | ||
@@ -38,14 +38,14 @@ "bin": "bin/monkberry", | ||
"commander": "^2.9.0", | ||
"monkberry-parser": "^3.2.0", | ||
"source-map": "^0.5.3" | ||
"monkberry-parser": "^3.4.0", | ||
"source-map": "^0.5.3", | ||
"asciitree": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"asciitree": "^1.0.0", | ||
"babel-cli": "^6.2.4", | ||
"babel-preset-es2015": "^6.2.4", | ||
"babel-cli": "^6.4.0", | ||
"babel-preset-es2015": "^6.3.13", | ||
"doctoc": "^0.15.0", | ||
"jasmine": "^2.3.2", | ||
"testem": "^0.9.11", | ||
"jasmine": "^2.4.1", | ||
"testem": "^1.0.0", | ||
"through": "^2.3.8" | ||
} | ||
} |
114
README.md
@@ -31,4 +31,7 @@ # Monkberry - JavaScript template engine | ||
- [For](#for) | ||
- [Default values](#default-values) | ||
- [Filters](#filters) | ||
- [Custom tags](#custom-tags) | ||
- [Spread attributes](#spread-attributes) | ||
- [Importing](#importing) | ||
- [Event Handling](#event-handling) | ||
@@ -40,2 +43,3 @@ - [Globals](#globals) | ||
- [Parsers](#parsers) | ||
- [Comments](#comments) | ||
- [API Reference](#api-reference) | ||
@@ -239,2 +243,38 @@ - [Monkberry](#monkberry) | ||
### Default values | ||
Render of view contains two phase: node creation and update of node contents with data. | ||
```js | ||
var view = monkberry.render('template', data); | ||
// Equals to: | ||
var view = monkberry.render('template'); | ||
view.update(data); | ||
``` | ||
Some times data for view does not available and it's use full to place come data as default. | ||
Best way to do it is use logical _OR_ operator `||`. | ||
```twig | ||
<div class="foo {{ modify || 'baz' }}"> | ||
{{ content || "No content" }} | ||
</div> | ||
``` | ||
In this case on first phase of render view will be filled with default data: | ||
```twig | ||
<div class="foo baz"> | ||
No content | ||
</div> | ||
``` | ||
Note if you will use some variable in right side of _OR_ operator, what can't be used as default data. | ||
```twig | ||
{{ content || "No content" + foo }} | ||
``` | ||
### Filters | ||
@@ -317,2 +357,57 @@ | ||
### Spread attributes | ||
Spread attributes allow easily convert object into node attributes. | ||
The properties of the object that you pass in are copied onto the node's attributes. | ||
```twig | ||
<input {{...attr}}/> | ||
``` | ||
```js | ||
var view = monkberry.render('template', {attr: { | ||
id: 'foo', | ||
value: 'baz' | ||
}}); | ||
``` | ||
You can combine it with other attributes. | ||
```twig | ||
<input {{...attr}} value={{ value }}/> | ||
``` | ||
Note what later updates of attributes override previous ones. | ||
```js | ||
view.update({value: 'baz'}); | ||
// ... | ||
view.update({attr: {value: 'new baz'}}); // Will override previous value. | ||
``` | ||
Spread operator also works well with custom attributes. In fact, this is best way to pass data into custom tag. | ||
```twig | ||
<my-tag {{...attr}}/> | ||
``` | ||
```twig | ||
<my-tag> | ||
<input type={{ type }} value={{ value }}> | ||
</my-tag> | ||
``` | ||
### Importing | ||
It is possible to require template within another template. | ||
```twig | ||
{% import './path/to/template.html' %} | ||
<template/> | ||
``` | ||
Import statement will require that template and automatically mount it to monkberry. | ||
### Event Handling | ||
@@ -416,4 +511,23 @@ | ||
### Unsafe | ||
Monkberry escape all inserted variables by default. But if some times you want to insert | ||
some HTML template via variable you can you _unsafe_ statement which is using `innerHTML`. | ||
Improper use of the _unsafe_ statement can open you up to a [cross-site scripting (XSS)](https://en.wikipedia.org/wiki/Cross-site_scripting) attack. | ||
```twig | ||
{% unsafe '<a href="XSS">...</a>' %} | ||
{% unsafe html %} | ||
``` | ||
### Comments | ||
You can use standard html comments. | ||
```twig | ||
<!-- Comment does here --> | ||
``` | ||
Comments will be cut out from template. | ||
## API Reference | ||
@@ -420,0 +534,0 @@ |
Sorry, the diff of this file is not supported yet
96658
6
31
2045
638
4
+ Addedasciitree@^1.0.2
+ Addedasciitree@1.0.2(transitive)
Updatedmonkberry-parser@^3.4.0