export function html(strings:TemplateStringsArray, ...args) { return function append(elm?:HTMLElement) { let template = document.createElement('template'); let rand = `e${`${Math.random()}`.substring(2)}`; let combinedHTML = ''; const withStr = (string, i) => { combinedHTML += string; if(args[i] instanceof HTMLElement) { combinedHTML += ``; } else if(args[i] !== undefined) { combinedHTML += args[i]; } } strings.forEach(withStr); template.innerHTML = combinedHTML; let dummyNodes = Array.from(template.content.querySelectorAll(`#${rand}`)); const withArg = (arg,i) => { if(arg instanceof HTMLElement) { let dummyNode = dummyNodes[i]; if (dummyNode) { dummyNode.parentNode.replaceChild(arg, dummyNode); } } } args.forEach(withArg); if(elm) { elm.appendChild(template.content); return elm; } return template.content; }; } /** * * let htmlfunction = html`` //e.g. some html * htmlfunction(document.body); //append the template string to the desired element, or just return the prepared template element * * Syntax highlighting with lit-html plugin: https://open-vsx.org/extension/meganrogge/template-string-converter // Example usage of the html function document.addEventListener("DOMContentLoaded", () => { const app = document.getElementById('app'); const title = document.createElement('h1'); title.textContent = "Welcome to HTML Function Test"; const paragraph = document.createElement('p'); paragraph.textContent = "This is a paragraph element created using native DOM API."; // Creating HTML structure using the html function const content = html`
This is a paragraph element created using the html function.
${paragraph}${xmlString}`;
console.log(xmlString);
document.body.appendChild(resultContainer);
};
testXmlFunction();
*
*
*/