]>
git.proxmox.com Git - rustc.git/blob - vendor/minifier/src/css/tests.rs
1 // Take a look at the license at the top of the repository in the LICENSE file.
3 use crate::css
::minify
;
6 /// Rule starting with `@`:
14 /// Any "normal" CSS rule block.
16 /// Contains the selector(s) and its content.
17 ElementRule(Vec<&'a str>, Vec<Property<'a>>),
20 fn get_property<'a>(source: &'a str, iterator: &mut Peekable<CharIndices>,
21 start_pos: &mut usize) -> Option<Property<'a>> {
22 let mut end_pos = None;
23 // First we get the property name.
24 while let Some((pos, c)) = iterator.next() {
25 if let Ok(c) = ReservedChar::try_from(c) {
28 } else if c == ReservedChar::OpenCurlyBrace {
30 } else if c == ReservedChar::Colon {
33 } else { // Invalid character.
36 } else if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-' {
37 // everything's fine for now...
39 return None; // invalid character
42 if end_pos.is_none() || end_pos == Some(*start_pos + 1) {
45 while let Some((pos, c)) = iterator.next() {
46 if let Ok(c) = ReservedChar::try_from(c) {
47 if c == ReservedChar::DoubleQuote || c == ReservedChar::Quote {
48 get_string(source, iterator, &mut 0, c)
49 } else if c == ReservedChar::SemiColon {
50 // we reached the end!
51 let end_pos = end_pos.unwrap();
54 name: &source[start_pos..end_pos],
55 value: &source[end_pos..pos],
69 /// '<', '>', '(', ')', '+', ' ', '[', ']'
73 struct ElementRule<'a> {
74 selectors: Vec<Selector<'a>>,
75 properties: Vec<Property<'a>>,
78 fn get_element_rule<'a>(source: &'a str, iterator: &mut Peekable<CharIndices>,
79 c: char) -> Option<Token<'a>> {
80 let mut selectors = Vec::with_capacity(2);
82 while let Some(s) = get_next_selector(source, iterator, c) {
83 if !selectors.is_empty() || !s.empty_operator() {
89 fn get_media_query<'a>(source: &'a str, iterator: &mut Peekable<CharIndices>,
90 start_pos: &mut usize) -> Option<Token<'a>> {
91 while let Some((pos, c)) = iterator.next() {
96 None // An error occurred, sad life...
100 fn get_properties<'a>(source: &'a str, iterator: &mut Peekable<CharIndices>,
101 start_pos: &mut usize) -> Vec<Property> {
102 let mut ret = Vec::with_capacity(2);
103 while let Some(property) = get_property(source, iterator, start_pos) {
109 pub struct Property<'a> {
114 pub enum AtRule<'a> {
115 /// Contains the charset. Supposed to be the first rule in the style sheet and be present
119 FontFace(Vec<Property<'a>>),
120 /// Contains the import.
122 /// Contains the rule and the block.
123 Keyframes(&'a str, Tokens<'a>),
124 /// Contains the rules and the block.
125 Media(Vec<&'a str>, Tokens<'a>),
128 impl fmt::Display for AtRule {
129 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
130 write!(f, "@{}", &match *self {
131 AtRule::Charset(c) => format!("charset {};", c),
132 AtRule::FontFace(t) => format!("font-face {{{}}};", t),
133 AtRule::Import(i) => format!("import {};", i),
134 AtRule::Keyframes(r, t) => format!("keyframes {} {{{}}}", r, t),
135 AtRule::Media(r, t) => format!("media {} {{{}}}", r.join(" ").collect::<String>(), t),
141 fn check_minification() {
148 a[target = "_blank"] {
149 /* I like weird tests. */
150 border: 1px solid yellow ;
153 let expected
= r
#"/*! Baguette! */
154 .b>p+div:hover{background:#fff;}a[target="_blank"]{border:1px solid yellow;}"#;
155 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
159 fn check_minification2() {
161 h2, h3:not(.impl):not(.method):not(.type) {
162 background-color: #0a042f !important;
165 :target { background: #494a3d; }
167 .table-display tr td:first-child {
178 @media (max-width: 700px) {
183 background-color: rgba(0, 0 , 0 , 0);
184 font: 15px "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
187 let expected
= "h2,h3:not(.impl):not(.method):not(.type){background-color:#0a042f !important;}\
188 :target{background:#494a3d;}.table-display tr td:first-child{float:right;}\
189 @media (max-width:700px){.theme-picker{left:10px;top:54px;z-index:1;\
190 background-color:rgba(0,0,0,0);font:15px \"SFMono-Regular\",Consolas,\
191 \"Liberation Mono\",Menlo,Courier,monospace;}}";
192 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
197 let s
= ".foo { width: calc(100% - 34px); }";
198 let expected
= ".foo{width:calc(100% - 34px);}";
199 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
204 let s
= ".line-numbers .line-highlighted { color: #0a042f !important; }";
205 let expected
= ".line-numbers .line-highlighted{color:#0a042f !important;}";
206 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
210 fn check_space_after_paren() {
211 let s
= ".docblock:not(.type-decl) a:not(.srclink) {}";
212 let expected
= ".docblock:not(.type-decl) a:not(.srclink){}";
213 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
217 fn check_space_after_and() {
218 let s
= "@media only screen and (max-width : 600px) {}";
219 let expected
= "@media only screen and (max-width:600px){}";
220 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
224 fn check_space_after_or_not() {
225 let s
= "@supports not ((text-align-last: justify) or (-moz-text-align-last: justify)) {}";
226 let expected
= "@supports not ((text-align-last:justify) or (-moz-text-align-last:justify)){}";
227 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
231 fn check_space_after_brackets() {
232 let s
= "#main[data-behavior = \"1\"] {}";
233 let expected
= "#main[data-behavior=\"1\"]{}";
234 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
236 let s
= "#main[data-behavior = \"1\"] .aclass";
237 let expected
= "#main[data-behavior=\"1\"] .aclass";
238 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
240 let s
= "#main[data-behavior = \"1\"] ul.aclass";
241 let expected
= "#main[data-behavior=\"1\"] ul.aclass";
242 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
246 fn check_whitespaces_in_calc() {
247 let s
= ".foo { width: calc(130px + 10%); }";
248 let expected
= ".foo{width:calc(130px + 10%);}";
249 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
251 let s
= ".foo { width: calc(130px + (45% - 10% + (12 * 2px))); }";
252 let expected
= ".foo{width:calc(130px + (45% - 10% + (12 * 2px)));}";
253 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
257 fn check_weird_comments() {
267 let expected
= ".test1{font-weight:30em;}.test2{font-weight:30em;}.test3{font-weight:30em;}";
268 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
272 fn check_slash_slash() {
274 background-image: url(data:image/webp;base64,c//S4KP//ZZ/19Uj/UA==);
276 let expected
= "body{background-image:url(data:image/webp;base64,c//S4KP//ZZ/19Uj/UA==);}";
277 assert_eq
!(minify(s
).expect("minify failed").to_string(), expected
);
283 minify("@import 'i';t{x: #fff;}").unwrap().to_string(),
284 "@import 'i';t{x:#fff;}",