Files
iTop/js/DI/toolkit.js
2023-08-31 15:47:58 +02:00

70 lines
1.1 KiB
JavaScript

/**
* Toolkit.
*
* @returns {{init: init, parseTextToHtml: (function(*): Document), createElementFromHtml: (function(*): DocumentFragment)}}
* @constructor
*/
const Toolkit = function(){
function init(){
installStringFormatFunction();
}
/**
* installStringFormatFunction.
*
* String formatter utility.
*/
function installStringFormatFunction(){
if (!String.format) {
String.format = function(format) {
const args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
};
}
}
/**
* parseTextToHtml.
*
* @param sText
* @returns {Document}
*/
function parseTextToHtml(sText){
const oParser = new DOMParser();
return oParser.parseFromString(sText, 'text/html');
}
/**
*
* @param html
* @returns {DocumentFragment}
*/
function createElementFromHtml(html) {
const t = document.createElement('template');
t.innerHTML = html;
return t.content;
}
return {
init,
parseTextToHtml,
createElementFromHtml
}
};