@@ -48,6 +48,8 @@ class DomFragment {
4848 /** @type {string|undefined } */ tagName ;
4949 /** @type {Node[] } */ classList = [ ] ;
5050 /** @type {{key: string, value: Node}[] } */ attributes = [ ] ;
51+ /** @type {{key: string, value: Node}[] } */ style = [ ] ;
52+ /** @type {{key: string, value: Node}[] } */ eventListeners = [ ] ;
5153 /** @type {Node } */ textContent ;
5254 /** @type {DomFragment[] } */ children = [ ] ;
5355 /** @type {DomFragment|undefined } */ parent ;
@@ -97,13 +99,20 @@ class DomFragment {
9799 for ( const attribute of this . attributes || [ ] ) {
98100 appendExpression ( `${ attribute . key } =${ attributeValue ( toOutputString ( attribute . value ) ) } ` ) ;
99101 }
102+ for ( const eventListener of this . eventListeners || [ ] ) {
103+ appendExpression ( `@${ eventListener . key } =${ attributeValue ( toOutputString ( eventListener . value ) ) } ` ) ;
104+ }
105+ if ( this . style . length ) {
106+ const style = this . style . map ( s => `${ s . key } :${ toOutputString ( s . value ) } ` ) . join ( '; ' ) ;
107+ appendExpression ( `style="${ style } "` ) ;
108+ }
100109 if ( lineLength > MAX_LINE_LENGTH ) {
101110 components . push ( `\n${ ' ' . repeat ( indent ) } ` ) ;
102111 }
103112 components . push ( '>' ) ;
104113 if ( this . textContent ) {
105114 components . push ( toOutputString ( this . textContent ) ) ;
106- } else {
115+ } else if ( this . children ?. length ) {
107116 for ( const child of this . children || [ ] ) {
108117 components . push ( ...child . toTemplateLiteral ( sourceCode , indent + 2 ) ) ;
109118 }
@@ -172,7 +181,17 @@ module.exports = {
172181 const isMethodCall = isAccessed && grandParent . type === 'CallExpression' && grandParent . callee === parent ;
173182 const firstArg = isMethodCall ? /** @type {Node } */ ( grandParent . arguments [ 0 ] ) : null ;
174183 const secondArg = isMethodCall ? /** @type {Node } */ ( grandParent . arguments [ 1 ] ) : null ;
175-
184+ const grandGrandParent = grandParent . parent ;
185+ const isPropertyMethodCall = isAccessed && grandParent . type === 'MemberExpression' &&
186+ grandParent . object === parent && grandGrandParent . type === 'CallExpression' &&
187+ grandGrandParent . callee === grandParent ;
188+ const propertyMethodArgument = isPropertyMethodCall ? /** @type {Node } */ ( grandGrandParent . arguments [ 0 ] ) : null ;
189+ const isSubpropertyAssignment = isAccessed && grandParent . type === 'MemberExpression' &&
190+ grandParent . object === parent && grandParent . property . type === 'Identifier' &&
191+ grandGrandParent . type === 'AssignmentExpression' && grandGrandParent . left === grandParent ;
192+ const subproperty =
193+ isSubpropertyAssignment && grandParent . property . type === 'Identifier' ? grandParent . property : null ;
194+ const subpropertyValue = isSubpropertyAssignment ? /** @type {Node } */ ( grandGrandParent . right ) : null ;
176195 reference . processed = true ;
177196 if ( isPropertyAssignment && isIdentifier ( property , 'className' ) ) {
178197 domFragment . classList . push ( propertyValue ) ;
@@ -182,9 +201,23 @@ module.exports = {
182201 const attribute = firstArg ;
183202 const value = secondArg ;
184203 if ( attribute . type === 'Literal' && value . type !== 'SpreadElement' ) {
185- domFragment . attributes . push ( {
186- key : attribute . value . toString ( ) ,
187- value,
204+ domFragment . attributes . push ( { key : attribute . value . toString ( ) , value} ) ;
205+ }
206+ } else if ( isMethodCall && isIdentifier ( property , 'addEventListener' ) ) {
207+ const event = firstArg ;
208+ const value = secondArg ;
209+ if ( event . type === 'Literal' && value . type !== 'SpreadElement' ) {
210+ domFragment . eventListeners . push ( { key : event . value . toString ( ) , value} ) ;
211+ }
212+ } else if (
213+ isPropertyMethodCall && isIdentifier ( property , 'classList' ) && isIdentifier ( grandParent . property , 'add' ) ) {
214+ domFragment . classList . push ( propertyMethodArgument ) ;
215+ } else if ( isSubpropertyAssignment && isIdentifier ( property , 'style' ) ) {
216+ const property = subproperty . name . replace ( / ( [ a - z ] ) ( [ A - Z ] ) / g, '$1-$2' ) . toLowerCase ( ) ;
217+ if ( subpropertyValue . type !== 'SpreadElement' ) {
218+ domFragment . style . push ( {
219+ key : property ,
220+ value : subpropertyValue ,
188221 } ) ;
189222 }
190223 } else if ( isMethodCall && isIdentifier ( property , 'appendChild' ) ) {
0 commit comments