@@ -16,7 +16,7 @@ function indentStr(str: string, indent: number): string {
1616 * Converts a Rule to CSS string.
1717 */
1818export default function toCss (
19- selector : string ,
19+ selector ? : string ,
2020 style : JssStyle ,
2121 options : ToCssOptions = { }
2222) : string {
@@ -27,7 +27,7 @@ export default function toCss(
2727 let { indent = 0 } = options
2828 const { fallbacks} = style
2929
30- indent ++
30+ if ( selector ) indent ++
3131
3232 // Apply fallbacks first.
3333 if ( fallbacks ) {
@@ -38,7 +38,8 @@ export default function toCss(
3838 for ( const prop in fallback ) {
3939 const value = fallback [ prop ]
4040 if ( value != null ) {
41- result += `\n${ indentStr ( `${ prop } : ${ toCssValue ( value ) } ;` , indent ) } `
41+ if ( result ) result += '\n'
42+ result += `${ indentStr ( `${ prop } : ${ toCssValue ( value ) } ;` , indent ) } `
4243 }
4344 }
4445 }
@@ -47,7 +48,8 @@ export default function toCss(
4748 for ( const prop in fallbacks ) {
4849 const value = fallbacks [ prop ]
4950 if ( value != null ) {
50- result += `\n${ indentStr ( `${ prop } : ${ toCssValue ( value ) } ;` , indent ) } `
51+ if ( result ) result += '\n'
52+ result += `${ indentStr ( `${ prop } : ${ toCssValue ( value ) } ;` , indent ) } `
5153 }
5254 }
5355 }
@@ -56,15 +58,20 @@ export default function toCss(
5658 for ( const prop in style ) {
5759 const value = style [ prop ]
5860 if ( value != null && prop !== 'fallbacks' ) {
59- result += `\n${ indentStr ( `${ prop } : ${ toCssValue ( value ) } ;` , indent ) } `
61+ if ( result ) result += '\n'
62+ result += `${ indentStr ( `${ prop } : ${ toCssValue ( value ) } ;` , indent ) } `
6063 }
6164 }
6265
6366 // Allow empty style in this case, because properties will be added dynamically.
6467 if ( ! result && ! options . allowEmpty ) return result
6568
69+ // When rule is being stringified before selector was defined.
70+ if ( ! selector ) return result
71+
6672 indent --
67- result = indentStr ( `${ selector } {${ result } \n` , indent ) + indentStr ( '}' , indent )
6873
69- return result
74+ if ( result ) result = `\n${ result } \n`
75+
76+ return indentStr ( `${ selector } {${ result } ` , indent ) + indentStr ( '}' , indent )
7077}
0 commit comments