forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyle.rs
More file actions
178 lines (157 loc) · 5.46 KB
/
style.rs
File metadata and controls
178 lines (157 loc) · 5.46 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
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::Location;
use parcel_selectors::SelectorList;
use crate::context::DeclarationContext;
use crate::selector::{Selectors, is_compatible, is_unused};
use crate::traits::ToCss;
use crate::printer::Printer;
use crate::declaration::DeclarationBlock;
use crate::vendor_prefix::VendorPrefix;
use crate::targets::Browsers;
use crate::rules::{CssRuleList, ToCssWithContext, StyleContext};
use crate::compat::Feature;
use crate::error::{PrinterError, PrinterErrorKind, MinifyError};
use super::MinifyContext;
#[derive(Debug, PartialEq, Clone)]
pub struct StyleRule<'i> {
pub selectors: SelectorList<'i, Selectors>,
pub vendor_prefix: VendorPrefix,
pub declarations: DeclarationBlock<'i>,
pub rules: CssRuleList<'i>,
pub loc: Location
}
impl<'i> StyleRule<'i> {
pub(crate) fn minify(&mut self, context: &mut MinifyContext<'_, 'i>, parent_is_unused: bool) -> Result<bool, MinifyError> {
let mut unused = false;
if !context.unused_symbols.is_empty() {
if is_unused(&mut self.selectors.0.iter(), &context.unused_symbols, parent_is_unused) {
if self.rules.0.is_empty() {
return Ok(true)
}
self.declarations.declarations.clear();
self.declarations.important_declarations.clear();
unused = true;
}
}
context.handler_context.context = DeclarationContext::StyleRule;
self.declarations.minify(context.handler, context.important_handler, context.handler_context);
context.handler_context.context = DeclarationContext::None;
if !self.rules.0.is_empty() {
self.rules.minify(context, unused)?;
if unused && self.rules.0.is_empty() {
return Ok(true)
}
}
Ok(false)
}
pub fn is_compatible(&self, targets: Option<Browsers>) -> bool {
is_compatible(&self.selectors, targets)
}
}
impl<'a, 'i> ToCssWithContext<'a, 'i> for StyleRule<'i> {
fn to_css_with_context<W>(&self, dest: &mut Printer<W>, context: Option<&StyleContext<'a, 'i>>) -> Result<(), PrinterError> where W: std::fmt::Write {
if self.vendor_prefix.is_empty() {
self.to_css_base(dest, context)
} else {
let mut first_rule = true;
macro_rules! prefix {
($prefix: ident) => {
if self.vendor_prefix.contains(VendorPrefix::$prefix) {
#[allow(unused_assignments)]
if first_rule {
first_rule = false;
} else {
if !dest.minify {
dest.write_char('\n')?; // no indent
}
dest.newline()?;
}
dest.vendor_prefix = VendorPrefix::$prefix;
self.to_css_base(dest, context)?;
}
};
}
prefix!(WebKit);
prefix!(Moz);
prefix!(Ms);
prefix!(O);
prefix!(None);
dest.vendor_prefix = VendorPrefix::empty();
Ok(())
}
}
}
impl<'a, 'i> StyleRule<'i> {
fn to_css_base<W>(&self, dest: &mut Printer<W>, context: Option<&StyleContext<'a, 'i>>) -> Result<(), PrinterError> where W: std::fmt::Write {
// If supported, or there are no targets, preserve nesting. Otherwise, write nested rules after parent.
let supports_nesting = self.rules.0.is_empty() || dest.targets.is_none() || Feature::CssNesting.is_compatible(dest.targets.unwrap());
let len = self.declarations.declarations.len() + self.declarations.important_declarations.len();
let has_declarations = supports_nesting || len > 0 || self.rules.0.is_empty();
if has_declarations {
dest.add_mapping(self.loc);
self.selectors.to_css_with_context(dest, context)?;
dest.whitespace()?;
dest.write_char('{')?;
dest.indent();
let mut i = 0;
macro_rules! write {
($decls: ident, $important: literal) => {
for decl in &self.declarations.$decls {
// The CSS modules `composes` property is handled specially, and omitted during printing.
// We need to add the classes it references to the list for the selectors in this rule.
if let crate::properties::Property::Composes(composes) = &decl {
if dest.is_nested() && dest.css_module.is_some() {
return Err(dest.error(PrinterErrorKind::InvalidComposesNesting, composes.loc))
}
if let Some(css_module) = &mut dest.css_module {
css_module.handle_composes(&self.selectors, &composes)
.map_err(|e| dest.error(e, composes.loc))?;
continue;
}
}
dest.newline()?;
decl.to_css(dest, $important)?;
if i != len - 1 || !dest.minify {
dest.write_char(';')?;
}
i += 1;
}
};
}
write!(declarations, false);
write!(important_declarations, true);
}
macro_rules! newline {
() => {
if !dest.minify && (supports_nesting || len > 0) && !self.rules.0.is_empty() {
if len > 0 {
dest.write_char('\n')?;
}
dest.newline()?;
}
};
}
macro_rules! end {
() => {
if has_declarations {
dest.dedent();
dest.newline()?;
dest.write_char('}')?;
}
};
}
// Write nested rules after the parent.
if supports_nesting {
newline!();
self.rules.to_css(dest)?;
end!();
} else {
end!();
newline!();
self.rules.to_css_with_context(dest, Some(&StyleContext {
rule: self,
parent: context
}))?;
}
Ok(())
}
}