@aevantec/litevue 0.5.7 → 0.5.9

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.
@@ -34,6 +34,7 @@ var LiteVue = (function(exports) {
34
34
  var toRawType = (value) => {
35
35
  return toTypeString(value).slice(8, -1);
36
36
  };
37
+ var isPlainObject = (val) => toTypeString(val) === "[object Object]";
37
38
  var isIntegerKey = (key) => isString(key) && key !== "NaN" && key[0] !== "-" && "" + parseInt(key, 10) === key;
38
39
  var cacheStringFunction = (fn) => {
39
40
  const cache = /* @__PURE__ */ Object.create(null);
@@ -616,41 +617,6 @@ getter: `, this.getter);
616
617
  return !!(r && r.__v_isRef === true);
617
618
  }
618
619
  //#endregion
619
- //#region src/magics.ts
620
- var uid = 0;
621
- /**
622
- * $id('name') — unique ids for accessibility attributes. Stable within a scope
623
- * so label/input pairs match, unique across scopes.
624
- */
625
- var createId = () => {
626
- const memo = Object.create(null);
627
- return (name = "id") => memo[name] || (memo[name] = `${name}-${++uid}`);
628
- };
629
- /**
630
- * $watch(source, callback) — watches a dot-path string or getter and calls
631
- * back with (value, oldValue). Tied to the owning scope's effects, so it stops
632
- * when the scope unmounts.
633
- */
634
- var createWatch = (ctx) => (source, cb) => {
635
- const getter = typeof source === "function" ? source : () => source.split(".").reduce((o, k) => o == null ? o : o[k], ctx.scope);
636
- let oldValue;
637
- let init = false;
638
- ctx.effect(() => {
639
- const value = getter();
640
- const previous = oldValue;
641
- oldValue = value;
642
- if (init && value !== previous) {
643
- pauseTracking();
644
- try {
645
- cb(value, previous);
646
- } finally {
647
- resetTracking();
648
- }
649
- }
650
- init = true;
651
- });
652
- };
653
- //#endregion
654
620
  //#region src/devtools.ts
655
621
  var scopes = /* @__PURE__ */ new Map();
656
622
  var exps = /* @__PURE__ */ new Map();
@@ -846,17 +812,27 @@ getter: `, this.getter);
846
812
  var queued = false;
847
813
  var queue = [];
848
814
  var p = Promise.resolve();
815
+ var flushIndex = -1;
816
+ var MAX_RUNS = 100;
849
817
  var nextTick = (fn) => p.then(fn);
850
818
  var queueJob = (job) => {
851
- if (!queue.includes(job)) queue.push(job);
819
+ if (!queue.includes(job, flushIndex + 1)) queue.push(job);
852
820
  if (!queued) {
853
821
  queued = true;
854
822
  nextTick(flushJobs);
855
823
  }
856
824
  };
857
825
  var flushJobs = () => {
858
- for (const job of queue) {
826
+ const runs = /* @__PURE__ */ new Map();
827
+ for (flushIndex = 0; flushIndex < queue.length; flushIndex++) {
828
+ const job = queue[flushIndex];
859
829
  if (stopped.has(job)) continue;
830
+ const n = (runs.get(job) || 0) + 1;
831
+ runs.set(job, n);
832
+ if (n > MAX_RUNS) {
833
+ handleError(Error(`an effect re-ran ${MAX_RUNS} times in one update and was stopped. Two effects are probably each writing state the other reads; derive one from the other with a getter or computed() instead.`), { phase: "effect" });
834
+ continue;
835
+ }
860
836
  try {
861
837
  job();
862
838
  } catch (e) {
@@ -864,15 +840,52 @@ getter: `, this.getter);
864
840
  }
865
841
  }
866
842
  queue.length = 0;
843
+ flushIndex = -1;
867
844
  queued = false;
868
845
  emitFlush();
869
846
  };
870
847
  //#endregion
848
+ //#region src/magics.ts
849
+ var uid = 0;
850
+ /**
851
+ * $id('name') — unique ids for accessibility attributes. Stable within a scope
852
+ * so label/input pairs match, unique across scopes.
853
+ */
854
+ var createId = () => {
855
+ const memo = Object.create(null);
856
+ return (name = "id") => memo[name] || (memo[name] = `${name}-${++uid}`);
857
+ };
858
+ /**
859
+ * $watch(source, callback, { immediate }) — watches a dot-path string or getter
860
+ * and calls back with (value, oldValue). Returns a stop function; it also stops
861
+ * when the owning scope unmounts.
862
+ */
863
+ var createWatch = (ctx) => (source, cb, options) => {
864
+ const getter = typeof source === "function" ? source : () => source.split(".").reduce((o, k) => o == null ? o : o[k], ctx.scope);
865
+ let oldValue;
866
+ let init = false;
867
+ const runner = ctx.effect(() => {
868
+ const value = getter();
869
+ const previous = oldValue;
870
+ oldValue = value;
871
+ if (init ? value !== previous : options?.immediate) {
872
+ pauseTracking();
873
+ try {
874
+ cb(value, previous);
875
+ } finally {
876
+ resetTracking();
877
+ }
878
+ }
879
+ init = true;
880
+ });
881
+ return () => runner.effect && stopEffect(runner);
882
+ };
883
+ //#endregion
871
884
  //#region src/directives/bind.ts
872
885
  var forceAttrRE = /^(spellcheck|draggable|form|list|type)$/;
873
886
  var bind = ({ el, get, effect, arg, modifiers }) => {
874
887
  let prevValue;
875
- if (arg === "class") el._class = el.className;
888
+ if (arg === "class") el._class = el.getAttribute("class");
876
889
  effect(() => {
877
890
  let value = get();
878
891
  if (arg) {
@@ -882,7 +895,7 @@ getter: `, this.getter);
882
895
  for (const key in value) setProp(el, key, value[key], prevValue && prevValue[key]);
883
896
  for (const key in prevValue) if (!value || !(key in value)) setProp(el, key, null);
884
897
  }
885
- prevValue = value;
898
+ prevValue = isPlainObject(value) ? { ...value } : value;
886
899
  });
887
900
  };
888
901
  var setProp = (el, key, value, prevValue) => {
@@ -900,7 +913,8 @@ getter: `, this.getter);
900
913
  }
901
914
  }
902
915
  } else if (!(el instanceof SVGElement) && key in el && !forceAttrRE.test(key)) {
903
- el[key] = value;
916
+ el[key] = value == null && typeof el[key] == "string" ? "" : value;
917
+ if (value == null) el.removeAttribute(key);
904
918
  if (key === "value") el._value = value;
905
919
  } else if (key === "true-value") el._trueValue = value;
906
920
  else if (key === "false-value") el._falseValue = value;
@@ -1099,7 +1113,7 @@ getter: `, this.getter);
1099
1113
  //#region src/directives/html.ts
1100
1114
  var html = ({ el, get, effect }) => {
1101
1115
  effect(() => {
1102
- el.innerHTML = get();
1116
+ el.innerHTML = get() ?? "";
1103
1117
  });
1104
1118
  };
1105
1119
  //#endregion
@@ -1135,13 +1149,12 @@ getter: `, this.getter);
1135
1149
  effect(() => {
1136
1150
  const value = get();
1137
1151
  const isMultiple = sel.multiple;
1152
+ if (isMultiple && !isArray(value)) warnOnce(`select-multiple:${exp}`, `<select multiple v-model="${exp}"> needs an array, but got ${value === null ? "null" : typeof value}, so no option is selected. Initialise it as [] — a change writes an array either way.`);
1138
1153
  for (let i = 0, l = sel.options.length; i < l; i++) {
1139
1154
  const option = sel.options[i];
1140
1155
  const optionValue = getValue(option);
1141
- if (isMultiple) {
1142
- if (isArray(value)) option.selected = looseIndexOf(value, optionValue) > -1;
1143
- else option.selected = value.has(optionValue);
1144
- } else if (looseEqual(getValue(option), value)) {
1156
+ if (isMultiple) option.selected = isArray(value) && looseIndexOf(value, optionValue) > -1;
1157
+ else if (looseEqual(getValue(option), value)) {
1145
1158
  if (sel.selectedIndex !== i) sel.selectedIndex = i;
1146
1159
  return;
1147
1160
  }
@@ -1178,9 +1191,8 @@ getter: `, this.getter);
1178
1191
  });
