1 // Copyright 2012-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc_data_structures::fx::FxHashSet;
13 use std::hash::{Hash, Hasher};
18 macro_rules! try_something {
19 ($e:expr, $diag:expr, $out:expr) => ({
23 $diag.struct_err(&e.to_string()).emit();
30 #[derive(Debug, Clone, Eq)]
33 pub children: FxHashSet<CssPath>,
36 // This PartialEq implementation IS NOT COMMUTATIVE!!!
38 // The order is very important: the second object must have all first's rules.
39 // However, the first doesn't require to have all second's rules.
40 impl PartialEq for CssPath {
41 fn eq(&self, other: &CssPath) -> bool {
42 if self.name != other.name {
45 for child in &self.children {
46 if !other.children.iter().any(|c| child == c) {
55 impl Hash for CssPath {
56 fn hash<H: Hasher>(&self, state: &mut H) {
57 self.name.hash(state);
58 for x in &self.children {
65 fn new(name: String) -> CssPath {
68 children: FxHashSet::default(),
73 /// All variants contain the position they occur.
74 #[derive(Debug, Clone, Copy)]
76 StartLineComment(usize),
84 fn get_pos(&self) -> usize {
86 Events::StartLineComment(p) |
87 Events::StartComment(p) |
88 Events::EndComment(p) |
90 Events::OutBlock(p) => p,
94 fn is_comment(&self) -> bool {
96 Events::StartLineComment(_) |
97 Events::StartComment(_) |
98 Events::EndComment(_) => true,
104 fn previous_is_line_comment(events: &[Events]) -> bool {
105 if let Some(&Events::StartLineComment(_)) = events.last() {
112 fn is_line_comment(pos: usize, v: &[u8], events: &[Events]) -> bool {
113 if let Some(&Events::StartComment(_)) = events.last() {
116 pos + 1 < v.len() && v[pos + 1] == b'/'
119 fn load_css_events(v: &[u8]) -> Vec<Events> {
121 let mut events = Vec::with_capacity(100);
123 while pos < v.len() - 1 {
125 b'/' if pos + 1 < v.len() && v[pos + 1] == b'*' => {
126 events.push(Events::StartComment(pos));
129 b'/' if is_line_comment(pos, v, &events) => {
130 events.push(Events::StartLineComment(pos));
133 b'\n' if previous_is_line_comment(&events) => {
134 events.push(Events::EndComment(pos));
136 b'*' if pos + 1 < v.len() && v[pos + 1] == b'/' => {
137 events.push(Events::EndComment(pos + 2));
140 b'{' if !previous_is_line_comment(&events) => {
141 if let Some(&Events::StartComment(_)) = events.last() {
145 events.push(Events::InBlock(pos + 1));
147 b'}' if !previous_is_line_comment(&events) => {
148 if let Some(&Events::StartComment(_)) = events.last() {
152 events.push(Events::OutBlock(pos + 1));
161 fn get_useful_next(events: &[Events], pos: &mut usize) -> Option<Events> {
162 while *pos < events.len() {
163 if !events[*pos].is_comment() {
164 return Some(events[*pos]);
171 fn get_previous_positions(events: &[Events], mut pos: usize) -> Vec<usize> {
172 let mut ret = Vec::with_capacity(3);
174 ret.push(events[pos].get_pos());
179 if pos < 1 || !events[pos].is_comment() {
180 let x = events[pos].get_pos();
181 if *ret.last().unwrap() != x {
188 ret.push(events[pos].get_pos());
191 if ret.len() & 1 != 0 && events[pos].is_comment() {
194 ret.iter().rev().cloned().collect()
197 fn build_rule(v: &[u8], positions: &[usize]) -> String {
199 .map(|x| ::std::str::from_utf8(&v[x[0]..x[1]]).unwrap_or(""))
208 .filter(|s| s.len() > 0)
209 .collect::<Vec<&str>>()
213 fn inner(v: &[u8], events: &[Events], pos: &mut usize) -> FxHashSet<CssPath> {
214 let mut paths = Vec::with_capacity(50);
216 while *pos < events.len() {
217 if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
221 if let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
222 paths.push(CssPath::new(build_rule(v, &get_previous_positions(events, *pos))));
225 while let Some(Events::InBlock(_)) = get_useful_next(events, pos) {
226 if let Some(ref mut path) = paths.last_mut() {
227 for entry in inner(v, events, pos).iter() {
228 path.children.insert(entry.clone());
232 if let Some(Events::OutBlock(_)) = get_useful_next(events, pos) {
236 paths.iter().cloned().collect()
239 pub fn load_css_paths(v: &[u8]) -> CssPath {
240 let events = load_css_events(v);
243 let mut parent = CssPath::new("parent".to_owned());
244 parent.children = inner(v, &events, &mut pos);
248 pub fn get_differences(against: &CssPath, other: &CssPath, v: &mut Vec<String>) {
249 if against.name != other.name {
252 for child in &against.children {
253 let mut found = false;
254 let mut found_working = false;
255 let mut tmp = Vec::new();
257 for other_child in &other.children {
258 if child.name == other_child.name {
259 if child != other_child {
260 get_differences(child, other_child, &mut tmp);
262 found_working = true;
269 v.push(format!(" Missing \"{}\" rule", child.name));
270 } else if found_working == false {
271 v.extend(tmp.iter().cloned());
277 pub fn test_theme_against<P: AsRef<Path>>(f: &P, against: &CssPath, diag: &Handler)
278 -> (bool, Vec<String>)
280 let data = try_something!(fs::read(f), diag, (false, vec![]));
281 let paths = load_css_paths(&data);
282 let mut ret = vec![];
283 get_differences(against, &paths, &mut ret);
292 fn test_comments_in_rules() {
301 // another line comment
308 rule g/* another multine
316 you like things like "{}" in there? :)
334 let mut ret = Vec::new();
335 get_differences(&load_css_paths(against.as_bytes()),
336 &load_css_paths(text.as_bytes()),
338 assert!(ret.is_empty());
350 let paths = load_css_paths(text.as_bytes());
351 assert!(paths.children.contains(&CssPath::new("a b c d".to_owned())));
355 fn test_comparison() {
370 let against = load_css_paths(y.as_bytes());
371 let other = load_css_paths(x.as_bytes());
373 let mut ret = Vec::new();
374 get_differences(&against, &other, &mut ret);
375 assert!(ret.is_empty());
376 get_differences(&other, &against, &mut ret);
377 assert_eq!(ret, vec![" Missing \"c\" rule".to_owned()]);