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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
use rustc_data_structures::fx::FxHashSet;
use std::fs;
use std::hash::{Hash, Hasher};
use std::path::Path;
use rustc_errors::Handler;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Eq)]
pub(crate) struct CssPath {
pub(crate) name: String,
pub(crate) children: FxHashSet<CssPath>,
}
// This PartialEq implementation IS NOT COMMUTATIVE!!!
//
// The order is very important: the second object must have all first's rules.
// However, the first is not required to have all of the second's rules.
impl PartialEq for CssPath {
fn eq(&self, other: &CssPath) -> bool {
if self.name != other.name {
false
} else {
for child in &self.children {
if !other.children.iter().any(|c| child == c) {
return false;
}
}
true
}
}
}
impl Hash for CssPath {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
for x in &self.children {
x.hash(state);
}
}
}
impl CssPath {
fn new(name: String) -> CssPath {
CssPath { name, children: FxHashSet::default() }
}
}
/// All variants contain the position they occur.
#[derive(Debug, Clone, Copy)]
enum Events {
StartLineComment(usize),
StartComment(usize),
EndComment(usize),
InBlock(usize),
OutBlock(usize),
}
impl Events {
fn get_pos(&self) -> usize {
match *self {
Events::StartLineComment(p)
| Events::StartComment(p)
| Events::EndComment(p)
| Events::InBlock(p)
| Events::OutBlock(p) => p,
}
}
fn is_comment(&self) -> bool {
matches!(
self,
Events::StartLineComment(_) | Events::StartComment(_) | Events::EndComment(_)
)
}
}
fn previous_is_line_comment(events: &[Events]) -> bool {
matches!(events.last(), Some(&Events::StartLineComment(_)))
}
fn is_line_comment(pos: usize, v: &[u8], events: &[Events]) -> bool {
if let Some(&Events::StartComment(_)) = events.last() {
return false;
}
v[pos + 1] == b'/'
}
fn load_css_events(v: &[u8]) -> Vec<Events> {
let mut pos = 0;
let mut events = Vec::with_capacity(100);
while pos + 1 < v.len() {
match v[pos] {
b'/' if v[pos + 1] == b'*' => {
events.push(Events::StartComment(pos));
pos += 1;
}
b'/' if is_line_comment(pos, v, &events) => {
events.push(Events::StartLineComment(pos));
pos += 1;
}
b'\n' if previous_is_line_comment(&events) => {
events.push(Events::EndComment(pos));
}
b'*' if v[pos + 1] == b'/' => {
events.push(Events::EndComment(pos + 2));
pos += 1;
}
b'{' if !previous_is_line_comment(&events) => {
if let Some(&Events::StartComment(_)) = events.last() {
pos += 1;
continue;
}
events.push(Events::InBlock(pos + 1));
}
b'}' if !previous_is_line_comment(&events) => {
if let Some(&Events::StartComment(_)) = events.last() {
pos += 1;
continue;
}
events.push(Events::OutBlock(pos + 1));
}
_ => {}
}
pos += 1;
}
events
}
fn get_useful_next(events: &[Events], pos: &mut usize) -> Option<Events> {
while *pos < events.len() {
if !events[*pos].is_comment() {
return Some(events[*pos]);
}
*pos += 1;
}
None
}
fn get_previous_positions(events: &[Events], mut pos: usize) -> Vec<usize> {
let mut ret = Vec::with_capacity(3);
ret.push(events[pos].get_pos());
if pos > 0 {
pos -= 1;
}
loop {
if pos < 1 || !events[pos].is_comment() {
let x = events[pos].get_pos();
if *ret.last().unwrap() != x {
ret.push(x);
} else {
ret.push(0);
}
break;
}
ret.push(events[pos].get_pos());
pos -= 1;
}
if ret.len() & 1 != 0 && events[pos].is_comment() {
ret.push(0);
}
ret.iter().rev().cloned().collect()
}
fn build_rule(v: &[u8], positions: &[usize]) -> String {
minifier::css::minify(
&positions
.chunks(2)
.map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or(""))
.collect::<String>()
.trim()
.chars()
.filter_map(|c| match c {
'\n' | '\t' => Some(' '),
'/' | '{' | '}' => None,
c => Some(c),
})
.collect::<String>()
.split(' ')
.filter(|s| !s.is_empty())
.intersperse(" ")
.collect::<String>(),
)
.map(|css| css.to_string())
.unwrap_or_else(|_| String::new())
}
fn inner(v: &[u8], events: &[Events], pos: &mut usize) -> FxHashSet<CssPath> {
let mut paths = Vec::with_capacity(50);
while *pos < events.len() {
if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
*pos += 1;
break;
}
if let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
paths.push(CssPath::new(build_rule(v, &get_previous_positions(events, *pos))));
*pos += 1;
}
while let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
if let Some(ref mut path) = paths.last_mut() {
for entry in inner(v, events, pos).iter() {
path.children.insert(entry.clone());
}
}
}
if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
*pos += 1;
}
}
paths.iter().cloned().collect()
}
pub(crate) fn load_css_paths(v: &[u8]) -> CssPath {
let events = load_css_events(v);
let mut pos = 0;
let mut parent = CssPath::new("parent".to_owned());
parent.children = inner(v, &events, &mut pos);
parent
}
pub(crate) fn get_differences(against: &CssPath, other: &CssPath, v: &mut Vec<String>) {
if against.name == other.name {
for child in &against.children {
let mut found = false;
let mut found_working = false;
let mut tmp = Vec::new();
for other_child in &other.children {
if child.name == other_child.name {
if child != other_child {
get_differences(child, other_child, &mut tmp);
} else {
found_working = true;
}
found = true;
break;
}
}
if !found {
v.push(format!(" Missing \"{}\" rule", child.name));
} else if !found_working {
v.extend(tmp.iter().cloned());
}
}
}
}
pub(crate) fn test_theme_against<P: AsRef<Path>>(
f: &P,
against: &CssPath,
diag: &Handler,
) -> (bool, Vec<String>) {
let data = match fs::read(f) {
Ok(c) => c,
Err(e) => {
diag.struct_err(&e.to_string()).emit();
return (false, vec![]);
}
};
let paths = load_css_paths(&data);
let mut ret = vec![];
get_differences(against, &paths, &mut ret);
(true, ret)
}
|