Skip to content

Commit 0ce9aac

Browse files
committed
fix doctype (add unsafe html function)
1 parent 6f95021 commit 0ce9aac

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

src/compiler/template/template-builder.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import {setAutoComponent} from '../states-collector'
1616
import {HashType, isVirtualElement, VirtualElement} from '../../jsx/VirtualElement'
1717
import {includeStateUsage, makeContext} from './state-usage'
1818
import {escapeHTML} from 'bun'
19+
import {UnsafeHTML} from 'src/jsx/insert-html'
1920

2021
let currentComponentHash
22+
2123
export function getCurrentComponentHash() {
2224
return currentComponentHash
2325
}
@@ -141,7 +143,11 @@ export default class TemplateBuilder {
141143
}
142144

143145
private stringifyTextNode(value: StringifiableType): [string, ServerTemplateTextNodeType] {
144-
const string = escapeHTML(stringifyValue(value))
146+
let string
147+
if (value instanceof UnsafeHTML)
148+
string = value.valueOf()
149+
else
150+
string = escapeHTML(stringifyValue(value))
145151
return [
146152
`[0"${string}"]`,
147153
{type: 'text', content: string}

src/jsx/dom/builtins/Html.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import {unsafeInsertHtmlDoNotUse} from '../../insert-html'
2+
13
export default function Html({...props}: JSX.IntrinsicElements['html']) {
24
return <>
3-
{'<!DOCTYPE html>'}
5+
{unsafeInsertHtmlDoNotUse('<!DOCTYPE html>')}
46
<html {...props}>{props.children}</html>
57
</>
68
}

src/jsx/insert-html.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {HashType} from './VirtualElement'
2+
import {getCurrentComponentHash} from '../compiler/template/template-builder'
3+
4+
export function unsafeInsertHtmlDoNotUse(value: string) {
5+
return new UnsafeHTML(value)
6+
}
7+
8+
export class UnsafeHTML {
9+
private readonly componentHash: HashType
10+
private readonly value: string
11+
12+
constructor(value: string) {
13+
this.componentHash = getCurrentComponentHash()
14+
this.value = value
15+
}
16+
17+
valueOf(): UnsafeHTML['value'] {
18+
return this.value
19+
}
20+
}

0 commit comments

Comments
 (0)