@@ -16,7 +16,6 @@ const CACHE = {};
1616const TEMPLATE = document . createElement ( 'template' ) ;
1717
1818const reg = / ( \$ _ h \[ \d + \] ) / g;
19- const nomExp = / ( [ \w - ] + ) = / g;
2019
2120export default function html ( statics ) {
2221 const tpl = CACHE [ statics ] || ( CACHE [ statics ] = build ( statics ) ) ;
@@ -26,73 +25,70 @@ export default function html(statics) {
2625
2726/** Create a template function given strings from a tagged template. */
2827function build ( statics ) {
29- const noms = { } ;
30- let str = '' , i = 0 ;
28+ let str = statics [ 0 ] , i = 1 ;
3129 while ( i < statics . length ) {
32- getNoms ( statics [ i ] , noms ) ;
33- str += statics [ i ++ ] + ( i < statics . length ? '$_h[' + i + ']' : '' ) ;
30+ str += '$_h[' + i + ']' + statics [ i ++ ] ;
3431 }
3532 // Template string preprocessing:
3633 // - replace <${Foo}> with <c c@=${Foo}>
3734 // - replace <x /> with <x></x>
3835 // - replace <${Foo}>a<//>b with <c c@=${Foo}>a</c>b
39- TEMPLATE . innerHTML = str . replace ( / < (?: ( \/ ) \/ | ( \/ ? ) ( \$ _ h \[ \d + \] ) ) / g, '<$1$2c c@=$3' ) . replace ( / < ( [ \w : - ] + ) ( \s [ ^ < > ] * ?) ? \/ > / gi, '<$1$2></$1>' ) . trim ( ) ;
40- return Function ( 'h' , '$_h' , 'return ' + walk ( ( TEMPLATE . content || TEMPLATE ) . firstChild , noms ) ) ;
36+ TEMPLATE . innerHTML = str
37+ . replace ( / < (?: ( \/ ) \/ | ( \/ ? ) ( \$ _ h \[ \d + \] ) ) / g, '<$1$2c c@=$3' )
38+ // .replace(/<([\w:-]+)((?:\s[^<>]*?)?)(\/?)>/gi, (str, name, attrs, a) => {
39+ // const casedAttrs = attrs.replace(/(?:'.*?'|".*?"|([A-Z]))/g, (s, c) => c ? ':::'+c : s);
40+ // return '<' + name + casedAttrs + '>' + (a ? '</'+name+'>' : '');
41+ // })
42+ . replace ( / < ( [ \w : - ] + ) (?: \s [ ^ < > ] * ?) ? ( \/ ? ) > / g, ( str , name , a ) => (
43+ str . replace ( / (?: ' .* ?' | " .* ?" | ( [ A - Z ] ) ) / g, ( s , c ) => c ? ':::' + c : s ) + ( a ? '</' + name + '>' : '' )
44+ ) )
45+ . trim ( ) ;
46+ return Function ( 'h' , '$_h' , 'return ' + walk ( ( TEMPLATE . content || TEMPLATE ) . firstChild ) ) ;
4147}
4248
4349/** Traverse a DOM tree and serialize it to hyperscript function calls */
44- function walk ( n , noms ) {
45- if ( n . nodeType !== 1 ) {
46- if ( n . nodeType === 3 && n . data ) return field ( n . data , ',' ) ;
50+ function walk ( n ) {
51+ if ( n . nodeType != 1 ) {
52+ if ( n . nodeType == 3 && n . data ) return field ( n . data , ',' ) ;
4753 return 'null' ;
4854 }
49- let nodeName = `"${ n . localName } "` , str = '' , pre , it , prevSpread , anySpread ;
55+ let str = '' ,
56+ nodeName = field ( n . localName , str ) ,
57+ sub = '' ,
58+ start = ',({' ;
5059 for ( let i = 0 ; i < n . attributes . length ; i ++ ) {
51- let { name } = n . attributes [ i ] ;
52- const { value } = n . attributes [ i ] ;
53- name = noms [ name ] || name ;
60+ const name = n . attributes [ i ] . name ;
61+ const value = n . attributes [ i ] . value ;
5462 if ( name == 'c@' ) {
5563 nodeName = value ;
56- continue ;
5764 }
58- if ( name . substring ( 0 , 3 ) = =='...' ) {
59- it = name . substring ( 3 ) ;
60- pre = str ? ( prevSpread ? ',' : '},' ) : ' ';
61- prevSpread = anySpread = true ;
65+ else if ( name . substring ( 0 , 3 ) == '...' ) {
66+ sub = '' ;
67+ start = ',Object.assign({ ';
68+ str += '},' + name . substring ( 3 ) + ',{' ;
6269 }
6370 else {
64- it = `"${ name . replace ( / : ( \w ) / g, upper ) } ":${ value ? field ( value , '+' ) : true } ` ;
65- pre = str ? ( prevSpread ? ',{' : ',' ) : '{' ;
66- prevSpread = false ;
71+ str += `${ sub } "${ name . replace ( / : : : ( \w ) / g, ( s , i ) => i . toUpperCase ( ) ) } ":${ value ? field ( value , '+' ) : true } ` ;
72+ sub = ',' ;
6773 }
68- str += pre + it ;
6974 }
70- str = 'h(' + nodeName + ',' + ( str ? ( anySpread ? 'Object.assign({},' + str + ( prevSpread ? '' : '}' ) + ')' : str + '}' ) : '{}' ) ;
75+ str = 'h(' + nodeName + start + str + '})' ;
7176 let child = n . firstChild ;
7277 while ( child ) {
73- str += ',' + walk ( child , noms ) ;
78+ str += ',' + walk ( child ) ;
7479 child = child . nextSibling ;
7580 }
7681 return str + ')' ;
7782}
7883
79- function getNoms ( str , noms ) {
80- let nom ;
81- while ( ( nom = nomExp . exec ( str ) ) !== null ) noms [ nom [ 1 ] . toLowerCase ( ) ] = nom [ 1 ] ;
82- }
83-
84- function upper ( s , i ) {
85- return i . toUpperCase ( ) ;
86- }
87-
8884/** Serialize a field to a String or reference for use in generated code. */
8985function field ( value , sep ) {
9086 const matches = value . match ( reg ) ;
9187 let strValue = JSON . stringify ( value ) ;
9288 if ( matches != null ) {
9389 if ( matches [ 0 ] === value ) return value ;
9490 strValue = strValue . replace ( reg , `"${ sep } $1${ sep } "` ) . replace ( / " [ + , ] " / g, '' ) ;
95- if ( sep === ',' ) strValue = `[${ strValue } ]` ;
91+ if ( sep == ',' ) strValue = `[${ strValue } ]` ;
9692 }
9793 return strValue ;
9894}
0 commit comments