-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic-ssr.jsx
61 lines (54 loc) · 1.51 KB
/
basic-ssr.jsx
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
function Paragraph({content}) {
return <p>{content}</p>;
}
const div = (
<div class={'some class'} onclick={() => ignored}>
<Paragraph content="some content" />
</div>
);
console.log(basicHTML(div));
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
import ESXToken from '@ungap/esxtoken';
import {escape} from 'html-escaper';
function basicHTML(esx, output = []) {
switch (esx.type) {
case ESXToken.COMPONENT:
return basicHTML(esx.value(esx.properties, ...esx.children), output);
case ESXToken.FRAGMENT:
addChildren(output, esx);
break;
case ESXToken.ELEMENT:
output.push('<', esx.value);
for (const [name, value] of Object.entries(esx.properties || {})) {
if (typeof value !== 'function')
addAttribute(output, name, value);
}
output.push('>');
addChildren(output, esx);
output.push(`</${esx.value}>`);
break;
}
return output.join('');
}
function addAttribute(output, name, value) {
output.push(` ${name}="${escape(String(value))}"`);
}
function addChildren(output, {children}) {
for (const child of children) {
const {type, value} = child;
switch (type) {
case ESXToken.STATIC:
output.push(escape(value));
break;
case ESXToken.INTERPOLATION:
if (value instanceof ESXToken)
basicHTML(value, output);
else
output.push(escape(value));
break;
default:
basicHTML(child, output);
break;
}
}
}