@fictjs/runtime 0.0.8 → 0.0.10

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/dist/slim.cjs CHANGED
@@ -766,10 +766,17 @@ function effectScopeOper() {
766
766
  }
767
767
  function batch(fn) {
768
768
  ++batchDepth;
769
+ let hasError = false;
769
770
  try {
770
771
  return fn();
772
+ } catch (e) {
773
+ hasError = true;
774
+ throw e;
771
775
  } finally {
772
- if (--batchDepth === 0) flush();
776
+ --batchDepth;
777
+ if (!hasError && batchDepth === 0) {
778
+ flush();
779
+ }
773
780
  }
774
781
  }
775
782
  function setActiveSub(sub) {
@@ -820,7 +827,7 @@ function effectRunDevtools(node) {
820
827
  function createSelector(source, equalityFn = (a, b) => a === b) {
821
828
  let current = source();
822
829
  const observers = /* @__PURE__ */ new Map();
823
- effect(() => {
830
+ const dispose = effect(() => {
824
831
  const next = source();
825
832
  if (equalityFn(current, next)) return;
826
833
  const prevSig = observers.get(current);
@@ -829,6 +836,10 @@ function createSelector(source, equalityFn = (a, b) => a === b) {
829
836
  if (nextSig) nextSig(true);
830
837
  current = next;
831
838
  });
839
+ registerRootCleanup(() => {
840
+ dispose();
841
+ observers.clear();
842
+ });
832
843
  return (key) => {
833
844
  let sig = observers.get(key);
834
845
  if (!sig) {
@@ -889,7 +900,8 @@ function createRenderEffect(fn) {
889
900
  cleanup = maybeCleanup;
890
901
  }
891
902
  } catch (err) {
892
- if (handleError(err, { source: "effect" }, rootForError)) {
903
+ const handled = handleError(err, { source: "effect" }, rootForError);
904
+ if (handled) {
893
905
  return;
894
906
  }
895
907
  throw err;
@@ -1117,6 +1129,85 @@ var DelegatedEvents = /* @__PURE__ */ new Set([
1117
1129
  "touchmove",
1118
1130
  "touchstart"
1119
1131
  ]);
1132
+ var SVGElements = /* @__PURE__ */ new Set([
1133
+ "altGlyph",
1134
+ "altGlyphDef",
1135
+ "altGlyphItem",
1136
+ "animate",
1137
+ "animateColor",
1138
+ "animateMotion",
1139
+ "animateTransform",
1140
+ "circle",
1141
+ "clipPath",
1142
+ "color-profile",
1143
+ "cursor",
1144
+ "defs",
1145
+ "desc",
1146
+ "ellipse",
1147
+ "feBlend",
1148
+ "feColorMatrix",
1149
+ "feComponentTransfer",
1150
+ "feComposite",
1151
+ "feConvolveMatrix",
1152
+ "feDiffuseLighting",
1153
+ "feDisplacementMap",
1154
+ "feDistantLight",
1155
+ "feDropShadow",
1156
+ "feFlood",
1157
+ "feFuncA",
1158
+ "feFuncB",
1159
+ "feFuncG",
1160
+ "feFuncR",
1161
+ "feGaussianBlur",
1162
+ "feImage",
1163
+ "feMerge",
1164
+ "feMergeNode",
1165
+ "feMorphology",
1166
+ "feOffset",
1167
+ "fePointLight",
1168
+ "feSpecularLighting",
1169
+ "feSpotLight",
1170
+ "feTile",
1171
+ "feTurbulence",
1172
+ "filter",
1173
+ "font",
1174
+ "font-face",
1175
+ "font-face-format",
1176
+ "font-face-name",
1177
+ "font-face-src",
1178
+ "font-face-uri",
1179
+ "foreignObject",
1180
+ "g",
1181
+ "glyph",
1182
+ "glyphRef",
1183
+ "hkern",
1184
+ "image",
1185
+ "line",
1186
+ "linearGradient",
1187
+ "marker",
1188
+ "mask",
1189
+ "metadata",
1190
+ "missing-glyph",
1191
+ "mpath",
1192
+ "path",
1193
+ "pattern",
1194
+ "polygon",
1195
+ "polyline",
1196
+ "radialGradient",
1197
+ "rect",
1198
+ "set",
1199
+ "stop",
1200
+ "svg",
1201
+ "switch",
1202
+ "symbol",
1203
+ "text",
1204
+ "textPath",
1205
+ "tref",
1206
+ "tspan",
1207
+ "use",
1208
+ "view",
1209
+ "vkern"
1210
+ ]);
1120
1211
  var SVGNamespace = {
1121
1212
  xlink: "http://www.w3.org/1999/xlink",
1122
1213
  xml: "http://www.w3.org/XML/1998/namespace"
@@ -1202,2213 +1293,2257 @@ var UnitlessStyles = /* @__PURE__ */ new Set([
1202
1293
  // src/jsx.ts
1203
1294
  var Fragment = Symbol("Fragment");
1204
1295
 
1205
- // src/node-ops.ts
1206
- function toNodeArray(node) {
1207
- try {
1208
- if (Array.isArray(node)) {
1209
- let allNodes = true;
1210
- for (const item of node) {
1211
- let isItemNode = false;
1212
- try {
1213
- isItemNode = item instanceof Node;
1214
- } catch {
1215
- isItemNode = false;
1216
- }
1217
- if (!isItemNode) {
1218
- allNodes = false;
1219
- break;
1220
- }
1221
- }
1222
- if (allNodes) {
1223
- return node;
1224
- }
1225
- const result = [];
1226
- for (const item of node) {
1227
- result.push(...toNodeArray(item));
1228
- }
1229
- return result;
1230
- }
1231
- if (node === null || node === void 0 || node === false) {
1232
- return [];
1233
- }
1234
- } catch {
1235
- return [];
1236
- }
1237
- let isNode = false;
1238
- try {
1239
- isNode = node instanceof Node;
1240
- } catch {
1241
- isNode = false;
1242
- }
1243
- if (isNode) {
1244
- try {
1245
- if (node instanceof DocumentFragment) {
1246
- return Array.from(node.childNodes);
1247
- }
1248
- } catch {
1249
- }
1250
- return [node];
1251
- }
1252
- try {
1253
- if (typeof node === "object" && node !== null && "marker" in node) {
1254
- return toNodeArray(node.marker);
1255
- }
1256
- } catch {
1296
+ // src/hooks.ts
1297
+ var ctxStack = [];
1298
+ function assertRenderContext(ctx, hookName) {
1299
+ if (!ctx.rendering) {
1300
+ throw new Error(`${hookName} can only be used during render execution`);
1257
1301
  }
1258
- try {
1259
- return [document.createTextNode(String(node))];
1260
- } catch {
1261
- return [document.createTextNode("")];
1302
+ }
1303
+ function __fictUseContext() {
1304
+ if (ctxStack.length === 0) {
1305
+ const ctx2 = { slots: [], cursor: 0, rendering: true };
1306
+ ctxStack.push(ctx2);
1307
+ return ctx2;
1262
1308
  }
1309
+ const ctx = ctxStack[ctxStack.length - 1];
1310
+ ctx.cursor = 0;
1311
+ ctx.rendering = true;
1312
+ return ctx;
1263
1313
  }
1264
- function insertNodesBefore(parent, nodes, anchor) {
1265
- if (nodes.length === 0) return;
1266
- if (nodes.length === 1) {
1267
- const node = nodes[0];
1268
- if (node === void 0 || node === null) return;
1269
- if (node.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
1270
- parent.ownerDocument.adoptNode(node);
1271
- }
1272
- try {
1273
- parent.insertBefore(node, anchor);
1274
- } catch (e) {
1275
- if (parent.ownerDocument) {
1276
- try {
1277
- const clone = parent.ownerDocument.importNode(node, true);
1278
- parent.insertBefore(clone, anchor);
1279
- return;
1280
- } catch {
1281
- }
1282
- }
1283
- throw e;
1284
- }
1285
- return;
1314
+ function __fictPushContext() {
1315
+ const ctx = { slots: [], cursor: 0 };
1316
+ ctxStack.push(ctx);
1317
+ return ctx;
1318
+ }
1319
+ function __fictPopContext() {
1320
+ ctxStack.pop();
1321
+ }
1322
+ function __fictResetContext() {
1323
+ ctxStack.length = 0;
1324
+ }
1325
+ function __fictUseSignal(ctx, initial, slot) {
1326
+ assertRenderContext(ctx, "__fictUseSignal");
1327
+ const index = slot ?? ctx.cursor++;
1328
+ if (!ctx.slots[index]) {
1329
+ ctx.slots[index] = signal(initial);
1286
1330
  }
1287
- const doc = parent.ownerDocument;
1288
- if (doc) {
1289
- const frag = doc.createDocumentFragment();
1290
- for (let i = 0; i < nodes.length; i++) {
1291
- const node = nodes[i];
1292
- if (node === void 0 || node === null) continue;
1293
- if (node.nodeType === 11) {
1294
- const childrenArr = Array.from(node.childNodes);
1295
- for (let j = 0; j < childrenArr.length; j++) {
1296
- frag.appendChild(childrenArr[j]);
1297
- }
1298
- } else {
1299
- if (node.ownerDocument !== doc) {
1300
- doc.adoptNode(node);
1301
- }
1302
- frag.appendChild(node);
1303
- }
1304
- }
1305
- parent.insertBefore(frag, anchor);
1306
- return;
1331
+ return ctx.slots[index];
1332
+ }
1333
+ function __fictUseMemo(ctx, fn, slot) {
1334
+ assertRenderContext(ctx, "__fictUseMemo");
1335
+ const index = slot ?? ctx.cursor++;
1336
+ if (!ctx.slots[index]) {
1337
+ ctx.slots[index] = createMemo(fn);
1307
1338
  }
1308
- const insertSingle = (nodeToInsert, anchorNode) => {
1309
- if (nodeToInsert.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
1310
- parent.ownerDocument.adoptNode(nodeToInsert);
1311
- }
1312
- try {
1313
- parent.insertBefore(nodeToInsert, anchorNode);
1314
- return nodeToInsert;
1315
- } catch (e) {
1316
- if (parent.ownerDocument) {
1317
- try {
1318
- const clone = parent.ownerDocument.importNode(nodeToInsert, true);
1319
- parent.insertBefore(clone, anchorNode);
1320
- return clone;
1321
- } catch {
1322
- }
1323
- }
1324
- throw e;
1325
- }
1326
- };
1327
- for (let i = nodes.length - 1; i >= 0; i--) {
1328
- const node = nodes[i];
1329
- if (node === void 0 || node === null) continue;
1330
- const isFrag = node.nodeType === 11;
1331
- if (isFrag) {
1332
- const childrenArr = Array.from(node.childNodes);
1333
- for (let j = childrenArr.length - 1; j >= 0; j--) {
1334
- const child = childrenArr[j];
1335
- anchor = insertSingle(child, anchor);
1336
- }
1337
- } else {
1338
- anchor = insertSingle(node, anchor);
1339
- }
1339
+ return ctx.slots[index];
1340
+ }
1341
+ function __fictUseEffect(ctx, fn, slot) {
1342
+ assertRenderContext(ctx, "__fictUseEffect");
1343
+ const index = slot ?? ctx.cursor++;
1344
+ if (!ctx.slots[index]) {
1345
+ ctx.slots[index] = createEffect(fn);
1340
1346
  }
1341
1347
  }
1342
- function removeNodes(nodes) {
1343
- for (const node of nodes) {
1344
- node.parentNode?.removeChild(node);
1348
+ function __fictRender(ctx, fn) {
1349
+ ctxStack.push(ctx);
1350
+ ctx.cursor = 0;
1351
+ ctx.rendering = true;
1352
+ try {
1353
+ return fn();
1354
+ } finally {
1355
+ ctx.rendering = false;
1356
+ ctxStack.pop();
1345
1357
  }
1346
1358
  }
1347
1359
 
1348
- // src/binding.ts
1349
- function isReactive(value) {
1350
- return typeof value === "function" && value.length === 0;
1360
+ // src/props.ts
1361
+ var propGetters = /* @__PURE__ */ new WeakSet();
1362
+ var rawToProxy = /* @__PURE__ */ new WeakMap();
1363
+ var proxyToRaw = /* @__PURE__ */ new WeakMap();
1364
+ function __fictProp(getter) {
1365
+ if (typeof getter === "function" && getter.length === 0) {
1366
+ propGetters.add(getter);
1367
+ }
1368
+ return getter;
1351
1369
  }
1352
- function callEventHandler(handler, event, node, data) {
1353
- if (!handler) return;
1354
- const context = node ?? event.currentTarget ?? void 0;
1355
- const invoke = (fn) => {
1356
- if (typeof fn === "function") {
1357
- const result = data === void 0 ? fn.call(context, event) : fn.call(context, data, event);
1358
- if (typeof result === "function" && result !== fn) {
1359
- if (data === void 0) {
1360
- result.call(context, event);
1361
- } else {
1362
- result.call(context, data, event);
1363
- }
1364
- } else if (result && typeof result.handleEvent === "function") {
1365
- result.handleEvent.call(result, event);
1366
- }
1367
- } else if (fn && typeof fn.handleEvent === "function") {
1368
- fn.handleEvent.call(fn, event);
1369
- }
1370
- };
1371
- invoke(handler);
1370
+ function isPropGetter(value) {
1371
+ return typeof value === "function" && propGetters.has(value);
1372
1372
  }
1373
- var PRIMITIVE_PROXY = Symbol("fict:primitive-proxy");
1374
- var PRIMITIVE_PROXY_RAW_VALUE = Symbol("fict:primitive-proxy:raw-value");
1375
- function createValueProxy(read) {
1376
- const getPrimitivePrototype = (value) => {
1377
- switch (typeof value) {
1378
- case "string":
1379
- return String.prototype;
1380
- case "number":
1381
- return Number.prototype;
1382
- case "boolean":
1383
- return Boolean.prototype;
1384
- case "bigint":
1385
- return BigInt.prototype;
1386
- case "symbol":
1387
- return Symbol.prototype;
1388
- default:
1389
- return void 0;
1390
- }
1391
- };
1392
- const target = {};
1393
- const handler = {
1394
- get(_target, prop, receiver) {
1395
- if (prop === PRIMITIVE_PROXY) {
1396
- return true;
1397
- }
1398
- if (prop === PRIMITIVE_PROXY_RAW_VALUE) {
1399
- return () => read();
1400
- }
1401
- if (prop === Symbol.toPrimitive) {
1402
- return (hint) => {
1403
- const value2 = read();
1404
- if (value2 != null && (typeof value2 === "object" || typeof value2 === "function")) {
1405
- const toPrimitive = value2[Symbol.toPrimitive];
1406
- if (typeof toPrimitive === "function") {
1407
- return toPrimitive.call(value2, hint);
1408
- }
1409
- if (hint === "string") return value2.toString?.() ?? "[object Object]";
1410
- if (hint === "number") return value2.valueOf?.() ?? value2;
1411
- return value2.valueOf?.() ?? value2;
1412
- }
1413
- return value2;
1414
- };
1415
- }
1416
- if (prop === "valueOf") {
1417
- return () => {
1418
- const value2 = read();
1419
- if (value2 != null && (typeof value2 === "object" || typeof value2 === "function")) {
1420
- return typeof value2.valueOf === "function" ? value2.valueOf() : value2;
1421
- }
1422
- return value2;
1423
- };
1424
- }
1425
- if (prop === "toString") {
1426
- return () => String(read());
1427
- }
1428
- const value = read();
1429
- if (value != null && (typeof value === "object" || typeof value === "function")) {
1430
- return Reflect.get(value, prop, receiver === _target ? value : receiver);
1431
- }
1432
- const proto = getPrimitivePrototype(value);
1433
- if (proto && prop in proto) {
1434
- const descriptor = Reflect.get(proto, prop, value);
1435
- return typeof descriptor === "function" ? descriptor.bind(value) : descriptor;
1436
- }
1437
- return void 0;
1438
- },
1439
- set(_target, prop, newValue, receiver) {
1440
- const value = read();
1441
- if (value != null && (typeof value === "object" || typeof value === "function")) {
1442
- return Reflect.set(value, prop, newValue, receiver === _target ? value : receiver);
1373
+ function createPropsProxy(props) {
1374
+ if (!props || typeof props !== "object") {
1375
+ return props;
1376
+ }
1377
+ if (proxyToRaw.has(props)) {
1378
+ return props;
1379
+ }
1380
+ const cached = rawToProxy.get(props);
1381
+ if (cached) {
1382
+ return cached;
1383
+ }
1384
+ const proxy = new Proxy(props, {
1385
+ get(target, prop, receiver) {
1386
+ const value = Reflect.get(target, prop, receiver);
1387
+ if (isPropGetter(value)) {
1388
+ return value();
1443
1389
  }
1444
- return false;
1390
+ return value;
1445
1391
  },
1446
- has(_target, prop) {
1447
- if (prop === PRIMITIVE_PROXY || prop === PRIMITIVE_PROXY_RAW_VALUE) {
1448
- return true;
1449
- }
1450
- const value = read();
1451
- if (value != null && (typeof value === "object" || typeof value === "function")) {
1452
- return prop in value;
1453
- }
1454
- const proto = getPrimitivePrototype(value);
1455
- return proto ? prop in proto : false;
1392
+ set(target, prop, value, receiver) {
1393
+ return Reflect.set(target, prop, value, receiver);
1456
1394
  },
1457
- ownKeys() {
1458
- const value = read();
1459
- if (value != null && (typeof value === "object" || typeof value === "function")) {
1460
- return Reflect.ownKeys(value);
1461
- }
1462
- const proto = getPrimitivePrototype(value);
1463
- return proto ? Reflect.ownKeys(proto) : [];
1395
+ has(target, prop) {
1396
+ return prop in target;
1464
1397
  },
1465
- getOwnPropertyDescriptor(_target, prop) {
1466
- const value = read();
1467
- if (value != null && (typeof value === "object" || typeof value === "function")) {
1468
- return Object.getOwnPropertyDescriptor(value, prop);
1469
- }
1470
- const proto = getPrimitivePrototype(value);
1471
- return proto ? Object.getOwnPropertyDescriptor(proto, prop) || void 0 : void 0;
1472
- }
1473
- };
1474
- return new Proxy(target, handler);
1475
- }
1476
- function bindText(textNode, getValue) {
1477
- return createRenderEffect(() => {
1478
- const value = formatTextValue(getValue());
1479
- if (textNode.data !== value) {
1480
- textNode.data = value;
1398
+ ownKeys(target) {
1399
+ return Reflect.ownKeys(target);
1400
+ },
1401
+ getOwnPropertyDescriptor(target, prop) {
1402
+ return Object.getOwnPropertyDescriptor(target, prop);
1481
1403
  }
1482
1404
  });
1405
+ rawToProxy.set(props, proxy);
1406
+ proxyToRaw.set(proxy, props);
1407
+ return proxy;
1483
1408
  }
1484
- function formatTextValue(value) {
1485
- if (value == null || value === false) {
1486
- return "";
1409
+ function unwrapProps(props) {
1410
+ if (!props || typeof props !== "object") {
1411
+ return props;
1487
1412
  }
1488
- return String(value);
1413
+ return proxyToRaw.get(props) ?? props;
1489
1414
  }
1490
- function createAttributeBinding(el, key, value, setter) {
1491
- if (isReactive(value)) {
1492
- createRenderEffect(() => {
1493
- setter(el, key, value());
1494
- });
1495
- } else {
1496
- setter(el, key, value);
1415
+ function __fictPropsRest(props, exclude) {
1416
+ const raw = unwrapProps(props);
1417
+ const out = {};
1418
+ const excludeSet = new Set(exclude);
1419
+ for (const key of Reflect.ownKeys(raw)) {
1420
+ if (excludeSet.has(key)) continue;
1421
+ out[key] = raw[key];
1497
1422
  }
1423
+ return createPropsProxy(out);
1498
1424
  }
1499
- function bindAttribute(el, key, getValue) {
1500
- let prevValue = void 0;
1501
- return createRenderEffect(() => {
1502
- const value = getValue();
1503
- if (value === prevValue) return;
1504
- prevValue = value;
1505
- if (value === void 0 || value === null || value === false) {
1506
- el.removeAttribute(key);
1507
- } else if (value === true) {
1508
- el.setAttribute(key, "");
1509
- } else {
1510
- el.setAttribute(key, String(value));
1425
+ function mergeProps(...sources) {
1426
+ const validSources = sources.filter(
1427
+ (s) => s != null && (typeof s === "object" || typeof s === "function")
1428
+ );
1429
+ if (validSources.length === 0) {
1430
+ return {};
1431
+ }
1432
+ if (validSources.length === 1 && typeof validSources[0] === "object") {
1433
+ return validSources[0];
1434
+ }
1435
+ const resolveSource = (src) => {
1436
+ const value = typeof src === "function" ? src() : src;
1437
+ if (!value || typeof value !== "object") return void 0;
1438
+ return unwrapProps(value);
1439
+ };
1440
+ return new Proxy({}, {
1441
+ get(_, prop) {
1442
+ if (typeof prop === "symbol") {
1443
+ return void 0;
1444
+ }
1445
+ for (let i = validSources.length - 1; i >= 0; i--) {
1446
+ const src = validSources[i];
1447
+ const raw = resolveSource(src);
1448
+ if (!raw || !(prop in raw)) continue;
1449
+ const value = raw[prop];
1450
+ if (typeof src === "function" && !isPropGetter(value)) {
1451
+ return __fictProp(() => {
1452
+ const latest = resolveSource(src);
1453
+ if (!latest || !(prop in latest)) return void 0;
1454
+ return latest[prop];
1455
+ });
1456
+ }
1457
+ return value;
1458
+ }
1459
+ return void 0;
1460
+ },
1461
+ has(_, prop) {
1462
+ for (const src of validSources) {
1463
+ const raw = resolveSource(src);
1464
+ if (raw && prop in raw) {
1465
+ return true;
1466
+ }
1467
+ }
1468
+ return false;
1469
+ },
1470
+ ownKeys() {
1471
+ const keys = /* @__PURE__ */ new Set();
1472
+ for (const src of validSources) {
1473
+ const raw = resolveSource(src);
1474
+ if (raw) {
1475
+ for (const key of Reflect.ownKeys(raw)) {
1476
+ keys.add(key);
1477
+ }
1478
+ }
1479
+ }
1480
+ return Array.from(keys);
1481
+ },
1482
+ getOwnPropertyDescriptor(_, prop) {
1483
+ for (let i = validSources.length - 1; i >= 0; i--) {
1484
+ const raw = resolveSource(validSources[i]);
1485
+ if (raw && prop in raw) {
1486
+ return {
1487
+ enumerable: true,
1488
+ configurable: true,
1489
+ get: () => {
1490
+ const value = raw[prop];
1491
+ return value;
1492
+ }
1493
+ };
1494
+ }
1495
+ }
1496
+ return void 0;
1511
1497
  }
1512
1498
  });
1513
1499
  }
1514
- function bindProperty(el, key, getValue) {
1515
- const PROPERTY_BINDING_KEYS = /* @__PURE__ */ new Set([
1516
- "value",
1517
- "checked",
1518
- "selected",
1519
- "disabled",
1520
- "readOnly",
1521
- "multiple",
1522
- "muted"
1523
- ]);
1524
- let prevValue = void 0;
1525
- return createRenderEffect(() => {
1526
- const next = getValue();
1527
- if (next === prevValue) return;
1528
- prevValue = next;
1529
- if (PROPERTY_BINDING_KEYS.has(key) && (next === void 0 || next === null)) {
1530
- const fallback = key === "checked" || key === "selected" ? false : "";
1531
- el[key] = fallback;
1532
- return;
1533
- }
1534
- ;
1535
- el[key] = next;
1536
- });
1500
+ function useProp(getter) {
1501
+ return __fictProp(createMemo(getter));
1537
1502
  }
1538
- function createStyleBinding(el, value) {
1539
- if (isReactive(value)) {
1540
- let prev;
1541
- createRenderEffect(() => {
1542
- const next = value();
1543
- applyStyle(el, next, prev);
1544
- prev = next;
1545
- });
1546
- } else {
1547
- applyStyle(el, value, void 0);
1503
+
1504
+ // src/scheduler.ts
1505
+ function batch2(fn) {
1506
+ return batch(fn);
1507
+ }
1508
+ function untrack2(fn) {
1509
+ return untrack(fn);
1510
+ }
1511
+
1512
+ // src/dom.ts
1513
+ var SVG_NS = "http://www.w3.org/2000/svg";
1514
+ var MATHML_NS = "http://www.w3.org/1998/Math/MathML";
1515
+ function render(view, container) {
1516
+ const root = createRootContext();
1517
+ const prev = pushRoot(root);
1518
+ let dom;
1519
+ try {
1520
+ const output = view();
1521
+ dom = createElement(output);
1522
+ } finally {
1523
+ popRoot(prev);
1548
1524
  }
1525
+ container.replaceChildren(dom);
1526
+ container.setAttribute("data-fict-fine-grained", "1");
1527
+ flushOnMount(root);
1528
+ const teardown = () => {
1529
+ destroyRoot(root);
1530
+ container.innerHTML = "";
1531
+ };
1532
+ return teardown;
1549
1533
  }
1550
- function bindStyle(el, getValue) {
1551
- let prev;
1552
- return createRenderEffect(() => {
1553
- const next = getValue();
1554
- applyStyle(el, next, prev);
1555
- prev = next;
1556
- });
1534
+ function createElement(node) {
1535
+ return createElementWithContext(node, null);
1557
1536
  }
1558
- function applyStyle(el, value, prev) {
1559
- if (typeof value === "string") {
1560
- el.style.cssText = value;
1561
- } else if (value && typeof value === "object") {
1562
- const styles = value;
1563
- if (typeof prev === "string") {
1564
- el.style.cssText = "";
1565
- }
1566
- if (prev && typeof prev === "object") {
1567
- const prevStyles = prev;
1568
- for (const key of Object.keys(prevStyles)) {
1569
- if (!(key in styles)) {
1570
- const cssProperty = key.replace(/([A-Z])/g, "-$1").toLowerCase();
1571
- el.style.removeProperty(cssProperty);
1537
+ function resolveNamespace(tagName, namespace) {
1538
+ if (tagName === "svg") return "svg";
1539
+ if (tagName === "math") return "mathml";
1540
+ if (namespace === "mathml") return "mathml";
1541
+ if (namespace === "svg") return "svg";
1542
+ if (SVGElements.has(tagName)) return "svg";
1543
+ return null;
1544
+ }
1545
+ function createElementWithContext(node, namespace) {
1546
+ if (node instanceof Node) {
1547
+ return node;
1548
+ }
1549
+ if (node === null || node === void 0 || node === false) {
1550
+ return document.createTextNode("");
1551
+ }
1552
+ if (typeof node === "object" && node !== null && !(node instanceof Node)) {
1553
+ if ("marker" in node) {
1554
+ const handle = node;
1555
+ if (typeof handle.dispose === "function") {
1556
+ registerRootCleanup(handle.dispose);
1557
+ }
1558
+ if (typeof handle.flush === "function") {
1559
+ const runFlush = () => handle.flush && handle.flush();
1560
+ if (typeof queueMicrotask === "function") {
1561
+ queueMicrotask(runFlush);
1562
+ } else {
1563
+ Promise.resolve().then(runFlush).catch(() => void 0);
1572
1564
  }
1573
1565
  }
1566
+ return createElement(handle.marker);
1574
1567
  }
1575
- for (const [prop, v] of Object.entries(styles)) {
1576
- if (v != null) {
1577
- const cssProperty = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
1578
- const unitless = isUnitlessStyleProperty(prop) || isUnitlessStyleProperty(cssProperty);
1579
- const valueStr = typeof v === "number" && !unitless ? `${v}px` : String(v);
1580
- el.style.setProperty(cssProperty, valueStr);
1581
- } else {
1582
- const cssProperty = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
1583
- el.style.removeProperty(cssProperty);
1584
- }
1568
+ const nodeRecord = node;
1569
+ if (nodeRecord[PRIMITIVE_PROXY]) {
1570
+ const primitiveGetter = nodeRecord[Symbol.toPrimitive];
1571
+ const value = typeof primitiveGetter === "function" ? primitiveGetter.call(node, "default") : node;
1572
+ return document.createTextNode(value == null || value === false ? "" : String(value));
1585
1573
  }
1586
- } else {
1587
- if (prev && typeof prev === "object") {
1588
- const prevStyles = prev;
1589
- for (const key of Object.keys(prevStyles)) {
1590
- const cssProperty = key.replace(/([A-Z])/g, "-$1").toLowerCase();
1591
- el.style.removeProperty(cssProperty);
1592
- }
1593
- } else if (typeof prev === "string") {
1594
- el.style.cssText = "";
1574
+ }
1575
+ if (Array.isArray(node)) {
1576
+ const frag = document.createDocumentFragment();
1577
+ for (const child of node) {
1578
+ appendChildNode(frag, child, namespace);
1595
1579
  }
1580
+ return frag;
1596
1581
  }
1597
- }
1598
- function isUnitlessStyleProperty(prop) {
1599
- return UnitlessStyles.has(prop);
1600
- }
1601
- function createClassBinding(el, value) {
1602
- if (isReactive(value)) {
1603
- let prev = {};
1604
- createRenderEffect(() => {
1605
- const next = value();
1606
- prev = applyClass(el, next, prev);
1582
+ if (typeof node === "string" || typeof node === "number") {
1583
+ return document.createTextNode(String(node));
1584
+ }
1585
+ if (typeof node === "boolean") {
1586
+ return document.createTextNode("");
1587
+ }
1588
+ const vnode = node;
1589
+ if (typeof vnode.type === "function") {
1590
+ const rawProps = unwrapProps(vnode.props ?? {});
1591
+ const baseProps = vnode.key === void 0 ? rawProps : new Proxy(rawProps, {
1592
+ get(target, prop, receiver) {
1593
+ if (prop === "key") return vnode.key;
1594
+ return Reflect.get(target, prop, receiver);
1595
+ },
1596
+ has(target, prop) {
1597
+ if (prop === "key") return true;
1598
+ return prop in target;
1599
+ },
1600
+ ownKeys(target) {
1601
+ const keys = new Set(Reflect.ownKeys(target));
1602
+ keys.add("key");
1603
+ return Array.from(keys);
1604
+ },
1605
+ getOwnPropertyDescriptor(target, prop) {
1606
+ if (prop === "key") {
1607
+ return { enumerable: true, configurable: true, value: vnode.key };
1608
+ }
1609
+ return Object.getOwnPropertyDescriptor(target, prop);
1610
+ }
1607
1611
  });
1608
- } else {
1609
- applyClass(el, value, {});
1612
+ const props = createPropsProxy(baseProps);
1613
+ __fictPushContext();
1614
+ try {
1615
+ const rendered = vnode.type(props);
1616
+ return createElementWithContext(rendered, namespace);
1617
+ } catch (err) {
1618
+ if (handleSuspend(err)) {
1619
+ return document.createComment("fict:suspend");
1620
+ }
1621
+ handleError(err, { source: "render", componentName: vnode.type.name });
1622
+ throw err;
1623
+ } finally {
1624
+ __fictPopContext();
1625
+ }
1626
+ }
1627
+ if (vnode.type === Fragment) {
1628
+ const frag = document.createDocumentFragment();
1629
+ const children = vnode.props?.children;
1630
+ appendChildren(frag, children, namespace);
1631
+ return frag;
1610
1632
  }
1633
+ const tagName = typeof vnode.type === "string" ? vnode.type : "div";
1634
+ const resolvedNamespace = resolveNamespace(tagName, namespace);
1635
+ const el = resolvedNamespace === "svg" ? document.createElementNS(SVG_NS, tagName) : resolvedNamespace === "mathml" ? document.createElementNS(MATHML_NS, tagName) : document.createElement(tagName);
1636
+ applyProps(el, vnode.props ?? {}, resolvedNamespace === "svg");
1637
+ appendChildren(
1638
+ el,
1639
+ vnode.props?.children,
1640
+ tagName === "foreignObject" ? null : resolvedNamespace
1641
+ );
1642
+ return el;
1611
1643
  }
1612
- function bindClass(el, getValue) {
1613
- let prev = {};
1614
- return createRenderEffect(() => {
1615
- const next = getValue();
1616
- prev = applyClass(el, next, prev);
1617
- });
1644
+ function template(html, isImportNode, isSVG, isMathML) {
1645
+ let node = null;
1646
+ const create = () => {
1647
+ const t = isMathML ? document.createElementNS(MATHML_NS, "template") : document.createElement("template");
1648
+ t.innerHTML = html;
1649
+ if (isSVG) {
1650
+ return t.content.firstChild.firstChild;
1651
+ }
1652
+ if (isMathML) {
1653
+ return t.firstChild;
1654
+ }
1655
+ return t.content.firstChild;
1656
+ };
1657
+ const fn = isImportNode ? () => untrack2(() => document.importNode(node || (node = create()), true)) : () => (node || (node = create())).cloneNode(true);
1658
+ fn.cloneNode = fn;
1659
+ return fn;
1618
1660
  }
1619
- function toggleClassKey(node, key, value) {
1620
- const classNames = key.trim().split(/\s+/);
1621
- for (let i = 0, len = classNames.length; i < len; i++) {
1622
- node.classList.toggle(classNames[i], value);
1623
- }
1661
+ function isBindingHandle(node) {
1662
+ return node !== null && typeof node === "object" && "marker" in node && "dispose" in node && typeof node.dispose === "function";
1624
1663
  }
1625
- function applyClass(el, value, prev) {
1626
- const prevState = prev && typeof prev === "object" ? prev : {};
1627
- if (typeof value === "string") {
1628
- el.className = value;
1629
- return {};
1664
+ function appendChildNode(parent, child, namespace) {
1665
+ if (child === null || child === void 0 || child === false) {
1666
+ return;
1630
1667
  }
1631
- if (value && typeof value === "object") {
1632
- const classes = value;
1633
- const classKeys = Object.keys(classes);
1634
- const prevKeys = Object.keys(prevState);
1635
- for (let i = 0, len = prevKeys.length; i < len; i++) {
1636
- const key = prevKeys[i];
1637
- if (!key || key === "undefined" || classes[key]) continue;
1638
- toggleClassKey(el, key, false);
1639
- delete prevState[key];
1668
+ if (isBindingHandle(child)) {
1669
+ appendChildNode(parent, child.marker, namespace);
1670
+ child.flush?.();
1671
+ return;
1672
+ }
1673
+ if (typeof child === "function" && child.length === 0) {
1674
+ const childGetter = child;
1675
+ createChildBinding(parent, childGetter, (node) => createElementWithContext(node, namespace));
1676
+ return;
1677
+ }
1678
+ if (Array.isArray(child)) {
1679
+ for (const item of child) {
1680
+ appendChildNode(parent, item, namespace);
1640
1681
  }
1641
- for (let i = 0, len = classKeys.length; i < len; i++) {
1642
- const key = classKeys[i];
1643
- const classValue = !!classes[key];
1644
- if (!key || key === "undefined" || prevState[key] === classValue || !classValue) continue;
1645
- toggleClassKey(el, key, true);
1646
- prevState[key] = classValue;
1682
+ return;
1683
+ }
1684
+ let domNode;
1685
+ if (typeof child !== "object" || child === null) {
1686
+ domNode = document.createTextNode(String(child ?? ""));
1687
+ } else {
1688
+ domNode = createElementWithContext(child, namespace);
1689
+ }
1690
+ if (domNode.nodeType === 11) {
1691
+ const children = Array.from(domNode.childNodes);
1692
+ for (const node of children) {
1693
+ appendChildNode(parent, node, namespace);
1647
1694
  }
1648
- return prevState;
1695
+ return;
1649
1696
  }
1650
- if (!value) {
1651
- for (const key of Object.keys(prevState)) {
1652
- if (key && key !== "undefined") {
1653
- toggleClassKey(el, key, false);
1654
- }
1697
+ if (domNode.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
1698
+ parent.ownerDocument.adoptNode(domNode);
1699
+ }
1700
+ try {
1701
+ parent.appendChild(domNode);
1702
+ } catch (e) {
1703
+ if (parent.ownerDocument) {
1704
+ const clone = parent.ownerDocument.importNode(domNode, true);
1705
+ parent.appendChild(clone);
1706
+ return;
1655
1707
  }
1656
- return {};
1708
+ throw e;
1657
1709
  }
1658
- return prevState;
1659
1710
  }
1660
- function insert(parent, getValue, markerOrCreateElement, createElementFn) {
1661
- let marker;
1662
- let ownsMarker = false;
1663
- let createFn = createElementFn;
1664
- if (markerOrCreateElement instanceof Node) {
1665
- marker = markerOrCreateElement;
1666
- createFn = createElementFn;
1667
- } else {
1668
- marker = document.createComment("fict:insert");
1669
- parent.appendChild(marker);
1670
- createFn = markerOrCreateElement;
1671
- ownsMarker = true;
1711
+ function appendChildren(parent, children, namespace) {
1712
+ if (children === void 0) return;
1713
+ if (Array.isArray(children)) {
1714
+ for (const child of children) {
1715
+ appendChildren(parent, child, namespace);
1716
+ }
1717
+ return;
1672
1718
  }
1673
- let currentNodes = [];
1674
- let currentText = null;
1675
- let currentRoot2 = null;
1676
- const clearCurrentNodes = () => {
1677
- if (currentNodes.length > 0) {
1678
- removeNodes(currentNodes);
1679
- currentNodes = [];
1719
+ appendChildNode(parent, children, namespace);
1720
+ }
1721
+ function applyRef(el, value) {
1722
+ if (typeof value === "function") {
1723
+ const refFn = value;
1724
+ refFn(el);
1725
+ if (getCurrentRoot()) {
1726
+ registerRootCleanup(() => {
1727
+ refFn(null);
1728
+ });
1680
1729
  }
1681
- };
1682
- const setTextNode = (textValue, shouldInsert, parentNode) => {
1683
- if (!currentText) {
1684
- currentText = document.createTextNode(textValue);
1685
- } else if (currentText.data !== textValue) {
1686
- currentText.data = textValue;
1730
+ } else if (value && typeof value === "object" && "current" in value) {
1731
+ const refObj = value;
1732
+ refObj.current = el;
1733
+ if (getCurrentRoot()) {
1734
+ registerRootCleanup(() => {
1735
+ refObj.current = null;
1736
+ });
1687
1737
  }
1688
- if (!shouldInsert) {
1689
- clearCurrentNodes();
1690
- return;
1738
+ }
1739
+ }
1740
+ function applyProps(el, props, isSVG = false) {
1741
+ props = unwrapProps(props);
1742
+ const tagName = el.tagName;
1743
+ const isCE = tagName.includes("-") || "is" in props;
1744
+ for (const [key, value] of Object.entries(props)) {
1745
+ if (key === "children") continue;
1746
+ if (key === "ref") {
1747
+ applyRef(el, value);
1748
+ continue;
1691
1749
  }
1692
- if (currentNodes.length === 1 && currentNodes[0] === currentText) {
1693
- return;
1750
+ if (isEventKey(key)) {
1751
+ bindEvent(
1752
+ el,
1753
+ eventNameFromProp(key),
1754
+ value
1755
+ );
1756
+ continue;
1694
1757
  }
1695
- clearCurrentNodes();
1696
- insertNodesBefore(parentNode, [currentText], marker);
1697
- currentNodes = [currentText];
1698
- };
1699
- const dispose = createRenderEffect(() => {
1700
- const value = getValue();
1701
- const parentNode = marker.parentNode;
1702
- const isPrimitive = value == null || value === false || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
1703
- if (isPrimitive) {
1704
- if (currentRoot2) {
1705
- destroyRoot(currentRoot2);
1706
- currentRoot2 = null;
1707
- }
1708
- if (!parentNode) {
1709
- clearCurrentNodes();
1710
- return;
1711
- }
1712
- const textValue = value == null || value === false ? "" : String(value);
1713
- const shouldInsert = value != null && value !== false;
1714
- setTextNode(textValue, shouldInsert, parentNode);
1715
- return;
1758
+ if (key.slice(0, 3) === "on:") {
1759
+ bindEvent(
1760
+ el,
1761
+ key.slice(3),
1762
+ value,
1763
+ false
1764
+ // Non-delegated
1765
+ );
1766
+ continue;
1716
1767
  }
1717
- if (currentRoot2) {
1718
- destroyRoot(currentRoot2);
1719
- currentRoot2 = null;
1768
+ if (key.slice(0, 10) === "oncapture:") {
1769
+ bindEvent(
1770
+ el,
1771
+ key.slice(10),
1772
+ value,
1773
+ true
1774
+ // Capture
1775
+ );
1776
+ continue;
1720
1777
  }
1721
- clearCurrentNodes();
1722
- const root = createRootContext();
1723
- const prev = pushRoot(root);
1724
- let nodes = [];
1725
- try {
1726
- let newNode;
1727
- if (value instanceof Node) {
1728
- newNode = value;
1729
- } else if (Array.isArray(value)) {
1730
- if (value.every((v) => v instanceof Node)) {
1731
- newNode = value;
1778
+ if (key === "class" || key === "className") {
1779
+ createClassBinding(el, value);
1780
+ continue;
1781
+ }
1782
+ if (key === "classList") {
1783
+ createClassBinding(el, value);
1784
+ continue;
1785
+ }
1786
+ if (key === "style") {
1787
+ createStyleBinding(
1788
+ el,
1789
+ value
1790
+ );
1791
+ continue;
1792
+ }
1793
+ if (key === "dangerouslySetInnerHTML" && value && typeof value === "object") {
1794
+ const htmlValue = value.__html;
1795
+ if (htmlValue !== void 0) {
1796
+ if (isReactive(htmlValue)) {
1797
+ createAttributeBinding(el, "innerHTML", htmlValue, setInnerHTML);
1732
1798
  } else {
1733
- newNode = createFn ? createFn(value) : document.createTextNode(String(value));
1799
+ el.innerHTML = htmlValue;
1734
1800
  }
1735
- } else {
1736
- newNode = createFn ? createFn(value) : document.createTextNode(String(value));
1737
- }
1738
- nodes = toNodeArray(newNode);
1739
- if (parentNode) {
1740
- insertNodesBefore(parentNode, nodes, marker);
1741
1801
  }
1742
- } finally {
1743
- popRoot(prev);
1744
- flushOnMount(root);
1802
+ continue;
1745
1803
  }
1746
- currentRoot2 = root;
1747
- currentNodes = nodes;
1748
- });
1749
- return () => {
1750
- dispose();
1751
- if (currentRoot2) {
1752
- destroyRoot(currentRoot2);
1753
- currentRoot2 = null;
1804
+ if (ChildProperties.has(key)) {
1805
+ createAttributeBinding(el, key, value, setProperty);
1806
+ continue;
1754
1807
  }
1755
- clearCurrentNodes();
1756
- if (ownsMarker) {
1757
- marker.parentNode?.removeChild(marker);
1808
+ if (key.slice(0, 5) === "attr:") {
1809
+ createAttributeBinding(el, key.slice(5), value, setAttribute);
1810
+ continue;
1758
1811
  }
1759
- };
1760
- }
1761
- function createChildBinding(parent, getValue, createElementFn) {
1762
- const marker = document.createComment("fict:child");
1763
- parent.appendChild(marker);
1764
- const dispose = createRenderEffect(() => {
1765
- const root = createRootContext();
1766
- const prev = pushRoot(root);
1767
- let nodes = [];
1768
- let handledError = false;
1769
- try {
1770
- const value = getValue();
1771
- if (value == null || value === false) {
1772
- return;
1773
- }
1774
- const output = createElementFn(value);
1775
- nodes = toNodeArray(output);
1776
- const parentNode = marker.parentNode;
1777
- if (parentNode) {
1778
- insertNodesBefore(parentNode, nodes, marker);
1779
- }
1780
- return () => {
1781
- destroyRoot(root);
1782
- removeNodes(nodes);
1783
- };
1784
- } catch (err) {
1785
- if (handleSuspend(err, root)) {
1786
- handledError = true;
1787
- destroyRoot(root);
1788
- return;
1789
- }
1790
- if (handleError(err, { source: "renderChild" }, root)) {
1791
- handledError = true;
1792
- destroyRoot(root);
1793
- return;
1794
- }
1795
- throw err;
1796
- } finally {
1797
- popRoot(prev);
1798
- if (!handledError) {
1799
- flushOnMount(root);
1800
- }
1812
+ if (key.slice(0, 5) === "bool:") {
1813
+ createAttributeBinding(el, key.slice(5), value, setBoolAttribute);
1814
+ continue;
1801
1815
  }
1802
- });
1803
- return {
1804
- marker,
1805
- dispose: () => {
1806
- dispose();
1807
- marker.parentNode?.removeChild(marker);
1816
+ if (key.slice(0, 5) === "prop:") {
1817
+ createAttributeBinding(el, key.slice(5), value, setProperty);
1818
+ continue;
1808
1819
  }
1809
- };
1810
- }
1811
- function delegateEvents(eventNames, doc = window.document) {
1812
- const e = doc[$$EVENTS] || (doc[$$EVENTS] = /* @__PURE__ */ new Set());
1813
- for (let i = 0, l = eventNames.length; i < l; i++) {
1814
- const name = eventNames[i];
1815
- if (!e.has(name)) {
1816
- e.add(name);
1817
- doc.addEventListener(name, globalEventHandler);
1820
+ const propAlias = !isSVG ? getPropAlias(key, tagName) : void 0;
1821
+ if (propAlias || !isSVG && Properties.has(key) || isCE && !isSVG) {
1822
+ const propName = propAlias || key;
1823
+ if (isCE && !Properties.has(key)) {
1824
+ createAttributeBinding(
1825
+ el,
1826
+ toPropertyName(propName),
1827
+ value,
1828
+ setProperty
1829
+ );
1830
+ } else {
1831
+ createAttributeBinding(el, propName, value, setProperty);
1832
+ }
1833
+ continue;
1834
+ }
1835
+ if (isSVG && key.indexOf(":") > -1) {
1836
+ const [prefix, name] = key.split(":");
1837
+ const ns = SVGNamespace[prefix];
1838
+ if (ns) {
1839
+ createAttributeBinding(
1840
+ el,
1841
+ key,
1842
+ value,
1843
+ (el2, _key, val) => setAttributeNS(el2, ns, name, val)
1844
+ );
1845
+ continue;
1846
+ }
1818
1847
  }
1848
+ const attrName = Aliases[key] || key;
1849
+ createAttributeBinding(el, attrName, value, setAttribute);
1819
1850
  }
1820
1851
  }
1821
- function clearDelegatedEvents(doc = window.document) {
1822
- const e = doc[$$EVENTS];
1823
- if (e) {
1824
- for (const name of e.keys()) {
1825
- doc.removeEventListener(name, globalEventHandler);
1852
+ function toPropertyName(name) {
1853
+ return name.toLowerCase().replace(/-([a-z])/g, (_, w) => w.toUpperCase());
1854
+ }
1855
+ var setAttribute = (el, key, value) => {
1856
+ if (value === void 0 || value === null || value === false) {
1857
+ el.removeAttribute(key);
1858
+ return;
1859
+ }
1860
+ if (value === true) {
1861
+ el.setAttribute(key, "");
1862
+ return;
1863
+ }
1864
+ const valueType = typeof value;
1865
+ if (valueType === "string" || valueType === "number") {
1866
+ el.setAttribute(key, String(value));
1867
+ return;
1868
+ }
1869
+ if (key in el) {
1870
+ el[key] = value;
1871
+ return;
1872
+ }
1873
+ el.setAttribute(key, String(value));
1874
+ };
1875
+ var setProperty = (el, key, value) => {
1876
+ if (value === void 0 || value === null) {
1877
+ const fallback = key === "checked" || key === "selected" ? false : "";
1878
+ el[key] = fallback;
1879
+ return;
1880
+ }
1881
+ if (key === "style" && typeof value === "object" && value !== null) {
1882
+ for (const k in value) {
1883
+ const v = value[k];
1884
+ if (v !== void 0) {
1885
+ el.style[k] = String(v);
1886
+ }
1826
1887
  }
1827
- delete doc[$$EVENTS];
1888
+ return;
1889
+ }
1890
+ el[key] = value;
1891
+ };
1892
+ var setInnerHTML = (el, _key, value) => {
1893
+ el.innerHTML = value == null ? "" : String(value);
1894
+ };
1895
+ var setBoolAttribute = (el, key, value) => {
1896
+ if (value) {
1897
+ el.setAttribute(key, "");
1898
+ } else {
1899
+ el.removeAttribute(key);
1900
+ }
1901
+ };
1902
+ function setAttributeNS(el, namespace, name, value) {
1903
+ if (value == null) {
1904
+ el.removeAttributeNS(namespace, name);
1905
+ } else {
1906
+ el.setAttributeNS(namespace, name, String(value));
1828
1907
  }
1829
1908
  }
1830
- function globalEventHandler(e) {
1831
- let node = e.target;
1832
- const key = `$$${e.type}`;
1833
- const dataKey = `${key}Data`;
1834
- const oriTarget = e.target;
1835
- const oriCurrentTarget = e.currentTarget;
1836
- const retarget = (value) => Object.defineProperty(e, "target", {
1837
- configurable: true,
1838
- value
1839
- });
1840
- const handleNode = () => {
1841
- if (!node) return false;
1842
- const handler = node[key];
1843
- if (handler && !node.disabled) {
1844
- const resolveData = (value) => {
1845
- if (typeof value === "function") {
1846
- try {
1847
- const fn = value;
1848
- return fn.length > 0 ? fn(e) : fn();
1849
- } catch {
1850
- return value();
1851
- }
1909
+ function isEventKey(key) {
1910
+ return key.startsWith("on") && key.length > 2 && key[2].toUpperCase() === key[2];
1911
+ }
1912
+ function eventNameFromProp(key) {
1913
+ return key.slice(2).toLowerCase();
1914
+ }
1915
+
1916
+ // src/node-ops.ts
1917
+ function toNodeArray(node) {
1918
+ try {
1919
+ if (Array.isArray(node)) {
1920
+ let allNodes = true;
1921
+ for (const item of node) {
1922
+ let isItemNode = false;
1923
+ try {
1924
+ isItemNode = item instanceof Node;
1925
+ } catch {
1926
+ isItemNode = false;
1927
+ }
1928
+ if (!isItemNode) {
1929
+ allNodes = false;
1930
+ break;
1852
1931
  }
1853
- return value;
1854
- };
1855
- const rawData = node[dataKey];
1856
- const hasData = rawData !== void 0;
1857
- const resolvedNodeData = hasData ? resolveData(rawData) : void 0;
1858
- if (typeof handler === "function") {
1859
- callEventHandler(handler, e, node, hasData ? resolvedNodeData : void 0);
1860
- } else if (Array.isArray(handler)) {
1861
- const tupleData = resolveData(handler[1]);
1862
- callEventHandler(handler[0], e, node, tupleData);
1863
1932
  }
1864
- if (e.cancelBubble) return false;
1865
- }
1866
- const shadowHost = node.host;
1867
- if (shadowHost && typeof shadowHost !== "string" && !shadowHost._$host && node.contains(e.target)) {
1868
- retarget(shadowHost);
1869
- }
1870
- return true;
1871
- };
1872
- const walkUpTree = () => {
1873
- while (handleNode() && node) {
1874
- node = node._$host || node.parentNode || node.host;
1875
- }
1876
- };
1877
- Object.defineProperty(e, "currentTarget", {
1878
- configurable: true,
1879
- get() {
1880
- return node || document;
1881
- }
1882
- });
1883
- if (e.composedPath) {
1884
- const path = e.composedPath();
1885
- retarget(path[0]);
1886
- for (let i = 0; i < path.length - 2; i++) {
1887
- node = path[i];
1888
- if (!handleNode()) break;
1889
- if (node._$host) {
1890
- node = node._$host;
1891
- walkUpTree();
1892
- break;
1933
+ if (allNodes) {
1934
+ return node;
1893
1935
  }
1894
- if (node.parentNode === oriCurrentTarget) {
1895
- break;
1936
+ const result = [];
1937
+ for (const item of node) {
1938
+ result.push(...toNodeArray(item));
1896
1939
  }
1940
+ return result;
1897
1941
  }
1898
- } else {
1899
- walkUpTree();
1942
+ if (node === null || node === void 0 || node === false) {
1943
+ return [];
1944
+ }
1945
+ } catch {
1946
+ return [];
1900
1947
  }
1901
- retarget(oriTarget);
1902
- }
1903
- function bindEvent(el, eventName, handler, options2) {
1904
- if (handler == null) return () => {
1905
- };
1906
- const rootRef = getCurrentRoot();
1907
- if (DelegatedEvents.has(eventName) && !options2) {
1908
- const key = `$$${eventName}`;
1909
- delegateEvents([eventName]);
1910
- const resolveHandler = isReactive(handler) ? handler : () => handler;
1911
- el[key] = function(...args) {
1912
- try {
1913
- const fn = resolveHandler();
1914
- callEventHandler(fn, args[0], el);
1915
- } catch (err) {
1916
- handleError(err, { source: "event", eventName }, rootRef);
1917
- }
1918
- };
1919
- return () => {
1920
- el[key] = void 0;
1921
- };
1948
+ let isNode = false;
1949
+ try {
1950
+ isNode = node instanceof Node;
1951
+ } catch {
1952
+ isNode = false;
1922
1953
  }
1923
- const getHandler = isReactive(handler) ? handler : () => handler;
1924
- const wrapped = (event) => {
1954
+ if (isNode) {
1925
1955
  try {
1926
- const resolved = getHandler();
1927
- callEventHandler(resolved, event, el);
1928
- } catch (err) {
1929
- if (handleError(err, { source: "event", eventName }, rootRef)) {
1930
- return;
1956
+ if (node instanceof DocumentFragment) {
1957
+ return Array.from(node.childNodes);
1931
1958
  }
1932
- throw err;
1933
- }
1934
- };
1935
- el.addEventListener(eventName, wrapped, options2);
1936
- const cleanup = () => el.removeEventListener(eventName, wrapped, options2);
1937
- registerRootCleanup(cleanup);
1938
- return cleanup;
1939
- }
1940
- function bindRef(el, ref) {
1941
- if (ref == null) return () => {
1942
- };
1943
- const getRef = isReactive(ref) ? ref : () => ref;
1944
- const applyRef2 = (refValue) => {
1945
- if (refValue == null) return;
1946
- if (typeof refValue === "function") {
1947
- refValue(el);
1948
- } else if (typeof refValue === "object" && "current" in refValue) {
1949
- refValue.current = el;
1959
+ } catch {
1950
1960
  }
1951
- };
1952
- const initialRef = getRef();
1953
- applyRef2(initialRef);
1954
- if (isReactive(ref)) {
1955
- const cleanup2 = createRenderEffect(() => {
1956
- const currentRef = getRef();
1957
- applyRef2(currentRef);
1958
- });
1959
- registerRootCleanup(cleanup2);
1960
- const nullifyCleanup = () => {
1961
- const currentRef = getRef();
1962
- if (currentRef && typeof currentRef === "object" && "current" in currentRef) {
1963
- currentRef.current = null;
1964
- }
1965
- };
1966
- registerRootCleanup(nullifyCleanup);
1967
- return () => {
1968
- cleanup2();
1969
- nullifyCleanup();
1970
- };
1961
+ return [node];
1971
1962
  }
1972
- const cleanup = () => {
1973
- const refValue = getRef();
1974
- if (refValue && typeof refValue === "object" && "current" in refValue) {
1975
- refValue.current = null;
1963
+ try {
1964
+ if (typeof node === "object" && node !== null && "marker" in node) {
1965
+ return toNodeArray(node.marker);
1976
1966
  }
1977
- };
1978
- registerRootCleanup(cleanup);
1979
- return cleanup;
1967
+ } catch {
1968
+ }
1969
+ try {
1970
+ return [document.createTextNode(String(node))];
1971
+ } catch {
1972
+ return [document.createTextNode("")];
1973
+ }
1980
1974
  }
1981
- function createConditional(condition, renderTrue, createElementFn, renderFalse) {
1982
- const startMarker = document.createComment("fict:cond:start");
1983
- const endMarker = document.createComment("fict:cond:end");
1984
- const fragment = document.createDocumentFragment();
1985
- fragment.append(startMarker, endMarker);
1986
- let currentNodes = [];
1987
- let currentRoot2 = null;
1988
- let lastCondition = void 0;
1989
- let pendingRender = false;
1990
- const conditionMemo = computed(condition);
1991
- const runConditional = () => {
1992
- const cond = conditionMemo();
1993
- const parent = startMarker.parentNode;
1994
- if (!parent) {
1995
- pendingRender = true;
1996
- return;
1997
- }
1998
- pendingRender = false;
1999
- if (lastCondition === cond && currentNodes.length > 0) {
2000
- return;
1975
+ function insertNodesBefore(parent, nodes, anchor) {
1976
+ if (nodes.length === 0) return;
1977
+ if (nodes.length === 1) {
1978
+ const node = nodes[0];
1979
+ if (node === void 0 || node === null) return;
1980
+ if (node.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
1981
+ parent.ownerDocument.adoptNode(node);
2001
1982
  }
2002
- if (lastCondition === cond && lastCondition === false && renderFalse === void 0) {
2003
- return;
1983
+ try {
1984
+ parent.insertBefore(node, anchor);
1985
+ } catch (e) {
1986
+ if (parent.ownerDocument) {
1987
+ try {
1988
+ const clone = parent.ownerDocument.importNode(node, true);
1989
+ parent.insertBefore(clone, anchor);
1990
+ return;
1991
+ } catch {
1992
+ }
1993
+ }
1994
+ throw e;
2004
1995
  }
2005
- lastCondition = cond;
2006
- if (currentRoot2) {
2007
- destroyRoot(currentRoot2);
2008
- currentRoot2 = null;
1996
+ return;
1997
+ }
1998
+ const doc = parent.ownerDocument;
1999
+ if (doc) {
2000
+ const frag = doc.createDocumentFragment();
2001
+ for (let i = 0; i < nodes.length; i++) {
2002
+ const node = nodes[i];
2003
+ if (node === void 0 || node === null) continue;
2004
+ if (node.nodeType === 11) {
2005
+ const childrenArr = Array.from(node.childNodes);
2006
+ for (let j = 0; j < childrenArr.length; j++) {
2007
+ frag.appendChild(childrenArr[j]);
2008
+ }
2009
+ } else {
2010
+ if (node.ownerDocument !== doc) {
2011
+ doc.adoptNode(node);
2012
+ }
2013
+ frag.appendChild(node);
2014
+ }
2009
2015
  }
2010
- removeNodes(currentNodes);
2011
- currentNodes = [];
2012
- const render2 = cond ? renderTrue : renderFalse;
2013
- if (!render2) {
2014
- return;
2016
+ parent.insertBefore(frag, anchor);
2017
+ return;
2018
+ }
2019
+ const insertSingle = (nodeToInsert, anchorNode) => {
2020
+ if (nodeToInsert.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
2021
+ parent.ownerDocument.adoptNode(nodeToInsert);
2015
2022
  }
2016
- const root = createRootContext();
2017
- const prev = pushRoot(root);
2018
- let handledError = false;
2019
2023
  try {
2020
- const output = untrack(render2);
2021
- if (output == null || output === false) {
2022
- return;
2023
- }
2024
- const el = createElementFn(output);
2025
- const nodes = toNodeArray(el);
2026
- insertNodesBefore(parent, nodes, endMarker);
2027
- currentNodes = nodes;
2028
- } catch (err) {
2029
- if (handleSuspend(err, root)) {
2030
- handledError = true;
2031
- destroyRoot(root);
2032
- return;
2033
- }
2034
- if (handleError(err, { source: "renderChild" }, root)) {
2035
- handledError = true;
2036
- destroyRoot(root);
2037
- return;
2038
- }
2039
- throw err;
2040
- } finally {
2041
- popRoot(prev);
2042
- if (!handledError) {
2043
- flushOnMount(root);
2044
- currentRoot2 = root;
2045
- } else {
2046
- currentRoot2 = null;
2024
+ parent.insertBefore(nodeToInsert, anchorNode);
2025
+ return nodeToInsert;
2026
+ } catch (e) {
2027
+ if (parent.ownerDocument) {
2028
+ try {
2029
+ const clone = parent.ownerDocument.importNode(nodeToInsert, true);
2030
+ parent.insertBefore(clone, anchorNode);
2031
+ return clone;
2032
+ } catch {
2033
+ }
2047
2034
  }
2035
+ throw e;
2048
2036
  }
2049
2037
  };
2050
- const dispose = createRenderEffect(runConditional);
2051
- return {
2052
- marker: fragment,
2053
- flush: () => {
2054
- if (pendingRender) {
2055
- runConditional();
2056
- }
2057
- },
2058
- dispose: () => {
2059
- dispose();
2060
- if (currentRoot2) {
2061
- destroyRoot(currentRoot2);
2038
+ for (let i = nodes.length - 1; i >= 0; i--) {
2039
+ const node = nodes[i];
2040
+ if (node === void 0 || node === null) continue;
2041
+ const isFrag = node.nodeType === 11;
2042
+ if (isFrag) {
2043
+ const childrenArr = Array.from(node.childNodes);
2044
+ for (let j = childrenArr.length - 1; j >= 0; j--) {
2045
+ const child = childrenArr[j];
2046
+ anchor = insertSingle(child, anchor);
2062
2047
  }
2063
- removeNodes(currentNodes);
2064
- currentNodes = [];
2065
- startMarker.parentNode?.removeChild(startMarker);
2066
- endMarker.parentNode?.removeChild(endMarker);
2048
+ } else {
2049
+ anchor = insertSingle(node, anchor);
2067
2050
  }
2068
- };
2051
+ }
2069
2052
  }
2070
- function createList(items, renderItem, createElementFn, getKey) {
2071
- const startMarker = document.createComment("fict:list:start");
2072
- const endMarker = document.createComment("fict:list:end");
2073
- const fragment = document.createDocumentFragment();
2074
- fragment.append(startMarker, endMarker);
2075
- const nodeMap = /* @__PURE__ */ new Map();
2076
- let pendingItems = null;
2077
- const runListUpdate = () => {
2078
- const arr = items();
2079
- const parent = startMarker.parentNode;
2080
- if (!parent) {
2081
- pendingItems = arr;
2082
- return;
2053
+ function removeNodes(nodes) {
2054
+ for (const node of nodes) {
2055
+ node.parentNode?.removeChild(node);
2056
+ }
2057
+ }
2058
+
2059
+ // src/reconcile.ts
2060
+ function reconcileArrays(parentNode, a, b) {
2061
+ const bLength = b.length;
2062
+ let aEnd = a.length;
2063
+ let bEnd = bLength;
2064
+ let aStart = 0;
2065
+ let bStart = 0;
2066
+ const after = aEnd > 0 ? a[aEnd - 1].nextSibling : null;
2067
+ let map = null;
2068
+ while (aStart < aEnd || bStart < bEnd) {
2069
+ if (a[aStart] === b[bStart]) {
2070
+ aStart++;
2071
+ bStart++;
2072
+ continue;
2083
2073
  }
2084
- pendingItems = null;
2085
- const newNodeMap = /* @__PURE__ */ new Map();
2086
- const blocks = [];
2087
- for (let i = 0; i < arr.length; i++) {
2088
- const item = arr[i];
2089
- const key = getKey ? getKey(item, i) : i;
2090
- const existing = nodeMap.get(key);
2091
- let block;
2092
- if (existing) {
2093
- const previousValue = existing.value();
2094
- if (!getKey && previousValue !== item) {
2095
- destroyRoot(existing.root);
2096
- removeBlockNodes(existing);
2097
- block = mountBlock(item, i, renderItem, parent, endMarker, createElementFn);
2098
- } else {
2099
- const previousIndex = existing.index();
2100
- existing.value(item);
2101
- existing.index(i);
2102
- if (previousValue === item) {
2103
- bumpBlockVersion(existing);
2104
- }
2105
- const needsRerender = getKey ? true : previousValue !== item || previousIndex !== i;
2106
- block = needsRerender ? rerenderBlock(existing, createElementFn) : existing;
2074
+ while (a[aEnd - 1] === b[bEnd - 1]) {
2075
+ aEnd--;
2076
+ bEnd--;
2077
+ }
2078
+ if (aEnd === aStart) {
2079
+ const node = bEnd < bLength ? bStart ? b[bStart - 1].nextSibling : b[bEnd - bStart] ?? null : after;
2080
+ const count = bEnd - bStart;
2081
+ const doc = parentNode.ownerDocument;
2082
+ if (count > 1 && doc) {
2083
+ const frag = doc.createDocumentFragment();
2084
+ for (let i = bStart; i < bEnd; i++) {
2085
+ frag.appendChild(b[i]);
2107
2086
  }
2087
+ parentNode.insertBefore(frag, node);
2088
+ bStart = bEnd;
2108
2089
  } else {
2109
- block = mountBlock(item, i, renderItem, parent, endMarker, createElementFn);
2090
+ while (bStart < bEnd) {
2091
+ parentNode.insertBefore(b[bStart++], node);
2092
+ }
2110
2093
  }
2111
- newNodeMap.set(key, block);
2112
- blocks.push(block);
2113
- }
2114
- for (const [key, managed] of nodeMap) {
2115
- if (!newNodeMap.has(key)) {
2116
- destroyRoot(managed.root);
2117
- removeBlockNodes(managed);
2094
+ } else if (bEnd === bStart) {
2095
+ while (aStart < aEnd) {
2096
+ const nodeToRemove = a[aStart];
2097
+ if (!map || !map.has(nodeToRemove)) {
2098
+ nodeToRemove.parentNode?.removeChild(nodeToRemove);
2099
+ }
2100
+ aStart++;
2118
2101
  }
2119
- }
2120
- let anchor = endMarker;
2121
- for (let i = blocks.length - 1; i >= 0; i--) {
2122
- const block = blocks[i];
2123
- insertNodesBefore(parent, block.nodes, anchor);
2124
- if (block.nodes.length > 0) {
2125
- anchor = block.nodes[0];
2102
+ } else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
2103
+ const node = a[--aEnd].nextSibling;
2104
+ parentNode.insertBefore(b[bStart++], a[aStart++].nextSibling);
2105
+ parentNode.insertBefore(b[--bEnd], node);
2106
+ a[aEnd] = b[bEnd];
2107
+ } else {
2108
+ if (!map) {
2109
+ map = /* @__PURE__ */ new Map();
2110
+ let i = bStart;
2111
+ while (i < bEnd) {
2112
+ map.set(b[i], i++);
2113
+ }
2114
+ }
2115
+ const index = map.get(a[aStart]);
2116
+ if (index != null) {
2117
+ if (bStart < index && index < bEnd) {
2118
+ let i = aStart;
2119
+ let sequence = 1;
2120
+ let t;
2121
+ while (++i < aEnd && i < bEnd) {
2122
+ t = map.get(a[i]);
2123
+ if (t == null || t !== index + sequence) break;
2124
+ sequence++;
2125
+ }
2126
+ if (sequence > index - bStart) {
2127
+ const node = a[aStart];
2128
+ while (bStart < index) {
2129
+ parentNode.insertBefore(b[bStart++], node);
2130
+ }
2131
+ } else {
2132
+ parentNode.replaceChild(b[bStart++], a[aStart++]);
2133
+ }
2134
+ } else {
2135
+ aStart++;
2136
+ }
2137
+ } else {
2138
+ const nodeToRemove = a[aStart++];
2139
+ nodeToRemove.parentNode?.removeChild(nodeToRemove);
2126
2140
  }
2127
2141
  }
2128
- nodeMap.clear();
2129
- for (const [k, v] of newNodeMap) {
2130
- nodeMap.set(k, v);
2142
+ }
2143
+ }
2144
+
2145
+ // src/list-helpers.ts
2146
+ function moveNodesBefore(parent, nodes, anchor) {
2147
+ for (let i = nodes.length - 1; i >= 0; i--) {
2148
+ const node = nodes[i];
2149
+ if (!node || !(node instanceof Node)) {
2150
+ throw new Error("Invalid node in moveNodesBefore");
2131
2151
  }
2132
- };
2133
- const dispose = createRenderEffect(runListUpdate);
2134
- return {
2135
- marker: fragment,
2136
- flush: () => {
2137
- if (pendingItems !== null) {
2138
- runListUpdate();
2152
+ if (node.nextSibling !== anchor) {
2153
+ if (node.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
2154
+ parent.ownerDocument.adoptNode(node);
2139
2155
  }
2140
- },
2141
- dispose: () => {
2142
- dispose();
2143
- for (const [, managed] of nodeMap) {
2144
- destroyRoot(managed.root);
2145
- removeBlockNodes(managed);
2156
+ try {
2157
+ parent.insertBefore(node, anchor);
2158
+ } catch (e) {
2159
+ if (parent.ownerDocument) {
2160
+ try {
2161
+ const clone = parent.ownerDocument.importNode(node, true);
2162
+ parent.insertBefore(clone, anchor);
2163
+ continue;
2164
+ } catch {
2165
+ }
2166
+ }
2167
+ throw e;
2146
2168
  }
2147
- nodeMap.clear();
2148
- startMarker.parentNode?.removeChild(startMarker);
2149
- endMarker.parentNode?.removeChild(endMarker);
2150
2169
  }
2151
- };
2170
+ anchor = node;
2171
+ }
2152
2172
  }
2153
- function mountBlock(initialValue, initialIndex, renderItem, parent, anchor, createElementFn) {
2154
- const start = document.createComment("fict:block:start");
2155
- const end = document.createComment("fict:block:end");
2156
- const valueSig = signal(initialValue);
2157
- const indexSig = signal(initialIndex);
2158
- const versionSig = signal(0);
2159
- const valueProxy = createValueProxy(() => {
2160
- versionSig();
2161
- return valueSig();
2162
- });
2163
- const renderCurrent = () => renderItem(valueProxy, indexSig());
2164
- const root = createRootContext();
2165
- const prev = pushRoot(root);
2166
- const nodes = [start];
2167
- let handledError = false;
2168
- try {
2169
- const output = renderCurrent();
2170
- if (output != null && output !== false) {
2171
- const el = createElementFn(output);
2172
- const rendered = toNodeArray(el);
2173
- nodes.push(...rendered);
2174
- }
2175
- nodes.push(end);
2176
- insertNodesBefore(parent, nodes, anchor);
2177
- } catch (err) {
2178
- if (handleSuspend(err, root)) {
2179
- handledError = true;
2180
- nodes.push(end);
2181
- insertNodesBefore(parent, nodes, anchor);
2182
- } else if (handleError(err, { source: "renderChild" }, root)) {
2183
- handledError = true;
2184
- nodes.push(end);
2185
- insertNodesBefore(parent, nodes, anchor);
2186
- } else {
2187
- throw err;
2188
- }
2189
- } finally {
2190
- popRoot(prev);
2191
- if (!handledError) {
2192
- flushOnMount(root);
2193
- } else {
2194
- destroyRoot(root);
2195
- }
2173
+ function moveMarkerBlock(parent, block, anchor) {
2174
+ const nodes = collectBlockNodes(block);
2175
+ if (nodes.length === 0) return;
2176
+ moveNodesBefore(parent, nodes, anchor);
2177
+ }
2178
+ function destroyMarkerBlock(block) {
2179
+ if (block.root) {
2180
+ destroyRoot(block.root);
2196
2181
  }
2197
- return {
2198
- nodes,
2199
- root,
2200
- value: valueSig,
2201
- index: indexSig,
2202
- version: versionSig,
2203
- start,
2204
- end,
2205
- valueProxy,
2206
- renderCurrent
2207
- };
2182
+ removeBlockRange(block);
2208
2183
  }
2209
- function rerenderBlock(block, createElementFn) {
2210
- const currentContent = block.nodes.slice(1, Math.max(1, block.nodes.length - 1));
2211
- const currentNode = currentContent.length === 1 ? currentContent[0] : null;
2212
- clearRoot(block.root);
2213
- const prev = pushRoot(block.root);
2214
- let nextOutput;
2215
- let handledError = false;
2216
- try {
2217
- nextOutput = block.renderCurrent();
2218
- } catch (err) {
2219
- if (handleSuspend(err, block.root)) {
2220
- handledError = true;
2221
- popRoot(prev);
2222
- destroyRoot(block.root);
2223
- block.nodes = [block.start, block.end];
2224
- return block;
2225
- }
2226
- if (handleError(err, { source: "renderChild" }, block.root)) {
2227
- handledError = true;
2228
- popRoot(prev);
2229
- destroyRoot(block.root);
2230
- block.nodes = [block.start, block.end];
2231
- return block;
2232
- }
2233
- throw err;
2234
- } finally {
2235
- if (!handledError) {
2236
- popRoot(prev);
2237
- }
2238
- }
2239
- if (isFragmentVNode(nextOutput) && currentContent.length > 0) {
2240
- const patched = patchFragmentChildren(currentContent, nextOutput.props?.children);
2241
- if (patched) {
2242
- block.nodes = [block.start, ...currentContent, block.end];
2243
- return block;
2244
- }
2245
- }
2246
- if (currentNode && patchNode(currentNode, nextOutput)) {
2247
- block.nodes = [block.start, currentNode, block.end];
2248
- return block;
2249
- }
2250
- clearContent(block);
2251
- if (nextOutput != null && nextOutput !== false) {
2252
- const newNodes = toNodeArray(
2253
- nextOutput instanceof Node ? nextOutput : createElementFn(nextOutput)
2254
- );
2255
- insertNodesBefore(block.start.parentNode, newNodes, block.end);
2256
- block.nodes = [block.start, ...newNodes, block.end];
2257
- } else {
2258
- block.nodes = [block.start, block.end];
2259
- }
2260
- return block;
2261
- }
2262
- function patchElement(el, output) {
2263
- if (output === null || output === void 0 || output === false || typeof output === "string" || typeof output === "number") {
2264
- el.textContent = output === null || output === void 0 || output === false ? "" : String(output);
2265
- return true;
2266
- }
2267
- if (output instanceof Text) {
2268
- el.textContent = output.data;
2269
- return true;
2270
- }
2271
- if (output && typeof output === "object" && !(output instanceof Node)) {
2272
- const vnode = output;
2273
- if (typeof vnode.type === "string" && vnode.type.toLowerCase() === el.tagName.toLowerCase()) {
2274
- const children = vnode.props?.children;
2275
- const props = vnode.props ?? {};
2276
- for (const [key, value] of Object.entries(props)) {
2277
- if (key === "children" || key === "key") continue;
2278
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null || value === void 0) {
2279
- if (key === "class" || key === "className") {
2280
- el.setAttribute("class", value === false || value === null ? "" : String(value));
2281
- } else if (key === "style" && typeof value === "string") {
2282
- el.style.cssText = value;
2283
- } else if (value === false || value === null || value === void 0) {
2284
- el.removeAttribute(key);
2285
- } else if (value === true) {
2286
- el.setAttribute(key, "");
2287
- } else {
2288
- el.setAttribute(key, String(value));
2289
- }
2290
- }
2291
- }
2292
- if (typeof children === "string" || typeof children === "number" || children === null || children === void 0 || children === false) {
2293
- el.textContent = children === null || children === void 0 || children === false ? "" : String(children);
2294
- return true;
2295
- }
2296
- if (children && typeof children === "object" && !Array.isArray(children) && !(children instanceof Node)) {
2297
- const childVNode = children;
2298
- if (typeof childVNode.type === "string") {
2299
- const childEl = el.querySelector(childVNode.type);
2300
- if (childEl && patchElement(childEl, children)) {
2301
- return true;
2302
- }
2303
- }
2304
- }
2305
- return false;
2306
- }
2307
- }
2308
- if (output instanceof Node) {
2309
- if (output.nodeType === Node.ELEMENT_NODE) {
2310
- const nextEl = output;
2311
- if (nextEl.tagName.toLowerCase() === el.tagName.toLowerCase()) {
2312
- el.textContent = nextEl.textContent;
2313
- return true;
2314
- }
2315
- } else if (output.nodeType === Node.TEXT_NODE) {
2316
- el.textContent = output.data;
2317
- return true;
2318
- }
2319
- }
2320
- return false;
2321
- }
2322
- function patchNode(currentNode, nextOutput) {
2323
- if (!currentNode) return false;
2324
- if (currentNode instanceof Text && (nextOutput === null || nextOutput === void 0 || nextOutput === false || typeof nextOutput === "string" || typeof nextOutput === "number" || nextOutput instanceof Text)) {
2325
- const nextText = nextOutput instanceof Text ? nextOutput.data : nextOutput === null || nextOutput === void 0 || nextOutput === false ? "" : String(nextOutput);
2326
- currentNode.data = nextText;
2327
- return true;
2328
- }
2329
- if (currentNode instanceof Element && patchElement(currentNode, nextOutput)) {
2330
- return true;
2331
- }
2332
- if (nextOutput instanceof Node && currentNode === nextOutput) {
2333
- return true;
2334
- }
2335
- return false;
2336
- }
2337
- function isFragmentVNode(value) {
2338
- return value != null && typeof value === "object" && !(value instanceof Node) && value.type === Fragment;
2339
- }
2340
- function normalizeChildren(children, result = []) {
2341
- if (children === void 0) {
2342
- return result;
2343
- }
2344
- if (Array.isArray(children)) {
2345
- for (const child of children) {
2346
- normalizeChildren(child, result);
2347
- }
2348
- return result;
2349
- }
2350
- if (children === null || children === false) {
2351
- return result;
2352
- }
2353
- result.push(children);
2354
- return result;
2355
- }
2356
- function patchFragmentChildren(nodes, children) {
2357
- const normalized = normalizeChildren(children);
2358
- if (normalized.length !== nodes.length) {
2359
- return false;
2360
- }
2361
- for (let i = 0; i < normalized.length; i++) {
2362
- if (!patchNode(nodes[i], normalized[i])) {
2363
- return false;
2184
+ function collectBlockNodes(block) {
2185
+ const nodes = [];
2186
+ let cursor = block.start;
2187
+ while (cursor) {
2188
+ nodes.push(cursor);
2189
+ if (cursor === block.end) {
2190
+ break;
2364
2191
  }
2192
+ cursor = cursor.nextSibling;
2365
2193
  }
2366
- return true;
2367
- }
2368
- function clearContent(block) {
2369
- const nodes = block.nodes.slice(1, Math.max(1, block.nodes.length - 1));
2370
- removeNodes(nodes);
2194
+ return nodes;
2371
2195
  }
2372
- function removeBlockNodes(block) {
2196
+ function removeBlockRange(block) {
2373
2197
  let cursor = block.start;
2374
- const end = block.end;
2375
2198
  while (cursor) {
2376
2199
  const next = cursor.nextSibling;
2377
2200
  cursor.parentNode?.removeChild(cursor);
2378
- if (cursor === end) break;
2201
+ if (cursor === block.end) {
2202
+ break;
2203
+ }
2379
2204
  cursor = next;
2380
2205
  }
2381
2206
  }
2382
- function bumpBlockVersion(block) {
2383
- block.version(block.version() + 1);
2207
+ function createVersionedSignalAccessor(initialValue) {
2208
+ let current = initialValue;
2209
+ let version = 0;
2210
+ const track = signal(version);
2211
+ function accessor(value) {
2212
+ if (arguments.length === 0) {
2213
+ track();
2214
+ return current;
2215
+ }
2216
+ current = value;
2217
+ version++;
2218
+ track(version);
2219
+ }
2220
+ return accessor;
2384
2221
  }
2385
-
2386
- // src/scope.ts
2387
- function createScope() {
2388
- let dispose = null;
2389
- const stop = () => {
2390
- if (dispose) {
2391
- dispose();
2392
- dispose = null;
2222
+ function createKeyedListContainer() {
2223
+ const startMarker = document.createComment("fict:list:start");
2224
+ const endMarker = document.createComment("fict:list:end");
2225
+ const dispose = () => {
2226
+ for (const block of container.blocks.values()) {
2227
+ destroyRoot(block.root);
2228
+ }
2229
+ container.blocks.clear();
2230
+ container.nextBlocks.clear();
2231
+ if (!startMarker.parentNode || !endMarker.parentNode) {
2232
+ container.currentNodes = [];
2233
+ container.nextNodes = [];
2234
+ container.orderedBlocks.length = 0;
2235
+ container.nextOrderedBlocks.length = 0;
2236
+ container.orderedIndexByKey.clear();
2237
+ return;
2393
2238
  }
2239
+ const range = document.createRange();
2240
+ range.setStartBefore(startMarker);
2241
+ range.setEndAfter(endMarker);
2242
+ range.deleteContents();
2243
+ container.currentNodes = [];
2244
+ container.nextNodes = [];
2245
+ container.nextBlocks.clear();
2246
+ container.orderedBlocks.length = 0;
2247
+ container.nextOrderedBlocks.length = 0;
2248
+ container.orderedIndexByKey.clear();
2394
2249
  };
2395
- const run = (fn) => {
2396
- stop();
2397
- const { dispose: rootDispose, value } = createRoot(fn);
2398
- dispose = rootDispose;
2399
- return value;
2250
+ const container = {
2251
+ startMarker,
2252
+ endMarker,
2253
+ blocks: /* @__PURE__ */ new Map(),
2254
+ nextBlocks: /* @__PURE__ */ new Map(),
2255
+ currentNodes: [startMarker, endMarker],
2256
+ nextNodes: [],
2257
+ orderedBlocks: [],
2258
+ nextOrderedBlocks: [],
2259
+ orderedIndexByKey: /* @__PURE__ */ new Map(),
2260
+ dispose
2400
2261
  };
2401
- registerRootCleanup(stop);
2402
- return { run, stop };
2262
+ return container;
2403
2263
  }
2404
- function runInScope(flag, fn) {
2405
- const scope = createScope();
2406
- const evaluate = () => isReactive(flag) ? flag() : !!flag;
2407
- createEffect(() => {
2408
- const enabled = evaluate();
2409
- if (enabled) {
2410
- scope.run(fn);
2264
+ function createKeyedBlock(key, item, index, render2, needsIndex = true, hostRoot) {
2265
+ const itemSig = createVersionedSignalAccessor(item);
2266
+ const indexSig = needsIndex ? signal(index) : ((next) => {
2267
+ if (arguments.length === 0) return index;
2268
+ index = next;
2269
+ return index;
2270
+ });
2271
+ const root = createRootContext(hostRoot);
2272
+ const prevRoot = pushRoot(root);
2273
+ const prevSub = setActiveSub(void 0);
2274
+ let nodes = [];
2275
+ try {
2276
+ const rendered = render2(itemSig, indexSig, key);
2277
+ if (rendered instanceof Node || Array.isArray(rendered) && rendered.every((n) => n instanceof Node)) {
2278
+ nodes = toNodeArray(rendered);
2411
2279
  } else {
2412
- scope.stop();
2280
+ const element = createElement(rendered);
2281
+ nodes = toNodeArray(element);
2413
2282
  }
2414
- });
2415
- onCleanup(scope.stop);
2416
- }
2417
-
2418
- // src/scheduler.ts
2419
- function batch2(fn) {
2420
- return batch(fn);
2421
- }
2422
- function untrack2(fn) {
2423
- return untrack(fn);
2424
- }
2425
-
2426
- // src/hooks.ts
2427
- var ctxStack = [];
2428
- function __fictUseContext() {
2429
- if (ctxStack.length === 0) {
2430
- const ctx2 = { slots: [], cursor: 0 };
2431
- ctxStack.push(ctx2);
2432
- return ctx2;
2433
- }
2434
- const ctx = ctxStack[ctxStack.length - 1];
2435
- ctx.cursor = 0;
2436
- return ctx;
2437
- }
2438
- function __fictPushContext() {
2439
- const ctx = { slots: [], cursor: 0 };
2440
- ctxStack.push(ctx);
2441
- return ctx;
2442
- }
2443
- function __fictPopContext() {
2444
- ctxStack.pop();
2445
- }
2446
- function __fictResetContext() {
2447
- ctxStack.length = 0;
2448
- }
2449
- function __fictUseSignal(ctx, initial, slot) {
2450
- const index = slot ?? ctx.cursor++;
2451
- if (!ctx.slots[index]) {
2452
- ctx.slots[index] = signal(initial);
2453
- }
2454
- return ctx.slots[index];
2455
- }
2456
- function __fictUseMemo(ctx, fn, slot) {
2457
- const index = slot ?? ctx.cursor++;
2458
- if (!ctx.slots[index]) {
2459
- ctx.slots[index] = createMemo(fn);
2460
- }
2461
- return ctx.slots[index];
2462
- }
2463
- function __fictUseEffect(ctx, fn, slot) {
2464
- const index = slot ?? ctx.cursor++;
2465
- if (!ctx.slots[index]) {
2466
- ctx.slots[index] = createEffect(fn);
2467
- }
2468
- }
2469
- function __fictRender(ctx, fn) {
2470
- ctxStack.push(ctx);
2471
- ctx.cursor = 0;
2472
- try {
2473
- return fn();
2474
2283
  } finally {
2475
- ctxStack.pop();
2476
- }
2477
- }
2478
-
2479
- // src/props.ts
2480
- var propGetters = /* @__PURE__ */ new WeakSet();
2481
- var rawToProxy = /* @__PURE__ */ new WeakMap();
2482
- var proxyToRaw = /* @__PURE__ */ new WeakMap();
2483
- function __fictProp(getter) {
2484
- if (typeof getter === "function" && getter.length === 0) {
2485
- propGetters.add(getter);
2486
- }
2487
- return getter;
2488
- }
2489
- function isPropGetter(value) {
2490
- return typeof value === "function" && propGetters.has(value);
2491
- }
2492
- function createPropsProxy(props) {
2493
- if (!props || typeof props !== "object") {
2494
- return props;
2495
- }
2496
- if (proxyToRaw.has(props)) {
2497
- return props;
2498
- }
2499
- const cached = rawToProxy.get(props);
2500
- if (cached) {
2501
- return cached;
2284
+ setActiveSub(prevSub);
2285
+ popRoot(prevRoot);
2502
2286
  }
2503
- const proxy = new Proxy(props, {
2504
- get(target, prop, receiver) {
2505
- const value = Reflect.get(target, prop, receiver);
2506
- if (isPropGetter(value)) {
2507
- return value();
2508
- }
2509
- return value;
2510
- },
2511
- set(target, prop, value, receiver) {
2512
- return Reflect.set(target, prop, value, receiver);
2513
- },
2514
- has(target, prop) {
2515
- return prop in target;
2516
- },
2517
- ownKeys(target) {
2518
- return Reflect.ownKeys(target);
2519
- },
2520
- getOwnPropertyDescriptor(target, prop) {
2521
- return Object.getOwnPropertyDescriptor(target, prop);
2522
- }
2523
- });
2524
- rawToProxy.set(props, proxy);
2525
- proxyToRaw.set(proxy, props);
2526
- return proxy;
2287
+ return {
2288
+ key,
2289
+ nodes,
2290
+ root,
2291
+ item: itemSig,
2292
+ index: indexSig,
2293
+ rawItem: item,
2294
+ rawIndex: index
2295
+ };
2527
2296
  }
2528
- function unwrapProps(props) {
2529
- if (!props || typeof props !== "object") {
2530
- return props;
2531
- }
2532
- return proxyToRaw.get(props) ?? props;
2297
+ function getFirstNodeAfter(marker) {
2298
+ return marker.nextSibling;
2533
2299
  }
2534
- function __fictPropsRest(props, exclude) {
2535
- const raw = unwrapProps(props);
2536
- const out = {};
2537
- const excludeSet = new Set(exclude);
2538
- for (const key of Reflect.ownKeys(raw)) {
2539
- if (excludeSet.has(key)) continue;
2540
- out[key] = raw[key];
2541
- }
2542
- return createPropsProxy(out);
2300
+ function createKeyedList(getItems, keyFn, renderItem, needsIndex) {
2301
+ const resolvedNeedsIndex = arguments.length >= 4 ? !!needsIndex : renderItem.length > 1;
2302
+ return createFineGrainedKeyedList(getItems, keyFn, renderItem, resolvedNeedsIndex);
2543
2303
  }
2544
- function mergeProps(...sources) {
2545
- const validSources = sources.filter(
2546
- (s) => s != null && (typeof s === "object" || typeof s === "function")
2547
- );
2548
- if (validSources.length === 0) {
2549
- return {};
2550
- }
2551
- if (validSources.length === 1 && typeof validSources[0] === "object") {
2552
- return validSources[0];
2553
- }
2554
- const resolveSource = (src) => {
2555
- const value = typeof src === "function" ? src() : src;
2556
- if (!value || typeof value !== "object") return void 0;
2557
- return unwrapProps(value);
2304
+ function createFineGrainedKeyedList(getItems, keyFn, renderItem, needsIndex) {
2305
+ const container = createKeyedListContainer();
2306
+ const hostRoot = getCurrentRoot();
2307
+ const fragment = document.createDocumentFragment();
2308
+ fragment.append(container.startMarker, container.endMarker);
2309
+ let disposed = false;
2310
+ let effectDispose;
2311
+ let connectObserver = null;
2312
+ let effectStarted = false;
2313
+ let startScheduled = false;
2314
+ const getConnectedParent = () => {
2315
+ const endParent = container.endMarker.parentNode;
2316
+ const startParent = container.startMarker.parentNode;
2317
+ if (endParent && startParent && endParent === startParent && endParent.nodeType !== 11) {
2318
+ return endParent;
2319
+ }
2320
+ return null;
2558
2321
  };
2559
- return new Proxy({}, {
2560
- get(_, prop) {
2561
- if (typeof prop === "symbol") {
2562
- return void 0;
2563
- }
2564
- for (let i = validSources.length - 1; i >= 0; i--) {
2565
- const src = validSources[i];
2566
- const raw = resolveSource(src);
2567
- if (!raw || !(prop in raw)) continue;
2568
- const value = raw[prop];
2569
- if (typeof src === "function" && !isPropGetter(value)) {
2570
- return __fictProp(() => {
2571
- const latest = resolveSource(src);
2572
- if (!latest || !(prop in latest)) return void 0;
2573
- return latest[prop];
2574
- });
2575
- }
2576
- return value;
2577
- }
2578
- return void 0;
2579
- },
2580
- has(_, prop) {
2581
- for (const src of validSources) {
2582
- const raw = resolveSource(src);
2583
- if (raw && prop in raw) {
2584
- return true;
2322
+ const performDiff = () => {
2323
+ if (disposed) return;
2324
+ const parent = getConnectedParent();
2325
+ if (!parent) return;
2326
+ batch2(() => {
2327
+ const oldBlocks = container.blocks;
2328
+ const newBlocks = container.nextBlocks;
2329
+ const prevOrderedBlocks = container.orderedBlocks;
2330
+ const nextOrderedBlocks = container.nextOrderedBlocks;
2331
+ const orderedIndexByKey = container.orderedIndexByKey;
2332
+ newBlocks.clear();
2333
+ nextOrderedBlocks.length = 0;
2334
+ orderedIndexByKey.clear();
2335
+ const createdBlocks = [];
2336
+ const newItems = getItems();
2337
+ if (newItems.length === 0) {
2338
+ if (oldBlocks.size > 0) {
2339
+ for (const block of oldBlocks.values()) {
2340
+ destroyRoot(block.root);
2341
+ }
2342
+ const range = document.createRange();
2343
+ range.setStartAfter(container.startMarker);
2344
+ range.setEndBefore(container.endMarker);
2345
+ range.deleteContents();
2585
2346
  }
2347
+ oldBlocks.clear();
2348
+ newBlocks.clear();
2349
+ prevOrderedBlocks.length = 0;
2350
+ nextOrderedBlocks.length = 0;
2351
+ orderedIndexByKey.clear();
2352
+ container.currentNodes.length = 0;
2353
+ container.currentNodes.push(container.startMarker, container.endMarker);
2354
+ container.nextNodes.length = 0;
2355
+ return;
2586
2356
  }
2587
- return false;
2588
- },
2589
- ownKeys() {
2590
- const keys = /* @__PURE__ */ new Set();
2591
- for (const src of validSources) {
2592
- const raw = resolveSource(src);
2593
- if (raw) {
2594
- for (const key of Reflect.ownKeys(raw)) {
2595
- keys.add(key);
2357
+ const prevCount = prevOrderedBlocks.length;
2358
+ let appendCandidate = prevCount > 0 && newItems.length >= prevCount;
2359
+ const appendedBlocks = [];
2360
+ newItems.forEach((item, index) => {
2361
+ const key = keyFn(item, index);
2362
+ let block = oldBlocks.get(key);
2363
+ const existed = block !== void 0;
2364
+ if (block) {
2365
+ if (block.rawItem !== item) {
2366
+ block.rawItem = item;
2367
+ block.item(item);
2368
+ }
2369
+ if (needsIndex && block.rawIndex !== index) {
2370
+ block.rawIndex = index;
2371
+ block.index(index);
2596
2372
  }
2597
2373
  }
2598
- }
2599
- return Array.from(keys);
2600
- },
2601
- getOwnPropertyDescriptor(_, prop) {
2602
- for (let i = validSources.length - 1; i >= 0; i--) {
2603
- const raw = resolveSource(validSources[i]);
2604
- if (raw && prop in raw) {
2605
- return {
2606
- enumerable: true,
2607
- configurable: true,
2608
- get: () => {
2609
- const value = raw[prop];
2610
- return value;
2611
- }
2612
- };
2374
+ if (block) {
2375
+ newBlocks.set(key, block);
2376
+ oldBlocks.delete(key);
2377
+ } else {
2378
+ const existingBlock = newBlocks.get(key);
2379
+ if (existingBlock) {
2380
+ destroyRoot(existingBlock.root);
2381
+ removeNodes(existingBlock.nodes);
2382
+ }
2383
+ block = createKeyedBlock(key, item, index, renderItem, needsIndex, hostRoot);
2384
+ createdBlocks.push(block);
2613
2385
  }
2614
- }
2615
- return void 0;
2616
- }
2617
- });
2618
- }
2619
- function useProp(getter) {
2620
- return __fictProp(createMemo(getter));
2621
- }
2622
-
2623
- // src/dom.ts
2624
- function render(view, container) {
2625
- const root = createRootContext();
2626
- const prev = pushRoot(root);
2627
- let dom;
2628
- try {
2629
- const output = view();
2630
- dom = createElement(output);
2631
- } finally {
2632
- popRoot(prev);
2633
- }
2634
- container.replaceChildren(dom);
2635
- container.setAttribute("data-fict-fine-grained", "1");
2636
- flushOnMount(root);
2637
- const teardown = () => {
2638
- destroyRoot(root);
2639
- container.innerHTML = "";
2386
+ const resolvedBlock = block;
2387
+ newBlocks.set(key, resolvedBlock);
2388
+ const position = orderedIndexByKey.get(key);
2389
+ if (position !== void 0) {
2390
+ appendCandidate = false;
2391
+ const prior = nextOrderedBlocks[position];
2392
+ if (prior && prior !== resolvedBlock) {
2393
+ destroyRoot(prior.root);
2394
+ removeNodes(prior.nodes);
2395
+ }
2396
+ nextOrderedBlocks[position] = resolvedBlock;
2397
+ } else {
2398
+ if (appendCandidate) {
2399
+ if (index < prevCount) {
2400
+ if (!prevOrderedBlocks[index] || prevOrderedBlocks[index].key !== key) {
2401
+ appendCandidate = false;
2402
+ }
2403
+ } else if (existed) {
2404
+ appendCandidate = false;
2405
+ }
2406
+ }
2407
+ orderedIndexByKey.set(key, nextOrderedBlocks.length);
2408
+ nextOrderedBlocks.push(resolvedBlock);
2409
+ }
2410
+ if (appendCandidate && index >= prevCount) {
2411
+ appendedBlocks.push(resolvedBlock);
2412
+ }
2413
+ });
2414
+ const canAppend = appendCandidate && prevCount > 0 && newItems.length > prevCount && oldBlocks.size === 0 && appendedBlocks.length > 0;
2415
+ if (canAppend) {
2416
+ const appendedNodes = [];
2417
+ for (const block of appendedBlocks) {
2418
+ for (let i = 0; i < block.nodes.length; i++) {
2419
+ appendedNodes.push(block.nodes[i]);
2420
+ }
2421
+ }
2422
+ if (appendedNodes.length > 0) {
2423
+ insertNodesBefore(parent, appendedNodes, container.endMarker);
2424
+ const currentNodes = container.currentNodes;
2425
+ currentNodes.pop();
2426
+ for (let i = 0; i < appendedNodes.length; i++) {
2427
+ currentNodes.push(appendedNodes[i]);
2428
+ }
2429
+ currentNodes.push(container.endMarker);
2430
+ }
2431
+ container.blocks = newBlocks;
2432
+ container.nextBlocks = oldBlocks;
2433
+ container.orderedBlocks = nextOrderedBlocks;
2434
+ container.nextOrderedBlocks = prevOrderedBlocks;
2435
+ for (const block of createdBlocks) {
2436
+ if (newBlocks.get(block.key) === block) {
2437
+ flushOnMount(block.root);
2438
+ }
2439
+ }
2440
+ return;
2441
+ }
2442
+ if (oldBlocks.size > 0) {
2443
+ for (const block of oldBlocks.values()) {
2444
+ destroyRoot(block.root);
2445
+ removeNodes(block.nodes);
2446
+ }
2447
+ oldBlocks.clear();
2448
+ }
2449
+ if (newBlocks.size > 0 || container.currentNodes.length > 0) {
2450
+ const prevNodes = container.currentNodes;
2451
+ const nextNodes = container.nextNodes;
2452
+ nextNodes.length = 0;
2453
+ nextNodes.push(container.startMarker);
2454
+ for (let i = 0; i < nextOrderedBlocks.length; i++) {
2455
+ const nodes = nextOrderedBlocks[i].nodes;
2456
+ for (let j = 0; j < nodes.length; j++) {
2457
+ nextNodes.push(nodes[j]);
2458
+ }
2459
+ }
2460
+ nextNodes.push(container.endMarker);
2461
+ reconcileArrays(parent, prevNodes, nextNodes);
2462
+ container.currentNodes = nextNodes;
2463
+ container.nextNodes = prevNodes;
2464
+ }
2465
+ container.blocks = newBlocks;
2466
+ container.nextBlocks = oldBlocks;
2467
+ container.orderedBlocks = nextOrderedBlocks;
2468
+ container.nextOrderedBlocks = prevOrderedBlocks;
2469
+ for (const block of createdBlocks) {
2470
+ if (newBlocks.get(block.key) === block) {
2471
+ flushOnMount(block.root);
2472
+ }
2473
+ }
2474
+ });
2640
2475
  };
2641
- return teardown;
2642
- }
2643
- function createElement(node) {
2644
- if (node instanceof Node) {
2645
- return node;
2646
- }
2647
- if (node === null || node === void 0 || node === false) {
2648
- return document.createTextNode("");
2649
- }
2650
- if (typeof node === "object" && node !== null && !(node instanceof Node)) {
2651
- if ("marker" in node) {
2652
- return createElement(node.marker);
2653
- }
2654
- const nodeRecord = node;
2655
- if (nodeRecord[PRIMITIVE_PROXY]) {
2656
- const primitiveGetter = nodeRecord[Symbol.toPrimitive];
2657
- const value = typeof primitiveGetter === "function" ? primitiveGetter.call(node, "default") : node;
2658
- return document.createTextNode(value == null || value === false ? "" : String(value));
2659
- }
2660
- }
2661
- if (Array.isArray(node)) {
2662
- const frag = document.createDocumentFragment();
2663
- for (const child of node) {
2664
- appendChildNode(frag, child);
2476
+ const disconnectObserver = () => {
2477
+ connectObserver?.disconnect();
2478
+ connectObserver = null;
2479
+ };
2480
+ const ensureEffectStarted = () => {
2481
+ if (disposed || effectStarted) return effectStarted;
2482
+ const parent = getConnectedParent();
2483
+ if (!parent) return false;
2484
+ const start = () => {
2485
+ effectDispose = createRenderEffect(performDiff);
2486
+ effectStarted = true;
2487
+ };
2488
+ if (hostRoot) {
2489
+ const prev = pushRoot(hostRoot);
2490
+ try {
2491
+ start();
2492
+ } finally {
2493
+ popRoot(prev);
2494
+ }
2495
+ } else {
2496
+ start();
2665
2497
  }
2666
- return frag;
2667
- }
2668
- if (typeof node === "string" || typeof node === "number") {
2669
- return document.createTextNode(String(node));
2670
- }
2671
- if (typeof node === "boolean") {
2672
- return document.createTextNode("");
2673
- }
2674
- const vnode = node;
2675
- if (typeof vnode.type === "function") {
2676
- const rawProps = unwrapProps(vnode.props ?? {});
2677
- const baseProps = vnode.key === void 0 ? rawProps : new Proxy(rawProps, {
2678
- get(target, prop, receiver) {
2679
- if (prop === "key") return vnode.key;
2680
- return Reflect.get(target, prop, receiver);
2681
- },
2682
- has(target, prop) {
2683
- if (prop === "key") return true;
2684
- return prop in target;
2685
- },
2686
- ownKeys(target) {
2687
- const keys = new Set(Reflect.ownKeys(target));
2688
- keys.add("key");
2689
- return Array.from(keys);
2690
- },
2691
- getOwnPropertyDescriptor(target, prop) {
2692
- if (prop === "key") {
2693
- return { enumerable: true, configurable: true, value: vnode.key };
2498
+ return true;
2499
+ };
2500
+ const waitForConnection = () => {
2501
+ if (connectObserver || typeof MutationObserver === "undefined") return;
2502
+ connectObserver = new MutationObserver(() => {
2503
+ if (disposed) return;
2504
+ if (getConnectedParent()) {
2505
+ disconnectObserver();
2506
+ if (ensureEffectStarted()) {
2507
+ flush();
2694
2508
  }
2695
- return Object.getOwnPropertyDescriptor(target, prop);
2696
2509
  }
2697
2510
  });
2698
- const props = createPropsProxy(baseProps);
2699
- try {
2700
- __fictPushContext();
2701
- const rendered = vnode.type(props);
2702
- __fictPopContext();
2703
- return createElement(rendered);
2704
- } catch (err) {
2705
- __fictPopContext();
2706
- if (handleSuspend(err)) {
2707
- return document.createComment("fict:suspend");
2511
+ connectObserver.observe(document, { childList: true, subtree: true });
2512
+ };
2513
+ const scheduleStart = () => {
2514
+ if (startScheduled || disposed || effectStarted) return;
2515
+ startScheduled = true;
2516
+ const run = () => {
2517
+ startScheduled = false;
2518
+ if (!ensureEffectStarted()) {
2519
+ waitForConnection();
2708
2520
  }
2709
- handleError(err, { source: "render", componentName: vnode.type.name });
2710
- throw err;
2521
+ };
2522
+ if (typeof queueMicrotask === "function") {
2523
+ queueMicrotask(run);
2524
+ } else {
2525
+ Promise.resolve().then(run).catch(() => void 0);
2711
2526
  }
2712
- }
2713
- if (vnode.type === Fragment) {
2714
- const frag = document.createDocumentFragment();
2715
- const children = vnode.props?.children;
2716
- appendChildren(frag, children);
2717
- return frag;
2718
- }
2719
- const tagName = typeof vnode.type === "string" ? vnode.type : "div";
2720
- const el = document.createElement(tagName);
2721
- applyProps(el, vnode.props ?? {});
2722
- return el;
2723
- }
2724
- function template(html, isImportNode, isSVG, isMathML) {
2725
- let node = null;
2726
- const create = () => {
2727
- const t = isMathML ? document.createElementNS("http://www.w3.org/1998/Math/MathML", "template") : document.createElement("template");
2728
- t.innerHTML = html;
2729
- if (isSVG) {
2730
- return t.content.firstChild.firstChild;
2527
+ };
2528
+ scheduleStart();
2529
+ return {
2530
+ get marker() {
2531
+ scheduleStart();
2532
+ return fragment;
2533
+ },
2534
+ startMarker: container.startMarker,
2535
+ endMarker: container.endMarker,
2536
+ // Flush pending items - call after markers are inserted into DOM
2537
+ flush: () => {
2538
+ if (disposed) return;
2539
+ scheduleStart();
2540
+ if (ensureEffectStarted()) {
2541
+ flush();
2542
+ } else {
2543
+ waitForConnection();
2544
+ }
2545
+ },
2546
+ dispose: () => {
2547
+ disposed = true;
2548
+ effectDispose?.();
2549
+ disconnectObserver();
2550
+ container.dispose();
2731
2551
  }
2732
- if (isMathML) {
2733
- return t.firstChild;
2552
+ };
2553
+ }
2554
+
2555
+ // src/binding.ts
2556
+ function isReactive(value) {
2557
+ return typeof value === "function" && value.length === 0;
2558
+ }
2559
+ function callEventHandler(handler, event, node, data) {
2560
+ if (!handler) return;
2561
+ const context = node ?? event.currentTarget ?? void 0;
2562
+ const invoke = (fn) => {
2563
+ if (typeof fn === "function") {
2564
+ const result = data === void 0 ? fn.call(context, event) : fn.call(context, data, event);
2565
+ if (typeof result === "function" && result !== fn) {
2566
+ if (data === void 0) {
2567
+ result.call(context, event);
2568
+ } else {
2569
+ result.call(context, data, event);
2570
+ }
2571
+ } else if (result && typeof result.handleEvent === "function") {
2572
+ result.handleEvent.call(result, event);
2573
+ }
2574
+ } else if (fn && typeof fn.handleEvent === "function") {
2575
+ fn.handleEvent.call(fn, event);
2734
2576
  }
2735
- return t.content.firstChild;
2736
2577
  };
2737
- const fn = isImportNode ? () => untrack2(() => document.importNode(node || (node = create()), true)) : () => (node || (node = create())).cloneNode(true);
2738
- fn.cloneNode = fn;
2739
- return fn;
2578
+ invoke(handler);
2740
2579
  }
2741
- function isBindingHandle(node) {
2742
- return node !== null && typeof node === "object" && "marker" in node && "dispose" in node && typeof node.dispose === "function";
2580
+ var PRIMITIVE_PROXY = Symbol("fict:primitive-proxy");
2581
+ function bindText(textNode, getValue) {
2582
+ return createRenderEffect(() => {
2583
+ const value = formatTextValue(getValue());
2584
+ if (textNode.data !== value) {
2585
+ textNode.data = value;
2586
+ }
2587
+ });
2743
2588
  }
2744
- function appendChildNode(parent, child) {
2745
- if (child === null || child === void 0 || child === false) {
2746
- return;
2589
+ function formatTextValue(value) {
2590
+ if (value == null || value === false) {
2591
+ return "";
2747
2592
  }
2748
- if (isBindingHandle(child)) {
2749
- appendChildNode(parent, child.marker);
2750
- child.flush?.();
2751
- return;
2593
+ return String(value);
2594
+ }
2595
+ function createAttributeBinding(el, key, value, setter) {
2596
+ if (isReactive(value)) {
2597
+ createRenderEffect(() => {
2598
+ setter(el, key, value());
2599
+ });
2600
+ } else {
2601
+ setter(el, key, value);
2752
2602
  }
2753
- if (typeof child === "function" && child.length === 0) {
2754
- const childGetter = child;
2755
- createChildBinding(parent, childGetter, createElement);
2756
- return;
2757
- }
2758
- if (Array.isArray(child)) {
2759
- for (const item of child) {
2760
- appendChildNode(parent, item);
2761
- }
2762
- return;
2763
- }
2764
- let domNode;
2765
- if (typeof child !== "object" || child === null) {
2766
- domNode = document.createTextNode(String(child ?? ""));
2767
- } else {
2768
- domNode = createElement(child);
2769
- }
2770
- if (domNode.nodeType === 11) {
2771
- const children = Array.from(domNode.childNodes);
2772
- for (const node of children) {
2773
- appendChildNode(parent, node);
2603
+ }
2604
+ function bindAttribute(el, key, getValue) {
2605
+ let prevValue = void 0;
2606
+ return createRenderEffect(() => {
2607
+ const value = getValue();
2608
+ if (value === prevValue) return;
2609
+ prevValue = value;
2610
+ if (value === void 0 || value === null || value === false) {
2611
+ el.removeAttribute(key);
2612
+ } else if (value === true) {
2613
+ el.setAttribute(key, "");
2614
+ } else {
2615
+ el.setAttribute(key, String(value));
2774
2616
  }
2775
- return;
2776
- }
2777
- if (domNode.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
2778
- parent.ownerDocument.adoptNode(domNode);
2779
- }
2780
- try {
2781
- parent.appendChild(domNode);
2782
- } catch (e) {
2783
- if (parent.ownerDocument) {
2784
- const clone = parent.ownerDocument.importNode(domNode, true);
2785
- parent.appendChild(clone);
2617
+ });
2618
+ }
2619
+ function bindProperty(el, key, getValue) {
2620
+ const PROPERTY_BINDING_KEYS = /* @__PURE__ */ new Set([
2621
+ "value",
2622
+ "checked",
2623
+ "selected",
2624
+ "disabled",
2625
+ "readOnly",
2626
+ "multiple",
2627
+ "muted"
2628
+ ]);
2629
+ let prevValue = void 0;
2630
+ return createRenderEffect(() => {
2631
+ const next = getValue();
2632
+ if (next === prevValue) return;
2633
+ prevValue = next;
2634
+ if (PROPERTY_BINDING_KEYS.has(key) && (next === void 0 || next === null)) {
2635
+ const fallback = key === "checked" || key === "selected" ? false : "";
2636
+ el[key] = fallback;
2786
2637
  return;
2787
2638
  }
2788
- throw e;
2789
- }
2639
+ ;
2640
+ el[key] = next;
2641
+ });
2790
2642
  }
2791
- function appendChildren(parent, children) {
2792
- if (children === void 0) return;
2793
- if (Array.isArray(children)) {
2794
- for (const child of children) {
2795
- appendChildren(parent, child);
2796
- }
2797
- return;
2643
+ function createStyleBinding(el, value) {
2644
+ const target = el;
2645
+ if (isReactive(value)) {
2646
+ let prev;
2647
+ createRenderEffect(() => {
2648
+ const next = value();
2649
+ applyStyle(target, next, prev);
2650
+ prev = next;
2651
+ });
2652
+ } else {
2653
+ applyStyle(target, value, void 0);
2798
2654
  }
2799
- appendChildNode(parent, children);
2800
2655
  }
2801
- function applyRef(el, value) {
2802
- if (typeof value === "function") {
2803
- const refFn = value;
2804
- refFn(el);
2805
- if (getCurrentRoot()) {
2806
- registerRootCleanup(() => {
2807
- refFn(null);
2808
- });
2809
- }
2810
- } else if (value && typeof value === "object" && "current" in value) {
2811
- const refObj = value;
2812
- refObj.current = el;
2813
- if (getCurrentRoot()) {
2814
- registerRootCleanup(() => {
2815
- refObj.current = null;
2816
- });
2817
- }
2818
- }
2656
+ function bindStyle(el, getValue) {
2657
+ const target = el;
2658
+ let prev;
2659
+ return createRenderEffect(() => {
2660
+ const next = getValue();
2661
+ applyStyle(target, next, prev);
2662
+ prev = next;
2663
+ });
2819
2664
  }
2820
- function applyProps(el, props, isSVG = false) {
2821
- props = unwrapProps(props);
2822
- const tagName = el.tagName;
2823
- const isCE = tagName.includes("-") || "is" in props;
2824
- for (const [key, value] of Object.entries(props)) {
2825
- if (key === "children") continue;
2826
- if (key === "ref") {
2827
- applyRef(el, value);
2828
- continue;
2829
- }
2830
- if (isEventKey(key)) {
2831
- bindEvent(
2832
- el,
2833
- eventNameFromProp(key),
2834
- value
2835
- );
2836
- continue;
2837
- }
2838
- if (key.slice(0, 3) === "on:") {
2839
- bindEvent(
2840
- el,
2841
- key.slice(3),
2842
- value,
2843
- false
2844
- // Non-delegated
2845
- );
2846
- continue;
2847
- }
2848
- if (key.slice(0, 10) === "oncapture:") {
2849
- bindEvent(
2850
- el,
2851
- key.slice(10),
2852
- value,
2853
- true
2854
- // Capture
2855
- );
2856
- continue;
2857
- }
2858
- if (key === "class" || key === "className") {
2859
- createClassBinding(el, value);
2860
- continue;
2861
- }
2862
- if (key === "classList") {
2863
- createClassBinding(el, value);
2864
- continue;
2865
- }
2866
- if (key === "style") {
2867
- createStyleBinding(
2868
- el,
2869
- value
2870
- );
2871
- continue;
2665
+ function applyStyle(el, value, prev) {
2666
+ if (typeof value === "string") {
2667
+ el.style.cssText = value;
2668
+ } else if (value && typeof value === "object") {
2669
+ const styles = value;
2670
+ if (typeof prev === "string") {
2671
+ el.style.cssText = "";
2872
2672
  }
2873
- if (key === "dangerouslySetInnerHTML" && value && typeof value === "object") {
2874
- const htmlValue = value.__html;
2875
- if (htmlValue !== void 0) {
2876
- if (isReactive(htmlValue)) {
2877
- createAttributeBinding(el, "innerHTML", htmlValue, setInnerHTML);
2878
- } else {
2879
- el.innerHTML = htmlValue;
2673
+ if (prev && typeof prev === "object") {
2674
+ const prevStyles = prev;
2675
+ for (const key of Object.keys(prevStyles)) {
2676
+ if (!(key in styles)) {
2677
+ const cssProperty = key.replace(/([A-Z])/g, "-$1").toLowerCase();
2678
+ el.style.removeProperty(cssProperty);
2880
2679
  }
2881
2680
  }
2882
- continue;
2883
- }
2884
- if (ChildProperties.has(key)) {
2885
- createAttributeBinding(el, key, value, setProperty);
2886
- continue;
2887
- }
2888
- if (key.slice(0, 5) === "attr:") {
2889
- createAttributeBinding(el, key.slice(5), value, setAttribute);
2890
- continue;
2891
- }
2892
- if (key.slice(0, 5) === "bool:") {
2893
- createAttributeBinding(el, key.slice(5), value, setBoolAttribute);
2894
- continue;
2895
- }
2896
- if (key.slice(0, 5) === "prop:") {
2897
- createAttributeBinding(el, key.slice(5), value, setProperty);
2898
- continue;
2899
2681
  }
2900
- const propAlias = !isSVG ? getPropAlias(key, tagName) : void 0;
2901
- if (propAlias || !isSVG && Properties.has(key) || isCE && !isSVG) {
2902
- const propName = propAlias || key;
2903
- if (isCE && !Properties.has(key)) {
2904
- createAttributeBinding(
2905
- el,
2906
- toPropertyName(propName),
2907
- value,
2908
- setProperty
2909
- );
2682
+ for (const [prop, v] of Object.entries(styles)) {
2683
+ if (v != null) {
2684
+ const cssProperty = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
2685
+ const unitless = isUnitlessStyleProperty(prop) || isUnitlessStyleProperty(cssProperty);
2686
+ const valueStr = typeof v === "number" && !unitless ? `${v}px` : String(v);
2687
+ el.style.setProperty(cssProperty, valueStr);
2910
2688
  } else {
2911
- createAttributeBinding(el, propName, value, setProperty);
2689
+ const cssProperty = prop.replace(/([A-Z])/g, "-$1").toLowerCase();
2690
+ el.style.removeProperty(cssProperty);
2912
2691
  }
2913
- continue;
2914
2692
  }
2915
- if (isSVG && key.indexOf(":") > -1) {
2916
- const [prefix, name] = key.split(":");
2917
- const ns = SVGNamespace[prefix];
2918
- if (ns) {
2919
- createAttributeBinding(
2920
- el,
2921
- key,
2922
- value,
2923
- (el2, _key, val) => setAttributeNS(el2, ns, name, val)
2924
- );
2925
- continue;
2693
+ } else {
2694
+ if (prev && typeof prev === "object") {
2695
+ const prevStyles = prev;
2696
+ for (const key of Object.keys(prevStyles)) {
2697
+ const cssProperty = key.replace(/([A-Z])/g, "-$1").toLowerCase();
2698
+ el.style.removeProperty(cssProperty);
2926
2699
  }
2700
+ } else if (typeof prev === "string") {
2701
+ el.style.cssText = "";
2927
2702
  }
2928
- const attrName = Aliases[key] || key;
2929
- createAttributeBinding(el, attrName, value, setAttribute);
2930
2703
  }
2931
- const children = props.children;
2932
- appendChildren(el, children);
2933
2704
  }
2934
- function toPropertyName(name) {
2935
- return name.toLowerCase().replace(/-([a-z])/g, (_, w) => w.toUpperCase());
2705
+ function isUnitlessStyleProperty(prop) {
2706
+ return UnitlessStyles.has(prop);
2936
2707
  }
2937
- var setAttribute = (el, key, value) => {
2938
- if (value === void 0 || value === null || value === false) {
2939
- el.removeAttribute(key);
2940
- return;
2941
- }
2942
- if (value === true) {
2943
- el.setAttribute(key, "");
2944
- return;
2708
+ function createClassBinding(el, value) {
2709
+ if (isReactive(value)) {
2710
+ let prev = {};
2711
+ createRenderEffect(() => {
2712
+ const next = value();
2713
+ prev = applyClass(el, next, prev);
2714
+ });
2715
+ } else {
2716
+ applyClass(el, value, {});
2945
2717
  }
2946
- const valueType = typeof value;
2947
- if (valueType === "string" || valueType === "number") {
2948
- el.setAttribute(key, String(value));
2949
- return;
2718
+ }
2719
+ function bindClass(el, getValue) {
2720
+ let prev = {};
2721
+ let prevString;
2722
+ return createRenderEffect(() => {
2723
+ const next = getValue();
2724
+ if (typeof next === "string") {
2725
+ if (next === prevString) return;
2726
+ prevString = next;
2727
+ el.className = next;
2728
+ prev = {};
2729
+ return;
2730
+ }
2731
+ prevString = void 0;
2732
+ prev = applyClass(el, next, prev);
2733
+ });
2734
+ }
2735
+ function toggleClassKey(node, key, value) {
2736
+ const classNames = key.trim().split(/\s+/);
2737
+ for (let i = 0, len = classNames.length; i < len; i++) {
2738
+ node.classList.toggle(classNames[i], value);
2950
2739
  }
2951
- if (key in el) {
2952
- el[key] = value;
2953
- return;
2740
+ }
2741
+ function applyClass(el, value, prev) {
2742
+ const prevState = prev && typeof prev === "object" ? prev : {};
2743
+ if (typeof value === "string") {
2744
+ el.className = value;
2745
+ return {};
2954
2746
  }
2955
- el.setAttribute(key, String(value));
2956
- };
2957
- var setProperty = (el, key, value) => {
2958
- if (value === void 0 || value === null) {
2959
- const fallback = key === "checked" || key === "selected" ? false : "";
2960
- el[key] = fallback;
2961
- return;
2747
+ if (value && typeof value === "object") {
2748
+ const classes = value;
2749
+ const classKeys = Object.keys(classes);
2750
+ const prevKeys = Object.keys(prevState);
2751
+ for (let i = 0, len = prevKeys.length; i < len; i++) {
2752
+ const key = prevKeys[i];
2753
+ if (!key || key === "undefined" || classes[key]) continue;
2754
+ toggleClassKey(el, key, false);
2755
+ delete prevState[key];
2756
+ }
2757
+ for (let i = 0, len = classKeys.length; i < len; i++) {
2758
+ const key = classKeys[i];
2759
+ const classValue = !!classes[key];
2760
+ if (!key || key === "undefined" || prevState[key] === classValue || !classValue) continue;
2761
+ toggleClassKey(el, key, true);
2762
+ prevState[key] = classValue;
2763
+ }
2764
+ return prevState;
2962
2765
  }
2963
- if (key === "style" && typeof value === "object" && value !== null) {
2964
- for (const k in value) {
2965
- const v = value[k];
2966
- if (v !== void 0) {
2967
- el.style[k] = String(v);
2766
+ if (!value) {
2767
+ for (const key of Object.keys(prevState)) {
2768
+ if (key && key !== "undefined") {
2769
+ toggleClassKey(el, key, false);
2968
2770
  }
2969
2771
  }
2970
- return;
2971
- }
2972
- el[key] = value;
2973
- };
2974
- var setInnerHTML = (el, _key, value) => {
2975
- el.innerHTML = value == null ? "" : String(value);
2976
- };
2977
- var setBoolAttribute = (el, key, value) => {
2978
- if (value) {
2979
- el.setAttribute(key, "");
2980
- } else {
2981
- el.removeAttribute(key);
2772
+ return {};
2982
2773
  }
2983
- };
2984
- function setAttributeNS(el, namespace, name, value) {
2985
- if (value == null) {
2986
- el.removeAttributeNS(namespace, name);
2774
+ return prevState;
2775
+ }
2776
+ function insert(parent, getValue, markerOrCreateElement, createElementFn) {
2777
+ const hostRoot = getCurrentRoot();
2778
+ let marker;
2779
+ let ownsMarker = false;
2780
+ let createFn = createElementFn;
2781
+ if (markerOrCreateElement instanceof Node) {
2782
+ marker = markerOrCreateElement;
2783
+ createFn = createElementFn;
2987
2784
  } else {
2988
- el.setAttributeNS(namespace, name, String(value));
2785
+ marker = document.createComment("fict:insert");
2786
+ parent.appendChild(marker);
2787
+ createFn = markerOrCreateElement;
2788
+ ownsMarker = true;
2989
2789
  }
2990
- }
2991
- function isEventKey(key) {
2992
- return key.startsWith("on") && key.length > 2 && key[2].toUpperCase() === key[2];
2993
- }
2994
- function eventNameFromProp(key) {
2995
- return key.slice(2).toLowerCase();
2996
- }
2997
-
2998
- // src/reconcile.ts
2999
- function reconcileArrays(parentNode, a, b) {
3000
- const bLength = b.length;
3001
- let aEnd = a.length;
3002
- let bEnd = bLength;
3003
- let aStart = 0;
3004
- let bStart = 0;
3005
- const after = aEnd > 0 ? a[aEnd - 1].nextSibling : null;
3006
- let map = null;
3007
- while (aStart < aEnd || bStart < bEnd) {
3008
- if (a[aStart] === b[bStart]) {
3009
- aStart++;
3010
- bStart++;
3011
- continue;
2790
+ let currentNodes = [];
2791
+ let currentText = null;
2792
+ let currentRoot2 = null;
2793
+ const clearCurrentNodes = () => {
2794
+ if (currentNodes.length > 0) {
2795
+ removeNodes(currentNodes);
2796
+ currentNodes = [];
3012
2797
  }
3013
- while (a[aEnd - 1] === b[bEnd - 1]) {
3014
- aEnd--;
3015
- bEnd--;
2798
+ };
2799
+ const setTextNode = (textValue, shouldInsert, parentNode) => {
2800
+ if (!currentText) {
2801
+ currentText = document.createTextNode(textValue);
2802
+ } else if (currentText.data !== textValue) {
2803
+ currentText.data = textValue;
3016
2804
  }
3017
- if (aEnd === aStart) {
3018
- const node = bEnd < bLength ? bStart ? b[bStart - 1].nextSibling : b[bEnd - bStart] ?? null : after;
3019
- const count = bEnd - bStart;
3020
- const doc = parentNode.ownerDocument;
3021
- if (count > 1 && doc) {
3022
- const frag = doc.createDocumentFragment();
3023
- for (let i = bStart; i < bEnd; i++) {
3024
- frag.appendChild(b[i]);
3025
- }
3026
- parentNode.insertBefore(frag, node);
3027
- bStart = bEnd;
3028
- } else {
3029
- while (bStart < bEnd) {
3030
- parentNode.insertBefore(b[bStart++], node);
3031
- }
3032
- }
3033
- } else if (bEnd === bStart) {
3034
- while (aStart < aEnd) {
3035
- const nodeToRemove = a[aStart];
3036
- if (!map || !map.has(nodeToRemove)) {
3037
- nodeToRemove.parentNode?.removeChild(nodeToRemove);
3038
- }
3039
- aStart++;
2805
+ if (!shouldInsert) {
2806
+ clearCurrentNodes();
2807
+ return;
2808
+ }
2809
+ if (currentNodes.length === 1 && currentNodes[0] === currentText) {
2810
+ return;
2811
+ }
2812
+ clearCurrentNodes();
2813
+ insertNodesBefore(parentNode, [currentText], marker);
2814
+ currentNodes = [currentText];
2815
+ };
2816
+ const dispose = createRenderEffect(() => {
2817
+ const value = getValue();
2818
+ const parentNode = marker.parentNode;
2819
+ const isPrimitive = value == null || value === false || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
2820
+ if (isPrimitive) {
2821
+ if (currentRoot2) {
2822
+ destroyRoot(currentRoot2);
2823
+ currentRoot2 = null;
3040
2824
  }
3041
- } else if (a[aStart] === b[bEnd - 1] && b[bStart] === a[aEnd - 1]) {
3042
- const node = a[--aEnd].nextSibling;
3043
- parentNode.insertBefore(b[bStart++], a[aStart++].nextSibling);
3044
- parentNode.insertBefore(b[--bEnd], node);
3045
- a[aEnd] = b[bEnd];
3046
- } else {
3047
- if (!map) {
3048
- map = /* @__PURE__ */ new Map();
3049
- let i = bStart;
3050
- while (i < bEnd) {
3051
- map.set(b[i], i++);
3052
- }
2825
+ if (!parentNode) {
2826
+ clearCurrentNodes();
2827
+ return;
3053
2828
  }
3054
- const index = map.get(a[aStart]);
3055
- if (index != null) {
3056
- if (bStart < index && index < bEnd) {
3057
- let i = aStart;
3058
- let sequence = 1;
3059
- let t;
3060
- while (++i < aEnd && i < bEnd) {
3061
- t = map.get(a[i]);
3062
- if (t == null || t !== index + sequence) break;
3063
- sequence++;
3064
- }
3065
- if (sequence > index - bStart) {
3066
- const node = a[aStart];
3067
- while (bStart < index) {
3068
- parentNode.insertBefore(b[bStart++], node);
2829
+ const textValue = value == null || value === false ? "" : String(value);
2830
+ const shouldInsert = value != null && value !== false;
2831
+ setTextNode(textValue, shouldInsert, parentNode);
2832
+ return;
2833
+ }
2834
+ if (currentRoot2) {
2835
+ destroyRoot(currentRoot2);
2836
+ currentRoot2 = null;
2837
+ }
2838
+ clearCurrentNodes();
2839
+ const root = createRootContext(hostRoot);
2840
+ const prev = pushRoot(root);
2841
+ let nodes = [];
2842
+ try {
2843
+ let newNode;
2844
+ if (value instanceof Node) {
2845
+ newNode = value;
2846
+ } else if (Array.isArray(value)) {
2847
+ if (value.every((v) => v instanceof Node)) {
2848
+ newNode = value;
2849
+ } else {
2850
+ if (createFn) {
2851
+ const mapped = [];
2852
+ for (const item of value) {
2853
+ mapped.push(...toNodeArray(createFn(item)));
3069
2854
  }
2855
+ newNode = mapped;
3070
2856
  } else {
3071
- parentNode.replaceChild(b[bStart++], a[aStart++]);
2857
+ newNode = document.createTextNode(String(value));
3072
2858
  }
3073
- } else {
3074
- aStart++;
3075
2859
  }
3076
2860
  } else {
3077
- const nodeToRemove = a[aStart++];
3078
- nodeToRemove.parentNode?.removeChild(nodeToRemove);
2861
+ newNode = createFn ? createFn(value) : document.createTextNode(String(value));
2862
+ }
2863
+ nodes = toNodeArray(newNode);
2864
+ if (parentNode) {
2865
+ insertNodesBefore(parentNode, nodes, marker);
3079
2866
  }
2867
+ } finally {
2868
+ popRoot(prev);
2869
+ flushOnMount(root);
3080
2870
  }
3081
- }
3082
- }
3083
-
3084
- // src/list-helpers.ts
3085
- function moveNodesBefore(parent, nodes, anchor) {
3086
- for (let i = nodes.length - 1; i >= 0; i--) {
3087
- const node = nodes[i];
3088
- if (!node || !(node instanceof Node)) {
3089
- throw new Error("Invalid node in moveNodesBefore");
2871
+ currentRoot2 = root;
2872
+ currentNodes = nodes;
2873
+ });
2874
+ return () => {
2875
+ dispose();
2876
+ if (currentRoot2) {
2877
+ destroyRoot(currentRoot2);
2878
+ currentRoot2 = null;
3090
2879
  }
3091
- if (node.nextSibling !== anchor) {
3092
- if (node.ownerDocument !== parent.ownerDocument && parent.ownerDocument) {
3093
- parent.ownerDocument.adoptNode(node);
3094
- }
3095
- try {
3096
- parent.insertBefore(node, anchor);
3097
- } catch (e) {
3098
- if (parent.ownerDocument) {
3099
- try {
3100
- const clone = parent.ownerDocument.importNode(node, true);
3101
- parent.insertBefore(clone, anchor);
3102
- continue;
3103
- } catch {
3104
- }
3105
- }
3106
- throw e;
3107
- }
2880
+ clearCurrentNodes();
2881
+ if (ownsMarker) {
2882
+ marker.parentNode?.removeChild(marker);
3108
2883
  }
3109
- anchor = node;
3110
- }
3111
- }
3112
- function moveMarkerBlock(parent, block, anchor) {
3113
- const nodes = collectBlockNodes(block);
3114
- if (nodes.length === 0) return;
3115
- moveNodesBefore(parent, nodes, anchor);
3116
- }
3117
- function destroyMarkerBlock(block) {
3118
- if (block.root) {
3119
- destroyRoot(block.root);
3120
- }
3121
- removeBlockRange(block);
2884
+ };
3122
2885
  }
3123
- function collectBlockNodes(block) {
3124
- const nodes = [];
3125
- let cursor = block.start;
3126
- while (cursor) {
3127
- nodes.push(cursor);
3128
- if (cursor === block.end) {
3129
- break;
2886
+ function createChildBinding(parent, getValue, createElementFn) {
2887
+ const marker = document.createComment("fict:child");
2888
+ parent.appendChild(marker);
2889
+ const hostRoot = getCurrentRoot();
2890
+ const dispose = createRenderEffect(() => {
2891
+ const root = createRootContext(hostRoot);
2892
+ const prev = pushRoot(root);
2893
+ let nodes = [];
2894
+ let handledError = false;
2895
+ try {
2896
+ const value = getValue();
2897
+ if (value == null || value === false) {
2898
+ return;
2899
+ }
2900
+ const output = createElementFn(value);
2901
+ nodes = toNodeArray(output);
2902
+ const parentNode = marker.parentNode;
2903
+ if (parentNode) {
2904
+ insertNodesBefore(parentNode, nodes, marker);
2905
+ }
2906
+ return () => {
2907
+ destroyRoot(root);
2908
+ removeNodes(nodes);
2909
+ };
2910
+ } catch (err) {
2911
+ if (handleSuspend(err, root)) {
2912
+ handledError = true;
2913
+ destroyRoot(root);
2914
+ return;
2915
+ }
2916
+ if (handleError(err, { source: "renderChild" }, root)) {
2917
+ handledError = true;
2918
+ destroyRoot(root);
2919
+ return;
2920
+ }
2921
+ throw err;
2922
+ } finally {
2923
+ popRoot(prev);
2924
+ if (!handledError) {
2925
+ flushOnMount(root);
2926
+ }
3130
2927
  }
3131
- cursor = cursor.nextSibling;
3132
- }
3133
- return nodes;
2928
+ });
2929
+ return {
2930
+ marker,
2931
+ dispose: () => {
2932
+ dispose();
2933
+ marker.parentNode?.removeChild(marker);
2934
+ }
2935
+ };
3134
2936
  }
3135
- function removeBlockRange(block) {
3136
- let cursor = block.start;
3137
- while (cursor) {
3138
- const next = cursor.nextSibling;
3139
- cursor.parentNode?.removeChild(cursor);
3140
- if (cursor === block.end) {
3141
- break;
2937
+ function delegateEvents(eventNames, doc = window.document) {
2938
+ const e = doc[$$EVENTS] || (doc[$$EVENTS] = /* @__PURE__ */ new Set());
2939
+ for (let i = 0, l = eventNames.length; i < l; i++) {
2940
+ const name = eventNames[i];
2941
+ if (!e.has(name)) {
2942
+ e.add(name);
2943
+ doc.addEventListener(name, globalEventHandler);
3142
2944
  }
3143
- cursor = next;
3144
2945
  }
3145
2946
  }
3146
- function createVersionedSignalAccessor(initialValue) {
3147
- let current = initialValue;
3148
- let version = 0;
3149
- const track = signal(version);
3150
- function accessor(value) {
3151
- if (arguments.length === 0) {
3152
- track();
3153
- return current;
2947
+ function clearDelegatedEvents(doc = window.document) {
2948
+ const e = doc[$$EVENTS];
2949
+ if (e) {
2950
+ for (const name of e.keys()) {
2951
+ doc.removeEventListener(name, globalEventHandler);
3154
2952
  }
3155
- current = value;
3156
- version++;
3157
- track(version);
2953
+ delete doc[$$EVENTS];
3158
2954
  }
3159
- return accessor;
3160
2955
  }
3161
- function createKeyedListContainer() {
3162
- const startMarker = document.createComment("fict:list:start");
3163
- const endMarker = document.createComment("fict:list:end");
3164
- const dispose = () => {
3165
- for (const block of container.blocks.values()) {
3166
- destroyRoot(block.root);
2956
+ function globalEventHandler(e) {
2957
+ const asNode = (value) => value && typeof value.nodeType === "number" ? value : null;
2958
+ const asElement = (value) => {
2959
+ const n = asNode(value);
2960
+ if (!n) return null;
2961
+ if (n.nodeType === 1) return n;
2962
+ return n.parentElement;
2963
+ };
2964
+ let node = asElement(e.target);
2965
+ const key = `$$${e.type}`;
2966
+ const dataKey = `${key}Data`;
2967
+ const oriTarget = e.target;
2968
+ const oriCurrentTarget = e.currentTarget;
2969
+ let lastHandled = null;
2970
+ const retarget = (value) => Object.defineProperty(e, "target", {
2971
+ configurable: true,
2972
+ value
2973
+ });
2974
+ const handleNode = () => {
2975
+ if (!node) return false;
2976
+ const handler = node[key];
2977
+ if (handler && !node.disabled) {
2978
+ const resolveData = (value) => {
2979
+ if (typeof value === "function") {
2980
+ try {
2981
+ const fn = value;
2982
+ return fn.length > 0 ? fn(e) : fn();
2983
+ } catch {
2984
+ return value();
2985
+ }
2986
+ }
2987
+ return value;
2988
+ };
2989
+ const rawData = node[dataKey];
2990
+ const hasData = rawData !== void 0;
2991
+ const resolvedNodeData = hasData ? resolveData(rawData) : void 0;
2992
+ batch2(() => {
2993
+ if (typeof handler === "function") {
2994
+ callEventHandler(handler, e, node, hasData ? resolvedNodeData : void 0);
2995
+ } else if (Array.isArray(handler)) {
2996
+ const tupleData = resolveData(handler[1]);
2997
+ callEventHandler(handler[0], e, node, tupleData);
2998
+ }
2999
+ });
3000
+ if (e.cancelBubble) return false;
3167
3001
  }
3168
- container.blocks.clear();
3169
- container.nextBlocks.clear();
3170
- const range = document.createRange();
3171
- range.setStartBefore(startMarker);
3172
- range.setEndAfter(endMarker);
3173
- range.deleteContents();
3174
- container.currentNodes = [];
3175
- container.nextNodes = [];
3176
- container.nextBlocks.clear();
3177
- container.orderedBlocks.length = 0;
3178
- container.nextOrderedBlocks.length = 0;
3179
- container.orderedIndexByKey.clear();
3002
+ const shadowHost = node.host;
3003
+ if (shadowHost && typeof shadowHost !== "string" && !shadowHost._$host && (() => {
3004
+ const targetNode = asNode(e.target);
3005
+ return targetNode ? node.contains(targetNode) : false;
3006
+ })()) {
3007
+ retarget(shadowHost);
3008
+ }
3009
+ return true;
3180
3010
  };
3181
- const container = {
3182
- startMarker,
3183
- endMarker,
3184
- blocks: /* @__PURE__ */ new Map(),
3185
- nextBlocks: /* @__PURE__ */ new Map(),
3186
- currentNodes: [startMarker, endMarker],
3187
- nextNodes: [],
3188
- orderedBlocks: [],
3189
- nextOrderedBlocks: [],
3190
- orderedIndexByKey: /* @__PURE__ */ new Map(),
3191
- dispose
3011
+ const walkUpTree = () => {
3012
+ while (handleNode() && node) {
3013
+ node = asElement(node._$host || node.parentNode || node.host);
3014
+ }
3192
3015
  };
3193
- return container;
3194
- }
3195
- function createKeyedBlock(key, item, index, render2, needsIndex = true) {
3196
- const itemSig = createVersionedSignalAccessor(item);
3197
- const indexSig = needsIndex ? signal(index) : ((next) => {
3198
- if (arguments.length === 0) return index;
3199
- index = next;
3200
- return index;
3016
+ Object.defineProperty(e, "currentTarget", {
3017
+ configurable: true,
3018
+ get() {
3019
+ return node || document;
3020
+ }
3201
3021
  });
3202
- const root = createRootContext();
3203
- const prevRoot = pushRoot(root);
3204
- const prevSub = setActiveSub(void 0);
3205
- let nodes = [];
3206
- try {
3207
- const rendered = render2(itemSig, indexSig, key);
3208
- if (rendered instanceof Node || Array.isArray(rendered) && rendered.every((n) => n instanceof Node)) {
3209
- nodes = toNodeArray(rendered);
3210
- } else {
3211
- const element = createElement(rendered);
3212
- nodes = toNodeArray(element);
3022
+ if (e.composedPath) {
3023
+ const path = e.composedPath();
3024
+ retarget(path[0]);
3025
+ for (let i = 0; i < path.length - 2; i++) {
3026
+ const nextNode = asElement(path[i]);
3027
+ if (!nextNode || nextNode === lastHandled) continue;
3028
+ node = nextNode;
3029
+ if (!handleNode()) break;
3030
+ lastHandled = node;
3031
+ if (node._$host) {
3032
+ node = node._$host;
3033
+ walkUpTree();
3034
+ break;
3035
+ }
3036
+ if (node.parentNode === oriCurrentTarget) {
3037
+ break;
3038
+ }
3213
3039
  }
3214
- } finally {
3215
- setActiveSub(prevSub);
3216
- popRoot(prevRoot);
3217
- flushOnMount(root);
3040
+ } else {
3041
+ walkUpTree();
3218
3042
  }
3219
- return {
3220
- key,
3221
- nodes,
3222
- root,
3223
- item: itemSig,
3224
- index: indexSig,
3225
- rawItem: item,
3226
- rawIndex: index
3227
- };
3228
- }
3229
- function getFirstNodeAfter(marker) {
3230
- return marker.nextSibling;
3231
- }
3232
- function createKeyedList(getItems, keyFn, renderItem, needsIndex) {
3233
- const resolvedNeedsIndex = arguments.length >= 4 ? !!needsIndex : renderItem.length > 1;
3234
- return createFineGrainedKeyedList(getItems, keyFn, renderItem, resolvedNeedsIndex);
3043
+ retarget(oriTarget);
3235
3044
  }
3236
- function createFineGrainedKeyedList(getItems, keyFn, renderItem, needsIndex) {
3237
- const container = createKeyedListContainer();
3238
- const fragment = document.createDocumentFragment();
3239
- fragment.append(container.startMarker, container.endMarker);
3240
- let pendingItems = null;
3241
- let disposed = false;
3242
- const performDiff = () => {
3243
- if (disposed) return;
3244
- batch2(() => {
3245
- const newItems = pendingItems || getItems();
3246
- pendingItems = null;
3247
- const oldBlocks = container.blocks;
3248
- const newBlocks = container.nextBlocks;
3249
- const prevOrderedBlocks = container.orderedBlocks;
3250
- const nextOrderedBlocks = container.nextOrderedBlocks;
3251
- const orderedIndexByKey = container.orderedIndexByKey;
3252
- newBlocks.clear();
3253
- nextOrderedBlocks.length = 0;
3254
- orderedIndexByKey.clear();
3255
- const endParent = container.endMarker.parentNode;
3256
- const startParent = container.startMarker.parentNode;
3257
- const parent = endParent && startParent && endParent === startParent && endParent.isConnected ? endParent : null;
3258
- if (!parent) {
3259
- pendingItems = newItems;
3260
- queueMicrotask(performDiff);
3045
+ function bindEvent(el, eventName, handler, options2) {
3046
+ if (handler == null) return () => {
3047
+ };
3048
+ const rootRef = getCurrentRoot();
3049
+ if (DelegatedEvents.has(eventName) && !options2) {
3050
+ const key = `$$${eventName}`;
3051
+ delegateEvents([eventName]);
3052
+ const resolveHandler = isReactive(handler) ? handler : () => handler;
3053
+ el[key] = function(...args) {
3054
+ try {
3055
+ const fn = resolveHandler();
3056
+ callEventHandler(fn, args[0], el);
3057
+ } catch (err) {
3058
+ handleError(err, { source: "event", eventName }, rootRef);
3059
+ }
3060
+ };
3061
+ return () => {
3062
+ el[key] = void 0;
3063
+ };
3064
+ }
3065
+ const getHandler = isReactive(handler) ? handler : () => handler;
3066
+ const wrapped = (event) => {
3067
+ try {
3068
+ const resolved = getHandler();
3069
+ callEventHandler(resolved, event, el);
3070
+ } catch (err) {
3071
+ if (handleError(err, { source: "event", eventName }, rootRef)) {
3261
3072
  return;
3262
3073
  }
3263
- if (newItems.length === 0) {
3264
- if (oldBlocks.size > 0) {
3265
- for (const block of oldBlocks.values()) {
3266
- destroyRoot(block.root);
3267
- removeNodes(block.nodes);
3268
- }
3269
- }
3270
- oldBlocks.clear();
3271
- newBlocks.clear();
3272
- prevOrderedBlocks.length = 0;
3273
- nextOrderedBlocks.length = 0;
3274
- orderedIndexByKey.clear();
3275
- container.currentNodes.length = 0;
3276
- container.currentNodes.push(container.startMarker, container.endMarker);
3277
- container.nextNodes.length = 0;
3074
+ throw err;
3075
+ }
3076
+ };
3077
+ el.addEventListener(eventName, wrapped, options2);
3078
+ const cleanup = () => el.removeEventListener(eventName, wrapped, options2);
3079
+ registerRootCleanup(cleanup);
3080
+ return cleanup;
3081
+ }
3082
+ function bindRef(el, ref) {
3083
+ if (ref == null) return () => {
3084
+ };
3085
+ const getRef = isReactive(ref) ? ref : () => ref;
3086
+ const applyRef2 = (refValue) => {
3087
+ if (refValue == null) return;
3088
+ if (typeof refValue === "function") {
3089
+ refValue(el);
3090
+ } else if (typeof refValue === "object" && "current" in refValue) {
3091
+ refValue.current = el;
3092
+ }
3093
+ };
3094
+ const initialRef = getRef();
3095
+ applyRef2(initialRef);
3096
+ if (isReactive(ref)) {
3097
+ const cleanup2 = createRenderEffect(() => {
3098
+ const currentRef = getRef();
3099
+ applyRef2(currentRef);
3100
+ });
3101
+ registerRootCleanup(cleanup2);
3102
+ const nullifyCleanup = () => {
3103
+ const currentRef = getRef();
3104
+ if (currentRef && typeof currentRef === "object" && "current" in currentRef) {
3105
+ currentRef.current = null;
3106
+ }
3107
+ };
3108
+ registerRootCleanup(nullifyCleanup);
3109
+ return () => {
3110
+ cleanup2();
3111
+ nullifyCleanup();
3112
+ };
3113
+ }
3114
+ const cleanup = () => {
3115
+ const refValue = getRef();
3116
+ if (refValue && typeof refValue === "object" && "current" in refValue) {
3117
+ refValue.current = null;
3118
+ }
3119
+ };
3120
+ registerRootCleanup(cleanup);
3121
+ return cleanup;
3122
+ }
3123
+ function createConditional(condition, renderTrue, createElementFn, renderFalse) {
3124
+ const startMarker = document.createComment("fict:cond:start");
3125
+ const endMarker = document.createComment("fict:cond:end");
3126
+ const fragment = document.createDocumentFragment();
3127
+ fragment.append(startMarker, endMarker);
3128
+ const hostRoot = getCurrentRoot();
3129
+ let currentNodes = [];
3130
+ let currentRoot2 = null;
3131
+ let lastCondition = void 0;
3132
+ let pendingRender = false;
3133
+ const conditionMemo = computed(condition);
3134
+ const runConditional = () => {
3135
+ const cond = conditionMemo();
3136
+ const parent = startMarker.parentNode;
3137
+ if (!parent) {
3138
+ pendingRender = true;
3139
+ return;
3140
+ }
3141
+ pendingRender = false;
3142
+ if (lastCondition === cond && currentNodes.length > 0) {
3143
+ return;
3144
+ }
3145
+ if (lastCondition === cond && lastCondition === false && renderFalse === void 0) {
3146
+ return;
3147
+ }
3148
+ lastCondition = cond;
3149
+ if (currentRoot2) {
3150
+ destroyRoot(currentRoot2);
3151
+ currentRoot2 = null;
3152
+ }
3153
+ removeNodes(currentNodes);
3154
+ currentNodes = [];
3155
+ const render2 = cond ? renderTrue : renderFalse;
3156
+ if (!render2) {
3157
+ return;
3158
+ }
3159
+ const root = createRootContext(hostRoot);
3160
+ const prev = pushRoot(root);
3161
+ let handledError = false;
3162
+ try {
3163
+ const output = untrack(render2);
3164
+ if (output == null || output === false) {
3278
3165
  return;
3279
3166
  }
3280
- const prevCount = prevOrderedBlocks.length;
3281
- let appendCandidate = prevCount > 0 && newItems.length >= prevCount;
3282
- const appendedBlocks = [];
3283
- newItems.forEach((item, index) => {
3284
- const key = keyFn(item, index);
3285
- const existed = oldBlocks.has(key);
3286
- let block = oldBlocks.get(key);
3287
- if (block) {
3288
- if (block.rawItem !== item) {
3289
- block.rawItem = item;
3290
- block.item(item);
3291
- }
3292
- if (needsIndex && block.rawIndex !== index) {
3293
- block.rawIndex = index;
3294
- block.index(index);
3295
- }
3296
- }
3297
- const existingBlock = newBlocks.get(key);
3298
- if (existingBlock && existingBlock !== block) {
3299
- destroyRoot(existingBlock.root);
3300
- removeNodes(existingBlock.nodes);
3301
- }
3302
- if (block) {
3303
- newBlocks.set(key, block);
3304
- oldBlocks.delete(key);
3305
- } else {
3306
- const existingBlock2 = newBlocks.get(key);
3307
- if (existingBlock2) {
3308
- destroyRoot(existingBlock2.root);
3309
- removeNodes(existingBlock2.nodes);
3310
- }
3311
- block = createKeyedBlock(key, item, index, renderItem, needsIndex);
3312
- }
3313
- const resolvedBlock = block;
3314
- newBlocks.set(key, resolvedBlock);
3315
- const position = orderedIndexByKey.get(key);
3316
- if (position !== void 0) {
3317
- appendCandidate = false;
3318
- }
3319
- if (appendCandidate) {
3320
- if (index < prevCount) {
3321
- if (!prevOrderedBlocks[index] || prevOrderedBlocks[index].key !== key) {
3322
- appendCandidate = false;
3323
- }
3324
- } else if (existed) {
3325
- appendCandidate = false;
3326
- }
3327
- }
3328
- if (position !== void 0) {
3329
- const prior = nextOrderedBlocks[position];
3330
- if (prior && prior !== resolvedBlock) {
3331
- destroyRoot(prior.root);
3332
- removeNodes(prior.nodes);
3333
- }
3334
- nextOrderedBlocks[position] = resolvedBlock;
3335
- } else {
3336
- orderedIndexByKey.set(key, nextOrderedBlocks.length);
3337
- nextOrderedBlocks.push(resolvedBlock);
3338
- }
3339
- if (appendCandidate && index >= prevCount) {
3340
- appendedBlocks.push(resolvedBlock);
3341
- }
3342
- });
3343
- const canAppend = appendCandidate && prevCount > 0 && newItems.length > prevCount && oldBlocks.size === 0 && appendedBlocks.length > 0;
3344
- if (canAppend) {
3345
- const appendedNodes = [];
3346
- for (const block of appendedBlocks) {
3347
- for (let i = 0; i < block.nodes.length; i++) {
3348
- appendedNodes.push(block.nodes[i]);
3349
- }
3350
- }
3351
- if (appendedNodes.length > 0) {
3352
- insertNodesBefore(parent, appendedNodes, container.endMarker);
3353
- const currentNodes = container.currentNodes;
3354
- currentNodes.pop();
3355
- for (let i = 0; i < appendedNodes.length; i++) {
3356
- currentNodes.push(appendedNodes[i]);
3357
- }
3358
- currentNodes.push(container.endMarker);
3359
- }
3360
- container.blocks = newBlocks;
3361
- container.nextBlocks = oldBlocks;
3362
- container.orderedBlocks = nextOrderedBlocks;
3363
- container.nextOrderedBlocks = prevOrderedBlocks;
3167
+ const el = createElementFn(output);
3168
+ const nodes = toNodeArray(el);
3169
+ insertNodesBefore(parent, nodes, endMarker);
3170
+ currentNodes = nodes;
3171
+ } catch (err) {
3172
+ if (handleSuspend(err, root)) {
3173
+ handledError = true;
3174
+ destroyRoot(root);
3364
3175
  return;
3365
3176
  }
3366
- if (oldBlocks.size > 0) {
3367
- for (const block of oldBlocks.values()) {
3368
- destroyRoot(block.root);
3369
- removeNodes(block.nodes);
3370
- }
3371
- oldBlocks.clear();
3177
+ if (handleError(err, { source: "renderChild" }, root)) {
3178
+ handledError = true;
3179
+ destroyRoot(root);
3180
+ return;
3372
3181
  }
3373
- if (newBlocks.size > 0 || container.currentNodes.length > 0) {
3374
- const prevNodes = container.currentNodes;
3375
- const nextNodes = container.nextNodes;
3376
- nextNodes.length = 0;
3377
- nextNodes.push(container.startMarker);
3378
- for (let i = 0; i < nextOrderedBlocks.length; i++) {
3379
- const nodes = nextOrderedBlocks[i].nodes;
3380
- for (let j = 0; j < nodes.length; j++) {
3381
- nextNodes.push(nodes[j]);
3382
- }
3383
- }
3384
- nextNodes.push(container.endMarker);
3385
- reconcileArrays(parent, prevNodes, nextNodes);
3386
- container.currentNodes = nextNodes;
3387
- container.nextNodes = prevNodes;
3182
+ throw err;
3183
+ } finally {
3184
+ popRoot(prev);
3185
+ if (!handledError) {
3186
+ flushOnMount(root);
3187
+ currentRoot2 = root;
3188
+ } else {
3189
+ currentRoot2 = null;
3388
3190
  }
3389
- container.blocks = newBlocks;
3390
- container.nextBlocks = oldBlocks;
3391
- container.orderedBlocks = nextOrderedBlocks;
3392
- container.nextOrderedBlocks = prevOrderedBlocks;
3393
- });
3191
+ }
3394
3192
  };
3395
- const effectDispose = createRenderEffect(performDiff);
3193
+ const dispose = createRenderEffect(runConditional);
3396
3194
  return {
3397
3195
  marker: fragment,
3398
- startMarker: container.startMarker,
3399
- endMarker: container.endMarker,
3400
- // Flush pending items - call after markers are inserted into DOM
3401
3196
  flush: () => {
3402
- if (pendingItems !== null) {
3403
- performDiff();
3404
- }
3197
+ if (pendingRender) {
3198
+ runConditional();
3199
+ }
3405
3200
  },
3406
3201
  dispose: () => {
3407
- disposed = true;
3408
- effectDispose?.();
3409
- container.dispose();
3202
+ dispose();
3203
+ if (currentRoot2) {
3204
+ destroyRoot(currentRoot2);
3205
+ }
3206
+ removeNodes(currentNodes);
3207
+ currentNodes = [];
3208
+ startMarker.parentNode?.removeChild(startMarker);
3209
+ endMarker.parentNode?.removeChild(endMarker);
3210
+ }
3211
+ };
3212
+ }
3213
+ function createList(items, renderItem, createElementFn, getKey) {
3214
+ const startMarker = document.createComment("fict:list:start");
3215
+ const endMarker = document.createComment("fict:list:end");
3216
+ const fragment = document.createDocumentFragment();
3217
+ fragment.append(startMarker, endMarker);
3218
+ const hostRoot = getCurrentRoot();
3219
+ const nodeMap = /* @__PURE__ */ new Map();
3220
+ let pendingItems = null;
3221
+ const runListUpdate = () => {
3222
+ const arr = items();
3223
+ const parent = startMarker.parentNode;
3224
+ if (!parent) {
3225
+ pendingItems = arr;
3226
+ return;
3227
+ }
3228
+ pendingItems = null;
3229
+ const newNodeMap = /* @__PURE__ */ new Map();
3230
+ const blocks = [];
3231
+ for (let i = 0; i < arr.length; i++) {
3232
+ const item = arr[i];
3233
+ const key = getKey ? getKey(item, i) : i;
3234
+ const existing = nodeMap.get(key);
3235
+ let block;
3236
+ if (existing) {
3237
+ const previousValue = existing.value();
3238
+ if (!getKey && previousValue !== item) {
3239
+ destroyRoot(existing.root);
3240
+ removeBlockNodes(existing);
3241
+ block = mountBlock(item, i, renderItem, parent, endMarker, createElementFn, hostRoot);
3242
+ } else {
3243
+ const previousIndex = existing.index();
3244
+ existing.value(item);
3245
+ existing.index(i);
3246
+ const needsRerender = getKey ? true : previousValue !== item || previousIndex !== i;
3247
+ block = needsRerender ? rerenderBlock(existing, createElementFn) : existing;
3248
+ }
3249
+ } else {
3250
+ block = mountBlock(item, i, renderItem, parent, endMarker, createElementFn, hostRoot);
3251
+ }
3252
+ newNodeMap.set(key, block);
3253
+ blocks.push(block);
3254
+ }
3255
+ for (const [key, managed] of nodeMap) {
3256
+ if (!newNodeMap.has(key)) {
3257
+ destroyRoot(managed.root);
3258
+ removeBlockNodes(managed);
3259
+ }
3260
+ }
3261
+ let anchor = endMarker;
3262
+ for (let i = blocks.length - 1; i >= 0; i--) {
3263
+ const block = blocks[i];
3264
+ insertNodesBefore(parent, block.nodes, anchor);
3265
+ if (block.nodes.length > 0) {
3266
+ anchor = block.nodes[0];
3267
+ }
3268
+ }
3269
+ nodeMap.clear();
3270
+ for (const [k, v] of newNodeMap) {
3271
+ nodeMap.set(k, v);
3272
+ }
3273
+ };
3274
+ const dispose = createRenderEffect(runListUpdate);
3275
+ return {
3276
+ marker: fragment,
3277
+ flush: () => {
3278
+ if (pendingItems !== null) {
3279
+ runListUpdate();
3280
+ }
3281
+ },
3282
+ dispose: () => {
3283
+ dispose();
3284
+ for (const [, managed] of nodeMap) {
3285
+ destroyRoot(managed.root);
3286
+ removeBlockNodes(managed);
3287
+ }
3288
+ nodeMap.clear();
3289
+ startMarker.parentNode?.removeChild(startMarker);
3290
+ endMarker.parentNode?.removeChild(endMarker);
3291
+ }
3292
+ };
3293
+ }
3294
+ function mountBlock(initialValue, initialIndex, renderItem, parent, anchor, createElementFn, hostRoot) {
3295
+ const start = document.createComment("fict:block:start");
3296
+ const end = document.createComment("fict:block:end");
3297
+ const valueSig = createVersionedSignalAccessor(initialValue);
3298
+ const indexSig = signal(initialIndex);
3299
+ const renderCurrent = () => renderItem(valueSig, indexSig);
3300
+ const root = createRootContext(hostRoot);
3301
+ const prev = pushRoot(root);
3302
+ const nodes = [start];
3303
+ let handledError = false;
3304
+ try {
3305
+ const output = renderCurrent();
3306
+ if (output != null && output !== false) {
3307
+ const el = createElementFn(output);
3308
+ const rendered = toNodeArray(el);
3309
+ nodes.push(...rendered);
3310
+ }
3311
+ nodes.push(end);
3312
+ insertNodesBefore(parent, nodes, anchor);
3313
+ } catch (err) {
3314
+ if (handleSuspend(err, root)) {
3315
+ handledError = true;
3316
+ nodes.push(end);
3317
+ insertNodesBefore(parent, nodes, anchor);
3318
+ } else if (handleError(err, { source: "renderChild" }, root)) {
3319
+ handledError = true;
3320
+ nodes.push(end);
3321
+ insertNodesBefore(parent, nodes, anchor);
3322
+ } else {
3323
+ throw err;
3324
+ }
3325
+ } finally {
3326
+ popRoot(prev);
3327
+ if (!handledError) {
3328
+ flushOnMount(root);
3329
+ } else {
3330
+ destroyRoot(root);
3331
+ }
3332
+ }
3333
+ return {
3334
+ nodes,
3335
+ root,
3336
+ value: valueSig,
3337
+ index: indexSig,
3338
+ start,
3339
+ end,
3340
+ renderCurrent
3341
+ };
3342
+ }
3343
+ function rerenderBlock(block, createElementFn) {
3344
+ const currentContent = block.nodes.slice(1, Math.max(1, block.nodes.length - 1));
3345
+ const currentNode = currentContent.length === 1 ? currentContent[0] : null;
3346
+ clearRoot(block.root);
3347
+ const prev = pushRoot(block.root);
3348
+ let nextOutput;
3349
+ let handledError = false;
3350
+ try {
3351
+ nextOutput = block.renderCurrent();
3352
+ } catch (err) {
3353
+ if (handleSuspend(err, block.root)) {
3354
+ handledError = true;
3355
+ popRoot(prev);
3356
+ destroyRoot(block.root);
3357
+ block.nodes = [block.start, block.end];
3358
+ return block;
3359
+ }
3360
+ if (handleError(err, { source: "renderChild" }, block.root)) {
3361
+ handledError = true;
3362
+ popRoot(prev);
3363
+ destroyRoot(block.root);
3364
+ block.nodes = [block.start, block.end];
3365
+ return block;
3366
+ }
3367
+ throw err;
3368
+ } finally {
3369
+ if (!handledError) {
3370
+ popRoot(prev);
3371
+ }
3372
+ }
3373
+ if (isFragmentVNode(nextOutput) && currentContent.length > 0) {
3374
+ const patched = patchFragmentChildren(currentContent, nextOutput.props?.children);
3375
+ if (patched) {
3376
+ block.nodes = [block.start, ...currentContent, block.end];
3377
+ return block;
3378
+ }
3379
+ }
3380
+ if (currentNode && patchNode(currentNode, nextOutput)) {
3381
+ block.nodes = [block.start, currentNode, block.end];
3382
+ return block;
3383
+ }
3384
+ clearContent(block);
3385
+ if (nextOutput != null && nextOutput !== false) {
3386
+ const newNodes = toNodeArray(
3387
+ nextOutput instanceof Node ? nextOutput : createElementFn(nextOutput)
3388
+ );
3389
+ insertNodesBefore(block.start.parentNode, newNodes, block.end);
3390
+ block.nodes = [block.start, ...newNodes, block.end];
3391
+ } else {
3392
+ block.nodes = [block.start, block.end];
3393
+ }
3394
+ return block;
3395
+ }
3396
+ function patchElement(el, output) {
3397
+ if (output === null || output === void 0 || output === false || typeof output === "string" || typeof output === "number") {
3398
+ el.textContent = output === null || output === void 0 || output === false ? "" : String(output);
3399
+ return true;
3400
+ }
3401
+ if (output instanceof Text) {
3402
+ el.textContent = output.data;
3403
+ return true;
3404
+ }
3405
+ if (output && typeof output === "object" && !(output instanceof Node)) {
3406
+ const vnode = output;
3407
+ if (typeof vnode.type === "string" && vnode.type.toLowerCase() === el.tagName.toLowerCase()) {
3408
+ const children = vnode.props?.children;
3409
+ const props = vnode.props ?? {};
3410
+ for (const [key, value] of Object.entries(props)) {
3411
+ if (key === "children" || key === "key") continue;
3412
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean" || value === null || value === void 0) {
3413
+ if (key === "class" || key === "className") {
3414
+ el.setAttribute("class", value === false || value === null ? "" : String(value));
3415
+ } else if (key === "style" && typeof value === "string") {
3416
+ el.style.cssText = value;
3417
+ } else if (value === false || value === null || value === void 0) {
3418
+ el.removeAttribute(key);
3419
+ } else if (value === true) {
3420
+ el.setAttribute(key, "");
3421
+ } else {
3422
+ el.setAttribute(key, String(value));
3423
+ }
3424
+ }
3425
+ }
3426
+ if (typeof children === "string" || typeof children === "number" || children === null || children === void 0 || children === false) {
3427
+ el.textContent = children === null || children === void 0 || children === false ? "" : String(children);
3428
+ return true;
3429
+ }
3430
+ if (children && typeof children === "object" && !Array.isArray(children) && !(children instanceof Node)) {
3431
+ const childVNode = children;
3432
+ if (typeof childVNode.type === "string") {
3433
+ const childEl = el.querySelector(childVNode.type);
3434
+ if (childEl && patchElement(childEl, children)) {
3435
+ return true;
3436
+ }
3437
+ }
3438
+ }
3439
+ return false;
3440
+ }
3441
+ }
3442
+ if (output instanceof Node) {
3443
+ if (output.nodeType === Node.ELEMENT_NODE) {
3444
+ const nextEl = output;
3445
+ if (nextEl.tagName.toLowerCase() === el.tagName.toLowerCase()) {
3446
+ el.textContent = nextEl.textContent;
3447
+ return true;
3448
+ }
3449
+ } else if (output.nodeType === Node.TEXT_NODE) {
3450
+ el.textContent = output.data;
3451
+ return true;
3452
+ }
3453
+ }
3454
+ return false;
3455
+ }
3456
+ function patchNode(currentNode, nextOutput) {
3457
+ if (!currentNode) return false;
3458
+ if (currentNode instanceof Text && (nextOutput === null || nextOutput === void 0 || nextOutput === false || typeof nextOutput === "string" || typeof nextOutput === "number" || nextOutput instanceof Text)) {
3459
+ const nextText = nextOutput instanceof Text ? nextOutput.data : nextOutput === null || nextOutput === void 0 || nextOutput === false ? "" : String(nextOutput);
3460
+ currentNode.data = nextText;
3461
+ return true;
3462
+ }
3463
+ if (currentNode instanceof Element && patchElement(currentNode, nextOutput)) {
3464
+ return true;
3465
+ }
3466
+ if (nextOutput instanceof Node && currentNode === nextOutput) {
3467
+ return true;
3468
+ }
3469
+ return false;
3470
+ }
3471
+ function isFragmentVNode(value) {
3472
+ return value != null && typeof value === "object" && !(value instanceof Node) && value.type === Fragment;
3473
+ }
3474
+ function normalizeChildren(children, result = []) {
3475
+ if (children === void 0) {
3476
+ return result;
3477
+ }
3478
+ if (Array.isArray(children)) {
3479
+ for (const child of children) {
3480
+ normalizeChildren(child, result);
3481
+ }
3482
+ return result;
3483
+ }
3484
+ if (children === null || children === false) {
3485
+ return result;
3486
+ }
3487
+ result.push(children);
3488
+ return result;
3489
+ }
3490
+ function patchFragmentChildren(nodes, children) {
3491
+ const normalized = normalizeChildren(children);
3492
+ if (normalized.length !== nodes.length) {
3493
+ return false;
3494
+ }
3495
+ for (let i = 0; i < normalized.length; i++) {
3496
+ if (!patchNode(nodes[i], normalized[i])) {
3497
+ return false;
3498
+ }
3499
+ }
3500
+ return true;
3501
+ }
3502
+ function clearContent(block) {
3503
+ const nodes = block.nodes.slice(1, Math.max(1, block.nodes.length - 1));
3504
+ removeNodes(nodes);
3505
+ }
3506
+ function removeBlockNodes(block) {
3507
+ let cursor = block.start;
3508
+ const end = block.end;
3509
+ while (cursor) {
3510
+ const next = cursor.nextSibling;
3511
+ cursor.parentNode?.removeChild(cursor);
3512
+ if (cursor === end) break;
3513
+ cursor = next;
3514
+ }
3515
+ }
3516
+
3517
+ // src/scope.ts
3518
+ function createScope() {
3519
+ let dispose = null;
3520
+ const stop = () => {
3521
+ if (dispose) {
3522
+ dispose();
3523
+ dispose = null;
3410
3524
  }
3411
3525
  };
3526
+ const run = (fn) => {
3527
+ stop();
3528
+ const { dispose: rootDispose, value } = createRoot(fn);
3529
+ dispose = rootDispose;
3530
+ return value;
3531
+ };
3532
+ registerRootCleanup(stop);
3533
+ return { run, stop };
3534
+ }
3535
+ function runInScope(flag, fn) {
3536
+ const scope = createScope();
3537
+ const evaluate = () => isReactive(flag) ? flag() : !!flag;
3538
+ createEffect(() => {
3539
+ const enabled = evaluate();
3540
+ if (enabled) {
3541
+ scope.run(fn);
3542
+ } else {
3543
+ scope.stop();
3544
+ }
3545
+ });
3546
+ onCleanup(scope.stop);
3412
3547
  }
3413
3548
 
3414
3549
  exports.$state = $state;