@estjs/template 0.0.14-beta.5 → 0.0.14-beta.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.
@@ -4,7 +4,7 @@ var shared = require('@estjs/shared');
4
4
  var signal = require('@estjs/signal');
5
5
 
6
6
  /**
7
- * @estjs/template v0.0.14-beta.5
7
+ * @estjs/template v0.0.14-beta.9
8
8
  * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
9
9
  * @license MIT
10
10
  **/
@@ -17,6 +17,7 @@ var CHILDREN_PROP = "children";
17
17
  var EMPTY_TEMPLATE = "";
18
18
  var FRAGMENT_PROP_KEY = "0";
19
19
  var SINGLE_PROP_KEY = "1";
20
+ var REF_KEY = "ref";
20
21
  var PLACEHOLDER = " __PLACEHOLDER__ ";
21
22
  var RenderContext = class {
22
23
  constructor() {
@@ -163,7 +164,7 @@ var SSGNode = class extends LifecycleContext {
163
164
  }
164
165
  normalizeProps(props) {
165
166
  Object.entries(props).forEach(([key, value]) => {
166
- if (key === "children") {
167
+ if (key === CHILDREN_PROP) {
167
168
  delete props[key];
168
169
  } else if (shared.isFunction(value)) {
169
170
  delete props[key];
@@ -173,7 +174,7 @@ var SSGNode = class extends LifecycleContext {
173
174
  });
174
175
  }
175
176
  generateAttributes(props) {
176
- return Object.entries(props).filter(([key, value]) => key !== "children" && !shared.isFunction(value)).map(([key, value]) => `${key}="${shared.escape(String(value))}"`).join(" ");
177
+ return Object.entries(props).filter(([key, value]) => key !== CHILDREN_PROP && !shared.isFunction(value)).map(([key, value]) => `${key}="${shared.escape(String(value))}"`).join(" ");
177
178
  }
178
179
  renderChildren(children, findIndex) {
179
180
  children.forEach(([child]) => {
@@ -198,9 +199,6 @@ var SSGNode = class extends LifecycleContext {
198
199
 
199
200
  // src/utils.ts
200
201
  var SELF_CLOSING_TAGS = "area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr".split(",");
201
- var HTML_TAGS = "a,abbr,acronym,address,applet,area,article,aside,audio,b,base,basefont,bdi,bdo,bgsound,big,blink,blockquote,body,br,button,canvas,caption,center,cite,code,col,colgroup,command,content,data,datalist,dd,del,details,dfn,dialog,dir,div,dl,dt,em,embed,fieldset,figcaption,figure,font,footer,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,image,img,input,ins,kbd,keygen,label,legend,li,link,listing,main,map,mark,marquee,menu,menuitem,meta,meter,nav,nobr,noframes,noscript,object,ol,optgroup,option,output,p,param,picture,plaintext,pre,progress,q,rb,rp,rt,rtc,ruby,s,samp,script,section,select,shadow,small,source,spacer,span,strike,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,tt,u,ul,var,video,wbr,xmp".split(
202
- ","
203
- );
204
202
  function coerceNode(data) {
205
203
  if (isJsxElement(data) || data instanceof Node || isSSGNode(data)) {
206
204
  return data;
@@ -317,7 +315,6 @@ function bindNode(node, setter) {
317
315
  });
318
316
  }
319
317
  }
320
- Promise.resolve();
321
318
  function addEventListener(node, eventName, handler) {
322
319
  node.addEventListener(eventName, handler);
323
320
  return () => node.removeEventListener(eventName, handler);
@@ -358,9 +355,6 @@ function closeHtmlTags(input) {
358
355
  }
359
356
  return output.join("");
360
357
  }
361
- function isHtmlTagName(tagName) {
362
- return HTML_TAGS.includes(tagName);
363
- }
364
358
  function convertToHtmlTag(tagName) {
365
359
  return SELF_CLOSING_TAGS.includes(tagName) ? `<${tagName}/>` : `<${tagName}></${tagName}>`;
366
360
  }
@@ -380,7 +374,7 @@ var ComponentNode = class extends LifecycleContext {
380
374
  this.emitter = /* @__PURE__ */ new Set();
381
375
  this.rootNode = null;
382
376
  this.trackMap = /* @__PURE__ */ new Map();
383
- this.key || (this.key = props == null ? void 0 : props.key);
377
+ this.key || (this.key = props && props.key);
384
378
  this.proxyProps = this.createProxyProps(props);
385
379
  }
386
380
  createProxyProps(props) {
@@ -407,7 +401,7 @@ var ComponentNode = class extends LifecycleContext {
407
401
  return (_b = (_a = this.rootNode) == null ? void 0 : _a.mount(parent, before)) != null ? _b : [];
408
402
  }
409
403
  this.initRef();
410
- this.rootNode = this.template(signal.useReactive(this.proxyProps, ["children"]));
404
+ this.rootNode = this.template(signal.useReactive(this.proxyProps, [CHILDREN_PROP]));
411
405
  const mountedNode = (_d = (_c = this.rootNode) == null ? void 0 : _c.mount(parent, before)) != null ? _d : [];
412
406
  this.callMountHooks();
413
407
  this.patchProps(this.props);
@@ -462,7 +456,7 @@ var ComponentNode = class extends LifecycleContext {
462
456
  for (const [key, prop] of Object.entries(props)) {
463
457
  if (shared.startsWith(key, EVENT_PREFIX) && ((_a = this.rootNode) == null ? void 0 : _a.firstChild)) {
464
458
  this.patchEventListener(key, prop);
465
- } else if (key === "ref") {
459
+ } else if (key === REF_KEY) {
466
460
  this.patchRef(prop);
467
461
  } else if (shared.startsWith(key, UPDATE_PREFIX)) {
468
462
  this.patchUpdateHandler(key, prop);
@@ -494,10 +488,6 @@ var ComponentNode = class extends LifecycleContext {
494
488
  }
495
489
  };
496
490
 
497
- // ../shared/src/comm.ts
498
- var _toString = Object.prototype.toString;
499
- var isPlainObject = (val) => _toString.call(val) === "[object Object]";
500
-
501
491
  // src/patch.ts
502
492
  function patchChildren(parent, childrenMap, nextChildren, before) {
503
493
  const result = /* @__PURE__ */ new Map();
@@ -618,7 +608,7 @@ var TemplateNode = class {
618
608
  this.trackMap = /* @__PURE__ */ new Map();
619
609
  this.bindValueKeys = [];
620
610
  this.parent = null;
621
- this.key || (this.key = props == null ? void 0 : props.key);
611
+ this.key || (this.key = props && props.key);
622
612
  if (renderContext.isSSR) {
623
613
  this.componentIndex = getComponentIndex(this.template);
624
614
  }
@@ -648,7 +638,7 @@ var TemplateNode = class {
648
638
  const firstChild = cloneNode.firstChild;
649
639
  if ((_a = firstChild == null ? void 0 : firstChild.hasAttribute) == null ? void 0 : _a.call(firstChild, "_svg_")) {
650
640
  firstChild.remove();
651
- firstChild == null ? void 0 : firstChild.childNodes.forEach((node) => {
641
+ firstChild.childNodes.forEach((node) => {
652
642
  cloneNode.append(node);
653
643
  });
654
644
  }
@@ -666,8 +656,7 @@ var TemplateNode = class {
666
656
  unmount() {
667
657
  var _a, _b;
668
658
  this.trackMap.forEach((track) => {
669
- var _a2;
670
- (_a2 = track.cleanup) == null ? void 0 : _a2.call(track);
659
+ track.cleanup && track.cleanup();
671
660
  });
672
661
  this.trackMap.clear();
673
662
  this.treeMap.clear();
@@ -688,14 +677,14 @@ var TemplateNode = class {
688
677
  this.mounted = false;
689
678
  }
690
679
  deleteFragmentTextNode(child) {
691
- var _a;
692
680
  if (shared.isPrimitive(child)) {
693
- (_a = this.parent) == null ? void 0 : _a.childNodes.forEach((node) => {
694
- var _a2;
695
- if (node.nodeType === Node.TEXT_NODE && node.textContent === `${child}`) {
696
- (_a2 = this.parent) == null ? void 0 : _a2.removeChild(node);
697
- }
698
- });
681
+ if (this.parent && this.parent.childNodes.length) {
682
+ this.parent.childNodes.forEach((node) => {
683
+ if (node.nodeType === Node.TEXT_NODE && node.textContent === `${child}`) {
684
+ this.parent.removeChild(node);
685
+ }
686
+ });
687
+ }
699
688
  } else {
700
689
  removeChild(child);
701
690
  }
@@ -765,9 +754,9 @@ var TemplateNode = class {
765
754
  Object.entries(props).forEach(([attr, value]) => {
766
755
  if (attr === CHILDREN_PROP && value) {
767
756
  this.patchChildren(key, node, value, isRoot);
768
- } else if (attr === "ref") {
757
+ } else if (attr === REF_KEY) {
769
758
  props[attr].value = node;
770
- } else if (shared.startsWith(attr, "on")) {
759
+ } else if (shared.startsWith(attr, EVENT_PREFIX)) {
771
760
  this.patchEventListener(key, node, attr, value);
772
761
  } else {
773
762
  if (this.bindValueKeys.includes(attr)) return;
@@ -777,8 +766,7 @@ var TemplateNode = class {
777
766
  });
778
767
  }
779
768
  getBindUpdateValue(props, key, attr) {
780
- const UPDATE_PREFIX2 = "update";
781
- const updateKey = `${UPDATE_PREFIX2}${shared.capitalize(attr)}`;
769
+ const updateKey = `${UPDATE_PREFIX}${shared.capitalize(attr)}`;
782
770
  if (updateKey && props[updateKey] && shared.isFunction(props[updateKey])) {
783
771
  this.bindValueKeys.push(updateKey);
784
772
  return props[updateKey];
@@ -812,7 +800,7 @@ var TemplateNode = class {
812
800
  setAttribute(element, attr, triggerValue.value);
813
801
  const cleanup = signal.useEffect(() => {
814
802
  const val2 = shared.isFunction(value) ? value() : value;
815
- if (isPlainObject(val2) && isPlainObject(triggerValue.peek()) && JSON.stringify(triggerValue.value) === JSON.stringify(val2))
803
+ if (shared.isPlainObject(val2) && shared.isPlainObject(triggerValue.peek()) && JSON.stringify(triggerValue.value) === JSON.stringify(val2))
816
804
  return;
817
805
  triggerValue.value = signal.isSignal(val2) ? val2.value : val2;
818
806
  setAttribute(element, attr, triggerValue.value);
@@ -829,7 +817,6 @@ var TemplateNode = class {
829
817
  };
830
818
  }
831
819
  getNodeTrack(trackKey, trackLastNodes, isRoot) {
832
- var _a;
833
820
  let track = this.trackMap.get(trackKey);
834
821
  if (!track) {
835
822
  track = { cleanup: () => {
@@ -842,7 +829,7 @@ var TemplateNode = class {
842
829
  }
843
830
  this.trackMap.set(trackKey, track);
844
831
  }
845
- (_a = track.cleanup) == null ? void 0 : _a.call(track);
832
+ track.cleanup && track.cleanup();
846
833
  return track;
847
834
  }
848
835
  patchChild(track, parent, child, before) {
@@ -896,16 +883,15 @@ var TemplateNode = class {
896
883
  // src/jsxRenderer.ts
897
884
  function h(template, props, key) {
898
885
  if (shared.isString(template)) {
899
- if (isHtmlTagName(template)) {
900
- const htmlTemplate = convertToHtmlTag(template);
901
- props = { [SINGLE_PROP_KEY]: props };
902
- return new TemplateNode(createTemplate(htmlTemplate), props, key);
903
- } else if (template === EMPTY_TEMPLATE) {
904
- props = { [FRAGMENT_PROP_KEY]: props };
905
- return new TemplateNode(createTemplate(EMPTY_TEMPLATE), props, key);
906
- }
886
+ const isEmptyTemplate = template === EMPTY_TEMPLATE;
887
+ const htmlTemplate = isEmptyTemplate ? EMPTY_TEMPLATE : convertToHtmlTag(template);
888
+ props = { [isEmptyTemplate ? FRAGMENT_PROP_KEY : SINGLE_PROP_KEY]: props };
889
+ return new TemplateNode(createTemplate(htmlTemplate), props, key);
890
+ }
891
+ if (shared.isFunction(template)) {
892
+ return new ComponentNode(template, props, key);
907
893
  }
908
- return shared.isFunction(template) ? new ComponentNode(template, props, key) : new TemplateNode(template, props, key);
894
+ return new TemplateNode(template, props, key);
909
895
  }
910
896
  function isComponent(node) {
911
897
  return node instanceof ComponentNode;
@@ -919,8 +905,10 @@ function createTemplate(html) {
919
905
  return template;
920
906
  }
921
907
  function Fragment(props) {
922
- return h(EMPTY_TEMPLATE, {
923
- children: shared.isArray(props.children) ? props.children.filter(Boolean) : [props.children]
908
+ return h(createTemplate(EMPTY_TEMPLATE), {
909
+ [FRAGMENT_PROP_KEY]: {
910
+ children: shared.isArray(props.children) ? props.children.filter(Boolean) : [props.children]
911
+ }
924
912
  });
925
913
  }
926
914
  function onMount(cb) {
@@ -1,8 +1,8 @@
1
- import { isString, isFunction, isArray, isSymbol, escape, startsWith, isPrimitive, capitalize, isNil, isHTMLElement, coerceArray, isFalsy, kebabCase } from '@estjs/shared';
1
+ import { isString, isFunction, isArray, isSymbol, escape, startsWith, isPrimitive, capitalize, isNil, isPlainObject, isHTMLElement, coerceArray, isFalsy, kebabCase } from '@estjs/shared';
2
2
  import { shallowSignal, isSignal, signalObject, useReactive, useSignal, useEffect } from '@estjs/signal';
3
3
 
4
4
  /**
5
- * @estjs/template v0.0.14-beta.5
5
+ * @estjs/template v0.0.14-beta.9
6
6
  * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
7
7
  * @license MIT
8
8
  **/
@@ -15,6 +15,7 @@ var CHILDREN_PROP = "children";
15
15
  var EMPTY_TEMPLATE = "";
16
16
  var FRAGMENT_PROP_KEY = "0";
17
17
  var SINGLE_PROP_KEY = "1";
18
+ var REF_KEY = "ref";
18
19
  var PLACEHOLDER = " __PLACEHOLDER__ ";
19
20
  var RenderContext = class {
20
21
  constructor() {
@@ -161,7 +162,7 @@ var SSGNode = class extends LifecycleContext {
161
162
  }
162
163
  normalizeProps(props) {
163
164
  Object.entries(props).forEach(([key, value]) => {
164
- if (key === "children") {
165
+ if (key === CHILDREN_PROP) {
165
166
  delete props[key];
166
167
  } else if (isFunction(value)) {
167
168
  delete props[key];
@@ -171,7 +172,7 @@ var SSGNode = class extends LifecycleContext {
171
172
  });
172
173
  }
173
174
  generateAttributes(props) {
174
- return Object.entries(props).filter(([key, value]) => key !== "children" && !isFunction(value)).map(([key, value]) => `${key}="${escape(String(value))}"`).join(" ");
175
+ return Object.entries(props).filter(([key, value]) => key !== CHILDREN_PROP && !isFunction(value)).map(([key, value]) => `${key}="${escape(String(value))}"`).join(" ");
175
176
  }
176
177
  renderChildren(children, findIndex) {
177
178
  children.forEach(([child]) => {
@@ -196,9 +197,6 @@ var SSGNode = class extends LifecycleContext {
196
197
 
197
198
  // src/utils.ts
198
199
  var SELF_CLOSING_TAGS = "area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr".split(",");
199
- var HTML_TAGS = "a,abbr,acronym,address,applet,area,article,aside,audio,b,base,basefont,bdi,bdo,bgsound,big,blink,blockquote,body,br,button,canvas,caption,center,cite,code,col,colgroup,command,content,data,datalist,dd,del,details,dfn,dialog,dir,div,dl,dt,em,embed,fieldset,figcaption,figure,font,footer,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,image,img,input,ins,kbd,keygen,label,legend,li,link,listing,main,map,mark,marquee,menu,menuitem,meta,meter,nav,nobr,noframes,noscript,object,ol,optgroup,option,output,p,param,picture,plaintext,pre,progress,q,rb,rp,rt,rtc,ruby,s,samp,script,section,select,shadow,small,source,spacer,span,strike,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,tt,u,ul,var,video,wbr,xmp".split(
200
- ","
201
- );
202
200
  function coerceNode(data) {
203
201
  if (isJsxElement(data) || data instanceof Node || isSSGNode(data)) {
204
202
  return data;
@@ -315,7 +313,6 @@ function bindNode(node, setter) {
315
313
  });
316
314
  }
317
315
  }
318
- Promise.resolve();
319
316
  function addEventListener(node, eventName, handler) {
320
317
  node.addEventListener(eventName, handler);
321
318
  return () => node.removeEventListener(eventName, handler);
@@ -356,9 +353,6 @@ function closeHtmlTags(input) {
356
353
  }
357
354
  return output.join("");
358
355
  }
359
- function isHtmlTagName(tagName) {
360
- return HTML_TAGS.includes(tagName);
361
- }
362
356
  function convertToHtmlTag(tagName) {
363
357
  return SELF_CLOSING_TAGS.includes(tagName) ? `<${tagName}/>` : `<${tagName}></${tagName}>`;
364
358
  }
@@ -378,7 +372,7 @@ var ComponentNode = class extends LifecycleContext {
378
372
  this.emitter = /* @__PURE__ */ new Set();
379
373
  this.rootNode = null;
380
374
  this.trackMap = /* @__PURE__ */ new Map();
381
- this.key || (this.key = props == null ? void 0 : props.key);
375
+ this.key || (this.key = props && props.key);
382
376
  this.proxyProps = this.createProxyProps(props);
383
377
  }
384
378
  createProxyProps(props) {
@@ -405,7 +399,7 @@ var ComponentNode = class extends LifecycleContext {
405
399
  return (_b = (_a = this.rootNode) == null ? void 0 : _a.mount(parent, before)) != null ? _b : [];
406
400
  }
407
401
  this.initRef();
408
- this.rootNode = this.template(useReactive(this.proxyProps, ["children"]));
402
+ this.rootNode = this.template(useReactive(this.proxyProps, [CHILDREN_PROP]));
409
403
  const mountedNode = (_d = (_c = this.rootNode) == null ? void 0 : _c.mount(parent, before)) != null ? _d : [];
410
404
  this.callMountHooks();
411
405
  this.patchProps(this.props);
@@ -460,7 +454,7 @@ var ComponentNode = class extends LifecycleContext {
460
454
  for (const [key, prop] of Object.entries(props)) {
461
455
  if (startsWith(key, EVENT_PREFIX) && ((_a = this.rootNode) == null ? void 0 : _a.firstChild)) {
462
456
  this.patchEventListener(key, prop);
463
- } else if (key === "ref") {
457
+ } else if (key === REF_KEY) {
464
458
  this.patchRef(prop);
465
459
  } else if (startsWith(key, UPDATE_PREFIX)) {
466
460
  this.patchUpdateHandler(key, prop);
@@ -492,10 +486,6 @@ var ComponentNode = class extends LifecycleContext {
492
486
  }
493
487
  };
494
488
 
495
- // ../shared/src/comm.ts
496
- var _toString = Object.prototype.toString;
497
- var isPlainObject = (val) => _toString.call(val) === "[object Object]";
498
-
499
489
  // src/patch.ts
500
490
  function patchChildren(parent, childrenMap, nextChildren, before) {
501
491
  const result = /* @__PURE__ */ new Map();
@@ -616,7 +606,7 @@ var TemplateNode = class {
616
606
  this.trackMap = /* @__PURE__ */ new Map();
617
607
  this.bindValueKeys = [];
618
608
  this.parent = null;
619
- this.key || (this.key = props == null ? void 0 : props.key);
609
+ this.key || (this.key = props && props.key);
620
610
  if (renderContext.isSSR) {
621
611
  this.componentIndex = getComponentIndex(this.template);
622
612
  }
@@ -646,7 +636,7 @@ var TemplateNode = class {
646
636
  const firstChild = cloneNode.firstChild;
647
637
  if ((_a = firstChild == null ? void 0 : firstChild.hasAttribute) == null ? void 0 : _a.call(firstChild, "_svg_")) {
648
638
  firstChild.remove();
649
- firstChild == null ? void 0 : firstChild.childNodes.forEach((node) => {
639
+ firstChild.childNodes.forEach((node) => {
650
640
  cloneNode.append(node);
651
641
  });
652
642
  }
@@ -664,8 +654,7 @@ var TemplateNode = class {
664
654
  unmount() {
665
655
  var _a, _b;
666
656
  this.trackMap.forEach((track) => {
667
- var _a2;
668
- (_a2 = track.cleanup) == null ? void 0 : _a2.call(track);
657
+ track.cleanup && track.cleanup();
669
658
  });
670
659
  this.trackMap.clear();
671
660
  this.treeMap.clear();
@@ -686,14 +675,14 @@ var TemplateNode = class {
686
675
  this.mounted = false;
687
676
  }
688
677
  deleteFragmentTextNode(child) {
689
- var _a;
690
678
  if (isPrimitive(child)) {
691
- (_a = this.parent) == null ? void 0 : _a.childNodes.forEach((node) => {
692
- var _a2;
693
- if (node.nodeType === Node.TEXT_NODE && node.textContent === `${child}`) {
694
- (_a2 = this.parent) == null ? void 0 : _a2.removeChild(node);
695
- }
696
- });
679
+ if (this.parent && this.parent.childNodes.length) {
680
+ this.parent.childNodes.forEach((node) => {
681
+ if (node.nodeType === Node.TEXT_NODE && node.textContent === `${child}`) {
682
+ this.parent.removeChild(node);
683
+ }
684
+ });
685
+ }
697
686
  } else {
698
687
  removeChild(child);
699
688
  }
@@ -763,9 +752,9 @@ var TemplateNode = class {
763
752
  Object.entries(props).forEach(([attr, value]) => {
764
753
  if (attr === CHILDREN_PROP && value) {
765
754
  this.patchChildren(key, node, value, isRoot);
766
- } else if (attr === "ref") {
755
+ } else if (attr === REF_KEY) {
767
756
  props[attr].value = node;
768
- } else if (startsWith(attr, "on")) {
757
+ } else if (startsWith(attr, EVENT_PREFIX)) {
769
758
  this.patchEventListener(key, node, attr, value);
770
759
  } else {
771
760
  if (this.bindValueKeys.includes(attr)) return;
@@ -775,8 +764,7 @@ var TemplateNode = class {
775
764
  });
776
765
  }
777
766
  getBindUpdateValue(props, key, attr) {
778
- const UPDATE_PREFIX2 = "update";
779
- const updateKey = `${UPDATE_PREFIX2}${capitalize(attr)}`;
767
+ const updateKey = `${UPDATE_PREFIX}${capitalize(attr)}`;
780
768
  if (updateKey && props[updateKey] && isFunction(props[updateKey])) {
781
769
  this.bindValueKeys.push(updateKey);
782
770
  return props[updateKey];
@@ -827,7 +815,6 @@ var TemplateNode = class {
827
815
  };
828
816
  }
829
817
  getNodeTrack(trackKey, trackLastNodes, isRoot) {
830
- var _a;
831
818
  let track = this.trackMap.get(trackKey);
832
819
  if (!track) {
833
820
  track = { cleanup: () => {
@@ -840,7 +827,7 @@ var TemplateNode = class {
840
827
  }
841
828
  this.trackMap.set(trackKey, track);
842
829
  }
843
- (_a = track.cleanup) == null ? void 0 : _a.call(track);
830
+ track.cleanup && track.cleanup();
844
831
  return track;
845
832
  }
846
833
  patchChild(track, parent, child, before) {
@@ -894,16 +881,15 @@ var TemplateNode = class {
894
881
  // src/jsxRenderer.ts
895
882
  function h(template, props, key) {
896
883
  if (isString(template)) {
897
- if (isHtmlTagName(template)) {
898
- const htmlTemplate = convertToHtmlTag(template);
899
- props = { [SINGLE_PROP_KEY]: props };
900
- return new TemplateNode(createTemplate(htmlTemplate), props, key);
901
- } else if (template === EMPTY_TEMPLATE) {
902
- props = { [FRAGMENT_PROP_KEY]: props };
903
- return new TemplateNode(createTemplate(EMPTY_TEMPLATE), props, key);
904
- }
884
+ const isEmptyTemplate = template === EMPTY_TEMPLATE;
885
+ const htmlTemplate = isEmptyTemplate ? EMPTY_TEMPLATE : convertToHtmlTag(template);
886
+ props = { [isEmptyTemplate ? FRAGMENT_PROP_KEY : SINGLE_PROP_KEY]: props };
887
+ return new TemplateNode(createTemplate(htmlTemplate), props, key);
888
+ }
889
+ if (isFunction(template)) {
890
+ return new ComponentNode(template, props, key);
905
891
  }
906
- return isFunction(template) ? new ComponentNode(template, props, key) : new TemplateNode(template, props, key);
892
+ return new TemplateNode(template, props, key);
907
893
  }
908
894
  function isComponent(node) {
909
895
  return node instanceof ComponentNode;
@@ -917,8 +903,10 @@ function createTemplate(html) {
917
903
  return template;
918
904
  }
919
905
  function Fragment(props) {
920
- return h(EMPTY_TEMPLATE, {
921
- children: isArray(props.children) ? props.children.filter(Boolean) : [props.children]
906
+ return h(createTemplate(EMPTY_TEMPLATE), {
907
+ [FRAGMENT_PROP_KEY]: {
908
+ children: isArray(props.children) ? props.children.filter(Boolean) : [props.children]
909
+ }
922
910
  });
923
911
  }
924
912
  function onMount(cb) {
@@ -1,13 +1,7 @@
1
- import { isString, isFunction, isArray, escape, startsWith, isPrimitive, capitalize, isNil, isHTMLElement, coerceArray, isFalsy, kebabCase } from '@estjs/shared';
2
- import { shallowSignal, isSignal, signalObject, useReactive, useSignal, useEffect } from '@estjs/signal';
3
-
4
- /**
5
- * @estjs/template v0.0.14-beta.5
1
+ import {isString,isFunction,isArray,escape,startsWith,isPrimitive,capitalize,isNil,isPlainObject,isHTMLElement,coerceArray,isFalsy,kebabCase}from'@estjs/shared';import {shallowSignal,isSignal,signalObject,useReactive,useSignal,useEffect}from'@estjs/signal';/**
2
+ * @estjs/template v0.0.14-beta.9
6
3
  * (c) 2023-Present jiangxd <jiangxd2016@gmail.com>
7
4
  * @license MIT
8
5
  **/
9
- var D="on",G="update",y="children",R="",A="0",q="1";var O=" __PLACEHOLDER__ ";var X=class{constructor(){this.renderMode=0;}get isSSG(){return this.renderMode===1}get isSSR(){return this.renderMode===2}get isClient(){return this.renderMode===0}setSSR(){this.renderMode=2;}setSSG(){this.renderMode=1;}setClient(){this.renderMode=0;}},m=new X,z=new Map;function Q(o,t){z.set(o,{index:t});}function Z(o){var t;return (t=z.get(o))==null?void 0:t.index}var T=class T{constructor(){this.hooks={mounted:new Set,destroy:new Set};}addEventListener(){}removeEventListener(){}addHook(t,e){var n;(n=this.hooks[t])==null||n.add(e);}getContext(t){return T.context[t]}setContext(t,e){T.context[t]=e;}initRef(){T.ref=this;}removeRef(){T.ref=null;}clearHooks(){Object.values(this.hooks).forEach(t=>t.clear());}};T.ref=null,T.context={};var d=T;function H(o){return o instanceof k}var M=1,k=class extends d{constructor(e,n={},r){super();this.template=e;this.props=n;this.key=r;Q(e,M),this.templates=this.processTemplate();}processTemplate(){if(isArray(this.template)){let e=this.template.join(O);return this.processHtmlString(e).split(O)}return []}processHtmlString(e){return e.replaceAll(/(<[^>]+>)|([^<]+)/g,(n,r,i)=>r?r.includes("data-ci")?n:r.replace(/<\s*([\da-z]+)(\s[^>]*)?>/i,(s,a,l)=>`<${a} data-ci="${M}"${l||""}>`):i&&i.replace(O,"").trim()?`<!--0-${M}-->${i}<!$>`:n)}mount(){this.initRef();let e=this.render();return this.removeRef(),e}render(){if(isFunction(this.template)){let e=this.template(this.props);return H(e)?e.mount():String(e)}return this.renderTemplate()}renderTemplate(){return Object.entries(this.props).forEach(([e,n])=>{let r=n.children;this.normalizeProps(n);let i=this.templates.findIndex(s=>s.includes(`data-hk="${e}"`));r&&this.renderChildren(r,i),this.templates[i]=this.templates[i].replace(`data-hk="${e}"`,`data-hk="${e}" ${this.generateAttributes(n)}`);}),this.templates.join("")}normalizeProps(e){Object.entries(e).forEach(([n,r])=>{n==="children"||isFunction(r)?delete e[n]:isSignal(r)&&(e[n]=r.value);});}generateAttributes(e){return Object.entries(e).filter(([n,r])=>n!=="children"&&!isFunction(r)).map(([n,r])=>`${n}="${escape(String(r))}"`).join(" ")}renderChildren(e,n){e.forEach(([r])=>{M++;let i=this.renderChild(r);this.templates[n]+=i;});}renderChild(e){if(isFunction(e))return this.renderChild(e(this.props));if(isSignal(e))return `<!--1-${M}-->${e.value}<!$>`;if(H(e)){let n=e.mount();return isFunction(n)?n(this.props):j(n)}else return `<!--1-${M}-->${e}<!$>`}};var ne="area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr".split(","),Te="a,abbr,acronym,address,applet,area,article,aside,audio,b,base,basefont,bdi,bdo,bgsound,big,blink,blockquote,body,br,button,canvas,caption,center,cite,code,col,colgroup,command,content,data,datalist,dd,del,details,dfn,dialog,dir,div,dl,dt,em,embed,fieldset,figcaption,figure,font,footer,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,header,hgroup,hr,html,i,iframe,image,img,input,ins,kbd,keygen,label,legend,li,link,listing,main,map,mark,marquee,menu,menuitem,meta,meter,nav,nobr,noframes,noscript,object,ol,optgroup,option,output,p,param,picture,plaintext,pre,progress,q,rb,rp,rt,rtc,ruby,s,samp,script,section,select,shadow,small,source,spacer,span,strike,strong,style,sub,summary,sup,table,tbody,td,template,textarea,tfoot,th,thead,time,title,tr,track,tt,u,ul,var,video,wbr,xmp".split(",");function J(o){if(N(o)||o instanceof Node||H(o))return o;let t=isFalsy(o)?"":String(o);return document.createTextNode(t)}function E(o,t,e=null){let n=N(e)?e.firstChild:e,r=m.isSSR;N(t)?t.mount(o,n):n&&!r?n.before(t):r||o.append(t);}function x(o){N(o)?o.unmount():o.parentNode&&o.remove();}function V(o,t,e){E(o,t,e),x(e);}function U(o,t,e){t==="class"?ye(o,e):t==="style"?xe(o,e):ve(o,t,e);}function ye(o,t){typeof t=="string"?o.className=t:isArray(t)?o.className=t.join(" "):t&&typeof t=="object"&&(o.className=Object.entries(t).reduce((e,[n,r])=>e+(r?` ${n}`:""),"").trim());}function xe(o,t){typeof t=="string"?o.style.cssText=t:t&&typeof t=="object"&&Object.entries(t).forEach(([n,r])=>{o.style.setProperty(kebabCase(n),String(r));});}function ve(o,t,e){isFalsy(e)?o.removeAttribute(t):e===!0?o.setAttribute(t,""):o instanceof HTMLInputElement&&t==="value"?o.value=String(e):o.setAttribute(t,String(e));}function oe(o,t){if(o instanceof HTMLInputElement)switch(o.type){case"checkbox":return g(o,"change",()=>{t(!!o.checked);});case"date":return g(o,"change",()=>{t(o.value?o.value:"");});case"file":return g(o,"change",()=>{o.files&&t(o.files);});case"number":return g(o,"input",()=>{let e=Number.parseFloat(o.value);t(Number.isNaN(e)?"":String(e));});case"radio":return g(o,"change",()=>{t(o.checked?o.value:"");});case"text":return g(o,"input",()=>{t(o.value);})}if(o instanceof HTMLSelectElement)return g(o,"change",()=>{t(o.value);});if(o instanceof HTMLTextAreaElement)return g(o,"input",()=>{t(o.value);})}Promise.resolve();function g(o,t,e){return o.addEventListener(t,e),()=>o.removeEventListener(t,e)}function re(o){let t=[],e=[],n=/<\/?([\da-z-]+)([^>]*)>/gi,r=0;for(;;){let i=n.exec(o);if(!i)break;let[s,a]=i,l=s[1]==="/";if(e.push(o.slice(r,i.index)),r=i.index+s.length,l){for(;t.length>0&&t[t.length-1]!==a;){let c=t.pop();c&&e.push(`</${c}>`);}t.length>0&&t.pop();}else ne.includes(a)||t.push(a);e.push(s);}for(e.push(o.slice(r));t.length>0;){let i=t.pop();i&&e.push(`</${i}>`);}return e.join("")}function ie(o){return Te.includes(o)}function se(o){return ne.includes(o)?`<${o}/>`:`<${o}></${o}>`}function j(o){return isSignal(o)?o.value:o}var C=class extends d{constructor(e,n,r){super();this.template=e;this.props=n;this.key=r;this.emitter=new Set;this.rootNode=null;this.trackMap=new Map;this.key||(this.key=n==null?void 0:n.key),this.proxyProps=this.createProxyProps(n);}createProxyProps(e){return e?signalObject(e,n=>startsWith(n,D)||startsWith(n,G)||n===y):{}}get firstChild(){var e,n;return (n=(e=this.rootNode)==null?void 0:e.firstChild)!=null?n:null}get isConnected(){var e,n;return (n=(e=this.rootNode)==null?void 0:e.isConnected)!=null?n:!1}mount(e,n){var i,s,a,l;if(!isFunction(this.template))throw new Error("Template must be a function");if(this.isConnected)return (s=(i=this.rootNode)==null?void 0:i.mount(e,n))!=null?s:[];this.initRef(),this.rootNode=this.template(useReactive(this.proxyProps,["children"]));let r=(l=(a=this.rootNode)==null?void 0:a.mount(e,n))!=null?l:[];return this.callMountHooks(),this.patchProps(this.props),this.removeRef(),r}unmount(){var e;this.callDestroyHooks(),this.clearHooks(),(e=this.rootNode)==null||e.unmount(),this.rootNode=null,this.proxyProps={},this.clearEmitter();}callMountHooks(){this.hooks.mounted.forEach(e=>e());}callDestroyHooks(){this.hooks.destroy.forEach(e=>e());}clearEmitter(){for(let e of this.emitter)e();this.emitter.clear();}inheritNode(e){Object.assign(this.proxyProps,e.proxyProps),this.rootNode=e.rootNode,this.trackMap=e.trackMap,this.hooks=e.hooks;let n=this.props;this.props=e.props,this.patchProps(n);}getNodeTrack(e){let n=this.trackMap.get(e);return n||(n={cleanup:()=>{}},this.trackMap.set(e,n)),n.cleanup(),n}patchProps(e){var n;if(e){for(let[r,i]of Object.entries(e))startsWith(r,D)&&((n=this.rootNode)!=null&&n.firstChild)?this.patchEventListener(r,i):r==="ref"?this.patchRef(i):startsWith(r,G)?this.patchUpdateHandler(r,i):r!==y&&this.patchNormalProp(r,i);this.props=e;}}patchEventListener(e,n){let r=e.slice(2).toLowerCase(),i=g(this.rootNode.nodes[0],r,n);this.emitter.add(i);}patchRef(e){var n,r;e.value=(r=(n=this.rootNode)==null?void 0:n.firstChild)!=null?r:null;}patchUpdateHandler(e,n){this.props[e]=j(n);}patchNormalProp(e,n){var s,a;let r=(a=(s=this.proxyProps)[e])!=null?a:s[e]=useSignal(n),i=this.getNodeTrack(e);i.cleanup=useEffect(()=>{r.value=isFunction(n)?n():n;});}};var le=Object.prototype.toString;var B=o=>le.call(o)==="[object Object]";function ce(o,t,e,n){let r=new Map,i=Array.from(t.values());if(i.length&&e.length===0)return Ce(o,i,n),r;let s=[],a=we(e),l=0;for(let[c,u]of e.entries()){let p=i[l],b=v(p,c);for(;p&&!a.has(b);)x(p),t.delete(b),p=i[++l],b=v(p,c);let W=v(u,c),K=t.get(W);if(K&&(u=Pe(o,K,u)),p)if(p===K)l++;else {let Y=document.createComment("");E(o,Y,p),s.push([Y,u]);}else E(o,u,n);r.set(W,u);}return s.forEach(([c,u])=>{V(o,u,c);}),t.forEach((c,u)=>{c.isConnected&&!r.has(u)&&x(c);}),r}function Ce(o,t,e){if(o.childNodes.length===t.length+(e?1:0))o.innerHTML="",e&&E(o,e);else {let n=document.createRange(),r=t[0],i=N(r)?r.firstChild:r;n.setStartBefore(i),e?n.setEndBefore(e):n.setEndAfter(o),n.deleteContents();}t.forEach(n=>{N(n)&&n.unmount();});}function Pe(o,t,e){return t===e?t:N(t)&&N(e)&&t.template===e.template?(e.inheritNode(t),e):t instanceof Text&&e instanceof Text?(t.textContent!==e.textContent&&(t.textContent=e.textContent),t):(V(o,e,t),e)}function we(o){let t=new Map;for(let[e,n]of o.entries()){let r=v(n,e);t.set(r,n);}return t}function v(o,t){if(N(o)){let e=o.key;if(e!=null)return String(e)}return `_$${t}$`}var S=class{constructor(t,e,n){this.template=t;this.props=e;this.key=n;this.treeMap=new Map;this.mounted=!1;this.nodes=[];this.trackMap=new Map;this.bindValueKeys=[];this.parent=null;this.key||(this.key=e==null?void 0:e.key),m.isSSR&&(this.componentIndex=Z(this.template));}get firstChild(){var t;return (t=this.nodes[0])!=null?t:null}get isConnected(){return this.mounted}addEventListener(){}removeEventListener(){}mount(t,e){var i;if(this.parent=t,this.isConnected)return this.nodes.forEach(s=>E(t,s,e)),this.nodes;isArray(this.template)&&(this.template=w(this.template.join("")));let n=this.template.content.cloneNode(!0),r=n.firstChild;return (i=r==null?void 0:r.hasAttribute)!=null&&i.call(r,"_svg_")&&(r.remove(),r==null||r.childNodes.forEach(s=>{n.append(s);})),this.nodes=Array.from(n.childNodes),m.isSSR?this.mapSSGNodeTree(t):this.mapNodeTree(t,n),E(t,n,e),this.patchProps(this.props),this.mounted=!0,this.nodes}unmount(){var t,e;if(this.trackMap.forEach(n=>{var r;(r=n.cleanup)==null||r.call(n);}),this.trackMap.clear(),this.treeMap.clear(),this.nodes.forEach(n=>x(n)),!this.template.innerHTML&&!this.nodes.length){let n=(e=(t=this.props)==null?void 0:t[A])==null?void 0:e.children;n&&(isArray(n)?n.forEach(r=>{this.deleteFragmentTextNode(r);}):this.deleteFragmentTextNode(n));}this.nodes=[],this.mounted=!1;}deleteFragmentTextNode(t){var e;isPrimitive(t)?(e=this.parent)==null||e.childNodes.forEach(n=>{var r;n.nodeType===Node.TEXT_NODE&&n.textContent===`${t}`&&((r=this.parent)==null||r.removeChild(n));}):x(t);}inheritNode(t){this.mounted=t.mounted,this.nodes=t.nodes,this.trackMap=t.trackMap,this.treeMap=t.treeMap;let e=this.props;this.props=t.props,this.patchProps(e);}mapSSGNodeTree(t){this.treeMap.set(0,t),this.walkNodeTree(t,this.handleSSGNode.bind(this));}mapNodeTree(t,e){let n=1;this.treeMap.set(0,t);let r=[t],i=s=>{s.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&(this.treeMap.set(n++,s),r.push(s));};this.walkNodeTree(e,i);}walkNodeTree(t,e){t.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&e(t);let n=t.firstChild;for(;n;)this.walkNodeTree(n,e),n=n.nextSibling;}handleSSGNode(t){var e;if(t.nodeType===Node.COMMENT_NODE){let[n,r]=((e=t.textContent)==null?void 0:e.split("-"))||[];if(0===+n&&+r===this.componentIndex){let i=t.nextSibling;this.treeMap.set(+r,i);}}else if(t.nodeType!==Node.TEXT_NODE){let{ci:n="-1",hk:r}=(t==null?void 0:t.dataset)||{};r&&+n===this.componentIndex&&this.treeMap.set(+r,t);}}patchProps(t){t&&(Object.entries(t).forEach(([e,n])=>{let r=Number(e),i=this.treeMap.get(r);i&&this.patchProp(e,i,n,r===0);}),this.props=t);}patchProp(t,e,n,r){n&&Object.entries(n).forEach(([i,s])=>{if(i===y&&s)this.patchChildren(t,e,s,r);else if(i==="ref")n[i].value=e;else if(startsWith(i,"on"))this.patchEventListener(t,e,i,s);else {if(this.bindValueKeys.includes(i))return;let a=this.getBindUpdateValue(n,t,i);this.patchAttribute(t,e,i,s,a);}});}getBindUpdateValue(t,e,n){let i=`update${capitalize(n)}`;if(i&&t[i]&&isFunction(t[i]))return this.bindValueKeys.push(i),t[i]}patchChildren(t,e,n,r){if(isArray(n))n.filter(Boolean).forEach((i,s)=>{var b;let[a,l]=isArray(i)?i:[i,null],c=isNil(l)?null:(b=this.treeMap.get(l))!=null?b:null,u=`${t}:${y}:${s}`,p=this.getNodeTrack(u,!0,r);this.patchChild(p,e,a,c);});else {let i=`${t}:${y}:0`,s=this.getNodeTrack(i,!0,r);this.patchChild(s,e,n,null);}}patchEventListener(t,e,n,r){let i=n.slice(2).toLowerCase(),s=this.getNodeTrack(`${t}:${n}`);s.cleanup=g(e,i,r);}patchAttribute(t,e,n,r,i){let s=this.getNodeTrack(`${t}:${n}`),a=isFunction(r)?r():r,l=isSignal(a)?a:shallowSignal(a);U(e,n,l.value);let c=useEffect(()=>{let p=isFunction(r)?r():r;B(p)&&B(l.peek())&&JSON.stringify(l.value)===JSON.stringify(p)||(l.value=isSignal(p)?p.value:p,U(e,n,l.value));}),u;i&&isHTMLElement(e)&&(u=oe(e,p=>{i(p);})),s.cleanup=()=>{c&&c(),u&&u();};}getNodeTrack(t,e,n){var i;let r=this.trackMap.get(t);return r||(r={cleanup:()=>{}},e&&(r.lastNodes=new Map),n&&(r.isRoot=!0),this.trackMap.set(t,r)),(i=r.cleanup)==null||i.call(r),r}patchChild(t,e,n,r){isFunction(n)?t.cleanup=useEffect(()=>{let i=coerceArray(n()).map(J);m.isSSR?t.lastNodes=this.reconcileChildren(e,i,r):t.lastNodes=ce(e,t.lastNodes,i,r);}):coerceArray(n).forEach((i,s)=>{let a=J(i),l=v(a,s);m.isSSR?t.lastNodes=this.reconcileChildren(e,[a],r):(t.lastNodes.set(l,a),E(e,a,r));});}reconcileChildren(t,e,n){let r=new Map,i=Array.from(t.childNodes).filter(s=>{var a,l;return s.nodeType===Node.TEXT_NODE&&((a=s.previousSibling)==null?void 0:a.nodeType)===Node.COMMENT_NODE&&((l=s.nextSibling)==null?void 0:l.nodeType)===Node.COMMENT_NODE});return e.forEach((s,a)=>{let l=v(s,a);s.nodeType===Node.TEXT_NODE?i.forEach(c=>{s.textContent===c.textContent&&t.replaceChild(s,c);}):E(t,s,n),r.set(l,s);}),r}};function L(o,t,e){if(isString(o)){if(ie(o)){let n=se(o);return t={[q]:t},new S(w(n),t,e)}else if(o===R)return t={[A]:t},new S(w(R),t,e)}return isFunction(o)?new C(o,t,e):new S(o,t,e)}function Fe(o){return o instanceof C}function N(o){return o instanceof C||o instanceof S}function w(o){let t=document.createElement("template");return t.innerHTML=re(o),t}function Ke(o){return L(R,{children:isArray(o.children)?o.children.filter(Boolean):[o.children]})}function De(o){d.ref&&d.ref.addHook("mounted",o);}function Ge(o){d.ref&&d.ref.addHook("destroy",o);}function Je(o,t){d.ref&&d.ref.setContext(o,t);}function Ve(o,t){var e;return (e=d.ref&&d.ref.getContext(o))!=null?e:t}function Ue(){return shallowSignal(null)}function Be(o,t){m.setSSG();let n=new k(o,t||{}).mount();return m.setClient(),n}function We(o,t){let e=typeof t=="string"?document.querySelector(t):t;if(!e)throw new Error(`Could not find container: ${t}`);m.setSSR(),L(o).mount(e),m.setClient();}function Ye(o,t){return m.isSSG?new k(o,t):L(o,t)}
10
-
11
- export { Ke as Fragment, L as h, We as hydrate, Fe as isComponent, N as isJsxElement, Ge as onDestroy, De as onMount, Be as renderToString, Ye as ssg, w as template, Ve as useInject, Je as useProvide, Ue as useRef };
12
- //# sourceMappingURL=template.esm.js.map
6
+ var P="on",L="update",N="children",O="",R="0",q="1";var _="ref",$=" __PLACEHOLDER__ ";var J=class{constructor(){this.renderMode=0;}get isSSG(){return this.renderMode===1}get isSSR(){return this.renderMode===2}get isClient(){return this.renderMode===0}setSSR(){this.renderMode=2;}setSSG(){this.renderMode=1;}setClient(){this.renderMode=0;}},m=new J,W=new Map;function Q(o,t){W.set(o,{index:t});}function Z(o){var t;return (t=W.get(o))==null?void 0:t.index}var v=class v{constructor(){this.hooks={mounted:new Set,destroy:new Set};}addEventListener(){}removeEventListener(){}addHook(t,e){var n;(n=this.hooks[t])==null||n.add(e);}getContext(t){return v.context[t]}setContext(t,e){v.context[t]=e;}initRef(){v.ref=this;}removeRef(){v.ref=null;}clearHooks(){Object.values(this.hooks).forEach(t=>t.clear());}};v.ref=null,v.context={};var u=v;function I(o){return o instanceof C}var b=1,C=class extends u{constructor(e,n={},r){super();this.template=e;this.props=n;this.key=r;Q(e,b),this.templates=this.processTemplate();}processTemplate(){if(isArray(this.template)){let e=this.template.join($);return this.processHtmlString(e).split($)}return []}processHtmlString(e){return e.replaceAll(/(<[^>]+>)|([^<]+)/g,(n,r,i)=>r?r.includes("data-ci")?n:r.replace(/<\s*([\da-z]+)(\s[^>]*)?>/i,(s,a,l)=>`<${a} data-ci="${b}"${l||""}>`):i&&i.replace($,"").trim()?`<!--0-${b}-->${i}<!$>`:n)}mount(){this.initRef();let e=this.render();return this.removeRef(),e}render(){if(isFunction(this.template)){let e=this.template(this.props);return I(e)?e.mount():String(e)}return this.renderTemplate()}renderTemplate(){return Object.entries(this.props).forEach(([e,n])=>{let r=n.children;this.normalizeProps(n);let i=this.templates.findIndex(s=>s.includes(`data-hk="${e}"`));r&&this.renderChildren(r,i),this.templates[i]=this.templates[i].replace(`data-hk="${e}"`,`data-hk="${e}" ${this.generateAttributes(n)}`);}),this.templates.join("")}normalizeProps(e){Object.entries(e).forEach(([n,r])=>{n===N||isFunction(r)?delete e[n]:isSignal(r)&&(e[n]=r.value);});}generateAttributes(e){return Object.entries(e).filter(([n,r])=>n!==N&&!isFunction(r)).map(([n,r])=>`${n}="${escape(String(r))}"`).join(" ")}renderChildren(e,n){e.forEach(([r])=>{b++;let i=this.renderChild(r);this.templates[n]+=i;});}renderChild(e){if(isFunction(e))return this.renderChild(e(this.props));if(isSignal(e))return `<!--1-${b}-->${e.value}<!$>`;if(I(e)){let n=e.mount();return isFunction(n)?n(this.props):j(n)}else return `<!--1-${b}-->${e}<!$>`}};var ne="area,base,br,col,embed,hr,img,input,link,meta,param,source,track,wbr".split(",");function V(o){if(f(o)||o instanceof Node||I(o))return o;let t=isFalsy(o)?"":String(o);return document.createTextNode(t)}function T(o,t,e=null){let n=f(e)?e.firstChild:e,r=m.isSSR;f(t)?t.mount(o,n):n&&!r?n.before(t):r||o.append(t);}function S(o){f(o)?o.unmount():o.parentNode&&o.remove();}function Y(o,t,e){T(o,t,e),S(e);}function U(o,t,e){t==="class"?Ee(o,e):t==="style"?Te(o,e):ve(o,t,e);}function Ee(o,t){typeof t=="string"?o.className=t:isArray(t)?o.className=t.join(" "):t&&typeof t=="object"&&(o.className=Object.entries(t).reduce((e,[n,r])=>e+(r?` ${n}`:""),"").trim());}function Te(o,t){typeof t=="string"?o.style.cssText=t:t&&typeof t=="object"&&Object.entries(t).forEach(([n,r])=>{o.style.setProperty(kebabCase(n),String(r));});}function ve(o,t,e){isFalsy(e)?o.removeAttribute(t):e===!0?o.setAttribute(t,""):o instanceof HTMLInputElement&&t==="value"?o.value=String(e):o.setAttribute(t,String(e));}function oe(o,t){if(o instanceof HTMLInputElement)switch(o.type){case"checkbox":return h(o,"change",()=>{t(!!o.checked);});case"date":return h(o,"change",()=>{t(o.value?o.value:"");});case"file":return h(o,"change",()=>{o.files&&t(o.files);});case"number":return h(o,"input",()=>{let e=Number.parseFloat(o.value);t(Number.isNaN(e)?"":String(e));});case"radio":return h(o,"change",()=>{t(o.checked?o.value:"");});case"text":return h(o,"input",()=>{t(o.value);})}if(o instanceof HTMLSelectElement)return h(o,"change",()=>{t(o.value);});if(o instanceof HTMLTextAreaElement)return h(o,"input",()=>{t(o.value);})}function h(o,t,e){return o.addEventListener(t,e),()=>o.removeEventListener(t,e)}function re(o){let t=[],e=[],n=/<\/?([\da-z-]+)([^>]*)>/gi,r=0;for(;;){let i=n.exec(o);if(!i)break;let[s,a]=i,l=s[1]==="/";if(e.push(o.slice(r,i.index)),r=i.index+s.length,l){for(;t.length>0&&t[t.length-1]!==a;){let c=t.pop();c&&e.push(`</${c}>`);}t.length>0&&t.pop();}else ne.includes(a)||t.push(a);e.push(s);}for(e.push(o.slice(r));t.length>0;){let i=t.pop();i&&e.push(`</${i}>`);}return e.join("")}function ie(o){return ne.includes(o)?`<${o}/>`:`<${o}></${o}>`}function j(o){return isSignal(o)?o.value:o}var M=class extends u{constructor(e,n,r){super();this.template=e;this.props=n;this.key=r;this.emitter=new Set;this.rootNode=null;this.trackMap=new Map;this.key||(this.key=n&&n.key),this.proxyProps=this.createProxyProps(n);}createProxyProps(e){return e?signalObject(e,n=>startsWith(n,P)||startsWith(n,L)||n===N):{}}get firstChild(){var e,n;return (n=(e=this.rootNode)==null?void 0:e.firstChild)!=null?n:null}get isConnected(){var e,n;return (n=(e=this.rootNode)==null?void 0:e.isConnected)!=null?n:!1}mount(e,n){var i,s,a,l;if(!isFunction(this.template))throw new Error("Template must be a function");if(this.isConnected)return (s=(i=this.rootNode)==null?void 0:i.mount(e,n))!=null?s:[];this.initRef(),this.rootNode=this.template(useReactive(this.proxyProps,[N]));let r=(l=(a=this.rootNode)==null?void 0:a.mount(e,n))!=null?l:[];return this.callMountHooks(),this.patchProps(this.props),this.removeRef(),r}unmount(){var e;this.callDestroyHooks(),this.clearHooks(),(e=this.rootNode)==null||e.unmount(),this.rootNode=null,this.proxyProps={},this.clearEmitter();}callMountHooks(){this.hooks.mounted.forEach(e=>e());}callDestroyHooks(){this.hooks.destroy.forEach(e=>e());}clearEmitter(){for(let e of this.emitter)e();this.emitter.clear();}inheritNode(e){Object.assign(this.proxyProps,e.proxyProps),this.rootNode=e.rootNode,this.trackMap=e.trackMap,this.hooks=e.hooks;let n=this.props;this.props=e.props,this.patchProps(n);}getNodeTrack(e){let n=this.trackMap.get(e);return n||(n={cleanup:()=>{}},this.trackMap.set(e,n)),n.cleanup(),n}patchProps(e){var n;if(e){for(let[r,i]of Object.entries(e))startsWith(r,P)&&((n=this.rootNode)!=null&&n.firstChild)?this.patchEventListener(r,i):r===_?this.patchRef(i):startsWith(r,L)?this.patchUpdateHandler(r,i):r!==N&&this.patchNormalProp(r,i);this.props=e;}}patchEventListener(e,n){let r=e.slice(2).toLowerCase(),i=h(this.rootNode.nodes[0],r,n);this.emitter.add(i);}patchRef(e){var n,r;e.value=(r=(n=this.rootNode)==null?void 0:n.firstChild)!=null?r:null;}patchUpdateHandler(e,n){this.props[e]=j(n);}patchNormalProp(e,n){var s,a;let r=(a=(s=this.proxyProps)[e])!=null?a:s[e]=useSignal(n),i=this.getNodeTrack(e);i.cleanup=useEffect(()=>{r.value=isFunction(n)?n():n;});}};function ae(o,t,e,n){let r=new Map,i=Array.from(t.values());if(i.length&&e.length===0)return Ce(o,i,n),r;let s=[],a=ke(e),l=0;for(let[c,d]of e.entries()){let p=i[l],x=y(p,c);for(;p&&!a.has(x);)S(p),t.delete(x),p=i[++l],x=y(p,c);let B=y(d,c),G=t.get(B);if(G&&(d=Me(o,G,d)),p)if(p===G)l++;else {let z=document.createComment("");T(o,z,p),s.push([z,d]);}else T(o,d,n);r.set(B,d);}return s.forEach(([c,d])=>{Y(o,d,c);}),t.forEach((c,d)=>{c.isConnected&&!r.has(d)&&S(c);}),r}function Ce(o,t,e){if(o.childNodes.length===t.length+(e?1:0))o.innerHTML="",e&&T(o,e);else {let n=document.createRange(),r=t[0],i=f(r)?r.firstChild:r;n.setStartBefore(i),e?n.setEndBefore(e):n.setEndAfter(o),n.deleteContents();}t.forEach(n=>{f(n)&&n.unmount();});}function Me(o,t,e){return t===e?t:f(t)&&f(e)&&t.template===e.template?(e.inheritNode(t),e):t instanceof Text&&e instanceof Text?(t.textContent!==e.textContent&&(t.textContent=e.textContent),t):(Y(o,e,t),e)}function ke(o){let t=new Map;for(let[e,n]of o.entries()){let r=y(n,e);t.set(r,n);}return t}function y(o,t){if(f(o)){let e=o.key;if(e!=null)return String(e)}return `_$${t}$`}var k=class{constructor(t,e,n){this.template=t;this.props=e;this.key=n;this.treeMap=new Map;this.mounted=!1;this.nodes=[];this.trackMap=new Map;this.bindValueKeys=[];this.parent=null;this.key||(this.key=e&&e.key),m.isSSR&&(this.componentIndex=Z(this.template));}get firstChild(){var t;return (t=this.nodes[0])!=null?t:null}get isConnected(){return this.mounted}addEventListener(){}removeEventListener(){}mount(t,e){var i;if(this.parent=t,this.isConnected)return this.nodes.forEach(s=>T(t,s,e)),this.nodes;isArray(this.template)&&(this.template=H(this.template.join("")));let n=this.template.content.cloneNode(!0),r=n.firstChild;return (i=r==null?void 0:r.hasAttribute)!=null&&i.call(r,"_svg_")&&(r.remove(),r.childNodes.forEach(s=>{n.append(s);})),this.nodes=Array.from(n.childNodes),m.isSSR?this.mapSSGNodeTree(t):this.mapNodeTree(t,n),T(t,n,e),this.patchProps(this.props),this.mounted=!0,this.nodes}unmount(){var t,e;if(this.trackMap.forEach(n=>{n.cleanup&&n.cleanup();}),this.trackMap.clear(),this.treeMap.clear(),this.nodes.forEach(n=>S(n)),!this.template.innerHTML&&!this.nodes.length){let n=(e=(t=this.props)==null?void 0:t[R])==null?void 0:e.children;n&&(isArray(n)?n.forEach(r=>{this.deleteFragmentTextNode(r);}):this.deleteFragmentTextNode(n));}this.nodes=[],this.mounted=!1;}deleteFragmentTextNode(t){isPrimitive(t)?this.parent&&this.parent.childNodes.length&&this.parent.childNodes.forEach(e=>{e.nodeType===Node.TEXT_NODE&&e.textContent===`${t}`&&this.parent.removeChild(e);}):S(t);}inheritNode(t){this.mounted=t.mounted,this.nodes=t.nodes,this.trackMap=t.trackMap,this.treeMap=t.treeMap;let e=this.props;this.props=t.props,this.patchProps(e);}mapSSGNodeTree(t){this.treeMap.set(0,t),this.walkNodeTree(t,this.handleSSGNode.bind(this));}mapNodeTree(t,e){let n=1;this.treeMap.set(0,t);let r=[t],i=s=>{s.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&(this.treeMap.set(n++,s),r.push(s));};this.walkNodeTree(e,i);}walkNodeTree(t,e){t.nodeType!==Node.DOCUMENT_FRAGMENT_NODE&&e(t);let n=t.firstChild;for(;n;)this.walkNodeTree(n,e),n=n.nextSibling;}handleSSGNode(t){var e;if(t.nodeType===Node.COMMENT_NODE){let[n,r]=((e=t.textContent)==null?void 0:e.split("-"))||[];if(0===+n&&+r===this.componentIndex){let i=t.nextSibling;this.treeMap.set(+r,i);}}else if(t.nodeType!==Node.TEXT_NODE){let{ci:n="-1",hk:r}=(t==null?void 0:t.dataset)||{};r&&+n===this.componentIndex&&this.treeMap.set(+r,t);}}patchProps(t){t&&(Object.entries(t).forEach(([e,n])=>{let r=Number(e),i=this.treeMap.get(r);i&&this.patchProp(e,i,n,r===0);}),this.props=t);}patchProp(t,e,n,r){n&&Object.entries(n).forEach(([i,s])=>{if(i===N&&s)this.patchChildren(t,e,s,r);else if(i===_)n[i].value=e;else if(startsWith(i,P))this.patchEventListener(t,e,i,s);else {if(this.bindValueKeys.includes(i))return;let a=this.getBindUpdateValue(n,t,i);this.patchAttribute(t,e,i,s,a);}});}getBindUpdateValue(t,e,n){let r=`${L}${capitalize(n)}`;if(r&&t[r]&&isFunction(t[r]))return this.bindValueKeys.push(r),t[r]}patchChildren(t,e,n,r){if(isArray(n))n.filter(Boolean).forEach((i,s)=>{var x;let[a,l]=isArray(i)?i:[i,null],c=isNil(l)?null:(x=this.treeMap.get(l))!=null?x:null,d=`${t}:${N}:${s}`,p=this.getNodeTrack(d,!0,r);this.patchChild(p,e,a,c);});else {let i=`${t}:${N}:0`,s=this.getNodeTrack(i,!0,r);this.patchChild(s,e,n,null);}}patchEventListener(t,e,n,r){let i=n.slice(2).toLowerCase(),s=this.getNodeTrack(`${t}:${n}`);s.cleanup=h(e,i,r);}patchAttribute(t,e,n,r,i){let s=this.getNodeTrack(`${t}:${n}`),a=isFunction(r)?r():r,l=isSignal(a)?a:shallowSignal(a);U(e,n,l.value);let c=useEffect(()=>{let p=isFunction(r)?r():r;isPlainObject(p)&&isPlainObject(l.peek())&&JSON.stringify(l.value)===JSON.stringify(p)||(l.value=isSignal(p)?p.value:p,U(e,n,l.value));}),d;i&&isHTMLElement(e)&&(d=oe(e,p=>{i(p);})),s.cleanup=()=>{c&&c(),d&&d();};}getNodeTrack(t,e,n){let r=this.trackMap.get(t);return r||(r={cleanup:()=>{}},e&&(r.lastNodes=new Map),n&&(r.isRoot=!0),this.trackMap.set(t,r)),r.cleanup&&r.cleanup(),r}patchChild(t,e,n,r){isFunction(n)?t.cleanup=useEffect(()=>{let i=coerceArray(n()).map(V);m.isSSR?t.lastNodes=this.reconcileChildren(e,i,r):t.lastNodes=ae(e,t.lastNodes,i,r);}):coerceArray(n).forEach((i,s)=>{let a=V(i),l=y(a,s);m.isSSR?t.lastNodes=this.reconcileChildren(e,[a],r):(t.lastNodes.set(l,a),T(e,a,r));});}reconcileChildren(t,e,n){let r=new Map,i=Array.from(t.childNodes).filter(s=>{var a,l;return s.nodeType===Node.TEXT_NODE&&((a=s.previousSibling)==null?void 0:a.nodeType)===Node.COMMENT_NODE&&((l=s.nextSibling)==null?void 0:l.nodeType)===Node.COMMENT_NODE});return e.forEach((s,a)=>{let l=y(s,a);s.nodeType===Node.TEXT_NODE?i.forEach(c=>{s.textContent===c.textContent&&t.replaceChild(s,c);}):T(t,s,n),r.set(l,s);}),r}};function A(o,t,e){if(isString(o)){let n=o===O,r=n?O:ie(o);return t={[n?R:q]:t},new k(H(r),t,e)}return isFunction(o)?new M(o,t,e):new k(o,t,e)}function Ie(o){return o instanceof M}function f(o){return o instanceof M||o instanceof k}function H(o){let t=document.createElement("template");return t.innerHTML=re(o),t}function je(o){return A(H(O),{[R]:{children:isArray(o.children)?o.children.filter(Boolean):[o.children]}})}function Xe(o){u.ref&&u.ref.addHook("mounted",o);}function De(o){u.ref&&u.ref.addHook("destroy",o);}function Fe(o,t){u.ref&&u.ref.setContext(o,t);}function Ge(o,t){var e;return (e=u.ref&&u.ref.getContext(o))!=null?e:t}function Je(){return shallowSignal(null)}function Ve(o,t){m.setSSG();let n=new C(o,t||{}).mount();return m.setClient(),n}function Ye(o,t){let e=typeof t=="string"?document.querySelector(t):t;if(!e)throw new Error(`Could not find container: ${t}`);m.setSSR(),A(o).mount(e),m.setClient();}function Ue(o,t){return m.isSSG?new C(o,t):A(o,t)}export{je as Fragment,A as h,Ye as hydrate,Ie as isComponent,f as isJsxElement,De as onDestroy,Xe as onMount,Ve as renderToString,Ue as ssg,H as template,Ge as useInject,Fe as useProvide,Je as useRef};//# sourceMappingURL=template.esm.js.map
13
7
  //# sourceMappingURL=template.esm.js.map