@kayjs/kay 2.0.0-beta.0 → 2.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/package.json +1 -1
  2. package/src/attrs.ts +35 -0
  3. package/src/build/bundle.ts +442 -0
  4. package/src/build/compile.ts +10 -10
  5. package/src/build/files.ts +136 -0
  6. package/src/build/graph.ts +158 -0
  7. package/src/build/index.ts +204 -1460
  8. package/src/build/islands.ts +177 -0
  9. package/src/build/manifest.ts +224 -0
  10. package/src/build/plugin-host.ts +184 -0
  11. package/src/build/prerender.ts +93 -0
  12. package/src/build/report.ts +157 -0
  13. package/src/build/resolve.ts +58 -0
  14. package/src/build/routes.ts +135 -0
  15. package/src/build/static.ts +73 -0
  16. package/src/cli/check.ts +228 -114
  17. package/src/cli/index.ts +297 -94
  18. package/src/cli/new.ts +53 -32
  19. package/src/compiler/analyze.ts +666 -91
  20. package/src/compiler/dom.ts +808 -743
  21. package/src/compiler/mistakes.ts +354 -0
  22. package/src/compiler/patch.ts +1 -1
  23. package/src/compiler/server-only.ts +164 -21
  24. package/src/compiler/shared.ts +442 -0
  25. package/src/compiler/ssr.ts +329 -342
  26. package/src/context.ts +120 -0
  27. package/src/dev/index.ts +271 -75
  28. package/src/dom/action.ts +23 -13
  29. package/src/dom/extras.ts +121 -83
  30. package/src/dom/index.ts +114 -77
  31. package/src/dom/island.ts +71 -36
  32. package/src/dom/loop.ts +28 -20
  33. package/src/index.ts +2 -1
  34. package/src/jsx-runtime.ts +11 -0
  35. package/src/plugins/context.ts +57 -0
  36. package/src/plugins/index.ts +114 -18
  37. package/src/reactivity/index.ts +237 -213
  38. package/src/reactivity/selector.ts +11 -9
  39. package/src/reactivity/store.ts +36 -11
  40. package/src/server/action.ts +147 -76
  41. package/src/server/compress.ts +116 -96
  42. package/src/server/cookies.ts +46 -16
  43. package/src/server/env.ts +18 -13
  44. package/src/server/http.ts +107 -54
  45. package/src/server/index.ts +887 -870
  46. package/src/server/loader.ts +120 -0
  47. package/src/server/router.ts +22 -8
  48. package/src/server/security.ts +48 -58
  49. package/src/server/standard-schema.ts +42 -11
  50. package/src/server/static-dir.ts +144 -0
  51. package/src/ssr/context.ts +95 -10
  52. package/src/ssr/index.ts +167 -66
  53. package/src/style.ts +33 -0
  54. package/src/test/index.ts +2 -1
  55. package/src/unrenderable.ts +35 -0
package/src/dom/extras.ts CHANGED
@@ -2,8 +2,10 @@
2
2
  // so Bun.build({ splitting: true }) keeps them out of minimal islands that don't use them.
3
3
 
4
4
  import type { Owner } from '../reactivity/index'
5
- import { createOwner, disposeOwner, effect, getOwner, runWithOwner, untrack } from '../reactivity/index'
6
- import { claimInsert, setAttr, setClass, setProp, setStyle, skipRange } from './index'
5
+ import { DOM_PROPS } from '../attrs'
6
+ import { batch, createOwner, disposeOwner, effect, getOwner, onCleanup, runWithOwner, untrack } from '../reactivity/index'
7
+ import { assertRenderable } from '../unrenderable'
8
+ import { claimInsert, isBlockedAttr, setAttr, setClass, setProp, setStyle, skipRange } from './index'
7
9
 
8
10
  const RAW_HTML = Symbol.for('kay.html')
9
11
 
@@ -12,23 +14,37 @@ function isRawHtml(value: unknown): value is { __html: string } {
12
14
  }
13
15
 
