jq79 0.7.11 → 0.7.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 +1 -1
- package/dist/jq79-sw.js +3 -3
- package/dist/jq79.cjs +15 -16
- package/dist/jq79.cjs.map +1 -1
- package/dist/jq79.d.ts +1 -1
- package/dist/jq79.global.js +15 -16
- package/dist/jq79.global.js.map +1 -1
- package/dist/jq79.js +15 -16
- package/dist/jq79.js.map +1 -1
- package/dist/precompile.cjs +4 -4
- package/dist/precompile.cjs.map +1 -1
- package/dist/precompile.js +4 -4
- package/dist/precompile.js.map +1 -1
- package/dist/reactive.d.ts +1 -0
- package/dist/source.d.ts +1 -1
- package/package.json +1 -1
- package/src/jq79.ts +383 -43
- package/src/precompile.ts +1 -0
- package/src/reactive.ts +55 -1
- package/src/source.ts +9 -4
package/dist/reactive.d.ts
CHANGED
|
@@ -41,6 +41,7 @@ export type Effect = {
|
|
|
41
41
|
adopted: Map<Set<Effect>, Unsubscribe> | null;
|
|
42
42
|
read: Set<Set<Effect>> | null;
|
|
43
43
|
};
|
|
44
|
+
export declare const $batch: <T>(fn: () => T) => T;
|
|
44
45
|
export declare const ALSO_WAKEN_BY: unique symbol;
|
|
45
46
|
export declare const OWNER: unique symbol;
|
|
46
47
|
export declare const $reactive: <T extends Record<string, any>>(data: T) => ReactiveDeepData<T>;
|
package/dist/source.d.ts
CHANGED
|
@@ -57,7 +57,7 @@ export declare const setupBody: (code: string) => string;
|
|
|
57
57
|
export declare const factoryParams: (helperNames: string[]) => string[];
|
|
58
58
|
export declare const factoryBody: (code: string) => string;
|
|
59
59
|
export declare const readSetupSignature: (script: TagBlock) => PropDecl[] | null;
|
|
60
|
-
export declare const declaredPropNames: (scripts: TagBlock[], signature: (script: TagBlock) => PropDecl[] | null) => Set<string>;
|
|
60
|
+
export declare const declaredPropNames: (scripts: TagBlock[], signature: (script: TagBlock) => PropDecl[] | null, factoryProps?: (src: string) => PropDecl[] | null) => Set<string>;
|
|
61
61
|
export declare const SETUP_HELPER_NAMES: string[];
|
|
62
62
|
export declare const INSTANCE_HELPER_NAMES: string[];
|
|
63
63
|
export declare const PRECOMPILED_QUEUE = "__jq79precompiled";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jq79",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.13",
|
|
4
4
|
"description": "Mini reactive component library: single-file .html components, Svelte-style setup scripts, fine-grained proxy reactivity. No compiler, no bundler, no dependencies — works from any static host.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"reactive",
|
package/src/jq79.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
import { $, $$, $create, sanitizeHTML, allowedHosts } from "./dom"
|
|
3
3
|
import type { AllowUrl, QueryAll, QueryOne } from "./dom"
|
|
4
|
-
import { $reactive, $toRaw, $computed, untracked, createEffectScope, ALSO_WAKEN_BY, OWNER } from "./reactive"
|
|
4
|
+
import { $reactive, $toRaw, $computed, $batch, untracked, createEffectScope, ALSO_WAKEN_BY, OWNER } from "./reactive"
|
|
5
5
|
import type { ReactiveDeepData, EffectScope } from "./reactive"
|
|
6
6
|
import { transformSetupScript, transformFactoryScript, parsePropsPattern, parseFactoryProps, preloadSpecifiers, type PropDecl } from "./transform"
|
|
7
7
|
import {
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
|
|
15
15
|
export { $, $$, $create } from "./dom"
|
|
16
16
|
export type { QueryOne, QueryAll } from "./dom"
|
|
17
|
-
export { $reactive, $toRaw, $computed } from "./reactive"
|
|
17
|
+
export { $reactive, $toRaw, $computed, $batch } from "./reactive"
|
|
18
18
|
export type { Computed } from "./reactive"
|
|
19
19
|
|
|
20
20
|
// the package version, substituted at build time (tsup/vitest `define`, read
|
|
@@ -1135,6 +1135,9 @@ const renderNestedComponent = (key: string, node: TemplateNode, scope: Record<st
|
|
|
1135
1135
|
// marks a spread: the whole object's properties, not one binding
|
|
1136
1136
|
const sources: Array<{ name?: string; expr: string }> = []
|
|
1137
1137
|
let hasSpread = false
|
|
1138
|
+
// directives written on the tag whose names a prop could have (see
|
|
1139
|
+
// warnDirectiveProps) - allocated only when there is one
|
|
1140
|
+
let directiveNamed: string[] | undefined
|
|
1138
1141
|
Object.entries(node.attrs).forEach(([attr, value]) => {
|
|
1139
1142
|
// the parent's scope stamp is stamped on every template element, this tag
|
|
1140
1143
|
// included - it's not a prop, and the child renders under its own scope
|
|
@@ -1147,7 +1150,10 @@ const renderNestedComponent = (key: string, node: TemplateNode, scope: Record<st
|
|
|
1147
1150
|
sources.push({ expr: value })
|
|
1148
1151
|
return
|
|
1149
1152
|
}
|
|
1150
|
-
if (isControlAttr(attr))
|
|
1153
|
+
if (isControlAttr(attr)) {
|
|
1154
|
+
if (DIRECTIVE_PROP_NAMES.has(attr)) (directiveNamed ??= []).push(attr.slice(1))
|
|
1155
|
+
return
|
|
1156
|
+
}
|
|
1151
1157
|
if (attr.startsWith("@")) {
|
|
1152
1158
|
events.push([attr, value])
|
|
1153
1159
|
} else if (attr === ":model" || attr.startsWith(":model.")) {
|
|
@@ -1318,6 +1324,7 @@ const renderNestedComponent = (key: string, node: TemplateNode, scope: Record<st
|
|
|
1318
1324
|
// on the next update
|
|
1319
1325
|
const declared = declaredPropSet(instance.scripts)
|
|
1320
1326
|
warnUndeclared(node, key, Object.keys(props), declared)
|
|
1327
|
+
if (directiveNamed) warnDirectiveProps(node, key, directiveNamed, declared)
|
|
1321
1328
|
const seed = pickDeclared(untracked(resolveProps), declared)
|
|
1322
1329
|
// mounting into a fragment attaches no shadow root of its own: a
|
|
1323
1330
|
// shadow-rendered child keeps its <style> elements inline, next to the DOM
|
|
@@ -2374,6 +2381,8 @@ type EachPlan = {
|
|
|
2374
2381
|
// can a row render anything into an effect scope of its own? Only then does
|
|
2375
2382
|
// a keyed row need the OWNER mark that lets its re-run reach them
|
|
2376
2383
|
nestsScopes: boolean
|
|
2384
|
+
// `:window="rowHeight"`: render only the rows in view (see openWindow)
|
|
2385
|
+
windowExpr: string | undefined
|
|
2377
2386
|
}
|
|
2378
2387
|
|
|
2379
2388
|
const eachPlans = new WeakMap<TemplateNode, EachPlan | null>()
|
|
@@ -2404,7 +2413,7 @@ const eachPlanOf = (node: TemplateNode): EachPlan | null => {
|
|
|
2404
2413
|
const keyProp = keyExpr !== undefined && !keyIsItem && KEY_MEMBER.test(keyExpr) && keyExpr.startsWith(`${itemName}.`)
|
|
2405
2414
|
? keyExpr.slice(itemName.length + 1)
|
|
2406
2415
|
: undefined
|
|
2407
|
-
const { [":each"]: _each, [":key"]: _key, ...itemAttrs } = node.attrs
|
|
2416
|
+
const { [":each"]: _each, [":key"]: _key, [":window"]: windowExpr, ...itemAttrs } = node.attrs
|
|
2408
2417
|
const itemNode: TemplateNode = { ...node, attrs: itemAttrs }
|
|
2409
2418
|
|
|
2410
2419
|
// An entry that changed position needs its position-only bindings re-run -
|
|
@@ -2421,16 +2430,187 @@ const eachPlanOf = (node: TemplateNode): EachPlan | null => {
|
|
|
2421
2430
|
|
|
2422
2431
|
const namesComponent = /^[A-Z]/.test(itemName) || (atName !== undefined && /^[A-Z]/.test(atName))
|
|
2423
2432
|
const nestsScopes = keyExpr !== undefined && rendersScopes(itemNode)
|
|
2424
|
-
const plan: EachPlan = { itemName, atName, listExpr, keyExpr, keyIsItem, keyProp, itemNode, readsPosition, namesComponent, nestsScopes }
|
|
2433
|
+
const plan: EachPlan = { itemName, atName, listExpr, keyExpr, keyIsItem, keyProp, itemNode, readsPosition, namesComponent, nestsScopes, windowExpr }
|
|
2425
2434
|
eachPlans.set(node, plan)
|
|
2426
2435
|
return plan
|
|
2427
2436
|
}
|
|
2428
2437
|
|
|
2438
|
+
// :window - a :each that renders only the rows in its scroll container's
|
|
2439
|
+
// viewport, plus OVERSCAN on either side, between two spacers whose heights
|
|
2440
|
+
// stand in for the rows that are not there. Rows are a fixed height (the
|
|
2441
|
+
// attribute's value, in pixels), which is what makes index <-> offset plain
|
|
2442
|
+
// arithmetic and $scrollTo exact. See RECORD/2026-11-12.a-windowed-each.md
|
|
2443
|
+
//
|
|
2444
|
+
// The window lives in a private store the list effect reads, so a scroll is an
|
|
2445
|
+
// ordinary wake of that effect - the same keyed diff, over list[start..end)
|
|
2446
|
+
const OVERSCAN = 8
|
|
2447
|
+
const SCROLLABLE_RE = /^(auto|scroll|overlay)$/
|
|
2448
|
+
|
|
2449
|
+
// the nearest ancestor that scrolls vertically, or null for the page. Crosses
|
|
2450
|
+
// a shadow root to its host, which is where a mountShadow'd list scrolls
|
|
2451
|
+
const scrollParentOf = (from: Node | null): HTMLElement | null => {
|
|
2452
|
+
for (let node = from; node; node = node instanceof ShadowRoot ? node.host : node.parentNode) {
|
|
2453
|
+
if (!(node instanceof HTMLElement) || node === document.body || node === document.documentElement) continue
|
|
2454
|
+
if (SCROLLABLE_RE.test(getComputedStyle(node).overflowY)) return node
|
|
2455
|
+
}
|
|
2456
|
+
return null
|
|
2457
|
+
}
|
|
2458
|
+
|
|
2459
|
+
type ListWindow = {
|
|
2460
|
+
top: Element
|
|
2461
|
+
bottom: Element
|
|
2462
|
+
// the slice of a list this long to render, spacers sized to match
|
|
2463
|
+
range: (length: number) => [number, number]
|
|
2464
|
+
// the pass is done: connect to the scroll container once the list is in the
|
|
2465
|
+
// document (it is not, on the pass that builds it)
|
|
2466
|
+
settled: () => void
|
|
2467
|
+
}
|
|
2468
|
+
|
|
2469
|
+
type ScrollToOptions79 = { block?: "nearest" | "start" | "center" | "end" }
|
|
2470
|
+
|
|
2471
|
+
const openWindow = (
|
|
2472
|
+
itemNode: TemplateNode,
|
|
2473
|
+
windowExpr: string,
|
|
2474
|
+
scope: Record<string, any>,
|
|
2475
|
+
fx: EffectScope,
|
|
2476
|
+
entries: () => EachEntry[]
|
|
2477
|
+
): ListWindow => {
|
|
2478
|
+
// the spacers are the rows' own tag, so they are valid wherever rows are (a
|
|
2479
|
+
// <tr> in a <tbody>, an <li> in a <ul>); a component row gets a <div>
|
|
2480
|
+
const spacer = () => {
|
|
2481
|
+
const el = itemNode.component !== undefined || itemNode.tag.includes("-") ? document.createElement("div") : createFor(itemNode)
|
|
2482
|
+
el.setAttribute("aria-hidden", "true")
|
|
2483
|
+
el.setAttribute("style", "height: 0px; margin: 0; padding: 0; border: 0; list-style: none; visibility: hidden")
|
|
2484
|
+
return el
|
|
2485
|
+
}
|
|
2486
|
+
const top = spacer()
|
|
2487
|
+
const bottom = spacer()
|
|
2488
|
+
// count -1: not measured yet - a screenful of the window's height is assumed
|
|
2489
|
+
const view = $reactive({ start: 0, count: -1 })
|
|
2490
|
+
let rowHeight = 0
|
|
2491
|
+
let length = 0
|
|
2492
|
+
let scroller: HTMLElement | null = null
|
|
2493
|
+
let connected = false
|
|
2494
|
+
let frame = 0
|
|
2495
|
+
let observer: ResizeObserver | null = null
|
|
2496
|
+
let holder: Element | null = null
|
|
2497
|
+
|
|
2498
|
+
const rowsFor = (viewport: number) => Math.ceil(viewport / rowHeight) + 1 + 2 * OVERSCAN
|
|
2499
|
+
const setWindow = (start: number, count: number) => {
|
|
2500
|
+
start = Math.max(0, Math.min(start, Math.max(0, length - count)))
|
|
2501
|
+
if (view.start !== start || view.count !== count) $batch(() => { view.start = start; view.count = count })
|
|
2502
|
+
}
|
|
2503
|
+
|
|
2504
|
+
// where the rows begin and the viewport is, in the container's content
|
|
2505
|
+
// coordinates (the page's, when the page scrolls)
|
|
2506
|
+
const metrics = () => {
|
|
2507
|
+
const at = top.getBoundingClientRect().top
|
|
2508
|
+
if (scroller) {
|
|
2509
|
+
const box = scroller.getBoundingClientRect()
|
|
2510
|
+
const scrollTop = scroller.scrollTop
|
|
2511
|
+
return { listTop: at - box.top - scroller.clientTop + scrollTop, scrollTop, height: scroller.clientHeight || window.innerHeight }
|
|
2512
|
+
}
|
|
2513
|
+
return { listTop: at + window.scrollY, scrollTop: window.scrollY, height: window.innerHeight }
|
|
2514
|
+
}
|
|
2515
|
+
|
|
2516
|
+
const measure = () => {
|
|
2517
|
+
frame = 0
|
|
2518
|
+
if (!(rowHeight > 0) || !top.isConnected) return
|
|
2519
|
+
const { listTop, scrollTop, height } = metrics()
|
|
2520
|
+
setWindow(Math.floor((scrollTop - listTop) / rowHeight) - OVERSCAN, rowsFor(height))
|
|
2521
|
+
}
|
|
2522
|
+
const schedule = () => {
|
|
2523
|
+
if (frame) return
|
|
2524
|
+
frame = typeof requestAnimationFrame === "function" ? requestAnimationFrame(measure) : (setTimeout(measure, 16) as any)
|
|
2525
|
+
}
|
|
2526
|
+
|
|
2527
|
+
// scrolls the container so row `index` is in view and renders it before
|
|
2528
|
+
// returning - an aria-activedescendant has to point at a row that exists
|
|
2529
|
+
const scrollTo = (index: number, { block = "nearest" }: ScrollToOptions79 = {}): Node | null => {
|
|
2530
|
+
connect()
|
|
2531
|
+
if (rowHeight > 0 && top.isConnected) {
|
|
2532
|
+
const { listTop, scrollTop, height } = metrics()
|
|
2533
|
+
const rowTop = listTop + index * rowHeight
|
|
2534
|
+
const rowBottom = rowTop + rowHeight
|
|
2535
|
+
let next = scrollTop
|
|
2536
|
+
if (block === "start") next = rowTop
|
|
2537
|
+
else if (block === "end") next = rowBottom - height
|
|
2538
|
+
else if (block === "center") next = rowTop - (height - rowHeight) / 2
|
|
2539
|
+
else if (rowTop < scrollTop) next = rowTop
|
|
2540
|
+
else if (rowBottom > scrollTop + height) next = rowBottom - height
|
|
2541
|
+
if (next !== scrollTop) {
|
|
2542
|
+
if (scroller) scroller.scrollTop = next
|
|
2543
|
+
else window.scrollTo(window.scrollX, next)
|
|
2544
|
+
}
|
|
2545
|
+
measure()
|
|
2546
|
+
}
|
|
2547
|
+
// whatever layout said (or could not say, before there is one), the row
|
|
2548
|
+
// is in the window when this returns
|
|
2549
|
+
const count = view.count > 0 ? view.count : rowsFor(window.innerHeight || 800)
|
|
2550
|
+
if (index < view.start || index >= view.start + count) setWindow(index - OVERSCAN, count)
|
|
2551
|
+
const entry = entries().find(row => row.scope.$index === index)
|
|
2552
|
+
return entry ? entry.range.first : null
|
|
2553
|
+
}
|
|
2554
|
+
|
|
2555
|
+
const connect = () => {
|
|
2556
|
+
if (connected || !top.isConnected) return
|
|
2557
|
+
connected = true
|
|
2558
|
+
holder = top.parentElement
|
|
2559
|
+
scroller = scrollParentOf(top.parentNode)
|
|
2560
|
+
;(scroller ?? window).addEventListener("scroll", schedule, { passive: true })
|
|
2561
|
+
window.addEventListener("resize", schedule)
|
|
2562
|
+
if (scroller && typeof ResizeObserver === "function") {
|
|
2563
|
+
observer = new ResizeObserver(schedule)
|
|
2564
|
+
observer.observe(scroller)
|
|
2565
|
+
}
|
|
2566
|
+
if (holder) Object.defineProperty(holder, "$scrollTo", { value: scrollTo, configurable: true, writable: true })
|
|
2567
|
+
measure()
|
|
2568
|
+
}
|
|
2569
|
+
|
|
2570
|
+
fx.onDispose(() => {
|
|
2571
|
+
if (frame) (typeof cancelAnimationFrame === "function" ? cancelAnimationFrame : clearTimeout)(frame)
|
|
2572
|
+
if (!connected) return
|
|
2573
|
+
;(scroller ?? window).removeEventListener("scroll", schedule)
|
|
2574
|
+
window.removeEventListener("resize", schedule)
|
|
2575
|
+
observer?.disconnect()
|
|
2576
|
+
if (holder && (holder as any).$scrollTo === scrollTo) delete (holder as any).$scrollTo
|
|
2577
|
+
})
|
|
2578
|
+
|
|
2579
|
+
let queued = false
|
|
2580
|
+
return {
|
|
2581
|
+
top,
|
|
2582
|
+
bottom,
|
|
2583
|
+
range: listLength => {
|
|
2584
|
+
length = listLength
|
|
2585
|
+
const height = Number(evalExpr(windowExpr, scope))
|
|
2586
|
+
rowHeight = height > 0 ? height : 0
|
|
2587
|
+
if (!rowHeight) {
|
|
2588
|
+
;(top as HTMLElement).style.height = (bottom as HTMLElement).style.height = "0px"
|
|
2589
|
+
return [0, listLength]
|
|
2590
|
+
}
|
|
2591
|
+
const count = view.count > 0 ? view.count : rowsFor(window.innerHeight || 800)
|
|
2592
|
+
const start = Math.max(0, Math.min(view.start, listLength - count))
|
|
2593
|
+
const end = Math.min(listLength, start + count)
|
|
2594
|
+
;(top as HTMLElement).style.height = `${start * rowHeight}px`
|
|
2595
|
+
;(bottom as HTMLElement).style.height = `${(listLength - end) * rowHeight}px`
|
|
2596
|
+
return [start, end]
|
|
2597
|
+
},
|
|
2598
|
+
settled: () => {
|
|
2599
|
+
if (connected || queued) return
|
|
2600
|
+
queued = true
|
|
2601
|
+
queueMicrotask(() => {
|
|
2602
|
+
queued = false
|
|
2603
|
+
connect()
|
|
2604
|
+
})
|
|
2605
|
+
},
|
|
2606
|
+
}
|
|
2607
|
+
}
|
|
2608
|
+
|
|
2429
2609
|
const renderEach = (node: TemplateNode, scope: Record<string, any>, fx: EffectScope, shadow: boolean): Node => {
|
|
2430
2610
|
const plan = eachPlanOf(node)
|
|
2431
2611
|
if (!plan) return document.createComment(`invalid :each expression "${node.attrs[":each"]}"`)
|
|
2432
2612
|
|
|
2433
|
-
const { itemName, atName, listExpr, keyExpr, keyIsItem, keyProp, itemNode, readsPosition, namesComponent, nestsScopes } = plan
|
|
2613
|
+
const { itemName, atName, listExpr, keyExpr, keyIsItem, keyProp, itemNode, readsPosition, namesComponent, nestsScopes, windowExpr } = plan
|
|
2434
2614
|
|
|
2435
2615
|
// `:key="row.id"`, or the loop variable itself, is what a key almost always
|
|
2436
2616
|
// is - and reading one needs neither a scope to resolve names against nor a
|
|
@@ -2445,6 +2625,8 @@ const renderEach = (node: TemplateNode, scope: Record<string, any>, fx: EffectSc
|
|
|
2445
2625
|
const anchor = document.createComment("each")
|
|
2446
2626
|
const wrapper = document.createDocumentFragment()
|
|
2447
2627
|
wrapper.appendChild(anchor)
|
|
2628
|
+
const win = windowExpr === undefined ? null : openWindow(itemNode, windowExpr, scope, fx, () => entries)
|
|
2629
|
+
if (win) wrapper.append(win.top, win.bottom)
|
|
2448
2630
|
|
|
2449
2631
|
// :if on the same element is not per-item filtering, and rendering
|
|
2450
2632
|
// everything in silence reads like a broken filter - say it out loud
|
|
@@ -2472,6 +2654,9 @@ const renderEach = (node: TemplateNode, scope: Record<string, any>, fx: EffectSc
|
|
|
2472
2654
|
const array = Array.isArray(list)
|
|
2473
2655
|
const keys: string[] | null = array ? null : isPlainObject(list) ? Object.keys(list) : []
|
|
2474
2656
|
const length = array ? list.length : keys!.length
|
|
2657
|
+
// a windowed list walks only the rows in view; the spacers stand in for
|
|
2658
|
+
// the rest, so `index` stays the row's real position
|
|
2659
|
+
const [from, to] = win ? win.range(length) : [0, length]
|
|
2475
2660
|
// buckets rather than a key->entry map: duplicate keys (a user error, but
|
|
2476
2661
|
// one that must degrade instead of corrupt) consume entries in order of
|
|
2477
2662
|
// appearance, so no entry is ever matched twice - matching one twice is
|
|
@@ -2504,7 +2689,7 @@ const renderEach = (node: TemplateNode, scope: Record<string, any>, fx: EffectSc
|
|
|
2504
2689
|
// scope of its own, below
|
|
2505
2690
|
let scratch: Record<string, any> | undefined
|
|
2506
2691
|
|
|
2507
|
-
for (let index =
|
|
2692
|
+
for (let index = from; index < to; index++) {
|
|
2508
2693
|
const at = array ? index : keys![index]
|
|
2509
2694
|
const item = array ? list[index] : list[at]
|
|
2510
2695
|
|
|
@@ -2607,7 +2792,7 @@ const renderEach = (node: TemplateNode, scope: Record<string, any>, fx: EffectSc
|
|
|
2607
2792
|
removeRuns(contiguousRuns(entries, entry => entry.dead === true))
|
|
2608
2793
|
}
|
|
2609
2794
|
|
|
2610
|
-
let prevNode: Node = anchor
|
|
2795
|
+
let prevNode: Node = win ? win.top : anchor
|
|
2611
2796
|
// rows already in order relative to each other stay where they are; the
|
|
2612
2797
|
// rest are placed after the row that precedes them in the new order,
|
|
2613
2798
|
// which is where the pass has just left `prevNode`
|
|
@@ -2631,6 +2816,7 @@ const renderEach = (node: TemplateNode, scope: Record<string, any>, fx: EffectSc
|
|
|
2631
2816
|
} finally {
|
|
2632
2817
|
closeRenderPass(pass)
|
|
2633
2818
|
}
|
|
2819
|
+
win?.settled()
|
|
2634
2820
|
})
|
|
2635
2821
|
// the rows live inside whatever holds this list, as a :if branch does (see
|
|
2636
2822
|
// renderConditional): torn down with it, not left subscribed to the store
|
|
@@ -3029,27 +3215,81 @@ const stampScope = (nodes: (TemplateNode | string)[], scope: string) => {
|
|
|
3029
3215
|
|
|
3030
3216
|
// the scope attribute goes on the selector's last compound - the element the
|
|
3031
3217
|
// rule actually targets - but *before* a pseudo-element, which must stay last
|
|
3032
|
-
// (".a::before" scopes to ".a[data-jq79='x']::before", not "::before[...]")
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
|
|
3037
|
-
|
|
3038
|
-
|
|
3039
|
-
|
|
3040
|
-
|
|
3041
|
-
|
|
3042
|
-
|
|
3043
|
-
|
|
3044
|
-
|
|
3045
|
-
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
if (
|
|
3051
|
-
|
|
3218
|
+
// (".a::before" scopes to ".a[data-jq79='x']::before", not "::before[...]").
|
|
3219
|
+
// This reads the author's text, not an engine's serialisation, so the CSS2
|
|
3220
|
+
// single-colon pseudo-elements are recognised by name: nothing has rewritten
|
|
3221
|
+
// ":before" to "::before" for us (see scopeCss)
|
|
3222
|
+
const LEGACY_PSEUDO_ELEMENT_RE = /^:(before|after|first-line|first-letter)(?![\w-])/i
|
|
3223
|
+
|
|
3224
|
+
// walks `text` at nesting depth 0 - outside (), [], strings and escapes -
|
|
3225
|
+
// calling `at(i)` on each such index; `at` returning true stops the walk
|
|
3226
|
+
const atTopLevel = (text: string, at: (i: number) => boolean | void) => {
|
|
3227
|
+
let depth = 0
|
|
3228
|
+
for (let i = 0; i < text.length; i++) {
|
|
3229
|
+
const char = text[i]
|
|
3230
|
+
if (char === "\\") i++
|
|
3231
|
+
else if (char === '"' || char === "'") {
|
|
3232
|
+
i++
|
|
3233
|
+
while (i < text.length && text[i] !== char) i += text[i] === "\\" ? 2 : 1
|
|
3234
|
+
} else if (char === "(" || char === "[") depth++
|
|
3235
|
+
else if (char === ")" || char === "]") depth--
|
|
3236
|
+
else if (depth === 0 && at(i)) return
|
|
3237
|
+
}
|
|
3238
|
+
}
|
|
3239
|
+
|
|
3240
|
+
const scopeSelector = (selector: string, scope: string): string => {
|
|
3241
|
+
let pseudoAt = selector.length
|
|
3242
|
+
atTopLevel(selector, i => {
|
|
3243
|
+
if (selector[i] !== ":") return false
|
|
3244
|
+
if (selector[i + 1] !== ":" && !LEGACY_PSEUDO_ELEMENT_RE.test(selector.slice(i))) return false
|
|
3245
|
+
pseudoAt = i
|
|
3246
|
+
return true
|
|
3247
|
+
})
|
|
3248
|
+
return `${selector.slice(0, pseudoAt)}[${SCOPE_ATTR}="${scope}"]${selector.slice(pseudoAt)}`
|
|
3249
|
+
}
|
|
3250
|
+
|
|
3251
|
+
// a selector list is split on its top-level commas only: `.a:is(.b, .c)` is
|
|
3252
|
+
// one selector. A comment in a selector is removed - CSS reads it as nothing,
|
|
3253
|
+
// so `.a/**/.b` is `.a.b` - since the attribute must never land inside one
|
|
3254
|
+
const scopeSelectorList = (prelude: string, scope: string): string => {
|
|
3255
|
+
const text = stripCssComments(prelude)
|
|
3256
|
+
const lead = text.match(/^\s*/)![0]
|
|
3257
|
+
const trail = text.match(/\s*$/)![0]
|
|
3258
|
+
const body = text.slice(lead.length, text.length - trail.length)
|
|
3259
|
+
if (!body) return prelude
|
|
3260
|
+
const parts: string[] = []
|
|
3261
|
+
let from = 0
|
|
3262
|
+
atTopLevel(body, i => {
|
|
3263
|
+
if (body[i] === ",") {
|
|
3264
|
+
parts.push(body.slice(from, i))
|
|
3265
|
+
from = i + 1
|
|
3266
|
+
}
|
|
3052
3267
|
})
|
|
3268
|
+
parts.push(body.slice(from))
|
|
3269
|
+
return lead + parts.map(part => part.trim() && scopeSelector(part.trim(), scope)).join(", ") + trail
|
|
3270
|
+
}
|
|
3271
|
+
|
|
3272
|
+
const stripCssComments = (text: string): string => {
|
|
3273
|
+
let out = ""
|
|
3274
|
+
for (let i = 0; i < text.length; ) {
|
|
3275
|
+
const char = text[i]
|
|
3276
|
+
if (char === "/" && text[i + 1] === "*") {
|
|
3277
|
+
const end = text.indexOf("*/", i + 2)
|
|
3278
|
+
i = end === -1 ? text.length : end + 2
|
|
3279
|
+
} else if (char === '"' || char === "'") {
|
|
3280
|
+
let j = i + 1
|
|
3281
|
+
while (j < text.length && text[j] !== char) j += text[j] === "\\" ? 2 : 1
|
|
3282
|
+
out += text.slice(i, j + 1)
|
|
3283
|
+
i = j + 1
|
|
3284
|
+
} else if (char === "\\") {
|
|
3285
|
+
out += text.slice(i, i + 2)
|
|
3286
|
+
i += 2
|
|
3287
|
+
} else {
|
|
3288
|
+
out += char
|
|
3289
|
+
i++
|
|
3290
|
+
}
|
|
3291
|
+
}
|
|
3292
|
+
return out
|
|
3053
3293
|
}
|
|
3054
3294
|
|
|
3055
3295
|
// A component's name in a selector - `Circle { color: red }` - names the box
|
|
@@ -3146,18 +3386,66 @@ const renameComponentSelectors = (css: string): string => {
|
|
|
3146
3386
|
return out
|
|
3147
3387
|
}
|
|
3148
3388
|
|
|
3149
|
-
//
|
|
3150
|
-
//
|
|
3151
|
-
//
|
|
3152
|
-
//
|
|
3389
|
+
// Scoping rewrites selectors and nothing else, on the SOURCE. It used to parse
|
|
3390
|
+
// the block into a CSSStyleSheet and write it back from rule.cssText, which
|
|
3391
|
+
// round-trips every declaration through the engine's serialiser - and Chromium
|
|
3392
|
+
// serialises `border: var(--w) solid red; border-bottom-width: 0` as a row of
|
|
3393
|
+
// empty longhands (`border-top-style: ;` …), so the border vanished once
|
|
3394
|
+
// scoped. See RECORD/2026-11-12.scope-the-source-not-the-cssom.md
|
|
3395
|
+
//
|
|
3396
|
+
// The scan tracks strings, comments and nesting the way renameComponentSelectors
|
|
3397
|
+
// does. In rule position (top level, or inside @media and the other at-rules
|
|
3398
|
+
// that hold rules) a prelude that is not an at-rule is a selector list and gets
|
|
3399
|
+
// scoped; a declaration block is copied whole, nested blocks included - CSS
|
|
3400
|
+
// nesting is relative to the parent, which is already scoped. @keyframes holds
|
|
3401
|
+
// frames, not rules, so it is copied whole too.
|
|
3402
|
+
//
|
|
3403
|
+
// Vue's :deep()/::v-deep/>>> escape hatches are not CSS: the browser drops a
|
|
3404
|
+
// rule it can't parse when the block is injected, so they are warned about
|
|
3405
|
+
// rather than left to vanish
|
|
3153
3406
|
const scopeCss = (css: string, scope: string): string => {
|
|
3154
3407
|
if (/:deep\(|::v-deep|>>>/.test(css)) {
|
|
3155
3408
|
console.warn("jq79: :deep()/::v-deep/>>> are not supported in <style scoped>; the rule will be dropped by the browser")
|
|
3156
3409
|
}
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3160
|
-
|
|
3410
|
+
let out = ""
|
|
3411
|
+
let prelude = "" // rule position: the text since the last delimiter
|
|
3412
|
+
const stack: boolean[] = [] // true = the block holds rules, not declarations
|
|
3413
|
+
const holdsRules = () => stack.length === 0 || stack[stack.length - 1]
|
|
3414
|
+
|
|
3415
|
+
for (let i = 0; i < css.length; ) {
|
|
3416
|
+
const char = css[i]
|
|
3417
|
+
let token = char
|
|
3418
|
+
if (char === "/" && css[i + 1] === "*") {
|
|
3419
|
+
const end = css.indexOf("*/", i + 2)
|
|
3420
|
+
token = css.slice(i, end === -1 ? css.length : end + 2)
|
|
3421
|
+
} else if (char === '"' || char === "'") {
|
|
3422
|
+
let j = i + 1
|
|
3423
|
+
while (j < css.length && css[j] !== char) j += css[j] === "\\" ? 2 : 1
|
|
3424
|
+
token = css.slice(i, j + 1)
|
|
3425
|
+
} else if (char === "\\") {
|
|
3426
|
+
token = css.slice(i, i + 2)
|
|
3427
|
+
}
|
|
3428
|
+
i += token.length
|
|
3429
|
+
|
|
3430
|
+
if (!holdsRules()) {
|
|
3431
|
+
out += token
|
|
3432
|
+
if (token === "{") stack.push(false)
|
|
3433
|
+
else if (token === "}") stack.pop()
|
|
3434
|
+
} else if (token === "{") {
|
|
3435
|
+
const bare = stripCssComments(prelude)
|
|
3436
|
+
const atRule = AT_RULE_RE.test(bare)
|
|
3437
|
+
out += (atRule ? prelude : scopeSelectorList(prelude, scope)) + "{"
|
|
3438
|
+
stack.push(atRule && NESTED_AT_RULE_RE.test(bare))
|
|
3439
|
+
prelude = ""
|
|
3440
|
+
} else if (token === "}" || token === ";") {
|
|
3441
|
+
out += prelude + token
|
|
3442
|
+
prelude = ""
|
|
3443
|
+
if (token === "}") stack.pop()
|
|
3444
|
+
} else {
|
|
3445
|
+
prelude += token
|
|
3446
|
+
}
|
|
3447
|
+
}
|
|
3448
|
+
return out + prelude
|
|
3161
3449
|
}
|
|
3162
3450
|
|
|
3163
3451
|
// converts a string of HTML into an AST representation of the component:
|
|
@@ -3599,8 +3887,40 @@ const declareProps = (store: Record<string, any>, props: PropDecl[] | null) => {
|
|
|
3599
3887
|
// Only the empty *value* is closed. An absent attribute (a factory <script>
|
|
3600
3888
|
// with no :setup at all) stays `null`, so its signature is still read from the
|
|
3601
3889
|
// factory's first parameter
|
|
3890
|
+
// What a script's text compiles to, memoized by that text. Every instance of a
|
|
3891
|
+
// definition runs the same scripts, and these passes are pure functions of the
|
|
3892
|
+
// string - re-running them per instance was most of a list's render time. The
|
|
3893
|
+
// values are shared, so the arrays are frozen on the way in. A throw (the
|
|
3894
|
+
// pre-0.4 factory signature) is not cached: it rethrows on every render, as it
|
|
3895
|
+
// always did. Kept here, not in transform.ts, because the editor's language
|
|
3896
|
+
// server re-reads a script on every keystroke and would grow a map per edit.
|
|
3897
|
+
// See RECORD/2026-11-15.cache-script-transforms.md
|
|
3898
|
+
const memoByText = <T>(fn: (src: string) => T): ((src: string) => T) => {
|
|
3899
|
+
const cache = new Map<string, T>()
|
|
3900
|
+
return src => {
|
|
3901
|
+
if (cache.has(src)) return cache.get(src)!
|
|
3902
|
+
const value = fn(src)
|
|
3903
|
+
cache.set(src, value)
|
|
3904
|
+
return value
|
|
3905
|
+
}
|
|
3906
|
+
}
|
|
3907
|
+
const freezeProps = (props: PropDecl[] | null): PropDecl[] | null => {
|
|
3908
|
+
props?.forEach(prop => Object.freeze(prop))
|
|
3909
|
+
if (props) Object.freeze(props)
|
|
3910
|
+
return props
|
|
3911
|
+
}
|
|
3912
|
+
const factoryCodeOf = memoByText(transformFactoryScript)
|
|
3913
|
+
const factoryPropsOf = memoByText(src => freezeProps(parseFactoryProps(src)))
|
|
3914
|
+
const setupTransformOf = memoByText(src => {
|
|
3915
|
+
const transform = transformSetupScript(src)
|
|
3916
|
+
Object.freeze(transform.vars)
|
|
3917
|
+
return Object.freeze(transform)
|
|
3918
|
+
})
|
|
3919
|
+
const signatureOf = memoByText(src => freezeProps(readSetupSignature({ attrs: { ":setup": src }, content: "" })))
|
|
3920
|
+
|
|
3602
3921
|
const setupSignature = (script: TagBlock): PropDecl[] | null => {
|
|
3603
|
-
const
|
|
3922
|
+
const pattern = script.attrs[":setup"]
|
|
3923
|
+
const props = pattern === undefined ? null : signatureOf(pattern)
|
|
3604
3924
|
if (!props && script.attrs[":setup"] !== undefined) warnUnreadableSignature(script, script.attrs[":setup"])
|
|
3605
3925
|
return props
|
|
3606
3926
|
}
|
|
@@ -3638,7 +3958,7 @@ const warnUnreadableSignature = (script: TagBlock, pattern: string) => {
|
|
|
3638
3958
|
const declaredPropSet = (scripts: TagBlock[]): Set<string> | null => {
|
|
3639
3959
|
let names: Set<string> | null = null
|
|
3640
3960
|
scripts.forEach(script => {
|
|
3641
|
-
const declarations =
|
|
3961
|
+
const declarations = factoryPropsOf(script.content) ?? setupSignature(script)
|
|
3642
3962
|
if (!declarations) return
|
|
3643
3963
|
const into = (names ??= new Set())
|
|
3644
3964
|
declarations.forEach(({ name }) => into.add(name))
|
|
@@ -3684,6 +4004,26 @@ const warnUndeclared = (node: TemplateNode, name: string, written: string[], dec
|
|
|
3684
4004
|
})
|
|
3685
4005
|
}
|
|
3686
4006
|
|
|
4007
|
+
// `:checked` on a component tag is the directive, not a prop: the prop
|
|
4008
|
+
// collection skips every control attribute, so a child that declares
|
|
4009
|
+
// `checked` gets nothing and says nothing. Said here, once per usage site, for
|
|
4010
|
+
// the case where the author can only have meant the prop - the child declares
|
|
4011
|
+
// one of that name. See RECORD/2026-11-12.a-prop-named-like-a-directive.md
|
|
4012
|
+
const DIRECTIVE_PROP_NAMES = new Set([":value", ":checked", ":selected", ":text", ":html", ":class"])
|
|
4013
|
+
const directivePropsWarned = new WeakSet<TemplateNode>()
|
|
4014
|
+
|
|
4015
|
+
const warnDirectiveProps = (node: TemplateNode, name: string, written: string[], declared: Set<string> | null) => {
|
|
4016
|
+
if (declared === null || directivePropsWarned.has(node)) return
|
|
4017
|
+
const clashing = written.filter(prop => declared.has(prop))
|
|
4018
|
+
if (!clashing.length) return
|
|
4019
|
+
directivePropsWarned.add(node)
|
|
4020
|
+
clashing.forEach(prop => console.warn(
|
|
4021
|
+
`jq79: :${prop} on <${name}> is a directive, not a prop - <${name}> never receives its "${prop}". ` +
|
|
4022
|
+
`Rename the prop (${clashing.map(p => `:${p}`).join(", ")} are reserved on a component tag), ` +
|
|
4023
|
+
`or use :model for a value that goes both ways`
|
|
4024
|
+
))
|
|
4025
|
+
}
|
|
4026
|
+
|
|
3687
4027
|
// the sibling components this one resolves by name, or null when there are
|
|
3688
4028
|
// none left to resolve. They go on the store's *prototype* rather than in it:
|
|
3689
4029
|
// the component-key scan walks the chain, so <Row> resolves; they stay out of
|
|
@@ -4473,7 +4813,7 @@ export class Component79 {
|
|
|
4473
4813
|
// what this component can see of its file's other components, and which of
|
|
4474
4814
|
// its declared props arrived empty - both decided by the signature, before
|
|
4475
4815
|
// the store exists (see siblingsInScope / UNFILLED_PROPS)
|
|
4476
|
-
const declared = declaredPropNames(this.scripts, setupSignature)
|
|
4816
|
+
const declared = declaredPropNames(this.scripts, setupSignature, factoryPropsOf)
|
|
4477
4817
|
const siblingScope = siblingsInScope(this.siblings, declared)
|
|
4478
4818
|
const raw: Record<string, any> = siblingScope
|
|
4479
4819
|
? Object.assign(Object.create(siblingScope), data)
|
|
@@ -4709,7 +5049,7 @@ export class Component79 {
|
|
|
4709
5049
|
const instanceHelpers = { $mounted, $destroyed, $attached, $detached, $computed: $instanceComputed, $self, $$self, ...injected, $importAll, ...siblingScope }
|
|
4710
5050
|
const at: ScriptLocation = { filename: this.filename, index }
|
|
4711
5051
|
const deferred = ":mounted" in script.attrs
|
|
4712
|
-
const factoryCode =
|
|
5052
|
+
const factoryCode = factoryCodeOf(script.content)
|
|
4713
5053
|
const run = ((): ScriptRun => {
|
|
4714
5054
|
if (factoryCode !== null) {
|
|
4715
5055
|
// a factory publishes its names by returning them, so one that yields
|
|
@@ -4724,11 +5064,11 @@ export class Component79 {
|
|
|
4724
5064
|
"so none of its bindings exist yet - await $mounted() inside the factory instead."
|
|
4725
5065
|
)
|
|
4726
5066
|
}
|
|
4727
|
-
declareProps(store,
|
|
5067
|
+
declareProps(store, factoryPropsOf(script.content))
|
|
4728
5068
|
const body = deferred ? defer(factoryCode) : factoryCode
|
|
4729
5069
|
return runFactoryScript(body, store, run => fx.effect(run), instanceHelpers, $import, at)
|
|
4730
5070
|
}
|
|
4731
|
-
const { vars, code } =
|
|
5071
|
+
const { vars, code } = setupTransformOf(script.content)
|
|
4732
5072
|
declareProps(store, setupSignature(script))
|
|
4733
5073
|
// pre-declare script vars on the store so `with` resolves assignments
|
|
4734
5074
|
// to them (and reads of them) through the reactive proxy
|
|
@@ -5199,7 +5539,7 @@ let settleQueued = false
|
|
|
5199
5539
|
// declaration) shadows them. Their names, in this order, are also
|
|
5200
5540
|
// SETUP_HELPER_NAMES (source.ts) - what precompile compiles scripts with, so
|
|
5201
5541
|
// the two change together
|
|
5202
|
-
const SETUP_HELPERS: Record<string, any> = { $, $$, $create, $reactive, $toRaw, Component79, C79: Component79 }
|
|
5542
|
+
const SETUP_HELPERS: Record<string, any> = { $, $$, $create, $reactive, $toRaw, $batch, Component79, C79: Component79 }
|
|
5203
5543
|
|
|
5204
5544
|
// the hot-reload handshake. jq79/dev serves a classic script that sets the flag
|
|
5205
5545
|
// below; classic scripts run before deferred module ones, so the flag is always
|
package/src/precompile.ts
CHANGED
|
@@ -81,6 +81,7 @@ const collectExpressions = (nodes: (TemplateNode | string)[], known: string[], a
|
|
|
81
81
|
const match = attrs[":each"].match(EACH_PATTERN)
|
|
82
82
|
if (match) add(match[3])
|
|
83
83
|
if (":key" in attrs) add(attrs[":key"])
|
|
84
|
+
if (":window" in attrs) add(attrs[":window"])
|
|
84
85
|
}
|
|
85
86
|
if (":with" in attrs) add(attrs[":with"])
|
|
86
87
|
// bindSlotProps: a :slot binder's defaults, on a component tag or its <template>
|