import { VERSION } from './version.ts'
export const task = (task: () => void) => setTimeout(task, 0)
export const nodeToString = (node: Node) => { const tmpNode = document.createElement('div') tmpNode.appendChild(node.cloneNode(true)) return tmpNode.innerHTML}
export const detectSSR = (): boolean => { const isDeno = typeof Deno !== 'undefined' const hasWindow = typeof window !== 'undefined' ? true : false return (typeof _nano !== 'undefined' && _nano.isSSR) || isDeno || !hasWindow}
function isDescendant(desc: ParentNode | null, root: Node): boolean { return !!desc && (desc === root || isDescendant(desc.parentNode, root))}
export const onNodeRemove = (element: HTMLElement, callback: () => void) => { let observer = new MutationObserver(mutationsList => { mutationsList.forEach(mutation => { mutation.removedNodes.forEach(removed => { if (isDescendant(element, removed)) { callback() if (observer) { observer.disconnect() observer = undefined } } }) }) }) observer.observe(document, { childList: true, subtree: true }) return observer}
export const escapeHtml = (unsafe: string) => { if (unsafe && typeof unsafe === 'string') return unsafe .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, ''') return unsafe}
export const printVersion = () => { const info = `Powered by nano JSX v${VERSION}` console.log( `%c %c %c %c %c ${info} %c http://nanojsx.io`, 'background: #ff0000', 'background: #ffff00', 'background: #00ff00', 'background: #00ffff', 'color: #fff; background: #000000;', 'background: none' )}