14
16
  export function setInnerHtml(el: Element, value: unknown): void {
15
- const html = value == null || value === false
16
- ? ''
17
- : isRawHtml(value)
18
- ? String(value.__html)
19
- : String(value)
17
+ let html: string
18
+ if (value == null || value === false)
19
+ html = ''
20
+ else if (isRawHtml(value))
21
+ html = String(value.__html)
22
+ else if (typeof value === 'object' && '__html' in value)
23
+ throw new TypeError('kay: innerHTML takes the HTML string, got a { __html } object; pass the string, or raw(html) for a value typed Html.')
24
+ else
25
+ html = String(value)
20
26
  if (el.innerHTML !== html)
21
27
  el.innerHTML = html
22
28
  }
23
29
 
24
- const VALID_ATTR_NAME = /^[A-Za-z_:][-\w:.]*$/
25
- const SPREAD_DOM_PROPS = new Set(['value', 'checked', 'selected', 'muted', 'indeterminate'])
30
+ const EVENT_PROP = /^on([A-Z]\w*)$/
26
31
 
27
- function isBlockedSpreadAttr(name: string): boolean {
28
- if (!VALID_ATTR_NAME.test(name))
29
- return true
30
- const lower = name.toLowerCase()
31
- return lower.startsWith('on') || lower === 'innerhtml' || lower === 'srcdoc'
32
+ /** Merges prop objects while preserving getter descriptors so reactive props stay lazy across component spreads. */
33
+ export function mergeProps(...sources: (Record<string, unknown> | null | undefined)[]): Record<string, unknown> {
34
+ const out: Record<string, unknown> = {}
35
+ for (let i = 0; i < sources.length; i++) {
36
+ const src = sources[i]
37
+ if (src == null || typeof src !== 'object')
38
+ continue
39
+ const keys = Object.keys(src)
40
+ for (let j = 0; j < keys.length; j++) {
41
+ const key = keys[j]!
42
+ const desc = Object.getOwnPropertyDescriptor(src, key)
43
+ if (desc !== undefined)
44
+ Object.defineProperty(out, key, desc)
45
+ }
46
+ }
47
+ return out
32
48
  }
33
49
 
34
50
  export function omit(props: Record<string, unknown>, excluded: string[]): Record<string, unknown> {
@@ -46,8 +62,23 @@ export function omit(props: Record<string, unknown>, excluded: string[]): Record
46
62
 
47
63
  export function spread(el: Element, getProps: () => Record<string, unknown> | null | undefined): void {
48
64
  let prevKeys: string[] = []
65
+ let lastRef: unknown
66
+ // One listener per event type, calling whichever handler the props hold now.
67
+ const handlers = new Map<string, (e: Event) => void>()
68
+ const listen = (e: Event): void => {
69
+ const handler = handlers.get(e.type)
70
+ if (handler !== undefined)
71
+ batch(() => handler(e))
72
+ }
73
+
74
+ onCleanup(() => {
75
+ for (const type of handlers.keys())
76
+ el.removeEventListener(type, listen)
77
+ handlers.clear()
78
+ })
49
79
 
50
80
  effect(() => {
81
+ const types = new Set<string>()
51
82
  const props = getProps() ?? {}
52
83
  const nextKeys = Object.keys(props)
53
84
 
@@ -63,8 +94,11 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
63
94
  }
64
95
  else if (k !== 'key' && k !== 'ref' && k !== '$hydrate' && k !== 'children') {
65
96
  const name = k === 'htmlFor' ? 'for' : k
66
- if (!isBlockedSpreadAttr(name))
97
+ if (!isBlockedAttr(name)) {
98
+ if (DOM_PROPS.has(name))
99
+ setProp(el, name, name === 'value' ? '' : false)
67
100
  el.removeAttribute(name)
101
+ }
68
102
  }
69
103
  }
70
104
 
@@ -72,25 +106,43 @@ export function spread(el: Element, getProps: () => Record<string, unknown> | nu
72
106
  const k = nextKeys[i]!
73
107
  if (k === 'key' || k === '$hydrate' || k === 'children')
74
108
  continue
109
+ if (k === 'dangerouslySetInnerHTML')
110
+ throw new TypeError('kay: dangerouslySetInnerHTML in a spread: write innerHTML={html} on the element.')
75
111
  const v = props[k]
76
112
  if (k === 'ref') {
77
- if (typeof v === 'function')
78
- (v as (node: Element) => void)(el)
113
+ if (typeof v === 'function' && v !== lastRef) {
114
+ lastRef = v
115
+ ;(v as (node: Element) => void)(el)
116
+ }
117
+ continue
118
+ }
119
+ const event = typeof v === 'function' ? EVENT_PROP.exec(k) : null
120
+ if (event !== null) {
121
+ const type = event[1]!.toLowerCase()
122
+ types.add(type)
123
+ el.addEventListener(type, listen)
124
+ handlers.set(type, v as (e: Event) => void)
79
125
  continue
80
126
  }
81
127
  const name = k === 'className' ? 'class' : k === 'htmlFor' ? 'for' : k
82
- if (isBlockedSpreadAttr(name))
128
+ if (isBlockedAttr(name))
83
129
  continue
84
130
  if (name === 'class')
85
131
  setClass(el, v)
86
132
  else if (name === 'style')
87
133
  setStyle(el as HTMLElement, v)
88
- else if (SPREAD_DOM_PROPS.has(name))
134
+ else if (DOM_PROPS.has(name))
89
135
  setProp(el, name, v)
90
136
  else
91
137
  setAttr(el, name, v)
92
138
  }
93
139
 
140
+ for (const type of handlers.keys()) {
141
+ if (!types.has(type)) {
142
+ handlers.delete(type)
143
+ el.removeEventListener(type, listen)
144
+ }
145
+ }
94
146
  prevKeys = nextKeys
95
147
  })
96
148
  }
