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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use super::{CallArgs, Value};
use crate::output::Formatted;
use crate::value::{ListSeparator, Numeric, Operator};
use std::fmt::{self, Display, Write};
impl Display for Formatted<'_, Value> {
fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
match *self.value {
Value::Bang(ref s) => write!(out, "!{s}"),
Value::Literal(ref s) => s.fmt(out),
Value::Function(ref n, ref _f) => {
let name = n
.chars()
.flat_map(|c| match c {
'"' => vec!['\\', '"'],
c => vec![c],
})
.collect::<String>();
write!(out, "get-function(\"{name}\")")
}
Value::Numeric(ref num, _) => num.format(self.format).fmt(out),
Value::Color(ref col, ref name) => {
if let Some(ref name) = *name {
name.fmt(out)
} else {
col.format(self.format).fmt(out)
}
}
Value::List(ref v, sep, brackets) => {
let sep = sep.unwrap_or_default();
let introspect = self.format.is_introspection();
if brackets {
out.write_str("[")?;
} else if introspect && v.is_empty() {
return out.write_str("()");
}
let t = v
.iter()
// TODO: Get rid of this filter entirely?
.filter(|v| !v.is_null() || sep.is_slash() || introspect)
.map(|v| {
let needs_paren = match *v {
Value::List(ref v, inner, false) => {
((brackets
&& (sep < inner.unwrap_or_default()))
|| introspect
&& (sep <= inner.unwrap_or_default()))
&& !(introspect && v.len() < 2)
&& (introspect || v.len() != 1)
}
_ => false,
};
if needs_paren {
format!("({})", v.format(self.format))
} else {
format!("{}", v.format(self.format))
}
})
.collect::<Vec<_>>();
if let Some((first, rest)) = t.split_first() {
if rest.is_empty()
&& introspect
&& sep > ListSeparator::Space
{
if brackets {
write!(out, "{}{}", first, sep.sep(true))?;
} else {
write!(out, "({}{})", first, sep.sep(true))?;
}
} else {
write!(out, "{first}")?;
let sep = sep.sep(self.format.is_compressed());
for i in rest {
write!(out, "{sep}{i}")?;
}
}
}
if brackets {
out.write_str("]")?;
}
Ok(())
}
Value::Call(ref name, ref arg) => {
// TODO: Converting calc(1px) to 1px should probably be done earlier.
// it is for scss, but not when loading plain css.
if let Some(arg) = is_calc_result(name, arg) {
write!(out, "{}", arg.format(self.format))
} else {
write!(out, "{name}({arg})")
}
}
Value::BinOp(ref op) => op.format(self.format).fmt(out),
Value::UnaryOp(ref op, ref v) => {
op.fmt(out)?;
if *op == Operator::Not {
out.write_char(' ')?;
}
v.format(self.format).fmt(out)
}
Value::True => write!(out, "true"),
Value::False => write!(out, "false"),
Value::Null => {
if self.format.is_introspection() {
out.write_str("null")
} else {
Ok(())
}
}
Value::Map(ref map) => {
out.write_char('(')?;
for (i, (k, v)) in map.iter().enumerate() {
if i > 0 {
out.write_str(", ")?;
}
if matches!(
k,
Value::List(_, Some(ListSeparator::Comma), _)
) && self.format.is_introspection()
{
write!(out, "({})", k.format(self.format))?;
} else {
write!(out, "{}", k.format(self.format))?;
}
out.write_str(": ")?;
if matches!(
v,
Value::List(_, Some(ListSeparator::Comma), _)
) && self.format.is_introspection()
{
write!(out, "({})", v.format(self.format))?;
} else {
write!(out, "{}", v.format(self.format))?;
}
}
out.write_char(')')
}
Value::UnicodeRange(ref s) => write!(out, "{s}"),
Value::Paren(ref v) => {
out.write_char('(')?;
v.format(self.format).fmt(out)?;
out.write_char(')')
}
Value::ArgList(ref args) => {
// Note: named args not included in output.
if let Some((first, rest)) = args.positional.split_first() {
if self.format.is_introspection() && rest.is_empty() {
out.write_char('(')?;
first.format(self.format).fmt(out)?;
out.write_str(",)")?;
} else {
first.format(self.format).fmt(out)?;
let sep = ListSeparator::Comma
.sep(self.format.is_compressed());
for item in rest {
out.write_str(sep)?;
item.format(self.format).fmt(out)?;
}
}
} else {
out.write_str("()")?;
}
Ok(())
}
}
}
}
/// If a css `calc` can be evaluated to a static numerical result, return that value.
fn is_calc_result<'a>(name: &str, arg: &'a CallArgs) -> Option<&'a Numeric> {
if name == "calc" {
arg.get_single().ok().and_then(|a| match a {
Value::Numeric(n, _) if n.unit.valid_in_css() => Some(n),
_ => None,
})
} else {
None
}
}