1179
1192
  } else {
1180
1193
  const resolveValue = (val) => {
1181
- if (trim) return val.trim();
1182
- if (number) return toNumber(val);
1183
- return val;
1194
+ if (trim) val = val.trim();
1195
+ return number ? toNumber(val) : val;
1184
1196
  };
1185
1197
  let write = assign;
1186
1198
  let debounceTimer;
@@ -1234,13 +1246,13 @@ getter: `, this.getter);
1234
1246
  }
1235
1247
  };
1236
1248
  var trigger = (el, type) => {
1237
- const e = document.createEvent("HTMLEvents");
1238
- e.initEvent(type, true, true);
1239
- el.dispatchEvent(e);
1249
+ el.dispatchEvent(new Event(type, {
1250
+ bubbles: true,
1251
+ cancelable: true
1252
+ }));
1240
1253
  };
1241
- //#endregion
1242
- //#region src/eval.ts
1243
1254
  var evalCache = Object.create(null);
1255
+ var cached = 0;
1244
1256
  var mkDispatch = (el) => (event, detail) => el.dispatchEvent(new CustomEvent(event, {
1245
1257
  detail,
1246
1258
  bubbles: true
@@ -1248,7 +1260,14 @@ getter: `, this.getter);
1248
1260
  var unwrap = (exp) => exp.replace(/^return\(([^]*)\)$/, "$1");
