@@ -7,6 +7,9 @@ import { is_void } from '../../../../../utils.js';
77 * @param {TemplateOperations } items
88 */
99export function template_to_string ( items ) {
10+ /**
11+ * @type {Array<Element> }
12+ */
1013 let elements = [ ] ;
1114
1215 /**
@@ -19,41 +22,53 @@ export function template_to_string(items) {
1922 */
2023 let last_current_element ;
2124
22- for ( let instruction of items ) {
23- // on push element we add the element to the stack, from this moment on every insert will
24- // happen on the last element in the stack
25- if ( instruction . kind === 'push_element' && last_current_element ) {
26- elements_stack . push ( last_current_element ) ;
27- continue ;
28- }
29- // we closed one element, we remove it from the stack
30- if ( instruction . kind === 'pop_element' ) {
31- elements_stack . pop ( ) ;
32- continue ;
25+ /**
26+ * @template {Node} T
27+ * @param {T } child
28+ */
29+ function insert ( child ) {
30+ if ( last_current_element ) {
31+ last_current_element . children ??= [ ] ;
32+ last_current_element . children . push ( child ) ;
33+ } else {
34+ elements . push ( /** @type {Element } */ ( child ) ) ;
3335 }
34- /**
35- * @type {Node | void }
36- */
37- // @ts -expect-error we can't be here if `swap_current_element` but TS doesn't know that
38- const value = map [ instruction . kind ] (
39- ...[
40- // for set prop we need to send the last element (not the one in the stack since
41- // it get's added to the stack only after the push_element instruction)
42- ...( instruction . kind === 'set_prop' ? [ last_current_element ] : [ ] ) ,
43- ...( instruction . args ?? [ ] )
44- ]
45- ) ;
46- // with set_prop we don't need to do anything else, in all other cases we also need to
47- // append the element/node/anchor to the current active element or push it in the elements array
48- if ( instruction . kind !== 'set_prop' ) {
49- if ( elements_stack . length >= 1 && value ) {
50- map . insert ( /** @type {Element } */ ( elements_stack . at ( - 1 ) ) , value ) ;
51- } else if ( value ) {
52- elements . push ( value ) ;
53- }
54- // keep track of the last created element (it will be pushed to the stack after the props are set)
55- if ( instruction . kind === 'create_element' ) {
56- last_current_element = /** @type {Element } */ ( value ) ;
36+ return child ;
37+ }
38+
39+ for ( let instruction of items ) {
40+ switch ( instruction . kind ) {
41+ case 'push_element' :
42+ elements_stack . push ( /** @type {Element } */ ( last_current_element ) ) ;
43+ break ;
44+ case 'pop_element' :
45+ elements_stack . pop ( ) ;
46+ last_current_element = elements_stack . at ( - 1 ) ;
47+ break ;
48+ case 'create_element' :
49+ last_current_element = insert ( {
50+ kind : 'element' ,
51+ element : /** @type {string[] } */ ( instruction . args ) [ 0 ]
52+ } ) ;
53+ break ;
54+ case 'create_text' :
55+ insert ( {
56+ kind : 'text' ,
57+ value : /** @type {string[] } */ ( instruction . args ) [ 0 ]
58+ } ) ;
59+ break ;
60+ case 'create_anchor' :
61+ insert ( {
62+ kind : 'anchor' ,
63+ data : instruction . args ?. [ 0 ]
64+ } ) ;
65+ break ;
66+ case 'set_prop' : {
67+ const el = /** @type {Element } */ ( last_current_element ) ;
68+ const [ prop , value ] = /** @type {string[] } */ ( instruction . args ) ;
69+ el . props ??= { } ;
70+ el . props [ prop ] = value ;
71+ break ;
5772 }
5873 }
5974 }
@@ -77,70 +92,6 @@ export function template_to_string(items) {
7792 * @typedef { Element | Anchor| Text } Node
7893 */
7994
80- /**
81- *
82- * @param {string } element
83- * @returns {Element }
84- */
85- function create_element ( element ) {
86- return {
87- kind : 'element' ,
88- element
89- } ;
90- }
91-
92- /**
93- * @param {string } data
94- * @returns {Anchor }
95- */
96- function create_anchor ( data ) {
97- return {
98- kind : 'anchor' ,
99- data
100- } ;
101- }
102-
103- /**
104- * @param {string } value
105- * @returns {Text }
106- */
107- function create_text ( value ) {
108- return {
109- kind : 'text' ,
110- value
111- } ;
112- }
113-
114- /**
115- *
116- * @param {Element } el
117- * @param {string } prop
118- * @param {string } value
119- */
120- function set_prop ( el , prop , value ) {
121- el . props ??= { } ;
122- el . props [ prop ] = value ;
123- }
124-
125- /**
126- *
127- * @param {Element } el
128- * @param {Node } child
129- * @param {Node } [anchor]
130- */
131- function insert ( el , child , anchor ) {
132- el . children ??= [ ] ;
133- el . children . push ( child ) ;
134- }
135-
136- let map = {
137- create_element,
138- create_text,
139- create_anchor,
140- set_prop,
141- insert
142- } ;
143-
14495/**
14596 *
14697 * @param {Node } el
0 commit comments