@@ -33,9 +33,16 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
3333 buffer += str ;
3434 }
3535
36- function escapeStringLiteral ( node ) {
36+ function escapeText ( text ) {
37+ if ( text . indexOf ( '<' ) < 0 ) {
38+ return raw ( text ) ;
39+ }
40+ return expr ( t . stringLiteral ( text ) ) ;
41+ }
42+
43+ function escapePropValue ( node ) {
3744 const value = node . value ;
38-
45+
3946 if ( value . match ( / ^ .* $ / u) ) {
4047 if ( value . indexOf ( '"' ) < 0 ) {
4148 return raw ( `"${ value } "` ) ;
@@ -47,7 +54,7 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
4754
4855 return expr ( t . stringLiteral ( node . value ) ) ;
4956 }
50-
57+
5158 function commit ( force ) {
5259 if ( ! buffer && ! force ) return ;
5360 quasis . push ( t . templateElement ( {
@@ -87,7 +94,7 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
8794 expr ( value . expression ) ;
8895 }
8996 else if ( t . isStringLiteral ( value ) ) {
90- escapeStringLiteral ( value ) ;
97+ escapePropValue ( value ) ;
9198 }
9299 else {
93100 expr ( value ) ;
@@ -96,24 +103,20 @@ export default function jsxToTaggedTemplatesBabelPlugin({ types: t }, options =
96103 }
97104 }
98105
99- if ( htmlOutput || node . children && node . children . length !== 0 ) {
106+ const children = t . react . buildChildren ( node ) ;
107+ if ( htmlOutput || children && children . length !== 0 ) {
100108 raw ( '>' ) ;
101- for ( let i = 0 ; i < node . children . length ; i ++ ) {
102- let child = node . children [ i ] ;
103- if ( t . isJSXText ( child ) ) {
109+ for ( let i = 0 ; i < children . length ; i ++ ) {
110+ let child = children [ i ] ;
111+ if ( t . isStringLiteral ( child ) ) {
104112 // @todo - expose `whitespace: true` option?
105- raw ( child . value . trim ( ) ) ;
113+ escapeText ( child . value ) ;
114+ }
115+ else if ( t . isJSXElement ( child ) ) {
116+ processNode ( child ) ;
106117 }
107118 else {
108- if ( t . isJSXExpressionContainer ( child ) ) {
109- child = child . expression ;
110- }
111- if ( t . isJSXElement ( child ) ) {
112- processNode ( child ) ;
113- }
114- else {
115- expr ( child ) ;
116- }
119+ expr ( child ) ;
117120 }
118121 }
119122
0 commit comments