diff --git a/karma.conf.js b/karma.conf.js index f10c55c..bc75f8d 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -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', diff --git a/src/js/app/services.js b/src/js/app/services.js index 9f007c3..6dc087f 100644 --- a/src/js/app/services.js +++ b/src/js/app/services.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); diff --git a/src/js/components/templatesFeature.js b/src/js/components/templatesFeature.js index 9d22927..7ce2d29 100644 --- a/src/js/components/templatesFeature.js +++ b/src/js/components/templatesFeature.js @@ -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); diff --git a/src/js/core/htmlBuilder.js b/src/js/core/htmlBuilder.js index 31f1b36..cdc10f7 100644 --- a/src/js/core/htmlBuilder.js +++ b/src/js/core/htmlBuilder.js @@ -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); \ No newline at end of file diff --git a/tests/core/htmlSpec.js b/tests/core/htmlSpec.js new file mode 100644 index 0000000..6078c63 --- /dev/null +++ b/tests/core/htmlSpec.js @@ -0,0 +1,10 @@ +describe('html templates', function () { + var html = core.html; + + it('should compile template', function() { + var t = "
{m.name}
"; + var compiled = html.compileTemplate(t); + expect(typeof compiled).toBe("function"); + expect(compiled({name: 'test'})).toBe('
test
'); + }); +}); \ No newline at end of file