@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.
@@ -160,6 +160,14 @@ function onCleanup(fn) {
160
160
  }
161
161
  scope.cleanup.push(fn);
162
162
  }
163
+ var DANGEROUS_DATA_MIME_RE = /^data:\s*(?:text\/html|text\/xml|application\/xhtml\+xml|image\/svg\+xml|application\/xml)/;
164
+ function isUnsafeUrl(value) {
165
+ const v = value.replaceAll(/[\u0000-\u0020]+/g, "").toLowerCase();
166
+ if (shared.startsWith(v, "javascript:") || shared.startsWith(v, "vbscript:")) {
167
+ return true;
168
+ }
169
+ return DANGEROUS_DATA_MIME_RE.test(v);
170
+ }
163
171
  function patchAttr(el, key, prev, next2) {
164
172
  if (key === KEY_PROP) {
165
173
  if (next2 == null) {
@@ -241,11 +249,8 @@ function patchAttr(el, key, prev, next2) {
241
249
  }
242
250
  const attrValue = shared.isSymbol(next2) ? String(next2) : next2;
243
251
  const isUrlAttr = lowerKey === "href" || lowerKey === "src" || lowerKey === "xlink:href" || lowerKey === "action" || lowerKey === "formaction" || lowerKey === "poster";
244
- if (isUrlAttr && shared.isString(attrValue)) {
245
- const v = attrValue.trim().toLowerCase();
246
- if (shared.startsWith(v, "javascript:") || shared.startsWith(v, "data:")) {
247
- return;
248
- }
252
+ if (isUrlAttr && shared.isString(attrValue) && isUnsafeUrl(attrValue)) {
253
+ return;
249
254
  }
250
255
  if (isXlink) {
251
256
  el.setAttributeNS(XLINK_NAMESPACE, key, String(attrValue));
@@ -1182,7 +1187,13 @@ function createApp(component, target) {
1182
1187
  const existingContent = container.innerHTML;
1183
1188
  if (existingContent) {
1184
1189
  {
1185
- shared.warn(`Target element is not empty, it will be cleared: ${target}`);
1190
+ if (container.querySelector("[data-hk]")) {
1191
+ shared.warn(
1192
+ `Target element contains server-rendered markup ([data-hk]); createApp() will discard it and render from scratch. Did you mean to call hydrate()?`
1193
+ );
1194
+ } else {
1195
+ shared.warn(`Target element is not empty, it will be cleared: ${target}`);
1196
+ }
1186
1197
  }
1187
1198
  container.innerHTML = "";
1188
1199
  }
@@ -1687,11 +1698,6 @@ function tryHydratePortal(props) {
1687
1698
  function isPortal(node) {
1688
1699
  return !!node && !!node[PORTAL_COMPONENT];
1689
1700
  }
1690
- function clearContainer(el) {
1691
- while (el.firstChild) {
1692
- el.removeChild(el.firstChild);
1693
- }
1694
- }
1695
1701
  function resolveNodeValue(value) {
1696
1702
  let current = value;
1697
1703
  while (shared.isFunction(current)) {
@@ -1700,6 +1706,9 @@ function resolveNodeValue(value) {
1700
1706
  if (signals.isSignal(current) || signals.isComputed(current)) {
1701
1707
  return resolveNodeValue(current.value);
1702
1708
  }
1709
+ if (Array.isArray(current)) {
1710
+ return current.map((item) => resolveNodeValue(item));
1711
+ }
1703
1712
  return current;
1704
1713
  }
1705
1714
  var SuspenseContext = /* @__PURE__ */ Symbol("SuspenseContext");
@@ -1708,129 +1717,127 @@ function Suspense(props) {
1708
1717
  if (!shared.isBrowser()) {
1709
1718
  return (_a2 = props.fallback) != null ? _a2 : "";
1710
1719
  }
1711
- const container = document.createElement("div");
1712
- container.style.display = "contents";
1713
- let isMounted = true;
1714
- let pendingCount = 0;
1715
- let isShowingFallback = false;
1716
- let resolvedChildren = null;
1717
- const materializeChild = (value) => {
1718
- const current = resolveNodeValue(value);
1719
- if (shared.isArray(current)) {
1720
- const nodes = [];
1721
- for (const item of current) {
1722
- const materialized = materializeChild(item);
1723
- if (shared.isArray(materialized)) {
1724
- nodes.push(...materialized);
1725
- } else {
1726
- nodes.push(materialized);
1727
- }
1728
- }
1729
- return nodes;
1730
- }
1731
- return normalizeNode(current);
1720
+ const owner = getActiveScope();
1721
+ const start = document.createComment("suspense");
1722
+ const end = document.createComment("/suspense");
1723
+ const frag = document.createDocumentFragment();
1724
+ frag.append(start, end);
1725
+ let mounted = true;
1726
+ let pending = 0;
1727
+ let mounting = false;
1728
+ let contentScope = null;
1729
+ let parked = null;
1730
+ let fallbackScope = null;
1731
+ let resolved = null;
1732
+ const parkContent = () => {
1733
+ const off = document.createDocumentFragment();
1734
+ while (end.previousSibling && end.previousSibling !== start) {
1735
+ off.prepend(end.previousSibling);
1736
+ }
1737
+ parked = off;
1732
1738
  };
1733
- const insertMaterializedChild = (value) => {
1734
- const normalized = materializeChild(value);
1735
- const nodes = shared.isArray(normalized) ? normalized : [normalized];
1736
- for (const node of nodes) {
1737
- if (node != null) {
1738
- insertNode(container, node);
1739
- }
1740
- }
1739
+ const restoreContent = () => {
1740
+ var _a3;
1741
+ if (!parked) return;
1742
+ const parent = (_a3 = end.parentNode) != null ? _a3 : frag;
1743
+ while (parked.firstChild) {
1744
+ parent.insertBefore(parked.firstChild, end);
1745
+ }
1746
+ parked = null;
1741
1747
  };
1742
- const renderFallbackContent = () => {
1743
- clearContainer(container);
1744
- if (props.fallback != null) {
1745
- insertMaterializedChild(props.fallback);
1746
- }
1748
+ const mountFallback = () => {
1749
+ if (props.fallback == null || fallbackScope) return;
1750
+ fallbackScope = createScope(owner);
1751
+ runWithScope(fallbackScope, () => {
1752
+ var _a3;
1753
+ insert((_a3 = end.parentNode) != null ? _a3 : frag, () => resolveNodeValue(props.fallback), end);
1754
+ });
1747
1755
  };
1748
- const showFallback = () => {
1749
- if (isShowingFallback) return;
1750
- isShowingFallback = true;
1751
- renderFallbackContent();
1756
+ const disposeFallback = () => {
1757
+ if (!fallbackScope) return;
1758
+ disposeScope(fallbackScope);
1759
+ fallbackScope = null;
1752
1760
  };
1753
- const showChildren = () => {
1754
- if (!isShowingFallback) return;
1755
- const hasContent = resolvedChildren || props.children != null && !shared.isPromise(props.children);
1756
- if (!hasContent) {
1757
- return;
1758
- }
1759
- isShowingFallback = false;
1760
- clearContainer(container);
1761
- if (resolvedChildren) {
1762
- renderChildren(resolvedChildren);
1763
- } else if (props.children != null && !shared.isPromise(props.children)) {
1764
- renderChildren(props.children);
1761
+ const mountContent = (children2) => {
1762
+ mounting = true;
1763
+ contentScope = createScope(owner);
1764
+ runWithScope(contentScope, () => {
1765
+ var _a3;
1766
+ insert((_a3 = end.parentNode) != null ? _a3 : frag, () => resolveNodeValue(children2), end);
1767
+ });
1768
+ mounting = false;
1769
+ if (pending && !parked) {
1770
+ parkContent();
1771
+ mountFallback();
1765
1772
  }
1766
1773
  };
1767
- const renderChildren = (children2) => {
1768
- if (isShowingFallback) return;
1769
- clearContainer(container);
1770
- if (children2 == null) return;
1771
- const childArray = shared.isArray(children2) ? children2 : [children2];
1772
- for (const child2 of childArray) {
1773
- if (child2 != null) {
1774
- insertMaterializedChild(child2);
1774
+ const showFallback = () => {
1775
+ if (parked) return;
1776
+ if (mounting) return;
1777
+ parkContent();
1778
+ mountFallback();
1779
+ };
1780
+ const showContent = () => {
1781
+ if (!parked) return;
1782
+ const hasSyncChildren = props.children != null && !shared.isPromise(props.children);
1783
+ if (contentScope == null && resolved == null && !hasSyncChildren) return;
1784
+ disposeFallback();
1785
+ restoreContent();
1786
+ if (!contentScope) {
1787
+ if (resolved) {
1788
+ mountContent(resolved);
1789
+ } else if (hasSyncChildren) {
1790
+ mountContent(props.children);
1775
1791
  }
1776
1792
  }
1777
- if (isShowingFallback) {
1778
- renderFallbackContent();
1779
- }
1780
1793
  };
1781
- const suspenseContext = {
1794
+ const settle = () => {
1795
+ if (!mounted) return;
1796
+ if (--pending === 0) showContent();
1797
+ };
1798
+ const ctx = {
1782
1799
  register: (promise) => {
1783
- pendingCount++;
1800
+ pending++;
1784
1801
  showFallback();
1785
- promise.then(() => {
1786
- if (!isMounted) return;
1787
- pendingCount--;
1788
- if (pendingCount === 0) {
1789
- showChildren();
1790
- }
1791
- }).catch((error5) => {
1792
- {
1793
- shared.warn("[Suspense] Resource failed:", error5);
1794
- }
1795
- if (!isMounted) return;
1796
- pendingCount--;
1797
- if (pendingCount === 0) {
1798
- showChildren();
1799
- }
1802
+ promise.then(settle).catch((error5) => {
1803
+ shared.warn("[Suspense] Resource failed:", error5);
1804
+ settle();
1800
1805
  });
1801
1806
  },
1802
1807
  increment: () => {
1803
- pendingCount++;
1808
+ pending++;
1804
1809
  showFallback();
1805
1810
  },
1806
1811
  decrement: () => {
1807
- pendingCount = Math.max(0, pendingCount - 1);
1808
- if (pendingCount === 0) {
1809
- showChildren();
1810
- }
1812
+ pending = Math.max(0, pending - 1);
1813
+ if (pending === 0) showContent();
1811
1814
  }
1812
1815
  };
1813
- provide(SuspenseContext, suspenseContext);
1816
+ provide(SuspenseContext, ctx);
1814
1817
  const children = props.children;
1815
1818
  if (shared.isPromise(children)) {
1816
- children.then((resolved) => {
1817
- resolvedChildren = resolved;
1819
+ children.then((value) => {
1820
+ resolved = value;
1818
1821
  }).catch(() => {
1819
1822
  });
1820
- suspenseContext.register(children);
1823
+ ctx.register(children);
1821
1824
  } else if (children != null) {
1822
- renderChildren(children);
1823
- } else {
1825
+ mountContent(children);
1826
+ } else if (props.fallback != null) {
1824
1827
  showFallback();
1825
1828
  }
1826
- onDestroy(() => {
1827
- isMounted = false;
1828
- pendingCount = 0;
1829
- resolvedChildren = null;
1830
- clearContainer(container);
1831
- container.remove();
1829
+ onCleanup(() => {
1830
+ mounted = false;
1831
+ pending = 0;
1832
+ resolved = null;
1833
+ if (contentScope) disposeScope(contentScope);
1834
+ if (fallbackScope) disposeScope(fallbackScope);
1835
+ contentScope = fallbackScope = null;
1836
+ parked = null;
1837
+ start.remove();
1838
+ end.remove();
1832
1839
  });
1833
- return container;
1840
+ return frag;
1834
1841
  }
1835
1842
  Suspense[SUSPENSE_COMPONENT] = true;
1836
1843
  function isSuspense(node) {
@@ -1904,185 +1911,130 @@ function createResource(fetcher, options) {
1904
1911
  function resolveModule(mod) {
1905
1912
  return shared.isFunction(mod) ? mod : mod.default;
1906
1913
  }
1907
- function renderInto(el, fn, props) {
1908
- const comp = new Component(fn, props);
1909
- comp.mount(el);
1910
- return comp;
1914
+ function defineServerAsyncComponent(loader, ssr) {
1915
+ if (ssr === "client-only") {
1916
+ const placeholder = () => "";
1917
+ placeholder.__asyncLoader = loader;
1918
+ placeholder.__asyncResolved = () => null;
1919
+ return placeholder;
1920
+ }
1921
+ let resolved = null;
1922
+ let promise = null;
1923
+ const load = () => promise != null ? promise : promise = loader().then((mod) => {
1924
+ resolved = resolveModule(mod);
1925
+ }).catch(() => {
1926
+ });
1927
+ load();
1928
+ const wrapper = (props) => resolved ? resolved(props) : "";
1929
+ wrapper.__asyncLoader = load;
1930
+ wrapper.__asyncResolved = () => resolved;
1931
+ return wrapper;
1911
1932
  }
1912
- function defineAsyncComponent(loader, options = {}) {
1913
- const { delay = 200, timeout, ssr = "blocking", onError } = options;
1914
- if (typeof window === "undefined") {
1915
- if (ssr === "client-only") {
1916
- const placeholder = () => "";
1917
- placeholder.__asyncLoader = loader;
1918
- placeholder.__asyncResolved = () => null;
1919
- return placeholder;
1920
- }
1921
- let ssrResolved = null;
1922
- let ssrPromise = null;
1923
- const ssrLoad = () => {
1924
- if (ssrPromise) return ssrPromise;
1925
- ssrPromise = loader().then((mod) => {
1926
- ssrResolved = resolveModule(mod);
1927
- }).catch(() => {
1928
- });
1929
- return ssrPromise;
1930
- };
1931
- ssrLoad();
1932
- const ssrWrapper = (props) => {
1933
- if (ssrResolved) {
1934
- return ssrResolved(props);
1935
- }
1936
- return "";
1937
- };
1938
- ssrWrapper.__asyncLoader = ssrLoad;
1939
- ssrWrapper.__asyncResolved = () => ssrResolved;
1940
- return ssrWrapper;
1941
- }
1942
- let cachedComponent = null;
1943
- let cachedError = null;
1944
- let cachedStatus = "pending";
1933
+ function defineClientAsyncComponent(loader, options) {
1934
+ const { loading, error: errorComp, delay = 200, timeout, onError } = options;
1935
+ let component = null;
1936
+ let error5 = null;
1937
+ let status = "pending";
1945
1938
  let loadPromise = null;
1946
- function load() {
1947
- if (loadPromise) return loadPromise;
1948
- loadPromise = loader().then((mod) => {
1949
- cachedComponent = resolveModule(mod);
1950
- cachedStatus = "resolved";
1951
- }).catch((error5) => {
1952
- cachedError = error5 instanceof Error ? error5 : new Error(String(error5));
1953
- cachedStatus = "errored";
1954
- loadPromise = null;
1955
- });
1956
- return loadPromise;
1957
- }
1939
+ const load = () => loadPromise != null ? loadPromise : loadPromise = loader().then((mod) => {
1940
+ component = resolveModule(mod);
1941
+ status = "resolved";
1942
+ }).catch((error_) => {
1943
+ error5 = error_ instanceof Error ? error_ : new Error(String(error_));
1944
+ status = "errored";
1945
+ loadPromise = null;
1946
+ });
1958
1947
  load();
1959
1948
  function AsyncWrapper(props) {
1960
1949
  var _a2;
1961
- if (cachedStatus === "resolved" && cachedComponent) {
1962
- const el = document.createElement("div");
1963
- el.style.display = "contents";
1964
- const comp = renderInto(el, cachedComponent, props);
1965
- onCleanup(() => comp.destroy());
1966
- return el;
1967
- }
1968
- if (cachedStatus === "errored" && cachedError) {
1969
- const el = document.createElement("div");
1970
- el.style.display = "contents";
1971
- if (options.error) {
1972
- let alive2 = true;
1973
- let currentComp2 = null;
1974
- const swap2 = (fn, swapProps) => {
1975
- if (!alive2) return;
1976
- currentComp2 == null ? void 0 : currentComp2.destroy();
1977
- currentComp2 = renderInto(el, fn, swapProps);
1978
- };
1979
- const retry = () => {
1980
- loadPromise = null;
1981
- cachedStatus = "pending";
1982
- cachedError = null;
1983
- if (options.loading) swap2(options.loading);
1984
- load().then(() => {
1985
- if (!alive2) return;
1986
- if (cachedStatus === "resolved" && cachedComponent) {
1987
- swap2(cachedComponent, props);
1988
- } else if (cachedStatus === "errored" && cachedError) {
1989
- if (options.error) swap2(options.error, { error: cachedError, retry });
1990
- }
1991
- });
1992
- };
1993
- swap2(options.error, { error: cachedError, retry });
1994
- onDestroy(() => {
1995
- alive2 = false;
1996
- currentComp2 == null ? void 0 : currentComp2.destroy();
1997
- currentComp2 = null;
1998
- });
1999
- }
2000
- return el;
2001
- }
2002
- const container = document.createElement("div");
2003
- container.style.display = "contents";
1950
+ const owner = getActiveScope();
1951
+ const end = document.createComment("async");
1952
+ const frag = document.createDocumentFragment();
1953
+ frag.appendChild(end);
2004
1954
  let alive = true;
2005
- let currentComp = null;
1955
+ let viewScope = null;
2006
1956
  let delayTimer = null;
2007
1957
  let timeoutTimer = null;
2008
- const swap = (fn, swapProps) => {
2009
- if (!alive) return;
2010
- currentComp == null ? void 0 : currentComp.destroy();
2011
- currentComp = renderInto(container, fn, swapProps);
1958
+ const clearTimers = () => {
1959
+ if (delayTimer != null) clearTimeout(delayTimer);
1960
+ if (timeoutTimer != null) clearTimeout(timeoutTimer);
1961
+ delayTimer = timeoutTimer = null;
2012
1962
  };
2013
- const retryWith = (retryProps) => () => {
2014
- loadPromise = null;
2015
- cachedStatus = "pending";
2016
- cachedError = null;
2017
- if (options.loading) swap(options.loading);
2018
- load().then(() => {
2019
- if (cachedStatus === "resolved" && cachedComponent) {
2020
- swap(cachedComponent, retryProps);
2021
- } else if (cachedStatus === "errored" && cachedError) {
2022
- if (options.error) {
2023
- swap(options.error, {
2024
- error: cachedError,
2025
- retry: retryWith(retryProps)
2026
- });
2027
- }
2028
- }
1963
+ const render = (fn, fnProps) => {
1964
+ if (!alive) return;
1965
+ if (viewScope) {
1966
+ disposeScope(viewScope);
1967
+ viewScope = null;
1968
+ }
1969
+ if (!fn) return;
1970
+ viewScope = createScope(owner);
1971
+ runWithScope(viewScope, () => {
1972
+ var _a3;
1973
+ insert((_a3 = end.parentNode) != null ? _a3 : frag, () => new Component(fn, fnProps), end);
2029
1974
  });
2030
1975
  };
2031
1976
  onDestroy(() => {
2032
1977
  alive = false;
2033
- currentComp == null ? void 0 : currentComp.destroy();
2034
- currentComp = null;
2035
- if (delayTimer != null) clearTimeout(delayTimer);
2036
- if (timeoutTimer != null) clearTimeout(timeoutTimer);
1978
+ clearTimers();
1979
+ if (viewScope) {
1980
+ disposeScope(viewScope);
1981
+ viewScope = null;
1982
+ }
1983
+ end.remove();
2037
1984
  });
2038
- const suspenseCtx = (_a2 = inject(SuspenseContext)) != null ? _a2 : null;
2039
- const showResolved = (compFn) => swap(compFn, props);
2040
- const showError = (err) => {
2041
- if (options.error) swap(options.error, { error: err, retry: retryWith(props) });
2042
- };
2043
- const showLoading = () => {
2044
- if (options.loading) swap(options.loading);
1985
+ const retryWith = (retryProps) => () => {
1986
+ loadPromise = null;
1987
+ status = "pending";
1988
+ error5 = null;
1989
+ if (loading) render(loading);
1990
+ load().then(() => settle(retryProps));
2045
1991
  };
2046
- const instancePromise = load().then(() => {
1992
+ const settle = (renderProps) => {
2047
1993
  if (!alive) return;
2048
- if (cachedStatus === "resolved" && cachedComponent) {
2049
- showResolved(cachedComponent);
2050
- } else if (cachedStatus === "errored" && cachedError) {
2051
- showError(cachedError);
2052
- if (onError) onError(cachedError, retryWith(props));
1994
+ clearTimers();
1995
+ if (status === "resolved" && component) {
1996
+ render(component, renderProps);
1997
+ } else if (status === "errored" && error5) {
1998
+ if (errorComp) render(errorComp, { error: error5, retry: retryWith(renderProps) });
1999
+ if (onError) onError(error5, retryWith(renderProps));
2053
2000
  }
2054
- if (delayTimer != null) clearTimeout(delayTimer);
2055
- if (timeoutTimer != null) clearTimeout(timeoutTimer);
2056
- });
2057
- if (suspenseCtx) {
2058
- suspenseCtx.register(instancePromise);
2001
+ };
2002
+ if (status === "resolved" && component) {
2003
+ render(component, props);
2004
+ return frag;
2005
+ }
2006
+ if (status === "errored" && error5) {
2007
+ if (errorComp) render(errorComp, { error: error5, retry: retryWith(props) });
2008
+ return frag;
2059
2009
  }
2010
+ const pending = load().then(() => settle(props));
2011
+ (_a2 = inject(SuspenseContext)) == null ? void 0 : _a2.register(pending);
2060
2012
  if (delay > 0) {
2061
2013
  delayTimer = setTimeout(() => {
2062
- if (alive && cachedStatus === "pending") {
2063
- showLoading();
2064
- }
2014
+ if (alive && status === "pending" && loading) render(loading);
2065
2015
  }, delay);
2066
- } else if (options.loading) {
2067
- showLoading();
2016
+ } else if (loading) {
2017
+ render(loading);
2068
2018
  }
2069
2019
  if (timeout != null) {
2070
2020
  timeoutTimer = setTimeout(() => {
2071
- if (alive && cachedStatus === "pending") {
2072
- const err = new Error(`[defineAsyncComponent] Timeout after ${timeout}ms`);
2073
- cachedError = err;
2074
- cachedStatus = "errored";
2075
- showError(err);
2076
- if (onError) onError(err, retryWith(props));
2077
- }
2021
+ if (!alive || status !== "pending") return;
2022
+ error5 = new Error(`[defineAsyncComponent] Timeout after ${timeout}ms`);
2023
+ status = "errored";
2024
+ if (errorComp) render(errorComp, { error: error5, retry: retryWith(props) });
2025
+ if (onError) onError(error5, retryWith(props));
2078
2026
  }, timeout);
2079
2027
  }
2080
- return container;
2028
+ return frag;
2081
2029
  }
2082
2030
  AsyncWrapper.__asyncLoader = load;
2083
- AsyncWrapper.__asyncResolved = () => cachedComponent;
2031
+ AsyncWrapper.__asyncResolved = () => component;
2084
2032
  return AsyncWrapper;
2085
2033
  }
2034
+ function defineAsyncComponent(loader, options = {}) {
2035
+ var _a2;
2036
+ return typeof window === "undefined" ? defineServerAsyncComponent(loader, (_a2 = options.ssr) != null ? _a2 : "blocking") : defineClientAsyncComponent(loader, options);
2037
+ }
2086
2038
  function For(props) {
2087
2039
  const fragment = document.createDocumentFragment();
2088
2040
  const marker = document.createComment("");
@@ -2131,8 +2083,10 @@ function For(props) {
2131
2083
  }
2132
2084
  fallbackNodes = [];
2133
2085
  };
2086
+ const ownerScope = getActiveScope();
2134
2087
  const renderItem = (item, index, parent, before, key = getKey(item, index)) => {
2135
- const parentScope = getActiveScope();
2088
+ var _a2;
2089
+ const parentScope = (_a2 = getActiveScope()) != null ? _a2 : ownerScope;
2136
2090
  const scope = createScope(parentScope);
2137
2091
  let mountedNodes = [];
2138
2092
  runWithScope(scope, () => {