forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeclaration.rs
More file actions
207 lines (191 loc) · 6.73 KB
/
declaration.rs
File metadata and controls
207 lines (191 loc) · 6.73 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
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
use cssparser::*;
use crate::properties::Property;
use crate::traits::{PropertyHandler, ToCss};
use crate::printer::Printer;
use crate::properties::{
align::AlignHandler,
background::BackgroundHandler,
flex::FlexHandler,
font::FontHandler,
margin_padding::*,
outline::OutlineHandler,
border::BorderHandler,
transition::TransitionHandler,
animation::AnimationHandler,
prefix_handler::PrefixHandler,
display::DisplayHandler,
transform::TransformHandler,
text::TextDecorationHandler,
position::PositionHandler,
overflow::OverflowHandler,
list::ListStyleHandler,
};
use crate::targets::Browsers;
#[derive(Debug, PartialEq)]
pub struct DeclarationBlock {
pub declarations: Vec<Declaration>
}
impl ToCss for DeclarationBlock {
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
dest.whitespace()?;
dest.write_char('{')?;
dest.indent();
let len = self.declarations.len();
for (i, decl) in self.declarations.iter().enumerate() {
dest.newline()?;
decl.to_css(dest)?;
if i != len - 1 || !dest.minify {
dest.write_char(';')?;
}
}
dest.dedent();
dest.newline()?;
dest.write_char('}')
}
}
impl DeclarationBlock {
pub(crate) fn minify(&mut self, handler: &mut DeclarationHandler, important_handler: &mut DeclarationHandler) {
let mut decls: Vec<Declaration> = vec![];
for decl in self.declarations.iter() {
let handled =
(decl.important && important_handler.handle_property(decl)) ||
(!decl.important && handler.handle_property(decl));
if !handled {
decls.push(decl.clone());
}
}
decls.extend(handler.finalize());
decls.extend(important_handler.finalize());
self.declarations = decls;
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct Declaration {
pub property: Property,
pub important: bool
}
impl Declaration {
pub fn parse<'i, 't>(name: CowRcStr<'i>, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> {
let property = input.parse_until_before(Delimiter::Bang, |input| Property::parse(name, input))?;
let important = input.try_parse(|input| {
input.expect_delim('!')?;
input.expect_ident_matching("important")
}).is_ok();
Ok(Declaration { property, important })
}
}
impl ToCss for Declaration {
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
self.property.to_css(dest, self.important)
}
}
#[derive(Default)]
pub(crate) struct DeclarationList {
important: bool,
pub declarations: Vec<Declaration>
}
impl DeclarationList {
pub fn new(important: bool) -> DeclarationList {
DeclarationList {
important,
declarations: Vec::new()
}
}
pub fn push(&mut self, property: Property) {
self.declarations.push(Declaration { property, important: self.important })
}
pub fn extend(&mut self, properties: &mut Vec<Property>) {
let important = self.important;
self.declarations.extend(properties.drain(..).map(|property| Declaration { property, important }))
}
}
#[derive(Default)]
pub(crate) struct DeclarationHandler {
background: BackgroundHandler,
border: BorderHandler,
outline: OutlineHandler,
flex: FlexHandler,
align: AlignHandler,
margin: MarginHandler,
padding: PaddingHandler,
scroll_margin: ScrollMarginHandler,
scroll_padding: ScrollPaddingHandler,
font: FontHandler,
text: TextDecorationHandler,
list: ListStyleHandler,
transition: TransitionHandler,
animation: AnimationHandler,
display: DisplayHandler,
position: PositionHandler,
inset: InsetHandler,
overflow: OverflowHandler,
transform: TransformHandler,
prefix: PrefixHandler,
decls: DeclarationList
}
impl DeclarationHandler {
pub fn new(important: bool, targets: Option<Browsers>) -> Self {
DeclarationHandler {
background: BackgroundHandler::new(targets),
border: BorderHandler::new(targets),
flex: FlexHandler::new(targets),
align: AlignHandler::new(targets),
transition: TransitionHandler::new(targets),
animation: AnimationHandler::new(targets),
display: DisplayHandler::new(targets),
position: PositionHandler::new(targets),
overflow: OverflowHandler::new(targets),
transform: TransformHandler::new(targets),
text: TextDecorationHandler::new(targets),
prefix: PrefixHandler::new(targets),
decls: DeclarationList::new(important),
..DeclarationHandler::default()
}
}
pub fn handle_property(&mut self, decl: &Declaration) -> bool {
let property = &decl.property;
self.background.handle_property(property, &mut self.decls) ||
self.border.handle_property(property, &mut self.decls) ||
self.outline.handle_property(property, &mut self.decls) ||
self.flex.handle_property(property, &mut self.decls) ||
self.align.handle_property(property, &mut self.decls) ||
self.margin.handle_property(property, &mut self.decls) ||
self.padding.handle_property(property, &mut self.decls) ||
self.scroll_margin.handle_property(property, &mut self.decls) ||
self.scroll_padding.handle_property(property, &mut self.decls) ||
self.font.handle_property(property, &mut self.decls) ||
self.text.handle_property(property, &mut self.decls) ||
self.list.handle_property(property, &mut self.decls) ||
self.transition.handle_property(property, &mut self.decls) ||
self.animation.handle_property(property, &mut self.decls) ||
self.display.handle_property(property, &mut self.decls) ||
self.position.handle_property(property, &mut self.decls) ||
self.inset.handle_property(property, &mut self.decls) ||
self.overflow.handle_property(property, &mut self.decls) ||
self.transform.handle_property(property, &mut self.decls) ||
self.prefix.handle_property(property, &mut self.decls)
}
pub fn finalize(&mut self) -> Vec<Declaration> {
self.background.finalize(&mut self.decls);
self.border.finalize(&mut self.decls);
self.outline.finalize(&mut self.decls);
self.flex.finalize(&mut self.decls);
self.align.finalize(&mut self.decls);
self.margin.finalize(&mut self.decls);
self.padding.finalize(&mut self.decls);
self.scroll_margin.finalize(&mut self.decls);
self.scroll_padding.finalize(&mut self.decls);
self.font.finalize(&mut self.decls);
self.text.finalize(&mut self.decls);
self.list.finalize(&mut self.decls);
self.transition.finalize(&mut self.decls);
self.animation.finalize(&mut self.decls);
self.display.finalize(&mut self.decls);
self.position.finalize(&mut self.decls);
self.inset.finalize(&mut self.decls);
self.overflow.finalize(&mut self.decls);
self.transform.finalize(&mut self.decls);
self.prefix.finalize(&mut self.decls);
std::mem::take(&mut self.decls.declarations)
}
}