1249
1261
  var evaluate = (scope, exp, el, meta) => execute(scope, `return(${exp})`, el, meta);
1250
1262
  var execute = (scope, exp, el, meta) => {
1251
- const fn = evalCache[exp] || (evalCache[exp] = toFunction(exp, el, meta));
1263
+ let fn = evalCache[exp];
1264
+ if (!fn) {
1265
+ if (++cached > 1e3) {
1266
+ evalCache = Object.create(null);
1267
+ cached = 1;
1268
+ }
1269
+ fn = evalCache[exp] = toFunction(exp, el, meta);
1270
+ }
1252
1271
  try {
1253
1272
  return fn(scope, el, el && mkDispatch(el));
1254
1273
  } catch (e) {
@@ -1352,11 +1371,10 @@ getter: `, this.getter);
1352
1371
  //#region src/directives/if.ts
1353
1372
  var _if = (el, exp, ctx) => {
1354
1373
  if (!exp.trim()) console.warn(`v-if expression cannot be empty.`);
1355
- const parent = el.parentElement;
1356
1374
  const anchor = new Comment("v-if");
1357
- parent.insertBefore(anchor, el);
1375
+ el.before(anchor);
1358
1376
  const branches = [{
1359
- exp,
1377
+ exp: exp.trim() || "0",
1360
1378
  el
1361
1379
  }];
1362
1380
  let elseEl;
@@ -1364,7 +1382,7 @@ getter: `, this.getter);
1364
1382
  while (elseEl = el.nextElementSibling) {
1365
1383
  elseExp = null;
1366
1384
  if (checkAttr(elseEl, "v-else") === "" || (elseExp = checkAttr(elseEl, "v-else-if"))) {
1367
- parent.removeChild(elseEl);
1385
+ elseEl.remove();
1368
1386
  branches.push({
1369
1387
  exp: elseExp,
1370
1388
  el: elseEl
@@ -1372,13 +1390,13 @@ getter: `, this.getter);
1372
1390
  } else break;
1373
1391
  }
1374
1392
  const nextNode = el.nextSibling;
1375
- parent.removeChild(el);
1393
+ el.remove();
1376
1394
  setOwner(anchor);
1377
1395
  let block;
1378
1396
  let activeBranchIndex = -1;
1379
1397
  const removeActiveBlock = () => {
1380
1398
  if (block) {
1381
- if (block.el.parentNode === parent) parent.insertBefore(anchor, block.el);
1399
+ if (!anchor.parentNode) block.el.before(anchor);
1382
1400
  block.remove();
1383
1401
  block = void 0;
1384
1402
  }
@@ -1394,10 +1412,11 @@ getter: `, this.getter);
1394
1412
  })) {
1395
1413
  if (i !== activeBranchIndex) {
1396
1414
  removeActiveBlock();
1415
+ const parent = anchor.parentNode;
1397
1416
  block = new Block(el, ctx);
1398
1417
  block.insert(parent, anchor);
1399
1418
  own(stopBranches, block.el);
1400
- if (block.el.parentNode === parent) parent.removeChild(anchor);
1419
+ if (block.el.parentNode === parent) anchor.remove();
1401
1420
  activeBranchIndex = i;
1402
1421
  }
1403
1422
  return;
@@ -1422,10 +1441,9 @@ getter: `, this.getter);
1422
1441
  return;
1423
1442
  }
1424
1443
  const nextNode = el.nextSibling;
1425
- const parent = el.parentElement;
1426
1444
  const anchor = new Text("");
1427
- parent.insertBefore(anchor, el);
1428
- parent.removeChild(el);
1445
+ el.replaceWith(anchor);
1446
+ const parent = () => anchor.parentNode;
1429
1447
  setOwner(anchor);
1430
1448
  const sourceExp = inMatch[2].trim();
1431
1449
  let valueExp = inMatch[1].trim().replace(stripParensRE, "").trim();
@@ -1484,7 +1502,7 @@ getter: `, this.getter);
1484
1502
  const mountBlock = (ctx, ref) => {
1485
1503
  const block = new Block(el, ctx);
1486
1504
  block.key = ctx.key;
1487
- block.insert(parent, ref);
1505
+ block.insert(parent(), ref);
1488
1506
  return block;
1489
1507
  };
1490
1508
  let prevItems;
@@ -1535,7 +1553,7 @@ getter: `, this.getter);
1535
1553
  if (oldIndex !== i) {
1536
1554
  if (blocks[oldIndex + 1] !== nextBlock || prevMovedBlock === nextBlock) {
1537
1555
  prevMovedBlock = block;
1538
- block.insert(parent, nextBlock ? nextBlock.el : anchor);
1556
+ block.insert(parent(), nextBlock ? nextBlock.el : anchor);
1539
1557
  }
1540
1558
  }
1541
1559
  }
@@ -1554,7 +1572,7 @@ getter: `, this.getter);
1554
1572
  const ref = get();
