@@ -6,15 +6,26 @@ let sanitized = {};
66
77/** Hyperscript reviver that constructs a sanitized HTML string. */
88export default function h ( name , attrs ) {
9+ let stack = [ ] ;
10+ for ( let i = arguments . length ; i -- > 2 ; ) {
11+ stack . push ( arguments [ i ] ) ;
12+ }
13+
14+ // Sortof component support!
15+ if ( typeof name === 'function' ) {
16+ attrs . children = stack . reverse ( ) ;
17+ return name ( attrs ) ;
18+ // return name(attrs, stack.reverse());
19+ }
20+
921 let s = `<${ name } ` ;
1022 if ( attrs ) for ( let i in attrs ) {
1123 if ( attrs [ i ] !== false && attrs [ i ] != null ) {
1224 s += ` ${ esc ( i ) } ="${ esc ( attrs [ i ] ) } "` ;
1325 }
1426 }
1527 s += '>' ;
16- let stack = [ ] ;
17- for ( let i = arguments . length ; i -- > 2 ; ) stack . push ( arguments [ i ] ) ;
28+
1829 while ( stack . length ) {
1930 let child = stack . pop ( ) ;
2031 if ( child ) {
@@ -26,18 +37,7 @@ export default function h(name, attrs) {
2637 }
2738 }
2839 }
40+
2941 sanitized [ s += `</${ name } >` ] = true ;
3042 return s ;
3143}
32-
33-
34-
35- // for fun:
36- /*
37- export default const h = (tag, attrs, ...kids) => (
38- `<${tag}${h.attrs(attrs)}>${[].concat(...kids).join('')}</${tag}>`
39- );
40- h.attrs = a => Object.keys(a || {}).reduce( (s,i) => `${s} ${h.esc(i)}="${h.esc(a[i]+'')}"`, '');
41- h.esc = str => str.replace(/[&<>"']/g, s=>`&${h.map[s]};`);
42- h.map = {'&':'amp','<':'lt','>':'gt','"':'quot',"'":'apos'};
43- */
0 commit comments