import { postcss } from "opticss" ; import { CLASS_NAME_IDENT } from "../../BlockSyntax" ; import { Block } from "../../BlockTree" ; import { Configuration } from "../../configuration" ; import { CssBlockError } from "../../errors" ; import { sourceRange } from "../../SourceLocation" ; import { getStyleTargets } from "../block-intermediates" ; import { stripQuotes } from "../utils" ; param configuration param root param block param file export function addPresetSelectors ( configuration , root , block , file ) { root . walkRules ( rule => { rule . walkDecls ( "block-class" , decl => { const val = stripQuotes ( decl . value . trim ( ) ) ; if ( ! CLASS_NAME_IDENT . test ( val ) ) { block . addError ( new CssBlockError ( `${ val } isn't a valid class name.` , sourceRange ( configuration , root , file , decl ) ) ) ; } const parsedSelectors = block . getParsedSelectors ( rule ) ; parsedSelectors . forEach ( sel => { const styleTarget = getStyleTargets ( block , sel . key ) ; if ( styleTarget . blockAttrs . length > 0 ) { styleTarget . blockAttrs [ 0 ] . setPresetClassName ( val ) ; } else if ( styleTarget . blockClasses . length > 0 ) { styleTarget . blockClasses [ 0 ] . setPresetClassName ( val ) ; } else { throw new Error ( `Couldn\'t find block class corresponding to selector ${ sel }. This shouldn't happen.` ) ; } } ) ; } ) ; } ) ; block . all ( true ) . forEach ( styleNode => { if ( ! styleNode . presetCssClass ) { block . addError ( new CssBlockError ( `Style node ${ styleNode . asSource ( ) } doesn't have a preset block class after parsing definition file. You may need to declare this style node in the definition file.` , { filename : file , } ) ) ; } } ) ; }