1555
1573
  {
1556
1574
  const existing = $refs[ref];
1557
- if (existing && existing !== el && existing.isConnected) warnOnce(`ref-dup:${ref}`, `two elements claim ref="${ref}" in the same scope; $refs.${ref} points at the last one mounted and the earlier element is no longer reachable. Give each element its own name, or key off the item inside v-for rather than using a ref.`);
1575
+ if (existing && existing !== el && existing.isConnected) warnOnce(`ref-dup:${ref}`, `two elements claim ref="${ref}" in the same scope; $refs.${ref} points at the last one mounted and the earlier element is no longer reachable. Give each element its own name.`);
1558
1576
  }
1559
1577
  $refs[ref] = el;
1560
1578
  if (prevRef && ref !== prevRef) delete $refs[prevRef];
@@ -1565,6 +1583,72 @@ getter: `, this.getter);
1565
1583
  };
1566
1584
  };
1567
1585
  //#endregion
1586
+ //#region src/lazy.ts
1587
+ var LAZY_MODES = [
1588
+ "visible",
1589
+ "idle",
1590
+ "interaction"
1591
+ ];
1592
+ /** Selector matching every scope root, deferred or not. */
1593
+ var SCOPE_SELECTOR = ["[v-scope]", ...LAZY_MODES.map((m) => `[v-scope\\.${m}]`)].join(",");
1594
+ var INTERACTIONS = [
1595
+ "pointerdown",
1596
+ "keydown",
1597
+ "focusin"
1598
+ ];
1599
+ /**
1600
+ * Arms `mode` and returns a disposer. Fires at most once; disposing first
1601
+ * cancels it, since a region can be unmounted while still waiting.
1602
+ */
1603
+ var arm = (el, mode, fire) => {
1604
+ let done = false;
1605
+ const once = () => {
1606
+ if (done) return;
1607
+ done = true;
1608
+ stop();
1609
+ fire();
1610
+ };
1611
+ let stop = () => {};
1612
+ if (mode === "visible" && typeof IntersectionObserver == "function") {
1613
+ const io = new IntersectionObserver((entries) => {
1614
+ if (entries.some((e) => e.isIntersecting)) once();
1615
+ });
1616
+ io.observe(el);
1617
+ stop = () => io.disconnect();
1618
+ } else if (mode !== "interaction") {
1619
+ const ric = window.requestIdleCallback;
1620
+ const id = ric ? ric(once) : setTimeout(once);
1621
+ stop = () => ric ? window.cancelIdleCallback(id) : clearTimeout(id);
1622
+ } else {
1623
+ for (const type of INTERACTIONS) el.addEventListener(type, once);
1624
+ stop = () => {
1625
+ for (const type of INTERACTIONS) el.removeEventListener(type, once);
1626
+ };
1627
+ }
1628
+ return () => {
1629
+ done = true;
1630
+ stop();
1631
+ };
1632
+ };
1633
+ /**
1634
+ * Returns true if `el` was deferred. The walk must not descend into it:
1635
+ * walking consumes directives, so it could not be walked again later.
1636
+ */
1637
+ var deferScope = (el, ctx, walk, once) => {
1638
+ const mode = LAZY_MODES.find((m) => el.hasAttribute(`v-scope.${m}`));
1639
+ if (!mode) return false;
1640
+ const attr = `v-scope.${mode}`;
1641
+ const dispose = arm(el, mode, () => {
1642
+ if (!el.hasAttribute(attr)) return;
1643
+ el.setAttribute("v-scope", el.getAttribute(attr));
1644
+ el.removeAttribute(attr);
1645
+ walk(el, ctx, once);
1646
+ });
1647
+ ctx.cleanups.push(dispose);
1648
+ own(dispose);
1649
+ return true;
1650
+ };
1651
+ //#endregion
1568
1652
  //#region src/walk.ts
1569
1653
  var dirRE = /^(?:v-|:|@)/;
1570
1654
  var modifierRE = /\.([\w-]+)/g;
@@ -1589,6 +1673,15 @@ getter: `, this.getter);
1589
1673
  }
