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
70 lines (63 loc) · 1.91 KB
/
style.rs
File metadata and controls
70 lines (63 loc) · 1.91 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
use cssparser::SourceLocation;
use selectors::SelectorList;
use crate::selector::{Selectors, is_compatible};
use crate::traits::ToCss;
use crate::printer::Printer;
use crate::declaration::{DeclarationBlock, DeclarationHandler};
use crate::vendor_prefix::VendorPrefix;
use crate::targets::Browsers;
#[derive(Debug, PartialEq)]
pub struct StyleRule {
pub selectors: SelectorList<Selectors>,
pub vendor_prefix: VendorPrefix,
pub declarations: DeclarationBlock,
pub loc: SourceLocation
}
impl StyleRule {
pub(crate) fn minify(&mut self, handler: &mut DeclarationHandler, important_handler: &mut DeclarationHandler) {
self.declarations.minify(handler, important_handler);
}
pub fn is_compatible(&self, targets: Option<Browsers>) -> bool {
is_compatible(&self.selectors, targets)
}
}
impl ToCss for StyleRule {
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
if self.vendor_prefix.is_empty() {
self.to_css_base(dest)
} 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)?;
}
};
}
prefix!(WebKit);
prefix!(Moz);
prefix!(Ms);
prefix!(O);
prefix!(None);
dest.vendor_prefix = VendorPrefix::empty();
Ok(())
}
}
}
impl StyleRule {
fn to_css_base<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
dest.add_mapping(self.loc);
self.selectors.to_css(dest)?;
self.declarations.to_css(dest)
}
}