@slim-lang/core 1.2.11 → 1.2.13
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.
- package/README.md +522 -14
- package/package.json +2 -6
- package/packages/slim/server/main.slim +433 -422
- package/run-dev-slim.js +10 -5
- package/run-slim.js +10 -20
- package/src/bin/api/github_get.js +7 -12
- package/src/bin/api/github_req.js +54 -241
- package/src/bin/api/lock.js +2 -1
- package/src/bin/api/spm.js +20 -91
- package/src/bin/cli.js +73 -102
- package/src/bin/config.default.json +11 -11
- package/src/bin/helpers.js +2 -7
- package/src/bin/spm.js +3 -7
- package/src/checker.js +1705 -388
- package/src/compile.js +131 -100
- package/src/external/classErrors.js +5 -6
- package/src/external/client.js +0 -2
- package/src/external/core.js +355 -237
- package/src/external/defaults.js +12 -4
- package/src/external/dom.js +663 -0
- package/src/external/helpers.js +87 -58
- package/src/external/reactive.js +601 -0
- package/src/external/router.js +117 -0
- package/src/external/slim-globals.d.ts +43 -4
- package/src/external/typeSyntax.js +258 -0
- package/src/external/types.js +1 -1
- package/src/format.js +183 -12
- package/src/handlers/errorHandler.js +9 -36
- package/src/jsdoc.js +36 -16
- package/src/modulePaths.js +15 -5
- package/src/reactivity.js +774 -0
- package/src/repl.js +1 -1
- package/src/suggest.js +60 -0
- package/src/syntax/babel.js +46 -0
- package/src/syntax/index.js +39 -0
- package/src/syntax/lower.js +666 -0
- package/src/syntax/markup.js +195 -0
- package/src/syntax/plugin.js +903 -0
- package/src/syntax/types.js +106 -0
- package/src/test-runner.js +5 -5
- package/src/transform.js +209 -244
- package/packages/slim/helpers/string.js +0 -43
- package/packages/slim/types/hash.js +0 -11
- package/src/handlers/parser/components.js +0 -295
- package/src/handlers/parserHandler.js +0 -789
- package/src/lexer.js +0 -174
- package/src/parser.js +0 -803
- package/src/sourcemap.js +0 -0
package/src/external/defaults.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import fs from "fs"
|
|
2
1
|
import { setSourceReader } from "./classErrors.js"
|
|
3
2
|
import {
|
|
4
3
|
htmlToVdom,
|
|
@@ -10,14 +9,23 @@ import {
|
|
|
10
9
|
__create_host__,
|
|
11
10
|
__adopt_into__,
|
|
12
11
|
__define_element__,
|
|
13
|
-
__resolve_bem__
|
|
12
|
+
__resolve_bem__,
|
|
13
|
+
raw,
|
|
14
|
+
escapeHtml
|
|
14
15
|
} from "./helpers.js"
|
|
16
|
+
import { DOM_GLOBALS } from "./dom.js"
|
|
17
|
+
import { ROUTER_GLOBALS } from "./router.js"
|
|
15
18
|
|
|
16
19
|
export * from "./core.js"
|
|
17
20
|
|
|
18
|
-
|
|
21
|
+
if (typeof process !== "undefined" && process.versions?.node) {
|
|
22
|
+
const { readFileSync } = await import("node:fs")
|
|
23
|
+
setSourceReader(file => readFileSync(file, "utf8"))
|
|
24
|
+
}
|
|
19
25
|
|
|
20
26
|
Object.assign(globalThis, {
|
|
21
27
|
htmlToVdom, HTMLElement, __html__, __flush_events__, __bind_events__, __lifecycle__,
|
|
22
|
-
__create_host__, __adopt_into__, __define_element__, __resolve_bem__
|
|
28
|
+
__create_host__, __adopt_into__, __define_element__, __resolve_bem__, raw, escapeHtml,
|
|
29
|
+
...DOM_GLOBALS,
|
|
30
|
+
...ROUTER_GLOBALS
|
|
23
31
|
})
|
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
import { SafeHtml, VNode, htmlToVdom, slimDocument, parseAttributeList } from "./helpers.js"
|
|
2
|
+
import {
|
|
3
|
+
Owner, Signal, __render_effect__, __computed__, __component__, batch, runWithOwner, untrack, onCleanup, changedInPlace,
|
|
4
|
+
setServerRendering, setRootHook, setServerData, setHydrationData
|
|
5
|
+
} from "./reactive.js"
|
|
6
|
+
|
|
7
|
+
const SVG_NS = "http://www.w3.org/2000/svg"
|
|
8
|
+
const START = "["
|
|
9
|
+
const END = "]"
|
|
10
|
+
|
|
11
|
+
let serverRendering = false
|
|
12
|
+
let cursor = null
|
|
13
|
+
|
|
14
|
+
const isComment = (node, data) => node?.nodeType === 8 && node.data === data
|
|
15
|
+
|
|
16
|
+
function build(node, doc, svg, server) {
|
|
17
|
+
if (typeof node === "string") return node === "" && server ? doc.createComment(END) : doc.createTextNode(node)
|
|
18
|
+
const [tag, attrs] = node
|
|
19
|
+
if (tag === "#comment") return doc.createComment(attrs)
|
|
20
|
+
|
|
21
|
+
const inSvg = svg || tag === "svg"
|
|
22
|
+
const el = inSvg ? doc.createElementNS(SVG_NS, tag) : doc.createElement(tag)
|
|
23
|
+
if (attrs) for (const name in attrs) el.setAttribute(name, attrs[name] === true ? "" : attrs[name])
|
|
24
|
+
const childSvg = inSvg && tag !== "foreignObject"
|
|
25
|
+
for (let i = 2; i < node.length; i++) el.appendChild(build(node[i], doc, childSvg, server))
|
|
26
|
+
return el
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function __template__(roots, svg = false) {
|
|
30
|
+
const protos = {}
|
|
31
|
+
return () => {
|
|
32
|
+
if (cursor) return claimTemplate(roots)
|
|
33
|
+
const mode = serverRendering ? "server" : "client"
|
|
34
|
+
let proto = protos[mode]
|
|
35
|
+
if (!proto) {
|
|
36
|
+
const doc = slimDocument
|
|
37
|
+
if (roots.length === 1) {
|
|
38
|
+
proto = build(roots[0], doc, svg, serverRendering)
|
|
39
|
+
} else {
|
|
40
|
+
proto = doc.createDocumentFragment()
|
|
41
|
+
for (const root of roots) proto.appendChild(build(root, doc, svg, serverRendering))
|
|
42
|
+
}
|
|
43
|
+
protos[mode] = proto
|
|
44
|
+
}
|
|
45
|
+
const copy = proto.cloneNode(true)
|
|
46
|
+
if (copy.nodeType === 11) copy.toString = fragmentToHtml
|
|
47
|
+
return copy
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function fragmentToHtml() {
|
|
52
|
+
let out = ""
|
|
53
|
+
for (const child of this.__slimNodes ?? this.childNodes) out += child.toString()
|
|
54
|
+
return out
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
function stepOver(node) {
|
|
59
|
+
if (!cursor || !isComment(node, START)) return node
|
|
60
|
+
let depth = 0
|
|
61
|
+
for (let next = node.nextSibling; next; next = next.nextSibling) {
|
|
62
|
+
if (isComment(next, START)) depth++
|
|
63
|
+
else if (isComment(next, END)) {
|
|
64
|
+
if (depth === 0) return next
|
|
65
|
+
depth--
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return node
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function __child__(parent, index) {
|
|
72
|
+
let node = stepOver(parent.firstChild)
|
|
73
|
+
for (let i = 0; i < index; i++) node = stepOver(node.nextSibling)
|
|
74
|
+
return node
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function __next__(node, count) {
|
|
78
|
+
for (let i = 0; i < count; i++) node = stepOver(node.nextSibling)
|
|
79
|
+
return node
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
function groupBefore(end) {
|
|
84
|
+
const content = []
|
|
85
|
+
let depth = 0
|
|
86
|
+
let node = end.previousSibling
|
|
87
|
+
for (; node; node = node.previousSibling) {
|
|
88
|
+
if (isComment(node, END)) depth++
|
|
89
|
+
else if (isComment(node, START)) {
|
|
90
|
+
if (depth === 0) break
|
|
91
|
+
depth--
|
|
92
|
+
}
|
|
93
|
+
content.unshift(node)
|
|
94
|
+
}
|
|
95
|
+
node?.parentNode.removeChild(node)
|
|
96
|
+
return content
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function skipGroup(nodes, index) {
|
|
100
|
+
let depth = 0
|
|
101
|
+
for (let i = index + 1; i < nodes.length; i++) {
|
|
102
|
+
if (isComment(nodes[i], START)) depth++
|
|
103
|
+
else if (isComment(nodes[i], END)) {
|
|
104
|
+
if (depth === 0) return i
|
|
105
|
+
depth--
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return nodes.length - 1
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const isBlank = node => node.nodeType === 3 && node.data.trim() === ""
|
|
112
|
+
|
|
113
|
+
function claim(expectElement) {
|
|
114
|
+
const { nodes } = cursor
|
|
115
|
+
while (cursor.index < nodes.length) {
|
|
116
|
+
const node = nodes[cursor.index]
|
|
117
|
+
if (expectElement && (isBlank(node) || (node.nodeType === 8 && node.data !== START && node.data !== END))) {
|
|
118
|
+
cursor.index++
|
|
119
|
+
continue
|
|
120
|
+
}
|
|
121
|
+
if (isComment(node, START)) {
|
|
122
|
+
const end = skipGroup(nodes, cursor.index)
|
|
123
|
+
cursor.index = end + 1
|
|
124
|
+
return nodes[end]
|
|
125
|
+
}
|
|
126
|
+
cursor.index++
|
|
127
|
+
return node
|
|
128
|
+
}
|
|
129
|
+
return slimDocument.createTextNode("")
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function claimed(first, last) {
|
|
133
|
+
return {
|
|
134
|
+
nodeType: 11,
|
|
135
|
+
firstChild: first,
|
|
136
|
+
get __slimNodes() {
|
|
137
|
+
const nodes = []
|
|
138
|
+
for (let node = first; node; node = node.nextSibling) {
|
|
139
|
+
nodes.push(node)
|
|
140
|
+
if (node === last) break
|
|
141
|
+
}
|
|
142
|
+
return nodes
|
|
143
|
+
},
|
|
144
|
+
get childNodes() { return this.__slimNodes }
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function claimTemplate(roots) {
|
|
149
|
+
const nodes = roots.map(root => claim(Array.isArray(root) && root[0] !== "#comment"))
|
|
150
|
+
return nodes.length === 1 ? nodes[0] : claimed(nodes[0], nodes[nodes.length - 1])
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function withCursor(nodes, fn) {
|
|
154
|
+
const previous = cursor
|
|
155
|
+
cursor = { nodes, index: 0 }
|
|
156
|
+
try {
|
|
157
|
+
return fn()
|
|
158
|
+
} finally {
|
|
159
|
+
cursor = previous
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function claimedContent(anchor) {
|
|
164
|
+
return isComment(anchor, END) ? groupBefore(anchor) : null
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
const nodesOfHtml = new WeakMap()
|
|
169
|
+
|
|
170
|
+
function toNodes(value, out) {
|
|
171
|
+
if (value == null || value === false || value === true) return out
|
|
172
|
+
if (typeof value === "function" && value.__slimLive) return toNodes(__root__(value), out)
|
|
173
|
+
if (Array.isArray(value)) {
|
|
174
|
+
for (const item of value) toNodes(item, out)
|
|
175
|
+
return out
|
|
176
|
+
}
|
|
177
|
+
if (typeof value === "object") {
|
|
178
|
+
if (typeof value.nodeType === "number") {
|
|
179
|
+
if (value.nodeType !== 11) out.push(value)
|
|
180
|
+
else out.push(...(value.__slimNodes ??= [...value.childNodes]))
|
|
181
|
+
return out
|
|
182
|
+
}
|
|
183
|
+
if (value instanceof SafeHtml || value instanceof VNode) {
|
|
184
|
+
let nodes = nodesOfHtml.get(value)
|
|
185
|
+
if (!nodes) {
|
|
186
|
+
const element = (value instanceof VNode ? value : htmlToVdom(value)).toElement(slimDocument)
|
|
187
|
+
nodes = element.nodeType === 11 ? [...element.childNodes] : [element]
|
|
188
|
+
nodesOfHtml.set(value, nodes)
|
|
189
|
+
}
|
|
190
|
+
out.push(...nodes)
|
|
191
|
+
return out
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
out.push(slimDocument.createTextNode(String(value)))
|
|
195
|
+
return out
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const isText = value => typeof value === "string" || typeof value === "number" || typeof value === "bigint"
|
|
199
|
+
|
|
200
|
+
function put(anchor, value, current) {
|
|
201
|
+
const parent = anchor.parentNode
|
|
202
|
+
if (isText(value)) {
|
|
203
|
+
const text = String(value)
|
|
204
|
+
if (anchor.nodeType === 3) {
|
|
205
|
+
if (current.length) remove(current)
|
|
206
|
+
if (anchor.data !== text) anchor.data = text
|
|
207
|
+
return EMPTY
|
|
208
|
+
}
|
|
209
|
+
let node = current.length === 1 && current[0].nodeType === 3 ? current[0] : null
|
|
210
|
+
if (!node) {
|
|
211
|
+
remove(current)
|
|
212
|
+
node = slimDocument.createTextNode(text)
|
|
213
|
+
} else if (node.data !== text) {
|
|
214
|
+
node.data = text
|
|
215
|
+
}
|
|
216
|
+
if (node.parentNode !== parent || node.nextSibling !== anchor) parent.insertBefore(node, anchor)
|
|
217
|
+
return [node]
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (anchor.nodeType === 3 && anchor.data !== "") anchor.data = ""
|
|
221
|
+
const next = toNodes(value, [])
|
|
222
|
+
if (next.length === 0) {
|
|
223
|
+
if (current.length) remove(current)
|
|
224
|
+
return EMPTY
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (current.length) {
|
|
228
|
+
const keep = new Set(next)
|
|
229
|
+
for (const node of current) if (!keep.has(node)) node.parentNode?.removeChild(node)
|
|
230
|
+
}
|
|
231
|
+
let ref = anchor
|
|
232
|
+
for (let i = next.length - 1; i >= 0; i--) {
|
|
233
|
+
const node = next[i]
|
|
234
|
+
if (node.nextSibling !== ref || node.parentNode !== parent) parent.insertBefore(node, ref)
|
|
235
|
+
ref = node
|
|
236
|
+
}
|
|
237
|
+
return next
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const EMPTY = []
|
|
241
|
+
|
|
242
|
+
function remove(nodes) {
|
|
243
|
+
for (const node of nodes) {
|
|
244
|
+
if (node.__leave) leave(node)
|
|
245
|
+
else node.parentNode?.removeChild(node)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function leave(node) {
|
|
250
|
+
const className = node.__leave
|
|
251
|
+
node.__leave = null
|
|
252
|
+
node.classList.add(className)
|
|
253
|
+
const animations = typeof node.getAnimations === "function" ? node.getAnimations({ subtree: true }) : []
|
|
254
|
+
if (animations.length === 0) {
|
|
255
|
+
node.parentNode?.removeChild(node)
|
|
256
|
+
return
|
|
257
|
+
}
|
|
258
|
+
Promise.all(animations.map(animation => animation.finished.catch(() => {})))
|
|
259
|
+
.then(() => node.parentNode?.removeChild(node))
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export function __leave__(el, className) {
|
|
263
|
+
if (!serverRendering) el.__leave = className
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export function __enter__(el, className) {
|
|
267
|
+
if (serverRendering || cursor) return
|
|
268
|
+
el.classList.add(className)
|
|
269
|
+
const done = event => {
|
|
270
|
+
if (event.target !== el) return
|
|
271
|
+
el.classList.remove(className)
|
|
272
|
+
el.removeEventListener("animationend", done)
|
|
273
|
+
el.removeEventListener("transitionend", done)
|
|
274
|
+
}
|
|
275
|
+
el.addEventListener("animationend", done)
|
|
276
|
+
el.addEventListener("transitionend", done)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function __once__(anchor, value) {
|
|
280
|
+
if (serverRendering) anchor.parentNode.insertBefore(slimDocument.createComment(START), anchor)
|
|
281
|
+
const content = cursor ? claimedContent(anchor) : null
|
|
282
|
+
put(anchor, value, content ?? EMPTY)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function __insert__(anchor, fn) {
|
|
286
|
+
if (serverRendering) anchor.parentNode.insertBefore(slimDocument.createComment(START), anchor)
|
|
287
|
+
let current = EMPTY
|
|
288
|
+
let content = cursor ? claimedContent(anchor) : null
|
|
289
|
+
__render_effect__(() => {
|
|
290
|
+
if (content) {
|
|
291
|
+
const claimed = content
|
|
292
|
+
content = null
|
|
293
|
+
current = claimed
|
|
294
|
+
current = put(anchor, withCursor(claimed, fn), current)
|
|
295
|
+
return
|
|
296
|
+
}
|
|
297
|
+
current = put(anchor, fn(), current)
|
|
298
|
+
})
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export function __root__(fn) {
|
|
302
|
+
if (cursor && isComment(cursor.nodes[cursor.index], START)) return hydrateRoot(fn)
|
|
303
|
+
const value = __computed__(fn)
|
|
304
|
+
const first = value.peek()
|
|
305
|
+
if (!value.sources) {
|
|
306
|
+
const nodes = toNodes(isText(first) ? slimDocument.createTextNode(String(first)) : first, [])
|
|
307
|
+
if (nodes.length === 1) return nodes[0]
|
|
308
|
+
}
|
|
309
|
+
const fragment = slimDocument.createDocumentFragment()
|
|
310
|
+
fragment.toString = fragmentToHtml
|
|
311
|
+
const anchor = serverRendering ? slimDocument.createComment(END) : slimDocument.createTextNode("")
|
|
312
|
+
fragment.appendChild(anchor)
|
|
313
|
+
__insert__(anchor, () => value.v)
|
|
314
|
+
return fragment
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function hydrateRoot(fn) {
|
|
318
|
+
const anchor = claim(false)
|
|
319
|
+
let first = anchor
|
|
320
|
+
for (let node = anchor.previousSibling; node && !isComment(node, START); node = node.previousSibling) first = node
|
|
321
|
+
const firstOf = first === anchor ? null : first
|
|
322
|
+
__insert__(anchor, fn)
|
|
323
|
+
return claimed(firstOf?.parentNode ? firstOf : anchor, anchor)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export function __branch__(test, yes, no) {
|
|
327
|
+
const truthy = __computed__(() => !!test())
|
|
328
|
+
return () => truthy.v ? yes() : no ? no() : null
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
const PROPERTIES = new Set(["value", "checked", "selected", "muted", "indeterminate", "disabled"])
|
|
333
|
+
|
|
334
|
+
function setAttribute(el, name, value) {
|
|
335
|
+
if (PROPERTIES.has(name) && name in el) {
|
|
336
|
+
const next = name === "value" ? (value == null ? "" : String(value)) : value === "" ? true : !!value
|
|
337
|
+
if (el[name] !== next) el[name] = next
|
|
338
|
+
}
|
|
339
|
+
if (typeof value === "boolean" && (name.startsWith("data-") || name.startsWith("aria-"))) el.setAttribute(name, String(value))
|
|
340
|
+
else if (value === true) el.setAttribute(name, "")
|
|
341
|
+
else if (value == null || value === false) el.removeAttribute(name)
|
|
342
|
+
else el.setAttribute(name, String(value))
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
export function __set__(el, name, value) {
|
|
346
|
+
setAttribute(el, name, value)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
export function __attr__(el, name, fn) {
|
|
350
|
+
let previous
|
|
351
|
+
__render_effect__(() => {
|
|
352
|
+
const value = fn()
|
|
353
|
+
if (Object.is(value, previous) && previous !== undefined) return
|
|
354
|
+
previous = value
|
|
355
|
+
setAttribute(el, name, value)
|
|
356
|
+
})
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export function __s__(value) {
|
|
360
|
+
if (value == null || value === false) return ""
|
|
361
|
+
if (value instanceof SafeHtml) return value.value
|
|
362
|
+
return String(value)
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
export function __spread__(el, fn) {
|
|
366
|
+
let previous = {}
|
|
367
|
+
__render_effect__(() => {
|
|
368
|
+
const value = fn()
|
|
369
|
+
const next = value == null || value === false ? {}
|
|
370
|
+
: typeof value === "object" && !(value instanceof SafeHtml) ? value
|
|
371
|
+
: parseAttributeList(__s__(value))
|
|
372
|
+
for (const name in previous) if (!(name in next)) el.removeAttribute(name)
|
|
373
|
+
for (const name in next) if (next[name] !== previous[name]) setAttribute(el, name, next[name])
|
|
374
|
+
previous = next
|
|
375
|
+
})
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const DELEGATED = new Set([
|
|
379
|
+
"click", "dblclick", "contextmenu", "input", "change", "submit", "keydown", "keyup",
|
|
380
|
+
"pointerdown", "pointerup", "pointermove", "mousedown", "mouseup", "touchstart", "touchend", "focusin", "focusout"
|
|
381
|
+
])
|
|
382
|
+
const listening = new Set()
|
|
383
|
+
|
|
384
|
+
function dispatch(e) {
|
|
385
|
+
const key = "$$" + e.type
|
|
386
|
+
batch(() => {
|
|
387
|
+
for (const node of e.composedPath()) {
|
|
388
|
+
const handler = node[key]
|
|
389
|
+
if (handler) {
|
|
390
|
+
Object.defineProperty(e, "currentTarget", { configurable: true, get: () => node })
|
|
391
|
+
handler.call(node, e)
|
|
392
|
+
if (e.cancelBubble) break
|
|
393
|
+
}
|
|
394
|
+
if (node === slimDocument) break
|
|
395
|
+
}
|
|
396
|
+
})
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function dispatchOutside(e) {
|
|
400
|
+
if (e.composedPath().includes(slimDocument)) return
|
|
401
|
+
dispatch(e)
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
setRootHook(value => {
|
|
405
|
+
if (!value || typeof value.nodeType !== "number" || listening.size === 0) return
|
|
406
|
+
const roots = value.nodeType === 11 ? [...(value.__slimNodes ?? value.childNodes)] : [value]
|
|
407
|
+
for (const root of roots) {
|
|
408
|
+
if (root.nodeType !== 1 || root.isConnected) continue
|
|
409
|
+
for (const event of listening) root.addEventListener(event, dispatchOutside)
|
|
410
|
+
}
|
|
411
|
+
})
|
|
412
|
+
|
|
413
|
+
export function __on__(el, event, handler) {
|
|
414
|
+
if (typeof handler !== "function" || serverRendering) return
|
|
415
|
+
if (DELEGATED.has(event) && typeof document !== "undefined" && slimDocument === document) {
|
|
416
|
+
el["$$" + event] = handler
|
|
417
|
+
if (!listening.has(event)) {
|
|
418
|
+
listening.add(event)
|
|
419
|
+
slimDocument.addEventListener(event, dispatch)
|
|
420
|
+
}
|
|
421
|
+
return
|
|
422
|
+
}
|
|
423
|
+
el.addEventListener(event, function (e) {
|
|
424
|
+
return batch(() => handler.call(el, e))
|
|
425
|
+
})
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
export function Portal(props = {}) {
|
|
430
|
+
return __component__(() => {
|
|
431
|
+
const placeholder = slimDocument.createTextNode("")
|
|
432
|
+
if (serverRendering || cursor) return placeholder
|
|
433
|
+
const to = props.to
|
|
434
|
+
const target = typeof to === "string" ? slimDocument.querySelector(to) : to ?? slimDocument.body
|
|
435
|
+
if (!target) throw new Error(`Portal: nothing matches "${to}"`)
|
|
436
|
+
const shown = toNodes(props.children, [])
|
|
437
|
+
for (const node of shown) target.appendChild(node)
|
|
438
|
+
onCleanup(() => remove(shown))
|
|
439
|
+
return placeholder
|
|
440
|
+
})
|
|
441
|
+
}
|
|
442
|
+
Portal.__component__ = true
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
export function __move__(el, duration) {
|
|
446
|
+
if (!serverRendering) el.__move = Number(duration) || 200
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
function positions(rows) {
|
|
450
|
+
let found = null
|
|
451
|
+
for (const row of rows) {
|
|
452
|
+
const node = row?.nodes[0]
|
|
453
|
+
if (node?.__move && node.isConnected) (found ??= new Map()).set(node, node.getBoundingClientRect())
|
|
454
|
+
}
|
|
455
|
+
return found
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
function slide(before) {
|
|
459
|
+
for (const [node, first] of before) {
|
|
460
|
+
if (!node.isConnected || typeof node.animate !== "function") continue
|
|
461
|
+
const last = node.getBoundingClientRect()
|
|
462
|
+
const dx = first.left - last.left
|
|
463
|
+
const dy = first.top - last.top
|
|
464
|
+
if (dx === 0 && dy === 0) continue
|
|
465
|
+
node.animate([{ transform: `translate(${dx}px, ${dy}px)` }, { transform: "none" }], { duration: node.__move, easing: "ease" })
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
export function __list__(anchor, source, render, keyOf, withIndex = false) {
|
|
471
|
+
if (serverRendering) anchor.parentNode.insertBefore(slimDocument.createComment(START), anchor)
|
|
472
|
+
const scope = new Owner()
|
|
473
|
+
let rows = []
|
|
474
|
+
scope.cleanups = [() => { for (const row of rows) row?.owner.dispose() }]
|
|
475
|
+
const options = { render, keyOf, withIndex }
|
|
476
|
+
let content = cursor ? claimedContent(anchor) : null
|
|
477
|
+
|
|
478
|
+
__render_effect__(() => {
|
|
479
|
+
const items = source()
|
|
480
|
+
untrack(() => {
|
|
481
|
+
const update = () => reconcile(anchor, rows, items == null ? [] : Array.from(items), options)
|
|
482
|
+
if (content) {
|
|
483
|
+
const nodes = content
|
|
484
|
+
content = null
|
|
485
|
+
const list = items == null ? [] : Array.from(items)
|
|
486
|
+
rows = runWithOwner(scope, () => withCursor(nodes, () =>
|
|
487
|
+
list.map((item, index) => makeRow(item, index, options, keyOf ? keyOf(item, index) : item))))
|
|
488
|
+
} else {
|
|
489
|
+
rows = runWithOwner(scope, update)
|
|
490
|
+
}
|
|
491
|
+
})
|
|
492
|
+
})
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function makeRow(item, index, { render, withIndex }, key) {
|
|
496
|
+
const row = { key, item: new Signal(item), index: withIndex ? new Signal(index) : null, nodes: null, owner: new Owner(undefined, false) }
|
|
497
|
+
const value = runWithOwner(row.owner, () => render(row.item, row.index))
|
|
498
|
+
row.nodes = isText(value) ? [cursor ? claim(false) : slimDocument.createTextNode(String(value))] : toNodes(value, [])
|
|
499
|
+
if (row.nodes.length === 0) row.nodes.push(slimDocument.createTextNode(""))
|
|
500
|
+
return row
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function reconcile(anchor, previous, items, options) {
|
|
504
|
+
const { keyOf } = options
|
|
505
|
+
const parent = anchor.parentNode
|
|
506
|
+
const next = new Array(items.length)
|
|
507
|
+
|
|
508
|
+
if (items.length === 0) {
|
|
509
|
+
if (previous.length === 0) return next
|
|
510
|
+
const first = previous[0].nodes[0]
|
|
511
|
+
const leaving = previous.some(row => row.nodes.some(node => node.__leave))
|
|
512
|
+
if (!leaving && parent.firstChild === first && parent.lastChild === anchor) {
|
|
513
|
+
parent.textContent = ""
|
|
514
|
+
parent.appendChild(anchor)
|
|
515
|
+
} else {
|
|
516
|
+
for (const row of previous) remove(row.nodes)
|
|
517
|
+
}
|
|
518
|
+
for (const row of previous) row.owner.dispose()
|
|
519
|
+
return next
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
const byKey = new Map()
|
|
523
|
+
previous.forEach((row, index) => {
|
|
524
|
+
const same = byKey.get(row.key)
|
|
525
|
+
if (same) same.push(index)
|
|
526
|
+
else byKey.set(row.key, [index])
|
|
527
|
+
})
|
|
528
|
+
|
|
529
|
+
const oldIndex = new Int32Array(items.length).fill(-1)
|
|
530
|
+
for (let i = 0; i < items.length; i++) {
|
|
531
|
+
const item = items[i]
|
|
532
|
+
const key = keyOf ? keyOf(item, i) : item
|
|
533
|
+
const same = byKey.get(key)
|
|
534
|
+
if (same && same.length) {
|
|
535
|
+
const index = same.shift()
|
|
536
|
+
const row = previous[index]
|
|
537
|
+
previous[index] = null
|
|
538
|
+
oldIndex[i] = index
|
|
539
|
+
if (!Object.is(row.item.value, item)) row.item.v = item
|
|
540
|
+
else if (changedInPlace(item)) row.item.notify()
|
|
541
|
+
if (row.index) row.index.v = i
|
|
542
|
+
next[i] = row
|
|
543
|
+
} else {
|
|
544
|
+
next[i] = makeRow(item, i, options, key)
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
for (const row of previous) {
|
|
549
|
+
if (!row) continue
|
|
550
|
+
remove(row.nodes)
|
|
551
|
+
row.owner.dispose()
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const before = positions(next)
|
|
555
|
+
const stable = longestIncreasing(oldIndex)
|
|
556
|
+
let ref = anchor
|
|
557
|
+
let i = next.length - 1
|
|
558
|
+
while (i >= 0) {
|
|
559
|
+
if (stable.has(i)) {
|
|
560
|
+
ref = next[i].nodes[0]
|
|
561
|
+
i--
|
|
562
|
+
continue
|
|
563
|
+
}
|
|
564
|
+
let start = i
|
|
565
|
+
while (start > 0 && !stable.has(start - 1)) start--
|
|
566
|
+
if (start === i) {
|
|
567
|
+
for (const node of next[i].nodes) parent.insertBefore(node, ref)
|
|
568
|
+
} else {
|
|
569
|
+
const fragment = slimDocument.createDocumentFragment()
|
|
570
|
+
for (let k = start; k <= i; k++) for (const node of next[k].nodes) fragment.appendChild(node)
|
|
571
|
+
parent.insertBefore(fragment, ref)
|
|
572
|
+
}
|
|
573
|
+
ref = next[start].nodes[0]
|
|
574
|
+
i = start - 1
|
|
575
|
+
}
|
|
576
|
+
if (before) slide(before)
|
|
577
|
+
return next
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
function longestIncreasing(values) {
|
|
581
|
+
const tails = []
|
|
582
|
+
const previous = new Int32Array(values.length)
|
|
583
|
+
for (let i = 0; i < values.length; i++) {
|
|
584
|
+
const value = values[i]
|
|
585
|
+
if (value < 0) continue
|
|
586
|
+
let low = 0
|
|
587
|
+
let high = tails.length
|
|
588
|
+
while (low < high) {
|
|
589
|
+
const mid = (low + high) >> 1
|
|
590
|
+
if (values[tails[mid]] < value) low = mid + 1
|
|
591
|
+
else high = mid
|
|
592
|
+
}
|
|
593
|
+
previous[i] = low > 0 ? tails[low - 1] : -1
|
|
594
|
+
tails[low] = i
|
|
595
|
+
}
|
|
596
|
+
const result = new Set()
|
|
597
|
+
let i = tails.length ? tails[tails.length - 1] : -1
|
|
598
|
+
while (i >= 0) {
|
|
599
|
+
result.add(i)
|
|
600
|
+
i = previous[i]
|
|
601
|
+
}
|
|
602
|
+
return result
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
|
|
606
|
+
export function renderToString(render) {
|
|
607
|
+
const scope = new Owner(null)
|
|
608
|
+
serverRendering = true
|
|
609
|
+
setServerRendering(true)
|
|
610
|
+
try {
|
|
611
|
+
const value = runWithOwner(scope, render)
|
|
612
|
+
if (value instanceof SafeHtml) return value.value
|
|
613
|
+
if (value == null || typeof value !== "object" || typeof value.nodeType !== "number") return String(value ?? "")
|
|
614
|
+
const box = slimDocument.createElement("div")
|
|
615
|
+
box.appendChild(value)
|
|
616
|
+
return box.innerHTML
|
|
617
|
+
} finally {
|
|
618
|
+
serverRendering = false
|
|
619
|
+
setServerRendering(false)
|
|
620
|
+
scope.dispose()
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
export async function renderToStringAsync(render, { passes = 5 } = {}) {
|
|
625
|
+
const data = { results: [], pending: [], index: 0 }
|
|
626
|
+
for (let pass = 1; ; pass++) {
|
|
627
|
+
data.index = 0
|
|
628
|
+
data.pending = []
|
|
629
|
+
setServerData(data)
|
|
630
|
+
let html
|
|
631
|
+
try {
|
|
632
|
+
html = renderToString(render)
|
|
633
|
+
} finally {
|
|
634
|
+
setServerData(null)
|
|
635
|
+
}
|
|
636
|
+
if (data.pending.length === 0 || pass === passes) {
|
|
637
|
+
const sent = data.results.map(result => result && (result.error !== undefined
|
|
638
|
+
? { error: String(result.error?.message ?? result.error) }
|
|
639
|
+
: { value: result.value }))
|
|
640
|
+
const json = JSON.stringify(sent).replace(/</g, "\\u003c")
|
|
641
|
+
return { html, data: sent, script: `<script>window.__slimData = ${json}</script>` }
|
|
642
|
+
}
|
|
643
|
+
await Promise.all(data.pending)
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
export function hydrate(render, container, data = globalThis.__slimData) {
|
|
648
|
+
const previous = cursor
|
|
649
|
+
cursor = { nodes: [...container.childNodes], index: 0 }
|
|
650
|
+
setHydrationData(Array.isArray(data) ? { values: data, index: 0 } : null)
|
|
651
|
+
try {
|
|
652
|
+
return render()
|
|
653
|
+
} finally {
|
|
654
|
+
cursor = previous
|
|
655
|
+
setHydrationData(null)
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
export const DOM_GLOBALS = {
|
|
660
|
+
__child__, __next__, __template__, __insert__, __once__, __root__, __branch__, __attr__, __set__, __s__, __spread__, __on__, __list__,
|
|
661
|
+
__enter__, __leave__, __move__, Portal,
|
|
662
|
+
renderToString, renderToStringAsync, hydrate
|
|
663
|
+
}
|