deno.land / x / nano_jsx@v0.1.0 / helpers.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { VERSION } from './version.ts'
/** Creates a new Task using setTimeout() */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 => { // @ts-ignore 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 { // @ts-ignore return !!desc && (desc === root || isDescendant(desc.parentNode, root))}
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserverexport 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) { // allow garbage collection observer.disconnect() // @ts-ignore observer = undefined } } }) }) }) observer.observe(document, { childList: true, subtree: true }) return observer}
// https://stackoverflow.com/a/6234804export const escapeHtml = (unsafe: string) => { if (unsafe && typeof unsafe === 'string') return unsafe .replace(/&/g, '&amp;') .replace(/</g, '&lt;') .replace(/>/g, '&gt;') .replace(/"/g, '&quot;') .replace(/'/g, '&apos;') 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' )}
nano_jsx

Version Info

Tagged at
8 months ago