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