-
Notifications
You must be signed in to change notification settings - Fork 1
/
stringify.ts
38 lines (28 loc) · 1.12 KB
/
stringify.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
const shadowRootScript = `<script>function __ssr() {var r,s=document.currentScript,f=s.parentNode;h=f.parentNode;f.removeChild(s);h.removeChild(f);r=h.attachShadow({mode:h.getAttribute('mode')||'open'});while(f&&f.firstChild)r.appendChild(f.firstChild);}</script>`;
const shadowRootScriptCall = `<script>__ssr()</script>`;
export function stringify(node) {
let str = '';
if (node.nodeName === '#document') {
node = node.documentElement;
str += '<!doctype html>';
}
if (node.nodeName === '#text') {
return node.textContent;
}
str += `<${node.localName}${(node.attributes || [])
.map(a => ` ${a.name}="${a.value}"`)
.join('')}>`;
if (node.nodeName === 'BODY') {
str += shadowRootScript;
}
if (node.shadowRoot) {
str += `<shadowroot>${node.shadowRoot.childNodes
.map(stringify)
.join('')}${shadowRootScriptCall}</shadowroot>`;
}
if (node.childNodes) {
str += node.childNodes.map(stringify).join('');
}
str += `</${node.localName}>`;
return str;
}