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
123 lines (115 loc) · 4.82 KB
/
declaration.rs
File metadata and controls
123 lines (115 loc) · 4.82 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
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,
};
use crate::properties::prefixes::Browsers;
#[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 struct DeclarationHandler {
important: bool,
background: BackgroundHandler,
border: BorderHandler,
outline: OutlineHandler,
flex: FlexHandler,
align: AlignHandler,
margin: MarginHandler,
padding: PaddingHandler,
scroll_margin: ScrollMarginHandler,
scroll_padding: ScrollPaddingHandler,
font: FontHandler,
transition: TransitionHandler,
animation: AnimationHandler,
prefix: PrefixHandler
}
impl DeclarationHandler {
pub fn new(important: bool, targets: Option<Browsers>) -> Self {
DeclarationHandler {
important,
border: BorderHandler::new(targets),
flex: FlexHandler::new(targets),
align: AlignHandler::new(targets),
transition: TransitionHandler::new(targets),
animation: AnimationHandler::new(targets),
prefix: PrefixHandler::new(targets),
..DeclarationHandler::default()
}
}
pub fn handle_property(&mut self, decl: &Declaration) -> bool {
let property = &decl.property;
self.background.handle_property(property) ||
self.border.handle_property(property) ||
self.outline.handle_property(property) ||
self.flex.handle_property(property) ||
self.align.handle_property(property) ||
self.margin.handle_property(property) ||
self.padding.handle_property(property) ||
self.scroll_margin.handle_property(property) ||
self.scroll_padding.handle_property(property) ||
self.font.handle_property(property) ||
self.transition.handle_property(property) ||
self.animation.handle_property(property) ||
self.prefix.handle_property(property)
}
pub fn finalize(&mut self) -> Vec<Declaration> {
let important = self.important;
let mut background = self.background.finalize();
let mut border = self.border.finalize();
let mut outline = self.outline.finalize();
let mut flex = self.flex.finalize();
let mut align = self.align.finalize();
let mut margin = self.margin.finalize();
let mut padding = self.padding.finalize();
let mut scroll_margin = self.scroll_margin.finalize();
let mut scroll_padding = self.scroll_padding.finalize();
let mut font = self.font.finalize();
let mut transition = self.transition.finalize();
let mut animation = self.animation.finalize();
let mut prefixed = self.prefix.finalize();
let mut decls = Vec::with_capacity(background.len() + border.len() + outline.len() + flex.len() + align.len() + margin.len() + padding.len() + scroll_margin.len() + scroll_padding.len() + font.len() + transition.len() + animation.len() + prefixed.len());
decls.extend(background.drain(..).map(|property| Declaration { property, important }));
decls.extend(border.drain(..).map(|property| Declaration { property, important }));
decls.extend(outline.drain(..).map(|property| Declaration { property, important }));
decls.extend(flex.drain(..).map(|property| Declaration { property, important }));
decls.extend(align.drain(..).map(|property| Declaration { property, important }));
decls.extend(margin.drain(..).map(|property| Declaration { property, important }));
decls.extend(padding.drain(..).map(|property| Declaration { property, important }));
decls.extend(scroll_margin.drain(..).map(|property| Declaration { property, important }));
decls.extend(scroll_padding.drain(..).map(|property| Declaration { property, important }));
decls.extend(font.drain(..).map(|property| Declaration { property, important }));
decls.extend(transition.drain(..).map(|property| Declaration { property, important }));
decls.extend(animation.drain(..).map(|property| Declaration { property, important }));
decls.extend(prefixed.drain(..).map(|property| Declaration { property, important }));
decls
}
}