(parsed: ParsedCSS)
| 7 | } |
| 8 | |
| 9 | export function formatParsedCSS(parsed: ParsedCSS): string { |
| 10 | const lines: string[] = []; |
| 11 | const tab = ' '; |
| 12 | |
| 13 | function formatRule(rule: ParsedAtRule | ParsedStyleRule, indent: string) { |
| 14 | if (isParsedStyleRule(rule)) { |
| 15 | formatStyleRule(rule as ParsedStyleRule, indent); |
| 16 | } else { |
| 17 | formatAtRule(rule, indent); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | function formatAtRule({type, query, rules}: ParsedAtRule, indent: string) { |
| 22 | lines.push(`${indent}${type} ${query} {`); |
| 23 | rules.forEach((child) => formatRule(child, `${indent}${tab}`)); |
| 24 | lines.push(`${indent}}`); |
| 25 | } |
| 26 | |
| 27 | function formatStyleRule({selectors, declarations}: ParsedStyleRule, indent: string) { |
| 28 | const lastSelectorIndex = selectors.length - 1; |
| 29 | selectors.forEach((selector, i) => { |
| 30 | lines.push(`${indent}${selector}${i < lastSelectorIndex ? ',' : ' {'}`); |
| 31 | }); |
| 32 | const sorted = sortDeclarations(declarations); |
| 33 | sorted.forEach(({property, value, important}) => { |
| 34 | lines.push(`${indent}${tab}${property}: ${value}${important ? ' !important' : ''};`); |
| 35 | }); |
| 36 | lines.push(`${indent}}`); |
| 37 | } |
| 38 | |
| 39 | clearEmptyRules(parsed); |
| 40 | parsed.forEach((rule) => formatRule(rule, '')); |
| 41 | return lines.join('\n'); |
| 42 | } |
| 43 | |
| 44 | function sortDeclarations(declarations: ParsedDeclaration[]) { |
| 45 | const prefixRegex = /^-[a-z]-/; |
no test coverage detected