mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-10 06:52:05 +01:00
renamed HtmlBuilder to html. implemented tests
This commit is contained in:
@@ -6,6 +6,7 @@ module.exports = function(config) {
|
||||
'src/js/core/core.js',
|
||||
'src/js/core/is.js',
|
||||
'src/js/core/di.js',
|
||||
'src/js/core/htmlBuilder.js',
|
||||
'src/js/core/should.js',
|
||||
'src/js/core/appShell.js',
|
||||
'src/js/core/observable.js',
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
(function(app, core){
|
||||
"use strict";
|
||||
|
||||
app.set('html', core.HtmlBuilder);
|
||||
app.set('html', core.html);
|
||||
app.set('is', core.is);
|
||||
app.set('should', core.should);
|
||||
app.set('bindr', core.bindr);
|
||||
|
||||
@@ -31,57 +31,21 @@
|
||||
var els = containerEl.querySelectorAll('[data-template]');
|
||||
var store = app.templates;
|
||||
|
||||
Array.prototype.forEach.call(els, function(element) {
|
||||
Array.prototype.forEach.call(els, function (element) {
|
||||
var key = element.getAttribute('data-template');
|
||||
|
||||
if(store[key] instanceof Template) {
|
||||
if (store[key] instanceof Template) {
|
||||
console.warn(key + ' templates already registered');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var template = new Template(element.innerHTML);
|
||||
store[key] = template;
|
||||
|
||||
if(element.hasAttribute('data-compiled')) {
|
||||
template.process = compile(template.html);
|
||||
if (element.hasAttribute('data-compiled')) {
|
||||
template.process = app.get('html').compileTemplate(template.html);
|
||||
template.isCompiled = true;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function compile (template) {
|
||||
var regex = /(?:{([^}]+)})/g;
|
||||
|
||||
var sb = [];
|
||||
|
||||
sb.push('(function() {')
|
||||
sb.push('return function (m) { ')
|
||||
sb.push('\tvar html = [];')
|
||||
sb.push('console.log(m)');
|
||||
var m, index = 0;
|
||||
while ((m = regex.exec(template)) !== null) {
|
||||
if(m.index > index) {
|
||||
sb.push("\t\thtml.push('" + normalize(template.substr(index, m.index - index)) + "');");
|
||||
}
|
||||
sb.push('\t\thtml.push(' + m[1] + ');');
|
||||
index = m.index + m[0].length;
|
||||
}
|
||||
|
||||
if(index < template.length - 1) {
|
||||
sb.push("\t\thtml.push('" + normalize(template.substr(index, template.length - index)) + "');");
|
||||
}
|
||||
sb.push("\treturn html.join('');");
|
||||
sb.push('}');
|
||||
sb.push('})()');
|
||||
// console.log(sb.join('\r\n'));
|
||||
return eval(sb.join('\r\n'));
|
||||
}
|
||||
};
|
||||
|
||||
function normalize(str) {
|
||||
return str.replace(/(\r|\n)+/g, '').replace("'", "\\\'");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
})(window.app);
|
||||
|
||||
@@ -25,19 +25,36 @@
|
||||
return html;
|
||||
};
|
||||
|
||||
function getAttributesStr(attr) {
|
||||
if(attr == null) {
|
||||
return '';
|
||||
}
|
||||
var str = [];
|
||||
HtmlBuilder.compileTemplate = function (template) {
|
||||
var regex = /(?:{([^}]+)})/g;
|
||||
|
||||
for(var key in attr) {
|
||||
if(key == 'html')
|
||||
continue;
|
||||
str.push(key + '="' + HtmlBuilder.escapeHtml(attr[key]) + '"');
|
||||
var sb = [];
|
||||
|
||||
sb.push('(function() {')
|
||||
sb.push('return function (m) { ')
|
||||
sb.push('\tvar html = [];')
|
||||
sb.push('console.log(m)');
|
||||
var m, index = 0;
|
||||
while ((m = regex.exec(template)) !== null) {
|
||||
if(m.index > index) {
|
||||
sb.push("\t\thtml.push('" + normalize(template.substr(index, m.index - index)) + "');");
|
||||
}
|
||||
sb.push('\t\thtml.push(' + m[1] + ');');
|
||||
index = m.index + m[0].length;
|
||||
}
|
||||
|
||||
return str.join(' ');
|
||||
if(index < template.length - 1) {
|
||||
sb.push("\t\thtml.push('" + normalize(template.substr(index, template.length - index)) + "');");
|
||||
}
|
||||
sb.push("\treturn html.join('');");
|
||||
sb.push('}');
|
||||
sb.push('})()');
|
||||
// console.log(sb.join('\r\n'));
|
||||
return eval(sb.join('\r\n'));
|
||||
};
|
||||
|
||||
function normalize(str) {
|
||||
return str.replace(/(\r|\n)+/g, '').replace("'", "\\\'");
|
||||
}
|
||||
|
||||
HtmlBuilder.escapeHtml = function(obj) {
|
||||
@@ -57,6 +74,6 @@
|
||||
.replace(/'/g, "'");
|
||||
};
|
||||
|
||||
core.HtmlBuilder = HtmlBuilder;
|
||||
core.html = HtmlBuilder;
|
||||
|
||||
})(window.core);
|
||||
10
tests/core/htmlSpec.js
Normal file
10
tests/core/htmlSpec.js
Normal file
@@ -0,0 +1,10 @@
|
||||
describe('html templates', function () {
|
||||
var html = core.html;
|
||||
|
||||
it('should compile template', function() {
|
||||
var t = "<div>{m.name}</div>";
|
||||
var compiled = html.compileTemplate(t);
|
||||
expect(typeof compiled).toBe("function");
|
||||
expect(compiled({name: 'test'})).toBe('<div>test</div>');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user