1590
1674
  });
1591
1675
  };
1676
+ var walkDeferred = (node, ctx, once) => {
1677
+ const previous = inOnce;
1678
+ inOnce = once;
1679
+ try {
1680
+ walk(node, ctx);
1681
+ } finally {
1682
+ inOnce = previous;
1683
+ }
1684
+ };
1592
1685
  var walk = (node, ctx) => {
1593
1686
  const previousOwner = setOwner(node);
1594
1687
  try {
@@ -1602,9 +1695,10 @@ getter: `, this.getter);
1602
1695
  if (type === 1) {
1603
1696
  const el = node;
1604
1697
  if (el.hasAttribute("v-pre")) return;
1698
+ if (deferScope(el, ctx, walkDeferred, inOnce)) return;
1605
1699
  checkAttr(el, "v-cloak");
1606
1700
  let exp;
1607
- if (exp = checkAttr(el, "v-if")) return _if(el, exp, ctx);
1701
+ if ((exp = checkAttr(el, "v-if")) != null) return _if(el, exp, ctx);
1608
1702
  if (exp = checkAttr(el, "v-for")) return _for(el, exp, ctx);
1609
1703
  const name = checkAttr(el, "v-name");
1610
1704
  let teleportNext;
@@ -1801,7 +1895,10 @@ getter: `, this.getter);
1801
1895
  return scopedCtx;
1802
1896
  };
