forked from parcel-bundler/lightningcss
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_custom_parser.rs
More file actions
145 lines (133 loc) · 3.21 KB
/
test_custom_parser.rs
File metadata and controls
145 lines (133 loc) · 3.21 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
use cssparser::*;
use lightningcss::{
declaration::DeclarationBlock,
error::{ParserError, PrinterError},
printer::Printer,
stylesheet::{ParserOptions, PrinterOptions, StyleSheet},
traits::{AtRuleParser, Parse, ToCss},
values::ident::Ident,
};
fn minify_test(source: &str, expected: &str) {
let mut stylesheet = StyleSheet::parse_with(&source, ParserOptions::default(), &mut TestAtRuleParser).unwrap();
stylesheet.minify(Default::default()).unwrap();
let res = stylesheet
.to_css(PrinterOptions {
minify: true,
..PrinterOptions::default()
})
.unwrap();
assert_eq!(res.code, expected);
}
#[test]
fn test_block() {
minify_test(
r#"
@block test {
color: yellow;
}
"#,
"@block test{color:#ff0}",
)
}
#[test]
fn test_inline() {
minify_test(
r#"
@inline test;
.foo {
color: yellow;
}
"#,
"@inline test;.foo{color:#ff0}",
)
}
enum Prelude<'i> {
Block(Ident<'i>),
Inline(Ident<'i>),
}
#[derive(Debug, Clone)]
enum AtRule<'i> {
Block(BlockRule<'i>),
Inline(InlineRule<'i>),
}
#[derive(Debug, Clone)]
struct BlockRule<'i> {
name: Ident<'i>,
declarations: DeclarationBlock<'i>,
}
#[derive(Debug, Clone)]
struct InlineRule<'i> {
name: Ident<'i>,
}
#[derive(Default)]
struct TestAtRuleParser;
impl<'i> AtRuleParser<'i> for TestAtRuleParser {
type Prelude = Prelude<'i>;
type Error = ParserError<'i>;
type AtRule = AtRule<'i>;
fn parse_prelude<'t>(
&mut self,
name: CowRcStr<'i>,
input: &mut Parser<'i, 't>,
_options: &ParserOptions<'_, 'i>,
) -> Result<Self::Prelude, ParseError<'i, Self::Error>> {
let location = input.current_source_location();
match_ignore_ascii_case! {&*name,
"block" => {
let name = Ident::parse(input)?;
Ok(Prelude::Block(name))
},
"inline" => {
let name = Ident::parse(input)?;
Ok(Prelude::Inline(name))
},
_ => Err(location.new_unexpected_token_error(
cssparser::Token::Ident(name.clone())
))
}
}
fn rule_without_block(
&mut self,
prelude: Self::Prelude,
_start: &ParserState,
_options: &ParserOptions<'_, 'i>,
_is_nested: bool,
) -> Result<Self::AtRule, ()> {
match prelude {
Prelude::Inline(name) => Ok(AtRule::Inline(InlineRule { name })),
_ => unreachable!(),
}
}
fn parse_block<'t>(
&mut self,
prelude: Self::Prelude,
_start: &ParserState,
input: &mut Parser<'i, 't>,
_options: &ParserOptions<'_, 'i>,
_is_nested: bool,
) -> Result<Self::AtRule, ParseError<'i, Self::Error>> {
match prelude {
Prelude::Block(name) => Ok(AtRule::Block(BlockRule {
name,
declarations: DeclarationBlock::parse(input, &ParserOptions::default())?,
})),
_ => unreachable!(),
}
}
}
impl<'i> ToCss for AtRule<'i> {
fn to_css<W: std::fmt::Write>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> {
match self {
AtRule::Block(rule) => {
dest.write_str("@block ")?;
rule.name.to_css(dest)?;
rule.declarations.to_css_block(dest)
}
AtRule::Inline(rule) => {
dest.write_str("@inline ")?;
rule.name.to_css(dest)?;
dest.write_char(';')
}
}
}
}