const { decode } = require ( 'he' ) ; import { IStyleAttr } from '../../typings/style' ; import { importantReg } from '../const/regs' ; const cssReg = /([^:;]+):((?:[^;'"]*?(?:(?:'[^']*?'|"[^"]*?"|\/\*.*?\*\/))*[^;'"]*?)*)(?=;|$)/gim ; export const parseStyle = ( styleStr ) : IStyleAttr [ ] => { const style = [ ] ; const str = decode ( styleStr , { isAttributeValue : true , } ) ; cssReg . lastIndex = 0 ; let match = cssReg . exec ( str ) ; while ( match !== null ) { const name = match [ 1 ] . replace ( /^(?:\s*\/\*.+?\*\/\s*)*/g , '' ) . trim ( ) . replace ( /\s/g , '' ) ; let value = match [ 2 ] . replace ( /^(?:\s*\/\*.+?\*\/\s*)*|(?:\s*\/\*.+?\*\/\s*)*$/g , '' ) . trim ( ) . replace ( /\s+/ , ' ' ) ; let important = false ; if ( importantReg . test ( value ) ) { value = value . replace ( importantReg , '' ) ; important = true ; } if ( name && value ) { style . push ( { fullname : name , name , value , important , } ) ; } match = cssReg . exec ( str ) ; } return style ; } ;