Skip to content

Commit 00f6d24

Browse files
committed
make HTML a class
1 parent d2f2ffc commit 00f6d24

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

src/client/parts.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
is_keyed,
66
is_renderable,
77
single_part_template,
8+
unwrap_html,
89
type Displayable,
910
type Key,
1011
type Renderable,
@@ -172,7 +173,7 @@ export function create_child_part(
172173
}
173174

174175
if (is_html(value)) {
175-
const { _dynamics: dynamics, _statics: statics } = value
176+
const { _statics: statics, _dynamics: dynamics } = unwrap_html(value)
176177
const template = compile_template(statics)
177178

178179
assert(

src/client/root.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
is_keyed,
66
is_renderable,
77
single_part_template,
8+
unwrap_html,
89
type Displayable,
910
type Key,
1011
type Renderable,
@@ -111,7 +112,8 @@ function hydrate_child_part(span: Span, value: unknown) {
111112
}
112113

113114
if (is_html(value)) {
114-
template = compile_template(value._statics)
115+
const { _statics: statics, _dynamics: dynamics } = unwrap_html(value)
116+
template = compile_template(statics)
115117

116118
const node_by_part: Array<Node | Span> = []
117119

@@ -180,7 +182,7 @@ function hydrate_child_part(span: Span, value: unknown) {
180182
_start: child.previousSibling,
181183
_end: end,
182184
},
183-
value._dynamics[dynamic_index],
185+
dynamics[dynamic_index],
184186
),
185187
]
186188
case PART_DIRECTIVE:

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { html, keyed, type Displayable, type HTML, type Renderable } from './shared.ts'
22

3-
import { is_html } from './shared.ts'
3+
import { is_html, unwrap_html } from './shared.ts'
44

55
if (__DEV__) {
66
type JsonML = string | readonly [tag: string, attrs?: Record<string, any>, ...children: JsonML[]]
@@ -13,11 +13,11 @@ if (__DEV__) {
1313
;((globalThis as { devtoolsFormatters?: Formatter[] }).devtoolsFormatters ??= []).push({
1414
header(value) {
1515
if (!is_html(value)) return null
16+
const { _statics: statics, _dynamics: dynamics } = unwrap_html(value)
1617

1718
const children: JsonML[] = []
18-
for (let i = 0; i < value._dynamics.length; i++)
19-
children.push(value._statics[i], ['object', { object: value._dynamics[i] }])
20-
children.push(value._statics[value._statics.length - 1])
19+
for (let i = 0; i < dynamics.length; i++) children.push(statics[i], ['object', { object: dynamics[i] }])
20+
children.push(statics[statics.length - 1])
2121

2222
return ['span', {}, 'html`', ...children, '`']
2323
},

src/server.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
import { assert, is_html, is_iterable, is_renderable, lexer, single_part_template, type Displayable } from './shared.ts'
1+
import {
2+
assert,
3+
is_html,
4+
is_iterable,
5+
is_renderable,
6+
lexer,
7+
single_part_template,
8+
unwrap_html,
9+
type Displayable,
10+
} from './shared.ts'
211

312
interface PartRenderer {
413
replace_start: number
@@ -153,7 +162,7 @@ function* render_child(value: unknown): Generator<string, void, void> {
153162
if (is_iterable(value)) {
154163
for (const item of value) yield* render_child(item)
155164
} else if (is_html(value)) {
156-
const { _statics: statics, _dynamics: dynamics } = value
165+
const { _statics: statics, _dynamics: dynamics } = unwrap_html(value)
157166
const template = compile_template(statics)
158167

159168
assert(

src/shared.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,26 @@ export function assert(value: unknown, message?: string): asserts value {
3131
}
3232
}
3333

34-
export interface HTML {
35-
[html_tag]: true
36-
/* @internal */ _statics: TemplateStringsArray
37-
/* @internal */ _dynamics: unknown[]
34+
export let unwrap_html: (value: HTML) => { _statics: TemplateStringsArray; _dynamics: unknown[] }
35+
36+
export class HTML {
37+
#statics: TemplateStringsArray
38+
#dynamics: unknown[]
39+
constructor(statics: TemplateStringsArray, dynamics: unknown[]) {
40+
this.#statics = statics
41+
this.#dynamics = dynamics
42+
}
43+
static {
44+
unwrap_html = value => ({ _statics: value.#statics, _dynamics: value.#dynamics })
45+
}
3846
}
3947

40-
const html_tag: unique symbol = Symbol()
4148
export function html(statics: TemplateStringsArray, ...dynamics: unknown[]): HTML {
42-
return {
43-
[html_tag]: true,
44-
_dynamics: dynamics,
45-
_statics: statics,
46-
}
49+
return new HTML(statics, dynamics)
4750
}
4851

4952
export function is_html(value: unknown): value is HTML {
50-
return typeof value === 'object' && value !== null && html_tag in value
53+
return value instanceof HTML
5154
}
5255

5356
export function single_part_template(part: Displayable): HTML {

0 commit comments

Comments
 (0)