vaderjs 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/vader.js CHANGED
@@ -1,74 +1,8 @@
1
- let dom = []
1
+ // @ts-nocheck
2
+ let dom = [];
2
3
  let states = {};
3
- let worker = new Worker(new URL('./worker.js', import.meta.url));
4
- /**
5
- * @function markdown
6
- * @param {String} content
7
- * @description Allows you to convert markdown to html
8
- */
9
- function markdown(content) {
10
-
11
- let headers = content.match(/(#+)(.*)/g);
12
- if (headers) {
13
- headers.forEach((header) => {
14
- if(header.includes('/') || header.includes('<') || header.includes('>')){
15
- return
16
-
17
- }
18
- let level = header.split('#').length;
19
- content = content.replace(header, `<h${level} class="markdown_heading">${header.replace(/#/g, '')}</h${level}>`);
20
- });
21
- }
22
-
23
- content = content.replace(/\*\*(.*?)\*\*/g, (match, text) => {
24
- return `<b class="markdown_bold">${text}</b>`;
25
- });
26
- content = content.replace(/\*(.*?)\*/g, (match, text) => {
27
- return `<i class="markdown_italic">${text}</i>`;
28
- })
29
- content = content.replace(/`(.*?)`/g, (match, text) => {
30
- return `<code>${text}</code>`;
31
- });
32
- content = content.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (match, text, url) => {
33
- return `<a class="markdown_link" href="${url}">${text}</a>`;
34
- });
35
- content = content.replace(/!\[([^\]]+)\]\(([^)]+)\)/g, (match, alt, src) => {
36
- return `<img class="markdown_image" src="${src}" alt="${alt}" />`;
37
- });
38
- content = content.split('\n').map((line, index, arr) => {
39
- if (line.match(/^\s*-\s+(.*?)$/gm)) {
40
- if (index === 0 || !arr[index - 1].match(/^\s*-\s+(.*?)$/gm)) {
41
- return `<ul class="markdown_unordered" style="list-style-type:disc;list-style:inside"><li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li>`;
42
- } else if (index === arr.length - 1 || !arr[index + 1].match(/^\s*-\s+(.*?)$/gm)) {
43
- return `<li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li></ul>`;
44
- } else {
45
- return `<li>${line.replace(/^\s*-\s+(.*?)$/gm, '$1')}</li>`;
46
- }
47
- } else {
48
- return line;
49
- }
50
- }).join('\n');
51
-
52
- content = content.split('\n').map((line, index, arr) => {
53
- if (line.match(/^\s*\d+\.\s+(.*?)$/gm)) {
54
- if (index === 0 || !arr[index - 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
55
- return `<ol class="markdown_ordered" style="list-style-type:decimal;"><li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li>`;
56
- } else if (index === arr.length - 1 || !arr[index + 1].match(/^\s*\d+\.\s+(.*?)$/gm)) {
57
- return `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li></ol>`;
58
- } else {
59
- return `<li>${line.replace(/^\s*\d+\.\s+(.*?)$/gm, '$1')}</li>`;
60
- }
61
- } else {
62
- return line;
63
- }
64
- }).join('\n');
65
-
66
-
67
- return content
68
-
69
- }
70
4
 
71
-
5
+ let worker = new Worker(new URL("./worker.js", import.meta.url));
72
6
 
73
7
  /**
74
8
  * @function useRef
@@ -76,7 +10,7 @@ function markdown(content) {
76
10
  * @param {String} ref
77
11
  * @returns {void | Object} {current, update}
78
12
  */
