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
// this module is documented at stylist::ast
mod block;
mod context;
mod rule;
mod rule_block_content;
mod scope_content;
mod selector;
mod sheet;
mod str_frag;
mod style_attr;
mod to_style_str;
pub use context::StyleContext;
pub use block::Block;
pub use rule::Rule;
pub use rule_block_content::RuleBlockContent;
pub use scope_content::ScopeContent;
pub use selector::Selector;
pub use sheet::Sheet;
pub use style_attr::StyleAttribute;
pub use to_style_str::ToStyleStr;
pub use str_frag::StringFragment;
#[cfg(test)]
mod tests {
use super::*;
use crate::*;
use std::borrow::Cow;
#[test]
fn test_scope_building_without_condition() -> Result<()> {
let test_block = Sheet::from(vec![
ScopeContent::Block(Block {
condition: Cow::Borrowed(&[]),
content: vec![StyleAttribute {
key: "width".into(),
value: vec!["100vw".into()].into(),
}
.into()]
.into(),
}),
ScopeContent::Block(Block {
condition: vec![vec![".inner".into()].into()].into(),
content: vec![StyleAttribute {
key: "background-color".into(),
value: vec!["red".into()].into(),
}
.into()]
.into(),
}),
ScopeContent::Rule(Rule {
condition: vec!["@keyframes move".into()].into(),
content: vec![
RuleBlockContent::Rule(
Rule {
condition: vec!["from".into()].into(),
content: vec![RuleBlockContent::StyleAttr(StyleAttribute {
key: "width".into(),
value: vec!["100px".into()].into(),
})]
.into(),
}
.into(),
),
RuleBlockContent::Rule(
Rule {
condition: vec!["to".into()].into(),
content: vec![RuleBlockContent::StyleAttr(StyleAttribute {
key: "width".into(),
value: vec!["200px".into()].into(),
})]
.into(),
}
.into(),
),
]
.into(),
}),
]);
assert_eq!(
test_block.to_style_str(Some("test")),
r#".test {
width: 100vw;
}
.test .inner {
background-color: red;
}
@keyframes move {
from {
width: 100px;
}
to {
width: 200px;
}
}
"#
);
Ok(())
}
#[test]
fn test_scope_building_with_condition() -> Result<()> {
let test_block = Sheet::from(vec![ScopeContent::Rule(Rule {
condition: vec!["@media only screen and (min-width: 1000px)".into()].into(),
content: vec![
RuleBlockContent::Block(
Block {
condition: Cow::Borrowed(&[]),
content: vec![StyleAttribute {
key: "width".into(),
value: vec!["100vw".into()].into(),
}
.into()]
.into(),
}
.into(),
),
RuleBlockContent::Block(
Block {
condition: vec![vec![".inner".into()].into()].into(),
content: vec![StyleAttribute {
key: "background-color".into(),
value: vec!["red".into()].into(),
}
.into()]
.into(),
}
.into(),
),
RuleBlockContent::Rule(
Rule {
condition: vec!["@keyframes move".into()].into(),
content: vec![
RuleBlockContent::Rule(
Rule {
condition: vec!["from".into()].into(),
content: vec![RuleBlockContent::StyleAttr(StyleAttribute {
key: "width".into(),
value: vec!["100px".into()].into(),
})]
.into(),
}
.into(),
),
RuleBlockContent::Rule(
Rule {
condition: vec!["to".into()].into(),
content: vec![RuleBlockContent::StyleAttr(StyleAttribute {
key: "width".into(),
value: vec!["200px".into()].into(),
})]
.into(),
}
.into(),
),
]
.into(),
}
.into(),
),
]
.into(),
})]);
assert_eq!(
test_block.to_style_str(Some("test")),
r#"@media only screen and (min-width: 1000px) {
.test {
width: 100vw;
}
}
@media only screen and (min-width: 1000px) {
.test .inner {
background-color: red;
}
}
@media only screen and (min-width: 1000px) {
@keyframes move {
from {
width: 100px;
}
to {
width: 200px;
}
}
}
"#
);
Ok(())
}
}