forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
146 lines (133 loc) · 5.43 KB
/
mod.rs
File metadata and controls
146 lines (133 loc) · 5.43 KB
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
pub mod keyframes;
pub mod font_face;
pub mod page;
pub mod supports;
pub mod counter_style;
pub mod namespace;
pub mod import;
pub mod media;
pub mod style;
use media::MediaRule;
use import::ImportRule;
use style::StyleRule;
use keyframes::KeyframesRule;
use font_face::FontFaceRule;
use page::PageRule;
use supports::SupportsRule;
use counter_style::CounterStyleRule;
use namespace::NamespaceRule;
use crate::traits::ToCss;
use crate::printer::Printer;
use crate::declaration::DeclarationHandler;
use crate::vendor_prefix::VendorPrefix;
use crate::prefixes::Feature;
use crate::targets::Browsers;
use std::collections::HashMap;
use crate::selector::{is_equivalent, get_prefix, get_necessary_prefixes};
#[derive(Debug, PartialEq)]
pub enum CssRule {
Media(MediaRule),
Import(ImportRule),
Style(StyleRule),
Keyframes(KeyframesRule),
FontFace(FontFaceRule),
Page(PageRule),
Supports(SupportsRule),
CounterStyle(CounterStyleRule),
Namespace(NamespaceRule)
}
impl ToCss for CssRule {
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
match self {
CssRule::Media(media) => media.to_css(dest),
CssRule::Import(import) => import.to_css(dest),
CssRule::Style(style) => style.to_css(dest),
CssRule::Keyframes(keyframes) => keyframes.to_css(dest),
CssRule::FontFace(font_face) => font_face.to_css(dest),
CssRule::Page(font_face) => font_face.to_css(dest),
CssRule::Supports(supports) => supports.to_css(dest),
CssRule::CounterStyle(counter_style) => counter_style.to_css(dest),
CssRule::Namespace(namespace) => namespace.to_css(dest)
}
}
}
#[derive(Debug, PartialEq)]
pub struct CssRuleList(pub Vec<CssRule>);
impl CssRuleList {
pub(crate) fn minify(&mut self, targets: Option<Browsers>, handler: &mut DeclarationHandler, important_handler: &mut DeclarationHandler) {
let mut keyframe_rules = HashMap::new();
let mut rules = Vec::new();
for mut rule in self.0.drain(..) {
match &mut rule {
CssRule::Keyframes(keyframes) => {
keyframes.minify(handler, important_handler);
macro_rules! set_prefix {
($keyframes: ident) => {
if $keyframes.vendor_prefix.contains(VendorPrefix::None) {
if let Some(targets) = targets {
$keyframes.vendor_prefix = Feature::AtKeyframes.prefixes_for(targets)
}
}
};
}
// If there is an existing rule with the same name and identical keyframes,
// merge the vendor prefixes from this rule into it.
if let Some(existing_idx) = keyframe_rules.get(&keyframes.name) {
if let Some(CssRule::Keyframes(existing)) = &mut rules.get_mut(*existing_idx) {
if existing.keyframes == keyframes.keyframes {
existing.vendor_prefix |= keyframes.vendor_prefix;
set_prefix!(existing);
continue;
}
}
}
set_prefix!(keyframes);
keyframe_rules.insert(keyframes.name.clone(), rules.len());
},
CssRule::Media(media) => media.minify(targets, handler, important_handler),
CssRule::Supports(supports) => supports.minify(targets, handler, important_handler),
CssRule::Style(style) => {
style.minify(handler, important_handler);
if let Some(targets) = targets {
style.vendor_prefix = get_prefix(&style.selectors);
if style.vendor_prefix.contains(VendorPrefix::None) {
style.vendor_prefix = get_necessary_prefixes(&style.selectors, targets);
}
}
if let Some(CssRule::Style(last_style_rule)) = rules.last_mut() {
// Merge declarations if the selectors are equivalent, and both are compatible with all targets.
if style.selectors == last_style_rule.selectors && style.is_compatible(targets) && last_style_rule.is_compatible(targets) {
last_style_rule.declarations.declarations.extend(style.declarations.declarations.drain(..));
last_style_rule.declarations.minify(handler, important_handler);
continue
} else if style.declarations == last_style_rule.declarations {
// Append the selectors to the last rule if the declarations are the same, and all selectors are compatible.
if style.is_compatible(targets) && last_style_rule.is_compatible(targets) {
last_style_rule.selectors.0.extend(style.selectors.0.drain(..));
continue
}
// If both selectors are potentially vendor prefixable, and they are
// equivalent minus prefixes, add the prefix to the last rule.
if !style.vendor_prefix.is_empty() &&
!last_style_rule.vendor_prefix.is_empty() &&
is_equivalent(&style.selectors, &last_style_rule.selectors)
{
// If the new rule is unprefixed, replace the prefixes of the last rule.
// Otherwise, add the new prefix.
if style.vendor_prefix.contains(VendorPrefix::None) {
last_style_rule.vendor_prefix = style.vendor_prefix;
} else {
last_style_rule.vendor_prefix |= style.vendor_prefix;
}
continue
}
}
}
},
_ => {}
}
rules.push(rule)
}
self.0 = rules;
}
}