mirror of
https://github.com/BorysLevytskyi/BitwiseCmd.git
synced 2025-12-22 12:42:44 +01:00
Introduced core.
This commit is contained in:
61
core/htmlBuilder.js
Normal file
61
core/htmlBuilder.js
Normal file
@@ -0,0 +1,61 @@
|
||||
(function(core){
|
||||
|
||||
var HtmlBuilder = {};
|
||||
var should = core.should;
|
||||
|
||||
HtmlBuilder.element = function(template, model) {
|
||||
var el = document.createElement('div');
|
||||
el.innerHTML = HtmlBuilder.template(template, model);
|
||||
return el.children[0];
|
||||
};
|
||||
|
||||
HtmlBuilder.template = function(template, model) {
|
||||
should.beString(template, "template");
|
||||
var regex = /(?:{([^}]+)})/g, html;
|
||||
|
||||
if(model == null){
|
||||
html = template;
|
||||
} else {
|
||||
html = template.replace(regex, function(m, g1) {
|
||||
return HtmlBuilder.escapeHtml(model[g1]);
|
||||
});
|
||||
}
|
||||
|
||||
return html;
|
||||
};
|
||||
|
||||
function getAttributesStr(attr) {
|
||||
if(attr == null) {
|
||||
return '';
|
||||
}
|
||||
var str = [];
|
||||
|
||||
for(var key in attr) {
|
||||
if(key == 'html')
|
||||
continue;
|
||||
str.push(key + '="' + HtmlBuilder.escapeHtml(attr[key]) + '"');
|
||||
}
|
||||
|
||||
return str.join(' ');
|
||||
}
|
||||
|
||||
HtmlBuilder.escapeHtml = function(obj) {
|
||||
if(obj == null) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
if(typeof obj != 'string') {
|
||||
obj = obj.toString();
|
||||
}
|
||||
|
||||
return obj
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
};
|
||||
|
||||
core.HtmlBuilder = HtmlBuilder;
|
||||
|
||||
})(window.core);
|
||||
Reference in New Issue
Block a user