1803
1897
  var bindContextMethods = (scope) => {
1804
- for (const key of Object.keys(scope)) if (typeof scope[key] === "function") scope[key] = scope[key].bind(scope);
1898
+ for (const key of Object.keys(scope)) {
1899
+ const fn = Object.getOwnPropertyDescriptor(scope, key).value;
1900
+ if (typeof fn === "function") scope[key] = fn.bind(scope);
1901
+ }
1805
1902
  };
1806
1903
  //#endregion
1807
1904
  //#region src/block.ts
@@ -1872,8 +1969,15 @@ getter: `, this.getter);
1872
1969
  }
1873
1970
  this.teardown();
1874
1971
  };
1875
- const leave = this.isFragment ? void 0 : this.template.__leave;
1876
- if (leave) leave().then(removeNow);
1972
+ const leaves = [];
1973
+ let node = this.start || this.template;
1974
+ while (node) {
1975
+ const leave = node.__leave;
1976
+ if (leave) leaves.push(leave());
1977
+ if (!this.start || node === this.end) break;
1978
+ node = node.nextSibling;
1979
+ }
1980
+ if (leaves.length) Promise.all(leaves).then(removeNow);
1877
1981
  else removeNow();
1878
1982
  }
1879
1983
  /**
@@ -2003,16 +2107,17 @@ getter: `, this.getter);
2003
2107
  },
2004
2108
  mount(el) {
2005
2109
  if (typeof el === "string") {
2006
- el = document.querySelector(el);
2007
- if (!el) {
2008
- console.error(`selector ${el} has no matching element.`);
2110
+ const target = document.querySelector(el);
2111
+ if (!target) {
2112
+ console.error(`mount: selector ${el} has no matching element.`);
2009
2113
  return;
2010
2114
  }
2115
+ el = target;
2011
2116
  }
2012
2117
  el = el || document.documentElement;
2013
2118
  let roots;
2014
- if (el.hasAttribute("v-scope")) roots = [el];
2015
- else roots = [...el.querySelectorAll(`[v-scope]`)].filter((root) => !root.matches(`[v-scope] [v-scope]`));
2119
+ if (el.matches(SCOPE_SELECTOR)) roots = [el];
2120
+ else roots = [...el.querySelectorAll(SCOPE_SELECTOR)].filter((root) => !root.matches(`:where(${SCOPE_SELECTOR}) *`));
2016
2121
  if (!roots.length) roots = [el];
2017
2122
  if (roots.length === 1 && roots[0] === document.documentElement) console.warn("Mounting on documentElement - this is non-optimal as LiteVue will be forced to crawl the entire page's DOM. Consider explicitly marking elements controlled by LiteVue with `v-scope`.");
2018
2123
  ctx.walk ??= walk;
@@ -2023,7 +2128,7 @@ getter: `, this.getter);
2023
2128
  const name = el.getAttribute("v-name");
2024
2129
  const block = new Block(el, ctx, true);
2025
2130
  el.__ctx ??= block.ctx;
2026
- if (!devtools.scopes.has(el)) block.ctx.cleanups.push(registerScope(el, block.ctx.scope, void 0, name || void 0));
2131
+ if (!devtools.scopes.has(el) && !el.matches(SCOPE_SELECTOR)) block.ctx.cleanups.push(registerScope(el, block.ctx.scope, void 0, name || void 0));
2027
2132
  rootBlocks.push(block);
2028
2133
  }
2029
2134
  return this;