import * as postcss from "postcss" ; import * as Stringifier from "postcss/lib/stringifier" ; import { convertSelectorForEmotion } from "./convertSelectorForEmotion" ; import { escapeScopedCss } from "./escapeScopedCss" ; export function convertScopedCssForEmotion ( scopedCss , scope , knownScopes ) { let scopedCssForEmotion = "" ; function builder ( output , node ? , flag ? ) { if ( ( flag === "start" || flag === "end" ) && node && node . type === "rule" ) { if ( node . selector === scope ) { if ( node . parent . type === "root" ) { return ; } else if ( flag === "start" ) { output = "& {" ; } } else { if ( flag === "start" ) { const convertedSelectors = new Set ( ) ; ( node . selectors || [ ] ) . forEach ( ( selector ) => { convertedSelectors . add ( convertSelectorForEmotion ( selector , scope , knownScopes ) ) ; } ) ; output = `${ [ ... convertedSelectors ] . join ( ", " ) } {` ; } } } scopedCssForEmotion += output ; } ( new Stringifier ( builder ) as postcss . Stringifier ) . stringify ( postcss . parse ( scopedCss ) ) ; return escapeScopedCss ( scopedCssForEmotion ) ; }