79
-
13
+
80
14
  export const useRef = (ref) => {
81
15
  const element = document.querySelector(`[ref="${ref}"]`);
82
16
  const getElement = () => element;
@@ -97,7 +31,7 @@ export const useRef = (ref) => {
97
31
 
98
32
  return {
99
33
  current: getElement(),
100
- update,
34
+ update
101
35
  };
102
36
  };
103
37
 
@@ -141,47 +75,63 @@ export class Component {
141
75
  this.$_useStore_subscribers = [];
142
76
  this.init();
143
77
  this.Componentcontent = null;
144
- this.$_signal_dispatch_event = new CustomEvent("signalDispatch", {
78
+ this.$_signal_dispatch_event = new CustomEvent("SignalDispatch", {
145
79
  detail: {
146
80
  hasUpdated: false,
147
- state: null,
148
- },
81
+ state: null
82
+ }
149
83
  });
84
+ /**
85
+ * @property {Object} $_signal_dispatch_cleanup_event
86
+ * @description Allows you to dispatch a signal cleanup event
87
+ * @private
88
+ */
89
+ this.$_signal_dispatch_cleanup_event = new CustomEvent(
90
+ "Signal_Cleanup_Dispatch",
91
+ {
92
+ detail: {
93
+ state: null,
94
+ lastState: null
95
+ }
96
+ }
97
+ );
98
+ /**
99
+ * @property {Array} snapshots
100
+ * @private
101
+ */
150
102
  this.snapshots = [];
151
103
  /**
152
104
  * @property {Object} dom
153
105
  * @description Allows you to get reference to DOM element
154
106
  * @returns {void | HTMLElement}
155
- *
107
+ *
156
108
  */
157
- this.dom = []
158
-
109
+ this.dom = [];
110
+
159
111
  /**
160
112
  * @property {Boolean} cfr
161
- * @description Allows you to compile html code on the fly - client fly rendering
162
- *
113
+ * @description Allows you to compile html code on the fly - client fly rendering
114
+ *
163
115
  */
164
- this.cfr = false
116
+ this.cfr = false;
165
117
  /**
166
118
  * @property {Boolean} worker
167
119
  * @description Allows you to use a web worker to compile html code on the fly - client fly rendering
168
120
 
169
121
  */
170
-
171
122
  }
172
123
 
173
124
  /**
174
125
  * @method adapter
175
126
  * @description Allows you to create an adapter - this is used to create custom logic
176
- *
177
- *
127
+ *
128
+ *
178
129
  */
179
- adapter() {
180
- return
130
+ adapter(options) {
131
+ // allow you to override the compoent logic
181
132
  }
182
133
  init() {
183
134
  this.registerComponent();
184
-
185
135
  }
186
136
 
187
137
  registerComponent() {
@@ -303,13 +253,20 @@ export class Component {
303
253
  this.$_signal_subscribe = (fn, runonce) => {
304
254
  this.$_signal_subscribers.push({
305
255
  function: fn,
306
- runonce: runonce,
256
+ runonce: runonce
307
257
  });
258
+ return fn;
308
259
  };
309
260
  this.$_signal_cleanup = (fn) => {
261
+ this.lastState = state;
310
262
  this.$_signal_subscribers = this.$_signal_subscribers.filter(
311
263
  (subscriber) => subscriber.function !== fn
312
264
  );
265
+ // @ts-ignore
266
+ this.$_signal_dispatch_cleanup_event.detail.state = this.states;
267
+ // @ts-ignore
268
+ this.$_signal_dispatch_cleanup_event.detail.lastState = this.lastState;
269
+ window.dispatchEvent(this.$_signal_dispatch_event);
313
270
  };
314
271
  this.$_signal_dispatch = () => {
315
272
  for (var i = 0; i < this.$_signal_subscribers.length; i++) {
@@ -382,7 +339,7 @@ export class Component {
382
339
  * @description Allows you to get the value of a signal
383
340
  * @returns {any}
384
341
  */
385
- get: this.$_signal_get,
342
+ get: this.$_signal_get
386
343
  };
387
344
  };
388
345
  /**
@@ -466,7 +423,7 @@ export class Component {
466
423
  return logicalOperator === "any"
467
424
  ? auth.canAnyOf(actions)
468
425
  : auth.canAllOf(actions);
469
- },
426
+ }
470
427
  };
471
428
  return auth;
472
429
  }
@@ -506,11 +463,10 @@ export class Component {
506
463
  (action) => {
507
464
  this.states[key] = reducer(this.states[key], action);
508
465
  this.updateComponent();
509
- },
466
+ }
510
467
  ];
511
468
  }
512
469
 
513
-
514
470
  runEffects() {
515
471
  Object.keys(this.effects).forEach((component) => {
516
472
  this.effects[component].forEach((effect) => {
@@ -521,7 +477,7 @@ export class Component {
521
477
  });
522
478
  });
523
479
  }
524
-
480
+
525
481
  /**
526
482
  * @method useSyncStore
527
483
  * @description Allows you to create a store
@@ -545,8 +501,7 @@ export class Component {
545
501
  subscriber(s);
546
502
  });
547
503
  }) ||
548
- {},
549
-
504
+ {}
550
505
  );
551
506
 
552
507
  const getField = (fieldName) => {
@@ -569,7 +524,7 @@ export class Component {
569
524
  getField,
570
525
  setField,
571
526
  subscribe,
572
- clear,
527
+ clear
573
528
  };
574
529
  }
575
530
  /**
@@ -588,12 +543,10 @@ export class Component {
588
543
  * setCount(count + 1)
589
544
  */
590
545
  useState(key, initialValue, callback = null) {
591
-
592
- if(!this.states[key]){
593
- this.states[key] = initialValue;
594
- }
595
-
596
-
546
+ if (!this.states[key]) {
547
+ this.states[key] = initialValue;
548
+ }
549
+
597
550
  return [
598
551
  this.states[key],
599
552
  /**
@@ -607,7 +560,7 @@ export class Component {
607
560
  this.updateComponent();
608
561
  // @ts-ignore
609
562
  typeof callback === "function" ? callback() : null;
610
- },
563
+ }
611
564
  ];
612
565
  }
613
566
  /**
@@ -624,9 +577,8 @@ export class Component {
624
577
 
625
578
  useRef(ref) {
626
579
  // get ref from array
627
- console.log(this.dom)
628
- const element = this.dom[ref]
629
-
580
+ const element = this.dom[ref];
581
+
630
582
  const getElement = () => element;
631
583
 
632
584
  const update = (data) => {
@@ -648,12 +600,12 @@ export class Component {
648
600
  // @ts-ignore
649
601
  current: getElement,
650
602
  /**@type {Function} */
651
- update,
603
+ update
652
604
  };
653
605
  }
654
606
 
655
607
  /**
656
- *
608
+ *
657
609
  * @function useEffect
658
610
  * @param {*} effectFn
659
611
  * @param {*} dependencies
@@ -682,10 +634,12 @@ export class Component {
682
634
 
683
635
  return {
684
636
  cleanup: () => {
637
+ // @ts-ignore
685
638
  this.effects[this.name] = this.effects[this.name].filter(
639
+ // @ts-ignore
686
640
  (effect) => effect !== effectFn
687
641
  );
688
- },
642
+ }
689
643
  };
690
644
  }
691
645
  /**
@@ -718,8 +672,7 @@ export class Component {
718
672
  const fragment = document.createDocumentFragment();
719
673
  Object.keys(components).forEach(async (component) => {
720
674
  const { name } = components[component];
721
-
722
-
675
+
723
676
  let componentContainer = document.querySelector(
724
677
  `[data-component="${name}"]`
725
678
  );
@@ -736,7 +689,7 @@ export class Component {
736
689
  prev_state: this.states,
737
690
  prev_props: this.storedProps,
738
691
  // @ts-ignore
739
- content: componentContainer.innerHTML,
692
+ content: componentContainer.innerHTML
740
693
  };
741
694
 
742
695
  if (!componentContainer) return;
@@ -796,141 +749,6 @@ export class Component {
796
749
  return /^[a-zA-Z0-9-_]+$/.test(className);
797
750
  }
798
751
 
799
-
800
- parseHTML(result) {
801
-
802
- const dom = new DOMParser().parseFromString(result, "text/html");
803
- console.log(dom)
804
- const elements = dom.documentElement.querySelectorAll("*");
805
-
806
- elements.forEach((element) => {
807
- switch (element.nodeName) {
808
- case "IMG":
809
- if (
810
- !element.hasAttribute("alt") &&
811
- !document.documentElement.outerHTML
812
- .trim()
813
- .includes("<!-- #vader-disable_accessibility -->")
814
- ) {
815
- throw new SyntaxError(
816
- `Image: ${element.outerHTML} missing alt attribute`
817
- );
818
- } else if (
819
- element.hasAttribute("alt") &&
820
- // @ts-ignore
821
- element.getAttribute("alt").length < 1 &&
822
- !document.documentElement.outerHTML
823
- .trim()
824
- .includes("<!-- #vader-disable_accessibility -->")
825
- ) {
826
- throw new SyntaxError(
827
- `Image: ${element.outerHTML} alt attribute cannot be empty`
828
- );
829
-
830
- } else if (
831
- element.hasAttribute("src") &&
832
- !element.getAttribute("src")?.includes("http") || !element.getAttribute("src")?.includes("https") &&
833
- !document.documentElement.outerHTML
834
- .trim()
835
- .includes("<!-- #vader-disable_accessibility -->")
836
- ) {
837
- let prevurl = element.getAttribute("src");
838
- element.setAttribute("aria-hidden", "true");
839
- element.setAttribute("hidden", "true");
840
- // if window.lcoation.pathname includes a html file remove it and only use the path
841
- let url = window.location.origin + window.location.pathname.replace(/\/[^\/]*$/, '') + '/public/' + element.getAttribute("src");
842
- let image = new Image();
843
- image.src = url;
844
- image.onerror = () => {
845
- // @ts-ignore
846
- element.setAttribute("src", prevurl);
847
- throw new Error(`Image: ${element.outerHTML} not found`);
848
- };
849
- element.setAttribute("src", url);
850
-
851
- image.onload = () => {
852
- document.querySelectorAll(`img[src="${url}"]`).forEach((img) => {
853
- img.setAttribute("src", url);
854
- img.removeAttribute("aria-hidden");
855
- img.removeAttribute("hidden");
856
- });
857
- };
858
- }
859
- break;
860
-
861
- default:
862
- if (element.hasAttribute("ref")) {
863
- // @ts-ignore
864
- dom[element.getAttribute("ref")] = element;
865
- }
866
- if(element.nodeName === "MARKDOWN"){
867
- element.innerHTML = markdown(element.innerHTML.replace(/\\n/g, '\n').trim())
868
- }
869
-
870
- if (element.hasAttribute("class")) {
871
- const allowClassComments =
872
- document.documentElement.outerHTML.includes(
873
- "<!-- #vader-allow_class -->"
874
- );
875
- if (!allowClassComments) {
876
- console.warn(
877
- "you can disable class errors using, <!-- #vader-allow_class -->"
878
- );
879
- throw new Error(
880
- "class attribute is not allowed, please use className instead"
881
- );
882
- }
883
- } else if (element.hasAttribute("className")) {
884
- // @ts-ignore
885
- element.setAttribute("class", element.getAttribute("className"));
886
- element.removeAttribute("className");
887
- }
888
-
889
- if (
890
- element.hasAttribute("href") &&
891
- // @ts-ignore
892
- element.getAttribute("href").startsWith("/") &&
893
- !document.documentElement.outerHTML
894
- .trim()
895
- .includes("<!-- #vader-disable_relative-paths -->")
896
- ) {
897
- element.setAttribute(
898
- "href",
899
- // @ts-ignore
900
- `#/${element.getAttribute("href").replace("/", "")}`
901
- );
902
- }
903
-
904
- if (
905
- element.hasAttribute("src") &&
906
- // @ts-ignore
907
- !element.getAttribute("src").includes("http") &&
908
- // @ts-ignore
909
- !element.getAttribute("src").includes("https") &&
910
- !document.documentElement.outerHTML.includes(`<!-- #vader-disable_relative-paths -->`)
911
- ) {
912
- element.setAttribute(
913
- "src",
914
- // @ts-ignore
915
- `./public/${element.getAttribute("src")}`
916
- );
917
- }
918
- break;
919
- }
920
-
921
- });
922
-
923
- result = dom.body.innerHTML;
924
-
925
- this.Componentcontent = result;
926
-
927
- if (!result.includes("<div data-component")) {
928
- result = `<div data-component="${this.name}">${result}</div>`;
929
- }
930
- return markdown(result.replace(/\\n/g, '\n').trim())
931
-
932
- }
933
-
934
752
  /**
935
753
  * The `html` method generates and processes HTML content for a component, performing various validations and tasks.
936
754
  *
@@ -977,104 +795,116 @@ export class Component {
977
795
  * @see {@link Component}
978
796
  * @see {@link Component#componentDidMount}
979
797
  */
980
-
981
-
982
-
798
+
983
799
  html(strings, ...args) {
984
800
  // @ts-ignore
985
- let tiemr = setInterval(()=>{
986
- if(document.querySelector(`[data-component="${this.name}"]`)){
987
- clearInterval(tiemr)
801
+ let timer = setInterval(() => {
802
+ if (document.querySelector(`[data-component="${this.name}"]`)) {
803
+ clearInterval(timer);
988
804
  this.componentMounted = true;
989
-
990
- document.querySelector(`[data-component="${this.name}"]`)?.querySelectorAll("*").forEach((element)=>{
991
- if(element.hasAttribute("ref")){
992
- // @ts-ignore
993
- this.dom[element.getAttribute("ref")] = element
994
- }
995
- })
805
+
806
+ document
807
+ .querySelector(`[data-component="${this.name}"]`)
808
+ ?.querySelectorAll("*")
809
+ .forEach((element) => {
810
+ if (element.hasAttribute("ref")) {
811
+ // @ts-ignore
812
+ this.dom[element.getAttribute("ref")] = element;
813
+ }
814
+ });
996
815
  this.componentDidMount();
997
816
  }
998
- }, 100)
817
+ }, 100);
999
818
  let script = document.createElement("script");
1000
819
  script.setAttribute("type", "text/javascript");
1001
820
  script.setAttribute(`data-component-script`, this.name);
1002
-
1003
821
 
1004
-
1005
- let dom = this.dom
1006
-
1007
- if(this.cfr){
1008
-
1009
- worker.postMessage({strings, args, location: window.location.origin + window.location.pathname.replace(/\/[^\/]*$/, '') + '/public/', name: this.name})
1010
- let promise = new Promise((resolve, reject)=>{
1011
- worker.onmessage = (e)=>{
1012
- if(e.data.error){
1013
- throw new Error(e.data.error)
1014
- }
1015
- const dom = this.dom; // Assuming this.dom is an object
1016
- console.log(this.dom)
1017
- let js = e.data.js
1018
- let template = e.data.template
1019
- // Bind the component's context
1020
-
1021
- const useState = this.useState.bind(this); // Bind the component's context
1022
- const useEffect = this.useEffect.bind(this); // Bind the component's context
1023
- const useReducer = this.useReducer.bind(this); // Bind the component's context
1024
- const useAuth = this.useAuth.bind(this); // Bind the component's context
1025
- const useSyncStore = this.useSyncStore.bind(this); // Bind the component's context
1026
- const signal = this.signal.bind(this); // Bind the component's context
1027
- const rf = this.$Function.bind(this); // Bind the component's context
1028
- let states = this.states
1029
- const useRef = this.useRef.bind(this); // Bind the component's context
1030
- new Function("useState", "useEffect", "useAuth", "useReducer", "useSyncStore", "signal", "rf", "dom", "render", "states", "useRef", js)(
822
+ worker.postMessage({
823
+ strings,
824
+ args,
825
+ location:
826
+ window.location.origin +
827
+ window.location.pathname.replace(/\/[^\/]*$/, "") +
828
+ "/public/",
829
+ name: this.name
830
+ });
831
+ let promise = new Promise((resolve, reject) => {
832
+ worker.onmessage = (e) => {
833
+ if (e.data.error) {
834
+ throw new Error(e.data.error);
835
+ }
836
+ const dom = this.dom; // Assuming this.dom is an object
837
+ let js = e.data.js;
838
+ let template = e.data.template;
839
+ // Bind the component's context
840
+
841
+ const useState = this.useState.bind(this); // Bind the component's context
842
+ const useEffect = this.useEffect.bind(this); // Bind the component's context
843
+ const useReducer = this.useReducer.bind(this); // Bind the component's context
844
+ const useAuth = this.useAuth.bind(this); // Bind the component's context
845
+ const useSyncStore = this.useSyncStore.bind(this); // Bind the component's context
846
+ const signal = this.signal.bind(this); // Bind the component's context
847
+ const $Function = this.$Function.bind(this); // Bind the component's context
848
+ let states = this.states;
849
+ const useRef = this.useRef.bind(this); // Bind the component's context
850
+ new Function(
851
+ "useState",
852
+ "useEffect",
853
+ "useAuth",
854
+ "useReducer",
855
+ "useSyncStore",
856
+ "signal",
857
+ "$Function",
858
+ "dom",
859
+ "render",
860
+ "states",
861
+ "useRef",
862
+ js
863
+ )(
864
+ useState,
865
+ useEffect,
866
+ useAuth,
867
+ useReducer,
868
+ useSyncStore,
869
+ signal,
870
+ $Function,
871
+ this.dom,
872
+ this.render,
873
+ this.states,
874
+ useRef
875
+ );
876
+
877
+ resolve(
878
+ new Function(
879
+ "useRef",
880
+ "states",
881
+ "signal",
882
+ "useState",
883
+ "useReducer",
884
+ "useAuth",
885
+ "useSyncStore",
886
+ "useRef",
887
+ "$Function",
888
+ "return" + "`" + template + "`"
889
+ )(
890
+ useRef,
891
+ states,
892
+ signal,
1031
893
  useState,
1032
- useEffect,
1033
- useAuth,
1034
894
  useReducer,
895
+ useAuth,
1035
896
  useSyncStore,
1036
- signal,
1037
- rf,
1038
- this.dom,
1039
- this.render,
1040
- this.states,
1041
- useRef
897
+ useRef,
898
+ $Function
1042
899
  )
1043
-
1044
- resolve(new Function("useRef", "states", "return" + "`" + template + "`")(useRef, states))
1045
-
1046
-
1047
-
1048
-
1049
- }
1050
- worker.onerror = (e)=>{
1051
- reject(e)
1052
- }
1053
- })
1054
- // @ts-ignore
1055
- return promise;
1056
- }else{
1057
- let result = "";
1058
- for (let i = 0; i < strings.length; i++) {
1059
- result += strings[i];
1060
- if (i < args.length) {
1061
- result += args[i];
1062
- }
1063
- }
1064
- result = new Function("useRef", `return \`${result}\``)(useRef)
1065
-
1066
- if (!result.trim().startsWith("<body>")) {
1067
- console.warn(
1068
- "You should wrap your html in a body tag, vader may not grab all html!"
1069
900
  );
1070
- }
1071
-
1072
-
1073
-
1074
- return this.parseHTML(result);
1075
- }
1076
-
1077
-
901
+ };
902
+ worker.onerror = (e) => {
903
+ reject(e);
904
+ };
905
+ });
906
+ // @ts-ignore
907
+ return promise;
1078
908
  }
1079
909
  // write types to ensure it returns a string
1080
910
  /**
@@ -1133,9 +963,14 @@ const Vader = {
1133
963
  * }
1134
964
  */
1135
965
  Component: Component,
1136
- useRef: useRef,
966
+ useRef: useRef
1137
967
  };
1138
- export const component = (name) => {
968
+ /**
969
+ * @function component
970
+ * @description Allows you to create a component
971
+ * @returns {Component}
972
+ */
973
+ export const component = () => {
1139
974
  return new Component();
1140
975
  };
1141
976
 
@@ -1151,81 +986,91 @@ export const rf = (name, fn) => {
1151
986
  window[name] = fn;
1152
987
  };
1153
988
  let cache = {};
1154
- async function handletemplate(data){
989
+ async function handletemplate(data) {
1155
990
  let dom = new DOMParser().parseFromString(data, "text/html");
1156
991
  let elements = dom.documentElement.querySelectorAll("*");
1157
-
992
+
1158
993
  if (elements.length > 0) {
1159
994
  for (var i = 0; i < elements.length; i++) {
1160
-
1161
995
  if (elements[i].nodeName === "INCLUDE") {
1162
- if(!elements[i].getAttribute("src") || elements[i].getAttribute("src") === ""){
1163
- throw new Error("Include tag must have src attribute")
996
+ if (
997
+ !elements[i].getAttribute("src") ||
998
+ elements[i].getAttribute("src") === ""
999
+ ) {
1000
+ throw new Error("Include tag must have src attribute");
1164
1001
  }
1165
-
1166
- let componentName = elements[i].getAttribute("src")?.split("/").pop()?.split(".")[0]
1167
- // @ts-ignore
1168
- let filedata = await include(elements[i].getAttribute("src"))
1169
- // replace ` with \`\` to allow for template literals
1170
- filedata = filedata.replace(/`/g, "\\`")
1171
- cache[elements[i].getAttribute("src")] = filedata
1172
- filedata = new Function(`return \`${filedata}\`;`)();
1173
- let newdom = new DOMParser().parseFromString(filedata, "text/html");
1174
1002
 
1175
- newdom.querySelectorAll("include").forEach((el)=>{
1176
- el.remove()
1177
- })
1178
- // @ts-ignore
1179
-
1180
- let els = dom.querySelectorAll(componentName)
1181
-
1182
- els.forEach((el)=>{
1183
-
1184
- if(el.attributes.length > 0){
1185
- for(var i = 0; i < el.attributes.length; i++){
1186
- newdom.body.outerHTML = newdom.body.outerHTML.replace(`{{${el.attributes[i].name}}}`, el.attributes[i].value)
1187
- }
1188
-
1189
- }
1190
- if(el.children.length > 0 && newdom.body.querySelector('slot')){
1191
- for(var i = 0; i < el.children.length; i++){
1192
- let slots = newdom.body.querySelectorAll("slot")
1193
- slots.forEach((slot)=>{
1194
- let id = slot.getAttribute("id")
1195
- if(id === el.nodeName.toLowerCase()){
1196
- slot.outerHTML = `<div>${el.innerHTML}</div>`
1197
- }
1198
- })
1199
-
1200
-
1201
- }
1202
-
1203
- }
1003
+ let componentName = elements[i]
1004
+ .getAttribute("src")
1005
+ ?.split("/")
1006
+ .pop()
1007
+ ?.split(".")[0];
1008
+ // @ts-ignore
1009
+ let filedata = await include(elements[i].getAttribute("src"));
1010
+ // replace ` with \`\` to allow for template literals
1011
+ filedata = filedata.replace(/`/g, "\\`");
1012
+ cache[elements[i].getAttribute("src")] = filedata;
1013
+ filedata = new Function(`return \`${filedata}\`;`)();
1014
+ let newdom = new DOMParser().parseFromString(filedata, "text/html");
1204
1015
 
1205
- dom.body.querySelectorAll('include').forEach((el)=>{
1206
- el.remove()
1207
- })
1208
- // replace ` with \`\` to allow for template literals
1209
- dom.body.outerHTML = dom.body.outerHTML.replace(/`/g, "\\`")
1210
- dom.body.outerHTML = dom.body.outerHTML.replace(el.outerHTML, new Function(`return \`${newdom.body.outerHTML}\`;`)())
1211
-
1212
-
1213
- })
1214
-
1215
-
1216
-
1016
+ newdom.querySelectorAll("include").forEach((el) => {
1017
+ el.remove();
1018
+ });
1019
+ // @ts-ignore
1020
+
1021
+ let els = dom.querySelectorAll(componentName);
1022
+
1023
+ els.forEach((el) => {
1024
+ if (el.attributes.length > 0) {
1025
+ for (var i = 0; i < el.attributes.length; i++) {
1026
+ // @ts-ignore
1027
+ let t = "{{" + el.attributes[i].name + "}}";
1028
+ if (newdom.body.innerHTML.includes(t)) {
1029
+ // @ts-ignore
1030
+ newdom.body.innerHTML = newdom.body.innerHTML.replaceAll(
1031
+ t,
1032
+ el.attributes[i].value
1033
+ );
1034
+ }
1035
+ }
1036
+ }
1037
+ if (el.children.length > 0 && newdom.body.querySelector("slot")) {
1038
+ for (var i = 0; i < el.children.length; i++) {
1039
+ let slots = newdom.body.querySelectorAll("slot");
1040
+ slots.forEach((slot) => {
1041
+ let id = slot.getAttribute("id");
1042
+
1043
+ if (
1044
+ (el.hasAttribute("key") && el.getAttribute("key") === id) ||
1045
+ (!el.hasAttribute("key") && el.nodeName === id)
1046
+ ) {
1047
+ if (el.children[i].innerHTML.length > 0) {
1048
+ slot.outerHTML = el.children[i].innerHTML;
1049
+ }
1050
+ }
1051
+ });
1052
+ }
1053
+ }
1217
1054
 
1055
+ dom.body.querySelectorAll("include").forEach((el) => {
1056
+ el.remove();
1057
+ });
1058
+ // replace ` with \`\` to allow for template literals
1059
+ dom.body.outerHTML = dom.body.outerHTML.replace(/`/g, "\\`");
1060
+ dom.body.outerHTML = dom.body.outerHTML.replace(
1061
+ el.outerHTML,
1062
+ new Function(`return \`${newdom.body.outerHTML}\`;`)()
1063
+ );
1064
+ });
1218
1065
  }
1219
1066
  }
1220
-
1221
-
1222
1067
  }
1223
-
1068
+
1224
1069
  // replace ` with \`\` to allow for template literals
1225
- dom.body.outerHTML = dom.body.outerHTML.replace(/`/g, "\\`")
1070
+ dom.body.outerHTML = dom.body.outerHTML.replace(/`/g, "\\`");
1226
1071
  data = new Function(`return \`${dom.body.outerHTML}\`;`)();
1227
-
1228
- return data;
1072
+
1073
+ return data;
1229
1074
  }
1230
1075
  /**
1231
1076
  * @function include
@@ -1234,10 +1079,7 @@ async function handletemplate(data){
1234
1079
  * @param {string} path
1235
1080
  */
1236
1081
 
1237
-
1238
-
1239
1082
  export const include = async (path) => {
1240
-
1241
1083
  if (
1242
1084
  path.startsWith("/") &&
1243
1085
  !path.includes("/src/") &&
@@ -1247,26 +1089,27 @@ export const include = async (path) => {
1247
1089
  ) {
1248
1090
  path = "/src/" + path;
1249
1091
  }
1092
+ // @ts-ignore
1250
1093
  if (cache[path]) {
1251
- return await handletemplate(new Function(`return \`${cache[path]}\`;`)())
1252
-
1253
- }else{
1094
+ // @ts-ignore
1095
+ return await handletemplate(new Function(`return \`${cache[path]}\`;`)());
1096
+ } else {
1254
1097
  return fetch(`./${path}`)
1255
- .then((res) => {
1256
- if (res.status === 404) {
1257
- throw new Error(`No file found at ${path}`);
1258
- }
1259
- return res.text();
1260
- })
1261
- .then(async (data) => {
1262
- cache[path] = data
1263
-
1264
- data = await handletemplate(new Function(`return \`${data}\`;`)())
1265
-
1266
- return data
1267
- });
1098
+ .then((res) => {
1099
+ if (res.status === 404) {
1100
+ throw new Error(`No file found at ${path}`);
1101
+ }
1102
+ return res.text();
1103
+ })
1104
+ .then(async (data) => {
1105
+ // @ts-ignore
1106
+ cache[path] = data;
1107
+
1108
+ data = await handletemplate(new Function(`return \`${data}\`;`)());
1109
+
1110
+ return data;
1111
+ });
1268
1112
  }
1269
-
1270
1113
  };
1271
1114
 
1272
- export default Vader;
1115
+ export default Vader