import { Rule } from 'postcss' ; import { strings , Mode , Source } from '@types' ; import { HTML_SELECTOR_REGEXP , ROOT_SELECTOR_REGEXP } from '@constants' ; import { store } from '@data/store' ; const addPrefix = ( prefix , selector ) : string => { if ( HTML_SELECTOR_REGEXP . test ( selector ) ) { return selector . replace ( HTML_SELECTOR_REGEXP , `$1${ prefix }` ) ; } if ( ROOT_SELECTOR_REGEXP . test ( selector ) ) { return selector . replace ( ROOT_SELECTOR_REGEXP , `${ prefix }$1` ) ; } return `${ prefix } ${ selector }` ; } ; export const addSelectorPrefixes = ( rule , prefixes ) : void => { rule . selectors = typeof prefixes === 'string' ? rule . selectors . map ( ( selector ) : string => { if ( store . rulesPrefixRegExp . test ( selector ) ) { return selector ; } return addPrefix ( prefixes , selector ) ; } ) : rule . selectors . reduce ( ( selectors , selector ) : string [ ] => { if ( store . rulesPrefixRegExp . test ( selector ) ) { selectors = [ ... selectors , selector ] ; } else { selectors = selectors . concat ( prefixes . map ( ( prefix ) : string => addPrefix ( prefix , selector ) ) ) ; } return selectors ; } , [ ] ) ; } ; export const hasSelectorsPrefixed = ( rule ) : boolean => { const prefixed = rule . selectors . find ( ( selector ) => store . rulesPrefixRegExp . test ( selector ) ) ; return ! ! prefixed ; } ; export const addProperSelectorPrefixes = ( ruleFlipped , ruleFlippedSecond , ruleBoth , ruleSafe ) : void => { const { mode , ltrPrefix , rtlPrefix , bothPrefix , source } = store . options ; if ( mode === Mode . combined ) { addSelectorPrefixes ( ruleFlipped , source === Source . ltr ? ltrPrefix : rtlPrefix ) ; addSelectorPrefixes ( ruleFlippedSecond , source === Source . rtl ? ltrPrefix : rtlPrefix ) ; } else { addSelectorPrefixes ( ruleFlipped , source === Source . ltr ? rtlPrefix : ltrPrefix ) ; addSelectorPrefixes ( ruleFlippedSecond , source === Source . rtl ? rtlPrefix : ltrPrefix ) ; } addSelectorPrefixes ( ruleBoth , bothPrefix ) ; addSelectorPrefixes ( ruleSafe , bothPrefix ) ; } ;