1+ var BOOL_PROPS = require ( './bool-props' )
2+
3+ var boolPropRx = new RegExp ( '(' + BOOL_PROPS . join ( '|' ) + ')=["\']?$' , 'i' )
4+
15module . exports = nanothtmlServer
26
37function nanothtmlServer ( src , filename , options , done ) {
@@ -9,12 +13,28 @@ function nanothtmlServer (src, filename, options, done) {
913 return require ( './babel' ) . apply ( this , arguments )
1014 }
1115
16+ var boolMatch
1217 var pieces = arguments [ 0 ]
1318 var output = ''
1419 for ( var i = 0 ; i < pieces . length ; i ++ ) {
15- output + = pieces [ i ]
20+ var piece = pieces [ i ]
1621 if ( i < pieces . length - 1 ) {
17- output += handleValue ( arguments [ i + 1 ] )
22+ if ( ( boolMatch = boolPropRx . exec ( piece ) ) ) {
23+ output += piece . slice ( 0 , boolMatch . index )
24+ if ( arguments [ i + 1 ] ) {
25+ output += boolMatch [ 1 ] + '="' + boolMatch [ 1 ] + '"'
26+ }
27+ continue
28+ }
29+
30+ var value = handleValue ( arguments [ i + 1 ] )
31+ if ( piece [ piece . length - 1 ] === '=' ) {
32+ output += piece + '"' + value + '"'
33+ } else {
34+ output += piece + value
35+ }
36+ } else {
37+ output += piece
1838 }
1939 }
2040
@@ -32,9 +52,26 @@ function handleValue (value) {
3252
3353 // Ignore event handlers. `onclick=${(e) => doSomething(e)}`
3454 // will become. `onclick=""`
35- if ( typeof value === 'function' ) return '"" '
55+ if ( typeof value === 'function' ) return ''
3656 if ( value === null || value === undefined ) return ''
3757 if ( value . __encoded ) return value
58+
59+ if ( typeof value === 'object' ) {
60+ return Object . keys ( value ) . reduce ( function ( str , key , i ) {
61+ if ( str . length > 0 ) str += ' '
62+
63+ if ( BOOL_PROPS . indexOf ( key ) !== - 1 ) {
64+ if ( value [ key ] ) {
65+ return str + key + '="' + key + '"'
66+ }
67+ return str
68+ }
69+
70+ var handled = handleValue ( value [ key ] )
71+ return str + key + '="' + handled + '"'
72+ } , '' )
73+ }
74+
3875 var str = value . toString ( )
3976 return str
4077 . replace ( / & / g, '&' )
0 commit comments