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/core.js',
|
||||||
'src/js/core/is.js',
|
'src/js/core/is.js',
|
||||||
'src/js/core/di.js',
|
'src/js/core/di.js',
|
||||||
|
'src/js/core/htmlBuilder.js',
|
||||||
'src/js/core/should.js',
|
'src/js/core/should.js',
|
||||||
'src/js/core/appShell.js',
|
'src/js/core/appShell.js',
|
||||||
'src/js/core/observable.js',
|
'src/js/core/observable.js',
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
(function(app, core){
|
(function(app, core){
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
app.set('html', core.HtmlBuilder);
|
app.set('html', core.html);
|
||||||
app.set('is', core.is);
|
app.set('is', core.is);
|
||||||
app.set('should', core.should);
|
app.set('should', core.should);
|
||||||
app.set('bindr', core.bindr);
|
app.set('bindr', core.bindr);
|
||||||
|
|||||||
@@ -31,57 +31,21 @@
|
|||||||
var els = containerEl.querySelectorAll('[data-template]');
|
var els = containerEl.querySelectorAll('[data-template]');
|
||||||
var store = app.templates;
|
var store = app.templates;
|
||||||
|
|
||||||
Array.prototype.forEach.call(els, function(element) {
|
Array.prototype.forEach.call(els, function (element) {
|
||||||
var key = element.getAttribute('data-template');
|
var key = element.getAttribute('data-template');
|
||||||
|
|
||||||
if(store[key] instanceof Template) {
|
if (store[key] instanceof Template) {
|
||||||
console.warn(key + ' templates already registered');
|
console.warn(key + ' templates already registered');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var template = new Template(element.innerHTML);
|
var template = new Template(element.innerHTML);
|
||||||
store[key] = template;
|
store[key] = template;
|
||||||
|
|
||||||
if(element.hasAttribute('data-compiled')) {
|
if (element.hasAttribute('data-compiled')) {
|
||||||
template.process = compile(template.html);
|
template.process = app.get('html').compileTemplate(template.html);
|
||||||
template.isCompiled = true;
|
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);
|
})(window.app);
|
||||||
|
|||||||
@@ -25,19 +25,36 @@
|
|||||||
return html;
|
return html;
|
||||||
};
|
};
|
||||||
|
|
||||||
function getAttributesStr(attr) {
|
HtmlBuilder.compileTemplate = function (template) {
|
||||||
if(attr == null) {
|
var regex = /(?:{([^}]+)})/g;
|
||||||
return '';
|
|
||||||
}
|
|
||||||
var str = [];
|
|
||||||
|
|
||||||
for(var key in attr) {
|
var sb = [];
|
||||||
if(key == 'html')
|
|
||||||
continue;
|
sb.push('(function() {')
|
||||||
str.push(key + '="' + HtmlBuilder.escapeHtml(attr[key]) + '"');
|
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) {
|
HtmlBuilder.escapeHtml = function(obj) {
|
||||||
@@ -57,6 +74,6 @@
|
|||||||
.replace(/'/g, "'");
|
.replace(/'/g, "'");
|
||||||
};
|
};
|
||||||
|
|
||||||
core.HtmlBuilder = HtmlBuilder;
|
core.html = HtmlBuilder;
|
||||||
|
|
||||||
})(window.core);
|
})(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