@brust/runtime-dom 2.0.0-alpha.20 → 2.0.0-alpha.21
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/package.json +1 -1
- package/src/directives/for.ts +9 -3
package/package.json
CHANGED
package/src/directives/for.ts
CHANGED
|
@@ -4,6 +4,11 @@ type RowScope = Record<string, unknown> & { __own: Signal<number> }
|
|
|
4
4
|
|
|
5
5
|
const SYNTAX = /^\s*([A-Za-z_$][\w$]*)\s*(?:,\s*([A-Za-z_$][\w$]*))?\s+in\s+([A-Za-z_$][\w$.]*(?::[A-Za-z_$][\w$]*(?:\s*,\s*[A-Za-z_$][\w$]*)*)?)\s+by\s+([A-Za-z_$][\w$.]*)\s*$/
|
|
6
6
|
const done = new WeakSet<Element>()
|
|
7
|
+
/** The comments this binder inserted: the server's own `<!--x-for-->` marker is not among them. */
|
|
8
|
+
const anchors = new WeakSet<Node>()
|
|
9
|
+
/** F396: the zero-row template is the element the server prints right after its `<!--x-for-->` marker. A row is not
|
|
10
|
+
* told apart by `hidden`: a row whose own root is `hidden={cond}` is server-rendered hidden and is still a row. */
|
|
11
|
+
const isTemplate = (s: Element) => { const p = s.previousSibling; return p instanceof Comment && p.data === 'x-for' && !anchors.has(p) }
|
|
7
12
|
|
|
8
13
|
BINDERS.push({
|
|
9
14
|
match: (a) => a === 'x-for',
|
|
@@ -17,13 +22,14 @@ BINDERS.push({
|
|
|
17
22
|
// All server-rendered siblings carrying the same x-for are rows of this list; the first is the template.
|
|
18
23
|
const parent = el.parentElement!
|
|
19
24
|
const siblings = Array.from(parent.children).filter((c) => c.getAttribute('x-for') === raw)
|
|
25
|
+
const templates = new Set(siblings.filter(isTemplate))
|
|
20
26
|
siblings.forEach((s) => done.add(s))
|
|
21
27
|
const template = el.cloneNode(true) as Element
|
|
22
28
|
template.removeAttribute('x-for'); template.removeAttribute('hidden')
|
|
23
|
-
const anchor = document.createComment('x-for')
|
|
29
|
+
const anchor = document.createComment('x-for'); anchors.add(anchor)
|
|
24
30
|
parent.insertBefore(anchor, el)
|
|
25
|
-
// Server-rendered rows (not the
|
|
26
|
-
const adopt = siblings.filter((s) => !
|
|
31
|
+
// Server-rendered rows (not the zero-row template) are adopted in order on the first run.
|
|
32
|
+
const adopt = siblings.filter((s) => !templates.has(s))
|
|
27
33
|
if (!adopt.includes(el)) el.remove()
|
|
28
34
|
let firstRun = true
|
|
29
35
|
|