|
1 | | -type VNodeStencil = { |
| 1 | +// Interface representing a Stencil VNode structure |
| 2 | +interface VNodeStencil { |
2 | 3 | $tag$?: string; |
3 | 4 | $attrs$?: Record<string, unknown> | null; |
4 | 5 | $children$?: Array<VNodeStencil | VNodeLike | string> | null; |
5 | 6 | $text$?: string | null; |
6 | | -}; |
| 7 | +} |
7 | 8 |
|
8 | | -type VNodeLike = { |
| 9 | +// Interface representing a React-like VNode structure |
| 10 | +interface VNodeLike { |
9 | 11 | type?: string | ((...args: unknown[]) => unknown); |
10 | 12 | props?: Record<string, unknown> & { children?: Array<VNodeLike | string> | VNodeLike | string }; |
11 | 13 | children?: Array<VNodeLike | string> | VNodeLike | string; |
12 | | -}; |
| 14 | +} |
13 | 15 |
|
14 | | -function escapeHtml(value: string): string { |
15 | | - return value |
| 16 | +// Escapes HTML special characters to prevent XSS attacks |
| 17 | +const escapeHtml = (value: string): string => |
| 18 | + value |
16 | 19 | .replace(/&/g, '&') |
17 | 20 | .replace(/</g, '<') |
18 | 21 | .replace(/>/g, '>') |
19 | 22 | .replace(/"/g, '"') |
20 | 23 | .replace(/'/g, '''); |
21 | | -} |
22 | 24 |
|
23 | | -function attrsToString(attrs: Record<string, unknown> | null | undefined): string { |
24 | | - if (!attrs) return ''; |
| 25 | +// Converts attribute object to HTML attribute string |
| 26 | +const attrsToString = (attrs: Record<string, unknown> | null | undefined): string => { |
| 27 | + if (!attrs) { |
| 28 | + return ''; |
| 29 | + } |
| 30 | + |
25 | 31 | return Object.entries(attrs) |
26 | 32 | .filter(([, v]) => v !== undefined && v !== null && v !== false) |
27 | 33 | .map(([k, v]) => (v === true ? k : `${k}="${escapeHtml(String(v))}"`)) |
28 | 34 | .join(' '); |
29 | | -} |
| 35 | +}; |
| 36 | + |
| 37 | +// Normalizes children to a consistent array format |
| 38 | +const normalizeChildren = (children: unknown): Array<VNodeStencil | VNodeLike | string> => { |
| 39 | + if (children == null) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + |
| 43 | + if (Array.isArray(children)) { |
| 44 | + return children as Array<VNodeStencil | VNodeLike | string>; |
| 45 | + } |
30 | 46 |
|
31 | | -function normalizeChildren(children: unknown): Array<VNodeStencil | VNodeLike | string> { |
32 | | - if (children == null) return []; |
33 | | - if (Array.isArray(children)) return children as Array<VNodeStencil | VNodeLike | string>; |
34 | 47 | return [children as VNodeStencil | VNodeLike | string]; |
35 | | -} |
| 48 | +}; |
36 | 49 |
|
37 | | -function renderStencil(node: VNodeStencil): string { |
| 50 | +// Renders a Stencil VNode to HTML string |
| 51 | +const renderStencil = (node: VNodeStencil): string => { |
38 | 52 | if (node.$text$ != null) { |
39 | 53 | return escapeHtml(String(node.$text$)); |
40 | 54 | } |
| 55 | + |
41 | 56 | const tag = node.$tag$ || 'div'; |
42 | 57 | const attrs = attrsToString(node.$attrs$ || undefined); |
43 | 58 | const children = (node.$children$ || []).map(renderAny).join(''); |
44 | 59 | const attrsStr = attrs ? ` ${attrs}` : ''; |
| 60 | + |
45 | 61 | return `<${tag}${attrsStr}>${children}</${tag}>`; |
46 | | -} |
| 62 | +}; |
47 | 63 |
|
48 | | -function renderLike(node: VNodeLike): string { |
| 64 | +// Renders a React-like VNode to HTML string |
| 65 | +const renderLike = (node: VNodeLike): string => { |
49 | 66 | const tag = typeof node.type === 'string' ? node.type : 'div'; |
50 | 67 | const props = node.props || {}; |
51 | 68 | const { children, ...rest } = props as Record<string, unknown> & { children?: unknown }; |
52 | 69 | const attrs = attrsToString(rest); |
53 | 70 | const childrenArr = normalizeChildren(children ?? node.children); |
54 | 71 | const childrenStr = childrenArr.map(renderAny).join(''); |
55 | 72 | const attrsStr = attrs ? ` ${attrs}` : ''; |
| 73 | + |
56 | 74 | return `<${tag}${attrsStr}>${childrenStr}</${tag}>`; |
57 | | -} |
| 75 | +}; |
58 | 76 |
|
59 | | -export function renderJsxToHtml(node: unknown): string { |
60 | | - return renderAny(node); |
61 | | -} |
| 77 | +// Main export function that converts JSX to HTML string |
| 78 | +export const renderJsxToHtml = (node: unknown): string => renderAny(node); |
| 79 | + |
| 80 | +// Renders any node type to HTML string with type detection |
| 81 | +const renderAny = (node: unknown): string => { |
| 82 | + if (node == null || node === false) { |
| 83 | + return ''; |
| 84 | + } |
| 85 | + |
| 86 | + if (typeof node === 'string' || typeof node === 'number') { |
| 87 | + return escapeHtml(String(node)); |
| 88 | + } |
62 | 89 |
|
63 | | -function renderAny(node: unknown): string { |
64 | | - if (node == null || node === false) return ''; |
65 | | - if (typeof node === 'string' || typeof node === 'number') return escapeHtml(String(node)); |
66 | | - // Heuristics for Stencil VNode |
| 90 | + // Heuristics for detecting node types |
67 | 91 | if (typeof node === 'object') { |
68 | 92 | const o = node as Record<string, unknown>; |
| 93 | + |
| 94 | + // Check for Stencil VNode |
69 | 95 | if ('$tag$' in o || '$text$' in o || '$children$' in o) { |
70 | 96 | return renderStencil(node as VNodeStencil); |
71 | 97 | } |
| 98 | + |
| 99 | + // Check for React-like VNode |
72 | 100 | if ('type' in o || 'props' in o || 'children' in o) { |
73 | 101 | return renderLike(node as VNodeLike); |
74 | 102 | } |
75 | 103 | } |
76 | | - // Fallback |
| 104 | + |
| 105 | + // Fallback for unknown node types |
77 | 106 | return escapeHtml(String(node)); |
78 | | -} |
| 107 | +}; |
0 commit comments