MCPcopy Index your code
hub / github.com/darkreader/darkreader / formatParsedCSS

Function formatParsedCSS

src/utils/css-text/format-css.ts:9–42  ·  view source on GitHub ↗
(parsed: ParsedCSS)

Source from the content-addressed store, hash-verified

7}
8
9export 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
44function sortDeclarations(declarations: ParsedDeclaration[]) {
45 const prefixRegex = /^-[a-z]-/;

Callers 1

formatCSSFunction · 0.85

Calls 2

clearEmptyRulesFunction · 0.85
formatRuleFunction · 0.85

Tested by

no test coverage detected