@estjs/template 0.0.17-beta.1 → 0.0.17-beta.2

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.
@@ -13,6 +13,14 @@ var SUSPENSE_COMPONENT = /* @__PURE__ */ Symbol("Suspense Component" );
13
13
  var FOR_COMPONENT = /* @__PURE__ */ Symbol("For Component" );
14
14
  var TRANSITION_COMPONENT = /* @__PURE__ */ Symbol("Transition Component" );
15
15
  var TRANSITION_GROUP_COMPONENT = /* @__PURE__ */ Symbol("TransitionGroup Component" );
16
+ var DANGEROUS_DATA_MIME_RE = /^data:\s*(?:text\/html|text\/xml|application\/xhtml\+xml|image\/svg\+xml|application\/xml)/;
17
+ function isUnsafeUrl(value) {
18
+ const v = value.replaceAll(/[\u0000-\u0020]+/g, "").toLowerCase();
19
+ if (startsWith(v, "javascript:") || startsWith(v, "vbscript:")) {
20
+ return true;
21
+ }
22
+ return DANGEROUS_DATA_MIME_RE.test(v);
23
+ }
16
24
  function patchAttr(el, key, prev, next2) {
17
25
  if (key === KEY_PROP) {
18
26
  if (next2 == null) {
@@ -94,11 +102,8 @@ function patchAttr(el, key, prev, next2) {
94
102
  }
95
103
  const attrValue = isSymbol(next2) ? String(next2) : next2;
96
104
  const isUrlAttr = lowerKey === "href" || lowerKey === "src" || lowerKey === "xlink:href" || lowerKey === "action" || lowerKey === "formaction" || lowerKey === "poster";
97
- if (isUrlAttr && isString(attrValue)) {
98
- const v = attrValue.trim().toLowerCase();
99
- if (startsWith(v, "javascript:") || startsWith(v, "data:")) {
100
- return;
101
- }
105
+ if (isUrlAttr && isString(attrValue) && isUnsafeUrl(attrValue)) {
106
+ return;
102
107
  }
103
108
  if (isXlink) {
104
109
  el.setAttributeNS(XLINK_NAMESPACE, key, String(attrValue));
@@ -1035,7 +1040,13 @@ function createApp(component, target) {
1035
1040
  const existingContent = container.innerHTML;
1036
1041
  if (existingContent) {
1037
1042
  {
1038
- warn(`Target element is not empty, it will be cleared: ${target}`);
1043
+ if (container.querySelector("[data-hk]")) {
1044
+ warn(
1045
+ `Target element contains server-rendered markup ([data-hk]); createApp() will discard it and render from scratch. Did you mean to call hydrate()?`
1046
+ );
1047
+ } else {
1048
+ warn(`Target element is not empty, it will be cleared: ${target}`);
1049
+ }
1039
1050
  }
1040
1051
  container.innerHTML = "";
1041
1052
  }
@@ -1540,11 +1551,6 @@ function tryHydratePortal(props) {
1540
1551
  function isPortal(node) {
1541
1552
  return !!node && !!node[PORTAL_COMPONENT];
1542
1553
  }
1543
- function clearContainer(el) {
1544
- while (el.firstChild) {
1545
- el.removeChild(el.firstChild);
1546
- }
1547
- }
1548
1554
  function resolveNodeValue(value) {
1549
1555
  let current = value;
1550
1556
  while (isFunction(current)) {
@@ -1553,6 +1559,9 @@ function resolveNodeValue(value) {
1553
1559
  if (isSignal(current) || isComputed(current)) {
1554
1560
  return resolveNodeValue(current.value);
1555
1561
  }
1562
+ if (Array.isArray(current)) {
1563
+ return current.map((item) => resolveNodeValue(item));
1564
+ }
1556
1565
  return current;
1557
1566
  }
1558
1567
  var SuspenseContext = /* @__PURE__ */ Symbol("SuspenseContext");
@@ -1561,129 +1570,127 @@ function Suspense(props) {
1561
1570
  if (!isBrowser()) {
1562
1571
  return (_a2 = props.fallback) != null ? _a2 : "";
1563
1572
  }
1564
- const container = document.createElement("div");
1565
- container.style.display = "contents";
1566
- let isMounted = true;
1567
- let pendingCount = 0;
1568
- let isShowingFallback = false;
1569
- let resolvedChildren = null;
1570
- const materializeChild = (value) => {
1571
- const current = resolveNodeValue(value);
1572
- if (isArray(current)) {
1573
- const nodes = [];
1574
- for (const item of current) {
1575
- const materialized = materializeChild(item);
1576
- if (isArray(materialized)) {
1577
- nodes.push(...materialized);
1578
- } else {
1579
- nodes.push(materialized);
1580
- }
1581
- }
1582
- return nodes;
1583
- }
1584
- return normalizeNode(current);
1573
+ const owner = getActiveScope();
1574
+ const start = document.createComment("suspense");
1575
+ const end = document.createComment("/suspense");
1576
+ const frag = document.createDocumentFragment();
1577
+ frag.append(start, end);
1578
+ let mounted = true;
1579
+ let pending = 0;
1580
+ let mounting = false;
1581
+ let contentScope = null;
1582
+ let parked = null;
1583
+ let fallbackScope = null;
1584
+ let resolved = null;
1585
+ const parkContent = () => {
1586
+ const off = document.createDocumentFragment();
1587
+ while (end.previousSibling && end.previousSibling !== start) {
1588
+ off.prepend(end.previousSibling);
1589
+ }
1590
+ parked = off;
1585
1591
  };
1586
- const insertMaterializedChild = (value) => {
1587
- const normalized = materializeChild(value);
1588
- const nodes = isArray(normalized) ? normalized : [normalized];
1589
- for (const node of nodes) {
1590
- if (node != null) {
1591
- insertNode(container, node);
1592
- }
1593
- }
1592
+ const restoreContent = () => {
1593
+ var _a3;
1594
+ if (!parked) return;
1595
+ const parent = (_a3 = end.parentNode) != null ? _a3 : frag;
1596
+ while (parked.firstChild) {
1597
+ parent.insertBefore(parked.firstChild, end);
1598
+ }
1599
+ parked = null;
1594
1600
  };
1595
- const renderFallbackContent = () => {
1596
- clearContainer(container);
1597
- if (props.fallback != null) {
1598
- insertMaterializedChild(props.fallback);
1599
- }
1601
+ const mountFallback = () => {
1602
+ if (props.fallback == null || fallbackScope) return;
1603
+ fallbackScope = createScope(owner);
1604
+ runWithScope(fallbackScope, () => {
1605
+ var _a3;
1606
+ insert((_a3 = end.parentNode) != null ? _a3 : frag, () => resolveNodeValue(props.fallback), end);
1607
+ });
1600
1608
  };
1601
- const showFallback = () => {
1602
- if (isShowingFallback) return;
1603
- isShowingFallback = true;
1604
- renderFallbackContent();
1609
+ const disposeFallback = () => {
1610
+ if (!fallbackScope) return;
1611
+ disposeScope(fallbackScope);
1612
+ fallbackScope = null;
1605
1613
  };
1606
- const showChildren = () => {
1607
- if (!isShowingFallback) return;
1608
- const hasContent = resolvedChildren || props.children != null && !isPromise(props.children);
1609
- if (!hasContent) {
1610
- return;
1611
- }
1612
- isShowingFallback = false;
1613
- clearContainer(container);
1614
- if (resolvedChildren) {
1615
- renderChildren(resolvedChildren);
1616
- } else if (props.children != null && !isPromise(props.children)) {
1617
- renderChildren(props.children);
1614
+ const mountContent = (children2) => {
1615
+ mounting = true;
1616
+ contentScope = createScope(owner);
1617
+ runWithScope(contentScope, () => {
1618
+ var _a3;
1619
+ insert((_a3 = end.parentNode) != null ? _a3 : frag, () => resolveNodeValue(children2), end);
1620
+ });
1621
+ mounting = false;
1622
+ if (pending && !parked) {
1623
+ parkContent();
1624
+ mountFallback();
1618
1625
  }
1619
1626
  };
1620
- const renderChildren = (children2) => {
1621
- if (isShowingFallback) return;
1622
- clearContainer(container);
1623
- if (children2 == null) return;
1624
- const childArray = isArray(children2) ? children2 : [children2];
1625
- for (const child2 of childArray) {
1626
- if (child2 != null) {
1627
- insertMaterializedChild(child2);
1627
+ const showFallback = () => {
1628
+ if (parked) return;
1629
+ if (mounting) return;
1630
+ parkContent();
1631
+ mountFallback();
1632
+ };
1633
+ const showContent = () => {
1634
+ if (!parked) return;
1635
+ const hasSyncChildren = props.children != null && !isPromise(props.children);
1636
+ if (contentScope == null && resolved == null && !hasSyncChildren) return;
1637
+ disposeFallback();
1638
+ restoreContent();
1639
+ if (!contentScope) {
1640
+ if (resolved) {
1641
+ mountContent(resolved);
1642
+ } else if (hasSyncChildren) {
1643
+ mountContent(props.children);
1628
1644
  }
1629
1645
  }
1630
- if (isShowingFallback) {
1631
- renderFallbackContent();
1632
- }
1633
1646
  };
1634
- const suspenseContext = {
1647
+ const settle = () => {
1648
+ if (!mounted) return;
1649
+ if (--pending === 0) showContent();
1650
+ };
1651
+ const ctx = {
1635
1652
  register: (promise) => {
1636
- pendingCount++;
1653
+ pending++;
1637
1654
  showFallback();
1638
- promise.then(() => {
1639
- if (!isMounted) return;
1640
- pendingCount--;
1641
- if (pendingCount === 0) {
1642
- showChildren();
1643
- }
1644
- }).catch((error4) => {
1645
- {
1646
- warn("[Suspense] Resource failed:", error4);
1647
- }
1648
- if (!isMounted) return;
1649
- pendingCount--;
1650
- if (pendingCount === 0) {
1651
- showChildren();
1652
- }
1655
+ promise.then(settle).catch((error4) => {
1656
+ warn("[Suspense] Resource failed:", error4);
1657
+ settle();
1653
1658
  });
1654
1659
  },
1655
1660
  increment: () => {
1656
- pendingCount++;
1661
+ pending++;
1657
1662
  showFallback();
1658
1663
  },
1659
1664
  decrement: () => {
1660
- pendingCount = Math.max(0, pendingCount - 1);
1661
- if (pendingCount === 0) {
1662
- showChildren();
1663
- }
1665
+ pending = Math.max(0, pending - 1);
1666
+ if (pending === 0) showContent();
1664
1667
  }
1665
1668
  };
1666
- provide(SuspenseContext, suspenseContext);
1669
+ provide(SuspenseContext, ctx);
1667
1670
  const children = props.children;
1668
1671
  if (isPromise(children)) {
1669
- children.then((resolved) => {
1670
- resolvedChildren = resolved;
1672
+ children.then((value) => {
1673
+ resolved = value;
1671
1674
  }).catch(() => {
1672
1675
  });
1673
- suspenseContext.register(children);
1676
+ ctx.register(children);
1674
1677
  } else if (children != null) {
1675
- renderChildren(children);
1676
- } else {
1678
+ mountContent(children);
1679
+ } else if (props.fallback != null) {
1677
1680
  showFallback();
1678
1681
  }
1679
- onDestroy(() => {
1680
- isMounted = false;
1681
- pendingCount = 0;
1682
- resolvedChildren = null;
1683
- clearContainer(container);
1684
- container.remove();
1682
+ onCleanup(() => {
1683
+ mounted = false;
1684
+ pending = 0;
1685
+ resolved = null;
1686
+ if (contentScope) disposeScope(contentScope);
1687
+ if (fallbackScope) disposeScope(fallbackScope);
1688
+ contentScope = fallbackScope = null;
1689
+ parked = null;
1690
+ start.remove();
1691
+ end.remove();
1685
1692
  });
1686
- return container;
1693
+ return frag;
1687
1694
  }
1688
1695
  Suspense[SUSPENSE_COMPONENT] = true;
1689
1696
  function isSuspense(node) {
@@ -1757,185 +1764,130 @@ function createResource(fetcher, options) {
1757
1764
  function resolveModule(mod) {
1758
1765
  return isFunction(mod) ? mod : mod.default;
1759
1766
  }
1760
- function renderInto(el, fn, props) {
1761
- const comp = new Component(fn, props);
1762
- comp.mount(el);
1763
- return comp;
1767
+ function defineServerAsyncComponent(loader, ssr) {
1768
+ if (ssr === "client-only") {
1769
+ const placeholder = () => "";
1770
+ placeholder.__asyncLoader = loader;
1771
+ placeholder.__asyncResolved = () => null;
1772
+ return placeholder;
1773
+ }
1774
+ let resolved = null;
1775
+ let promise = null;
1776
+ const load = () => promise != null ? promise : promise = loader().then((mod) => {
1777
+ resolved = resolveModule(mod);
1778
+ }).catch(() => {
1779
+ });
1780
+ load();
1781
+ const wrapper = (props) => resolved ? resolved(props) : "";
1782
+ wrapper.__asyncLoader = load;
1783
+ wrapper.__asyncResolved = () => resolved;
1784
+ return wrapper;
1764
1785
  }
1765
- function defineAsyncComponent(loader, options = {}) {
1766
- const { delay = 200, timeout, ssr = "blocking", onError } = options;
1767
- if (typeof window === "undefined") {
1768
- if (ssr === "client-only") {
1769
- const placeholder = () => "";
1770
- placeholder.__asyncLoader = loader;
1771
- placeholder.__asyncResolved = () => null;
1772
- return placeholder;
1773
- }
1774
- let ssrResolved = null;
1775
- let ssrPromise = null;
1776
- const ssrLoad = () => {
1777
- if (ssrPromise) return ssrPromise;
1778
- ssrPromise = loader().then((mod) => {
1779
- ssrResolved = resolveModule(mod);
1780
- }).catch(() => {
1781
- });
1782
- return ssrPromise;
1783
- };
1784
- ssrLoad();
1785
- const ssrWrapper = (props) => {
1786
- if (ssrResolved) {
1787
- return ssrResolved(props);
1788
- }
1789
- return "";
1790
- };
1791
- ssrWrapper.__asyncLoader = ssrLoad;
1792
- ssrWrapper.__asyncResolved = () => ssrResolved;
1793
- return ssrWrapper;
1794
- }
1795
- let cachedComponent = null;
1796
- let cachedError = null;
1797
- let cachedStatus = "pending";
1786
+ function defineClientAsyncComponent(loader, options) {
1787
+ const { loading, error: errorComp, delay = 200, timeout, onError } = options;
1788
+ let component = null;
1789
+ let error4 = null;
1790
+ let status = "pending";
1798
1791
  let loadPromise = null;
1799
- function load() {
1800
- if (loadPromise) return loadPromise;
1801
- loadPromise = loader().then((mod) => {
1802
- cachedComponent = resolveModule(mod);
1803
- cachedStatus = "resolved";
1804
- }).catch((error4) => {
1805
- cachedError = error4 instanceof Error ? error4 : new Error(String(error4));
1806
- cachedStatus = "errored";
1807
- loadPromise = null;
1808
- });
1809
- return loadPromise;
1810
- }
1792
+ const load = () => loadPromise != null ? loadPromise : loadPromise = loader().then((mod) => {
1793
+ component = resolveModule(mod);
1794
+ status = "resolved";
1795
+ }).catch((error_) => {
1796
+ error4 = error_ instanceof Error ? error_ : new Error(String(error_));
1797
+ status = "errored";
1798
+ loadPromise = null;
1799
+ });
1811
1800
  load();
1812
1801
  function AsyncWrapper(props) {
1813
1802
  var _a2;
1814
- if (cachedStatus === "resolved" && cachedComponent) {
1815
- const el = document.createElement("div");
1816
- el.style.display = "contents";
1817
- const comp = renderInto(el, cachedComponent, props);
1818
- onCleanup(() => comp.destroy());
1819
- return el;
1820
- }
1821
- if (cachedStatus === "errored" && cachedError) {
1822
- const el = document.createElement("div");
1823
- el.style.display = "contents";
1824
- if (options.error) {
1825
- let alive2 = true;
1826
- let currentComp2 = null;
1827
- const swap2 = (fn, swapProps) => {
1828
- if (!alive2) return;
1829
- currentComp2 == null ? void 0 : currentComp2.destroy();
1830
- currentComp2 = renderInto(el, fn, swapProps);
1831
- };
1832
- const retry = () => {
1833
- loadPromise = null;
1834
- cachedStatus = "pending";
1835
- cachedError = null;
1836
- if (options.loading) swap2(options.loading);
1837
- load().then(() => {
1838
- if (!alive2) return;
1839
- if (cachedStatus === "resolved" && cachedComponent) {
1840
- swap2(cachedComponent, props);
1841
- } else if (cachedStatus === "errored" && cachedError) {
1842
- if (options.error) swap2(options.error, { error: cachedError, retry });
1843
- }
1844
- });
1845
- };
1846
- swap2(options.error, { error: cachedError, retry });
1847
- onDestroy(() => {
1848
- alive2 = false;
1849
- currentComp2 == null ? void 0 : currentComp2.destroy();
1850
- currentComp2 = null;
1851
- });
1852
- }
1853
- return el;
1854
- }
1855
- const container = document.createElement("div");
1856
- container.style.display = "contents";
1803
+ const owner = getActiveScope();
1804
+ const end = document.createComment("async");
1805
+ const frag = document.createDocumentFragment();
1806
+ frag.appendChild(end);
1857
1807
  let alive = true;
1858
- let currentComp = null;
1808
+ let viewScope = null;
1859
1809
  let delayTimer = null;
1860
1810
  let timeoutTimer = null;
1861
- const swap = (fn, swapProps) => {
1862
- if (!alive) return;
1863
- currentComp == null ? void 0 : currentComp.destroy();
1864
- currentComp = renderInto(container, fn, swapProps);
1811
+ const clearTimers = () => {
1812
+ if (delayTimer != null) clearTimeout(delayTimer);
1813
+ if (timeoutTimer != null) clearTimeout(timeoutTimer);
1814
+ delayTimer = timeoutTimer = null;
1865
1815
  };
1866
- const retryWith = (retryProps) => () => {
1867
- loadPromise = null;
1868
- cachedStatus = "pending";
1869
- cachedError = null;
1870
- if (options.loading) swap(options.loading);
1871
- load().then(() => {
1872
- if (cachedStatus === "resolved" && cachedComponent) {
1873
- swap(cachedComponent, retryProps);
1874
- } else if (cachedStatus === "errored" && cachedError) {
1875
- if (options.error) {
1876
- swap(options.error, {
1877
- error: cachedError,
1878
- retry: retryWith(retryProps)
1879
- });
1880
- }
1881
- }
1816
+ const render = (fn, fnProps) => {
1817
+ if (!alive) return;
1818
+ if (viewScope) {
1819
+ disposeScope(viewScope);
1820
+ viewScope = null;
1821
+ }
1822
+ if (!fn) return;
1823
+ viewScope = createScope(owner);
1824
+ runWithScope(viewScope, () => {
1825
+ var _a3;
1826
+ insert((_a3 = end.parentNode) != null ? _a3 : frag, () => new Component(fn, fnProps), end);
1882
1827
  });
1883
1828
  };
1884
1829
  onDestroy(() => {
1885
1830
  alive = false;
1886
- currentComp == null ? void 0 : currentComp.destroy();
1887
- currentComp = null;
1888
- if (delayTimer != null) clearTimeout(delayTimer);
1889
- if (timeoutTimer != null) clearTimeout(timeoutTimer);
1831
+ clearTimers();
1832
+ if (viewScope) {
1833
+ disposeScope(viewScope);
1834
+ viewScope = null;
1835
+ }
1836
+ end.remove();
1890
1837
  });
1891
- const suspenseCtx = (_a2 = inject(SuspenseContext)) != null ? _a2 : null;
1892
- const showResolved = (compFn) => swap(compFn, props);
1893
- const showError = (err) => {
1894
- if (options.error) swap(options.error, { error: err, retry: retryWith(props) });
1895
- };
1896
- const showLoading = () => {
1897
- if (options.loading) swap(options.loading);
1838
+ const retryWith = (retryProps) => () => {
1839
+ loadPromise = null;
1840
+ status = "pending";
1841
+ error4 = null;
1842
+ if (loading) render(loading);
1843
+ load().then(() => settle(retryProps));
1898
1844
  };
1899
- const instancePromise = load().then(() => {
1845
+ const settle = (renderProps) => {
1900
1846
  if (!alive) return;
1901
- if (cachedStatus === "resolved" && cachedComponent) {
1902
- showResolved(cachedComponent);
1903
- } else if (cachedStatus === "errored" && cachedError) {
1904
- showError(cachedError);
1905
- if (onError) onError(cachedError, retryWith(props));
1847
+ clearTimers();
1848
+ if (status === "resolved" && component) {
1849
+ render(component, renderProps);
1850
+ } else if (status === "errored" && error4) {
1851
+ if (errorComp) render(errorComp, { error: error4, retry: retryWith(renderProps) });
1852
+ if (onError) onError(error4, retryWith(renderProps));
1906
1853
  }
1907
- if (delayTimer != null) clearTimeout(delayTimer);
1908
- if (timeoutTimer != null) clearTimeout(timeoutTimer);
1909
- });
1910
- if (suspenseCtx) {
1911
- suspenseCtx.register(instancePromise);
1854
+ };
1855
+ if (status === "resolved" && component) {
1856
+ render(component, props);
1857
+ return frag;
1858
+ }
1859
+ if (status === "errored" && error4) {
1860
+ if (errorComp) render(errorComp, { error: error4, retry: retryWith(props) });
1861
+ return frag;
1912
1862
  }
1863
+ const pending = load().then(() => settle(props));
1864
+ (_a2 = inject(SuspenseContext)) == null ? void 0 : _a2.register(pending);
1913
1865
  if (delay > 0) {
1914
1866
  delayTimer = setTimeout(() => {
1915
- if (alive && cachedStatus === "pending") {
1916
- showLoading();
1917
- }
1867
+ if (alive && status === "pending" && loading) render(loading);
1918
1868
  }, delay);
1919
- } else if (options.loading) {
1920
- showLoading();
1869
+ } else if (loading) {
1870
+ render(loading);
1921
1871
  }
1922
1872
  if (timeout != null) {
1923
1873
  timeoutTimer = setTimeout(() => {
1924
- if (alive && cachedStatus === "pending") {
1925
- const err = new Error(`[defineAsyncComponent] Timeout after ${timeout}ms`);
1926
- cachedError = err;
1927
- cachedStatus = "errored";
1928
- showError(err);
1929
- if (onError) onError(err, retryWith(props));
1930
- }
1874
+ if (!alive || status !== "pending") return;
1875
+ error4 = new Error(`[defineAsyncComponent] Timeout after ${timeout}ms`);
1876
+ status = "errored";
1877
+ if (errorComp) render(errorComp, { error: error4, retry: retryWith(props) });
1878
+ if (onError) onError(error4, retryWith(props));
1931
1879
  }, timeout);
1932
1880
  }
1933
- return container;
1881
+ return frag;
1934
1882
  }
1935
1883
  AsyncWrapper.__asyncLoader = load;
1936
- AsyncWrapper.__asyncResolved = () => cachedComponent;
1884
+ AsyncWrapper.__asyncResolved = () => component;
1937
1885
  return AsyncWrapper;
1938
1886
  }
1887
+ function defineAsyncComponent(loader, options = {}) {
1888
+ var _a2;
1889
+ return typeof window === "undefined" ? defineServerAsyncComponent(loader, (_a2 = options.ssr) != null ? _a2 : "blocking") : defineClientAsyncComponent(loader, options);
1890
+ }
1939
1891
  function For(props) {
1940
1892
  const fragment = document.createDocumentFragment();
1941
1893
  const marker = document.createComment("");
@@ -1984,8 +1936,10 @@ function For(props) {
1984
1936
  }
1985
1937
  fallbackNodes = [];
1986
1938
  };
1939
+ const ownerScope = getActiveScope();
1987
1940
  const renderItem = (item, index, parent, before, key = getKey(item, index)) => {
1988
- const parentScope = getActiveScope();
1941
+ var _a2;
1942
+ const parentScope = (_a2 = getActiveScope()) != null ? _a2 : ownerScope;
1989
1943
  const scope = createScope(parentScope);
1990
1944
  let mountedNodes = [];
1991
1945
  runWithScope(scope, () => {