@@ -107,6 +159,28 @@ function clearInserted(current: Node | Node[] | null): void {
107
159
  }
108
160
  }
109
161
 
162
+ // Inserts the nodes and texts of a rendered array before the marker; a fragment contributes its children.
163
+ function insertAll(parent: Node, marker: Node | null, items: unknown[]): Node[] {
164
+ const f = document.createDocumentFragment()
165
+ const nodes: Node[] = []
166
+ for (const item of items) {
167
+ if (item instanceof Node) {
168
+ if (item.nodeType === 11)
169
+ nodes.push(...Array.from(item.childNodes))
170
+ else
171
+ nodes.push(item)
172
+ f.appendChild(item)
173
+ }
174
+ else if (item != null && typeof item !== 'boolean' && item !== '') {
175
+ const t = document.createTextNode(String(item))
176
+ nodes.push(t)
177
+ f.appendChild(t)
178
+ }
179
+ }
180
+ parent.insertBefore(f, marker)
181
+ return nodes
182
+ }
183
+
110
184
  function claimTextNode(claimed: Node | null, str: string): Text | undefined {
111
185
  if (claimed === null || claimed.nodeType !== 3)
112
186
  return undefined
@@ -170,12 +244,8 @@ export function insert(parent: Node, value: () => unknown, marker: Node | null =
170
244
  const claimed = claimTextNode(res.claimed, str)
171
245
  if (claimed !== undefined) {
172
246
  textNode = claimed
173
- if (ssrNodes !== undefined && ssrNodes.length > 1) {
174
- for (let i = 0; i < ssrNodes.length; i++) {
175
- if (ssrNodes[i] !== claimed)
176
- ssrNodes[i]!.parentNode?.removeChild(ssrNodes[i]!)
177
- }
178
- }
247
+ if (ssrNodes !== undefined)
248
+ clearInserted(ssrNodes.filter(n => n !== claimed))
179
249
  }
180
250
  else {
181
251
  if (ssrNodes !== undefined)
@@ -190,6 +260,12 @@ export function insert(parent: Node, value: () => unknown, marker: Node | null =
190
260
  return
191
261
  }
192
262
 
263
+ if (process.env.NODE_ENV !== 'production') {
264
+ if (isRawHtml(next))
265
+ throw new TypeError('kay: raw() HTML cannot be a dynamic child in the browser; set it with innerHTML={html} on an element.')
266
+ assertRenderable(next, 'text')
267
+ }
268
+
193
269
  if (textNode !== undefined) {
194
270
  textNode.remove()
195
271
  textNode = undefined
@@ -199,12 +275,8 @@ export function insert(parent: Node, value: () => unknown, marker: Node | null =
199
275
  if (current === next)
200
276
  return
201
277
  if (current === null && next.parentNode === parent) {
202
- if (ssrNodes !== undefined && ssrNodes.length > 1) {
203
- for (let i = 0; i < ssrNodes.length; i++) {
204
- if (ssrNodes[i] !== next)
205
- ssrNodes[i]!.parentNode?.removeChild(ssrNodes[i]!)
206
- }
207
- }
278
+ if (ssrNodes !== undefined)
279
+ clearInserted(ssrNodes.filter(n => n !== next))
208
280
  current = next
209
281
  return
210
282
  }
@@ -226,36 +298,10 @@ export function insert(parent: Node, value: () => unknown, marker: Node | null =
226
298
  if (Array.isArray(next)) {
227
299
  clearInserted(current)
228
300
  if (ssrNodes !== undefined) {
229
- const kept = new Set<Node>()
230
- for (let i = 0; i < next.length; i++) {
231
- const item = next[i]
232
- if (item instanceof Node && item.parentNode === parent)
233
- kept.add(item)
234
- }
235
- for (let i = 0; i < ssrNodes.length; i++) {
236
- if (!kept.has(ssrNodes[i]!))
237
- ssrNodes[i]!.parentNode?.removeChild(ssrNodes[i]!)
238
- }
301
+ const kept = new Set(next)
302
+ clearInserted(ssrNodes.filter(n => !kept.has(n)))
239
303
  }
240
- const f = document.createDocumentFragment()
241
- const nodes: Node[] = []
242
- for (let i = 0; i < next.length; i++) {
243
- const item = next[i]
244
- if (item instanceof Node) {
245
- if (item.nodeType === 11)
246
- nodes.push(...Array.from(item.childNodes))
247
- else
248
- nodes.push(item)
249
- f.appendChild(item)
250
- }
251
- else if (item != null && typeof item !== 'boolean') {
252
- const t = document.createTextNode(String(item))
253
- nodes.push(t)
254
- f.appendChild(t)
255
- }
256
- }
257
- parent.insertBefore(f, marker)
258
- current = nodes
304
+ current = insertAll(parent, marker, next)
259
305
  }
260
306
  })
261
307
  }
@@ -271,6 +317,7 @@ export function show(
271
317
  let branch: 0 | 1 | 2 = 0
272
318
  let owner: Owner | undefined
273
319
  let nodes: Node[] = []
320
+ let firstRun = true
274
321
 
275
322
  const clear = () => {
276
323
  if (owner !== undefined) {
@@ -288,6 +335,18 @@ export function show(
288
335
  if (branch === nextBranch)
289
336
  return
290
337
 
338
+ let startMarker: Node | null | undefined
339
+ if (firstRun) {
340
+ firstRun = false
341
+ if (marker !== null && marker.nodeType === 8 && (marker as Comment).data === 'k[') {
342
+ const close = skipRange(marker)
343
+ if (close !== null) {
344
+ startMarker = marker
345
+ marker = close
346
+ }
347
+ }
348
+ }
349
+
291
350
  clear()
292
351
  branch = nextBranch
293
352
 
@@ -296,7 +355,7 @@ export function show(
296
355
  return
297
356
 
298
357
  owner = createOwner(parentOwner)
299
- const res = claimInsert(parent, marker, () => untrack(() => runWithOwner(owner, fn)))
358
+ const res = claimInsert(parent, marker, () => untrack(() => runWithOwner(owner, fn)), startMarker)
300
359
  marker = res.marker
301
360
  const rendered = res.result
302
361
  if (rendered == null || typeof rendered === 'boolean') {
@@ -329,28 +388,7 @@ export function show(
329
388
  }
330
389
 
331
390
  if (Array.isArray(rendered)) {
332
- const f = document.createDocumentFragment()
333
- const list: Node[] = []
334
- for (let i = 0; i < rendered.length; i++) {
335
- const item = rendered[i]
336
- if (item instanceof Node) {
337
- if (item.nodeType === 11)
338
- list.push(...Array.from(item.childNodes))
339
- else
340
- list.push(item)
341
- f.appendChild(item)
342
- }
343
- else if (item != null && typeof item !== 'boolean') {
344
- const str = String(item)
345
- if (str !== '') {
346
- const t = document.createTextNode(str)
347
- list.push(t)
348
- f.appendChild(t)
349
- }
350
- }
351
- }
352
- parent.insertBefore(f, marker)
353
- nodes = list
391
+ nodes = insertAll(parent, marker, rendered)
354
392
  }
355
393
  })
356
394
  }
package/src/dom/index.ts CHANGED
@@ -1,21 +1,32 @@
1
1
  // Kay v2 minimal DOM & hydration core (loop -> kay/dom-loop, extras -> kay/dom-extras for fine-grained chunk splitting).
2
2
 
3
+ import { isBlockedAttr, isStringBoolAttr, isUnsafeUrlAttr } from '../attrs'
3
4
  import { batch, createOwner, disposeOwner, runWithOwner } from '../reactivity/index'
5
+ import { styleText } from '../style'
6
+ import { assertRenderable } from '../unrenderable'
4
7
 
8
+ export { isBlockedAttr } from '../attrs'
5
9
  export { effect } from '../reactivity/index'
6
10
 
7
11
  let hydrating = false
8
12
  let hydrateNode: Node | null = null
13
+ let hydrateRoot: Element | null = null
9
14
 
10
- export function claimLoop(parent: Node, fn: () => void, skip = 0): boolean {
15
+ export function claimLoop(parent: Node, fn: () => void, skip = 0, startMarker?: Node | null): boolean {
11
16
  if (!hydrating)
12
17
  return false
13
18
  const prevCursor = hydrateNode
14
- let cur = parent.firstChild
15
- for (let i = 0; i < skip && cur !== null; i++) {
16
- cur = cur.nextSibling
17
- while (cur !== null && (cur as unknown as { __kayAnchor?: boolean }).__kayAnchor === true)
19
+ let cur: Node | null
20
+ if (startMarker !== undefined && startMarker !== null) {
21
+ cur = startMarker.nextSibling
22
+ }
23
+ else {
24
+ cur = parent.firstChild
25
+ for (let i = 0; i < skip && cur !== null; i++) {
18
26
  cur = cur.nextSibling
27
+ while (cur !== null && (cur as unknown as { __kayAnchor?: boolean }).__kayAnchor === true)
28
+ cur = cur.nextSibling
29
+ }
19
30
  }
20
31
  hydrateNode = cur
21
32
  try {
@@ -27,6 +38,14 @@ export function claimLoop(parent: Node, fn: () => void, skip = 0): boolean {
27
38
  return true
28
39
  }
29
40
 
41
+ // The siblings from `from` up to, not including, `to`; undefined when there are none.
42
+ function between(from: Node | null, to: Node | null): Node[] | undefined {
43
+ const nodes: Node[] = []
44
+ for (let n = from; n !== null && n !== to; n = n.nextSibling)
45
+ nodes.push(n)
46
+ return nodes.length > 0 ? nodes : undefined
47
+ }
48
+
30
49
  // `nodes` is the SSR range the render claimed through template() (a fragment hydrates to null, so the caller learns its nodes here).
31
50
  export function claimInsert<T>(
32
51
  parent: Node,
@@ -37,25 +56,14 @@ export function claimInsert<T>(
37
56
  if (!hydrating)
38
57
  return { result: fn(), marker, claimed: null, nodes: undefined }
39
58
 
59
+ let claimed: Node | null
40
60
  if (startMarker !== undefined) {
41
61
  const first = startMarker === null ? parent.firstChild : startMarker.nextSibling
42
- const claimed = first === marker ? null : first
43
- const prevNode = hydrateNode
44
- hydrateNode = claimed
45
- let result: T
46
- try {
47
- result = fn()
48
- }
49
- finally {
50
- hydrateNode = prevNode
51
- }
52
- const nodes: Node[] = []
53
- for (let n: Node | null = claimed; n !== null && n !== marker; n = n.nextSibling)
54
- nodes.push(n)
55
- return { result, marker, claimed, nodes: nodes.length > 0 ? nodes : undefined }
62
+ claimed = first === marker ? null : first
63
+ }
64
+ else {
65
+ claimed = marker === null ? parent.lastChild : marker.nodeType === 8 ? null : marker
56
66
  }
57
-
58
- const claimed: Node | null = marker === null ? parent.lastChild : marker.nodeType === 8 ? null : marker
59
67
  const prevNode = hydrateNode
60
68
  hydrateNode = claimed
61
69
  let result: T
@@ -67,15 +75,10 @@ export function claimInsert<T>(
67
75
  finally {
68
76
  hydrateNode = prevNode
69
77
  }
70
- if (marker === null) {
71
- let nodes: Node[] | undefined
72
- if (claimed !== null && end !== claimed) {
73
- nodes = []
74
- for (let n: Node | null = claimed; n !== null && n !== end; n = n.nextSibling)
75
- nodes.push(n)
76
- }
77
- return { result, marker: null, claimed, nodes }
78
- }
78
+ if (startMarker !== undefined)
79
+ return { result, marker, claimed, nodes: between(claimed, marker) }
80
+ if (marker === null)
81
+ return { result, marker: null, claimed, nodes: between(claimed, end) }
79
82
  if (claimed === null)
80
83
  return { result, marker, claimed, nodes: undefined }
81
84
 
@@ -84,21 +87,20 @@ export function claimInsert<T>(
84
87
  const anchor = document.createComment('')
85
88
  ;(anchor as unknown as { __kayAnchor?: boolean }).__kayAnchor = true
86
89
  parent.insertBefore(anchor, consumed ? end : claimed.nextSibling)
87
- let nodes: Node[] | undefined
88
- if (consumed) {
89
- nodes = []
90
- for (let n: Node | null = claimed; n !== null && n !== anchor; n = n.nextSibling)
91
- nodes.push(n)
92
- }
93
- return { result, marker: anchor, claimed, nodes }
90
+ return { result, marker: anchor, claimed, nodes: consumed ? between(claimed, anchor) : undefined }
94
91
  }
95
92
 
96
- export function hydrate(container: Element, render: () => Node | DocumentFragment | null): () => void {
93
+ export function hydrate(container: Element, render: () => Node | DocumentFragment | null, idPrefix?: string): () => void {
97
94
  const owner = createOwner(undefined)
95
+ if (idPrefix !== undefined)
96
+ owner.id = { p: idPrefix, n: 0 }
98
97
  const prevHydrating = hydrating
99
98
  const prevNode = hydrateNode
99
+ const prevRoot = process.env.NODE_ENV !== 'production' ? hydrateRoot : null
100
100
  hydrating = true
101
101
  hydrateNode = container.firstChild
102
+ if (process.env.NODE_ENV !== 'production')
103
+ hydrateRoot = container
102
104
 
103
105
  try {
104
106
  runWithOwner(owner, () => {
@@ -112,13 +114,17 @@ export function hydrate(container: Element, render: () => Node | DocumentFragmen
112
114
  finally {
113
115
  hydrating = prevHydrating
114
116
  hydrateNode = prevNode
117
+ if (process.env.NODE_ENV !== 'production')
118
+ hydrateRoot = prevRoot
115
119
  }
116
120
 
117
121
  return () => disposeOwner(owner)
118
122
  }
119
123
 
120
- export function mount(container: Element, render: () => Node | DocumentFragment | null): () => void {
124
+ export function mount(container: Element, render: () => Node | DocumentFragment | null, idPrefix?: string): () => void {
121
125
  const owner = createOwner(undefined)
126
+ if (idPrefix !== undefined)
127
+ owner.id = { p: idPrefix, n: 0 }
122
128
  const prevHydrating = hydrating
123
129
  hydrating = false
124
130
  try {
@@ -139,20 +145,46 @@ export function mount(container: Element, render: () => Node | DocumentFragment
139
145
 
140
146
  export function template(html: string, isSvg?: boolean): () => Node {
141
147
  let cached: Node | undefined
148
+ const expectedTag = process.env.NODE_ENV !== 'production'
149
+ ? /^<([A-Za-z][^\s/>]*)/.exec(html)?.[1]?.toLowerCase()
150
+ : undefined
151
+
152
+ const clone = (): Node => {
153
+ if (cached === undefined) {
154
+ const t = document.createElement('template')
155
+ t.innerHTML = isSvg ? `<svg>${html}</svg>` : html
156
+ cached = isSvg ? t.content.firstChild!.firstChild! : t.content.firstChild!
157
+ }
158
+ return cached.cloneNode(true)
159
+ }
142
160
 
143
161
  return (): Node => {
144
162
  if (hydrating && hydrateNode !== null) {
145
163
  const node = hydrateNode
164
+ if (process.env.NODE_ENV !== 'production' && expectedTag !== undefined) {
165
+ const foundTag = node.nodeType === 1 ? (node as Element).tagName.toLowerCase() : undefined
166
+ if (foundTag !== expectedTag) {
167
+ const island = hydrateRoot?.getAttribute('c') ?? hydrateRoot?.tagName.toLowerCase() ?? 'unknown'
168
+ const foundDesc = foundTag !== undefined
169
+ ? `<${foundTag}>`
170
+ : node.nodeType === 3
171
+ ? `#text(${JSON.stringify((node as Text).data.slice(0, 30))})`
172
+ : node.nodeType === 8
173
+ ? `<!--${(node as Comment).data}-->`
174
+ : `nodeType(${node.nodeType})`
175
+ console.warn(`kay: hydration mismatch in island "${island}": expected <${expectedTag}>, found ${foundDesc}; falling back to fresh clone.`)
176
+ hydrating = false
177
+ hydrateNode = null
178
+ const fresh = clone()
179
+ node.parentNode?.replaceChild(fresh, node)
180
+ return fresh
181
+ }
182
+ }
146
183
  hydrateNode = node.nextSibling
147
184
  return node
148
185
  }
149
186
 
150
- if (cached === undefined) {
151
- const t = document.createElement('template')
152
- t.innerHTML = isSvg ? `<svg>${html}</svg>` : html
153
- cached = isSvg ? t.content.firstChild!.firstChild! : t.content.firstChild!
154
- }
155
- return cached.cloneNode(true)
187
+ return clone()
156
188
  }
157
189
  }
158
190
 
@@ -174,16 +206,25 @@ export function eachStatic<T>(
174
206
  ): void {
175
207
  if (!items || items.length === 0)
176
208
  return
209
+ let startMarker: Node | null | undefined
210
+ let insertBefore = marker
211
+ if (marker !== null && marker.nodeType === 8 && (marker as Comment).data === 'k[') {
212
+ const close = skipRange(marker)
213
+ if (close !== null) {
214
+ startMarker = marker
215
+ insertBefore = close
216
+ }
217
+ }
177
218
  if (claimLoop(parent, () => {
178
219
  for (let i = 0; i < items.length; i++)
179
220
  render(items[i]!, i)
180
- }, skip)) {
221
+ }, skip, startMarker)) {
181
222
  return
182
223
  }
183
224
  const f = document.createDocumentFragment()
184
225
  for (let i = 0; i < items.length; i++)
185
226
  f.appendChild(render(items[i]!, i))
186
- parent.insertBefore(f, marker)
227
+ parent.insertBefore(f, insertBefore)
187
228
  }
188
229
 
189
230
  export function firstText(el: Node): Text {
@@ -210,21 +251,37 @@ export function skipText(marker: Node): Node | null {
210
251
  return next !== null && next.nodeType === 3 ? next.nextSibling : next
211
252
  }
212
253
 
213
- /** Advance past an SSR-bracketed dynamic range (`<!---->...<!---->`) to its closing comment marker. */
254
+ /** Advance past an SSR-bracketed dynamic range (`<!--k[-->...<!--k]-->` or `<!---->...<!---->`) to its closing comment marker. */
214
255
  export function skipRange(marker: Node): Node | null {
256
+ const balanced = marker.nodeType === 8 && (marker as Comment).data === 'k['
257
+ let depth = balanced ? 1 : 0
215
258
  let cur = marker.nextSibling
216
- while (cur !== null && cur.nodeType !== 8)
259
+ while (cur !== null) {
260
+ if (cur.nodeType === 8) {
261
+ if (!balanced)
262
+ return cur
263
+ const data = (cur as Comment).data
264
+ if (data === 'k[')
265
+ depth++
266
+ else if (data === 'k]' && --depth === 0)
267
+ return cur
268
+ }
217
269
  cur = cur.nextSibling
218
- return cur
270
+ }
271
+ return null
219
272
  }
220
273
 
221
274
  export function setText(node: Text, value: unknown): void {
275
+ if (process.env.NODE_ENV !== 'production')
276
+ assertRenderable(value, 'text')
222
277
  const str = value == null || typeof value === 'boolean' ? '' : String(value)
223
278
  if (node.data !== str)
224
279
  node.data = str
225
280
  }
226
281
 
227
282
  export function setClass(el: Element, value: unknown): void {
283
+ if (process.env.NODE_ENV !== 'production')
284
+ assertRenderable(value, 'class')
228
285
  const str = value == null || value === false ? '' : String(value)
229
286
  if (typeof el.className === 'string') {
230
287
  if (el.className !== str) {
@@ -242,32 +299,11 @@ export function setClass(el: Element, value: unknown): void {
242
299
  }
243
300
  }
244
301
 
245
- const VALID_ATTR_NAME = /^[A-Za-z_:][-\w:.]*$/
246
- const URL_ATTRS = new Set(['href', 'src', 'action', 'formaction', 'xlink:href'])
247
- const UNSAFE_URL_RE = /^(?:javascript|vbscript):/i
248
- const STRING_BOOL_ATTRS = new Set(['draggable', 'spellcheck', 'contenteditable'])
249
-
250
- function isBlockedAttr(name: string): boolean {
251
- if (!VALID_ATTR_NAME.test(name))
252
- return true
253
- const lower = name.toLowerCase()
254
- return lower.startsWith('on') || lower === 'innerhtml' || lower === 'srcdoc'
255
- }
256
-
257
- function isStringBoolAttr(name: string): boolean {
258
- const lower = name.toLowerCase()
259
- return lower.startsWith('aria-') || STRING_BOOL_ATTRS.has(lower)
260
- }
261
-
262
- function isUnsafeUrlAttr(name: string, value: unknown): boolean {
263
- if (!URL_ATTRS.has(name.toLowerCase()))
264
- return false
265
- return UNSAFE_URL_RE.test(String(value).replace(/[\u0000-\u0020]+/g, ''))
266
- }
267
-
268
302
  export function setAttr(el: Element, name: string, value: unknown): void {
269
303
  if (isBlockedAttr(name))
270
304
  return
305
+ if (process.env.NODE_ENV !== 'production')
306
+ assertRenderable(value, name)
271
307
  if (value == null) {
272
308
  el.removeAttribute(name)
273
309
  }
@@ -299,8 +335,7 @@ export function setStyle(el: HTMLElement, value: unknown): void {
299
335
  el.style.cssText = value
300
336
  }
301
337
  else if (typeof value === 'object') {
302
- for (const [k, v] of Object.entries(value as Record<string, unknown>))
303
- el.style.setProperty(k, v == null ? '' : String(v))
338
+ el.style.cssText = styleText(value as Record<string, unknown>)
304
339
  }
305
340
  }
306
341
 
@@ -316,7 +351,7 @@ function dispatchEvent(event: Event): void {
316
351
 
317
352
  while (node !== null) {
318
353
  const handler = node[key]
319
- if (handler !== undefined) {
354
+ if (typeof handler === 'function') {
320
355
  current = node
321
356
  batch(() => handler(event))
322
357
  if (event.cancelBubble)
@@ -338,7 +373,9 @@ export function delegate(events: string[]): void {
338
373
  }
339
374
  }
340
375
 
341
- export function on(el: Node, event: string, handler: (e: Event) => void): void {
376
+ export function on(el: Node, event: string, handler: ((e: Event) => void) | false | null | undefined): void {
377
+ if (typeof handler !== 'function')
378
+ return
342
379
  el.addEventListener(event, (e) => {
343
380
  batch(() => handler(e))
344
381
  })