1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//! Serialize the flattened output tree to CSS.
use crate::eval::{OutItem, OutNode};
use crate::OutputStyle;
pub(crate) fn emit(nodes: &[OutNode], style: OutputStyle) -> String {
match style {
OutputStyle::Expanded => emit_expanded(nodes),
OutputStyle::Compressed => emit_compressed(nodes),
}
}
fn emit_expanded(nodes: &[OutNode]) -> String {
let mut out = String::new();
for node in nodes {
match node {
OutNode::Rule { selectors, items } => {
out.push_str(&selectors.join(", "));
out.push_str(" {\n");
for item in items {
match item {
OutItem::Decl {
prop,
value,
important,
} => {
out.push_str(" ");
out.push_str(prop);
out.push_str(": ");
out.push_str(value);
if *important {
out.push_str(" !important");
}
out.push_str(";\n");
}
OutItem::Comment(text) => {
out.push_str(" /*");
out.push_str(text);
out.push_str("*/\n");
}
}
}
out.push_str("}\n");
}
OutNode::Comment(text) => {
out.push_str("/*");
out.push_str(text);
out.push_str("*/\n");
}
OutNode::Raw(s) => {
out.push_str(s);
out.push('\n');
}
OutNode::Blank => out.push('\n'),
}
}
out
}
fn emit_compressed(nodes: &[OutNode]) -> String {
let mut out = String::new();
for node in nodes {
match node {
OutNode::Rule { selectors, items } => {
let decls: Vec<String> = items
.iter()
.filter_map(|it| match it {
OutItem::Decl {
prop,
value,
important,
} => {
let imp = if *important { "!important" } else { "" };
Some(format!("{prop}:{value}{imp}"))
}
OutItem::Comment(_) => None,
})
.collect();
if decls.is_empty() {
continue;
}
out.push_str(&selectors.join(","));
out.push('{');
out.push_str(&decls.join(";"));
out.push('}');
}
// Loud comments are dropped in compressed output (the slice does
// not yet special-case `/*!` important comments).
OutNode::Comment(_) => {}
OutNode::Raw(s) => out.push_str(s),
OutNode::Blank => {}
}
}
out
}