Skip to content

Commit dc31875

Browse files
committed
Move remaining rules into the proper files
1 parent cdf6064 commit dc31875

File tree

10 files changed

+156
-130
lines changed

10 files changed

+156
-130
lines changed

src/declaration.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,49 @@ use crate::properties::{
2222
};
2323
use crate::properties::prefixes::Browsers;
2424

25+
#[derive(Debug, PartialEq)]
26+
pub struct DeclarationBlock {
27+
pub declarations: Vec<Declaration>
28+
}
29+
30+
impl ToCss for DeclarationBlock {
31+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
32+
dest.whitespace()?;
33+
dest.write_char('{')?;
34+
dest.indent();
35+
let len = self.declarations.len();
36+
for (i, decl) in self.declarations.iter().enumerate() {
37+
dest.newline()?;
38+
decl.to_css(dest)?;
39+
if i != len - 1 || !dest.minify {
40+
dest.write_char(';')?;
41+
}
42+
}
43+
dest.dedent();
44+
dest.newline()?;
45+
dest.write_char('}')
46+
}
47+
}
48+
49+
impl DeclarationBlock {
50+
pub fn minify(&mut self, handler: &mut DeclarationHandler, important_handler: &mut DeclarationHandler) {
51+
let mut decls: Vec<Declaration> = vec![];
52+
for decl in self.declarations.iter() {
53+
let handled =
54+
(decl.important && important_handler.handle_property(decl)) ||
55+
(!decl.important && handler.handle_property(decl));
56+
57+
if !handled {
58+
decls.push(decl.clone());
59+
}
60+
}
61+
62+
decls.extend(handler.finalize());
63+
decls.extend(important_handler.finalize());
64+
self.declarations = decls;
65+
}
66+
}
67+
2568
#[derive(Debug, Clone, PartialEq)]
2669
pub struct Declaration {
2770
pub property: Property,
@@ -94,7 +137,7 @@ pub struct DeclarationHandler {
94137
decls: DeclarationList
95138
}
96139

97-
impl DeclarationHandler{
140+
impl DeclarationHandler {
98141
pub fn new(important: bool, targets: Option<Browsers>) -> Self {
99142
DeclarationHandler {
100143
background: BackgroundHandler::new(targets),

src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use properties::prefixes::{Browsers, Feature};
2222
use declaration::DeclarationHandler;
2323
use std::collections::HashMap;
2424
use parser::TopLevelRuleParser;
25+
use rules::CssRule;
2526

2627
// ---------------------------------------------
2728

@@ -99,7 +100,7 @@ fn compile(code: &str, minify: bool, targets: Option<Browsers>) -> Result<String
99100
};
100101
// println!("{:?}", rule);
101102
let rule = match rule {
102-
parser::CssRule::Keyframes(mut keyframes) => {
103+
CssRule::Keyframes(mut keyframes) => {
103104
for keyframe in keyframes.keyframes.iter_mut() {
104105
keyframe.declarations.minify(&mut handler, &mut important_handler);
105106
}
@@ -117,7 +118,7 @@ fn compile(code: &str, minify: bool, targets: Option<Browsers>) -> Result<String
117118
// If there is an existing rule with the same name and identical keyframes,
118119
// merge the vendor prefixes from this rule into it.
119120
if let Some(existing_idx) = keyframe_rules.get(&keyframes.name) {
120-
if let Some(parser::CssRule::Keyframes(existing)) = &mut rules.get_mut(*existing_idx) {
121+
if let Some(CssRule::Keyframes(existing)) = &mut rules.get_mut(*existing_idx) {
121122
if existing.keyframes == keyframes.keyframes {
122123
existing.vendor_prefix |= keyframes.vendor_prefix;
123124
set_prefix!(existing);
@@ -128,20 +129,20 @@ fn compile(code: &str, minify: bool, targets: Option<Browsers>) -> Result<String
128129

129130
set_prefix!(keyframes);
130131
keyframe_rules.insert(keyframes.name.clone(), rules.len());
131-
parser::CssRule::Keyframes(keyframes)
132+
CssRule::Keyframes(keyframes)
132133
}
133-
parser::CssRule::Media(mut media) => {
134+
CssRule::Media(mut media) => {
134135
for rule in media.rules.iter_mut() {
135136
match rule {
136-
parser::CssRule::Style(style) => {
137+
CssRule::Style(style) => {
137138
style.declarations.minify(&mut handler, &mut important_handler)
138139
}
139140
_ => {}
140141
}
141142
}
142-
parser::CssRule::Media(media)
143+
CssRule::Media(media)
143144
}
144-
parser::CssRule::Style(mut style) => {
145+
CssRule::Style(mut style) => {
145146
for selector in style.selectors.0.iter() {
146147
for x in selector.iter() {
147148
match x {
@@ -162,7 +163,7 @@ fn compile(code: &str, minify: bool, targets: Option<Browsers>) -> Result<String
162163

163164
style.declarations.minify(&mut handler, &mut important_handler);
164165

165-
if let Some(parser::CssRule::Style(last_style_rule)) = rules.last_mut() {
166+
if let Some(CssRule::Style(last_style_rule)) = rules.last_mut() {
166167
if style.selectors == last_style_rule.selectors {
167168
last_style_rule.declarations.declarations.extend(style.declarations.declarations);
168169
last_style_rule.declarations.minify(&mut handler, &mut important_handler);
@@ -173,7 +174,7 @@ fn compile(code: &str, minify: bool, targets: Option<Browsers>) -> Result<String
173174
}
174175
}
175176

176-
parser::CssRule::Style(style)
177+
CssRule::Style(style)
177178
},
178179
r => r
179180
};

src/parser.rs

Lines changed: 8 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ use selectors::SelectorList;
33
use std::fmt;
44
use std::cell::RefCell;
55
use crate::media_query::*;
6-
use crate::printer::Printer;
7-
use crate::traits::{Parse, ToCss};
6+
use crate::traits::Parse;
87
use std::fmt::Write;
98
use crate::selector::{Selectors, SelectorParser};
109
use crate::rules::{
10+
CssRule,
1111
keyframes::{KeyframeListParser, KeyframesRule},
1212
font_face::{FontFaceRule, FontFaceDeclarationParser},
1313
page::{PageSelector, PageRule},
1414
supports::{SupportsCondition, SupportsRule},
1515
counter_style::CounterStyleRule,
1616
namespace::NamespaceRule,
17-
import::ImportRule
17+
import::ImportRule,
18+
media::MediaRule,
19+
style::StyleRule
1820
};
1921
use crate::values::ident::CustomIdent;
20-
use crate::declaration::{Declaration, DeclarationHandler};
22+
use crate::declaration::{Declaration, DeclarationBlock};
2123
use crate::properties::VendorPrefix;
2224

2325
#[derive(Eq, PartialEq, Clone)]
@@ -70,8 +72,7 @@ impl std::cmp::PartialEq<str> for CssString {
7072
}
7173

7274
/// The parser for the top-level rules in a stylesheet.
73-
pub struct TopLevelRuleParser {
74-
}
75+
pub struct TopLevelRuleParser {}
7576

7677
impl<'b> TopLevelRuleParser {
7778
fn nested<'a: 'b>(&'a self) -> NestedRuleParser {
@@ -212,116 +213,7 @@ impl<'a, 'i> QualifiedRuleParser<'i> for TopLevelRuleParser {
212213
}
213214

214215
#[derive(Clone)]
215-
struct NestedRuleParser {
216-
}
217-
218-
#[derive(Debug, PartialEq)]
219-
pub struct MediaRule {
220-
pub query: MediaList,
221-
pub rules: Vec<CssRule>
222-
}
223-
224-
impl ToCss for MediaRule {
225-
fn to_css<W>(&self, dest: &mut Printer<W>) -> fmt::Result where W: fmt::Write {
226-
dest.write_str("@media ")?;
227-
self.query.to_css(dest)?;
228-
dest.whitespace()?;
229-
dest.write_char('{')?;
230-
dest.indent();
231-
for rule in self.rules.iter() {
232-
dest.newline()?;
233-
rule.to_css(dest)?;
234-
}
235-
dest.dedent();
236-
dest.newline()?;
237-
dest.write_char('}')
238-
}
239-
}
240-
241-
#[derive(Debug, PartialEq)]
242-
pub struct StyleRule {
243-
pub selectors: SelectorList<Selectors>,
244-
pub declarations: DeclarationBlock
245-
}
246-
247-
impl ToCss for StyleRule {
248-
fn to_css<W>(&self, dest: &mut Printer<W>) -> fmt::Result where W: fmt::Write {
249-
self.selectors.to_css(dest)?;
250-
self.declarations.to_css(dest)
251-
}
252-
}
253-
254-
#[derive(Debug, PartialEq)]
255-
pub struct DeclarationBlock {
256-
pub declarations: Vec<Declaration>
257-
}
258-
259-
impl ToCss for DeclarationBlock {
260-
fn to_css<W>(&self, dest: &mut Printer<W>) -> fmt::Result where W: fmt::Write {
261-
dest.whitespace()?;
262-
dest.write_char('{')?;
263-
dest.indent();
264-
let len = self.declarations.len();
265-
for (i, decl) in self.declarations.iter().enumerate() {
266-
dest.newline()?;
267-
decl.to_css(dest)?;
268-
if i != len - 1 || !dest.minify {
269-
dest.write_char(';')?;
270-
}
271-
}
272-
dest.dedent();
273-
dest.newline()?;
274-
dest.write_char('}')
275-
}
276-
}
277-
278-
impl DeclarationBlock {
279-
pub fn minify(&mut self, handler: &mut DeclarationHandler, important_handler: &mut DeclarationHandler) {
280-
let mut decls: Vec<Declaration> = vec![];
281-
for decl in self.declarations.iter() {
282-
let handled =
283-
(decl.important && important_handler.handle_property(decl)) ||
284-
(!decl.important && handler.handle_property(decl));
285-
286-
if !handled {
287-
decls.push(decl.clone());
288-
}
289-
}
290-
291-
decls.extend(handler.finalize());
292-
decls.extend(important_handler.finalize());
293-
self.declarations = decls;
294-
}
295-
}
296-
297-
#[derive(Debug, PartialEq)]
298-
pub enum CssRule {
299-
Media(MediaRule),
300-
Import(ImportRule),
301-
Style(StyleRule),
302-
Keyframes(KeyframesRule),
303-
FontFace(FontFaceRule),
304-
Page(PageRule),
305-
Supports(SupportsRule),
306-
CounterStyle(CounterStyleRule),
307-
Namespace(NamespaceRule)
308-
}
309-
310-
impl ToCss for CssRule {
311-
fn to_css<W>(&self, dest: &mut Printer<W>) -> fmt::Result where W: fmt::Write {
312-
match self {
313-
CssRule::Media(media) => media.to_css(dest),
314-
CssRule::Import(import) => import.to_css(dest),
315-
CssRule::Style(style) => style.to_css(dest),
316-
CssRule::Keyframes(keyframes) => keyframes.to_css(dest),
317-
CssRule::FontFace(font_face) => font_face.to_css(dest),
318-
CssRule::Page(font_face) => font_face.to_css(dest),
319-
CssRule::Supports(supports) => supports.to_css(dest),
320-
CssRule::CounterStyle(counter_style) => counter_style.to_css(dest),
321-
CssRule::Namespace(namespace) => namespace.to_css(dest)
322-
}
323-
}
324-
}
216+
struct NestedRuleParser {}
325217

326218
impl<'a, 'b> NestedRuleParser {
327219
fn parse_nested_rules(&mut self, input: &mut Parser) -> Vec<CssRule> {

src/rules/counter_style.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::traits::ToCss;
2-
use crate::parser::{DeclarationBlock};
2+
use crate::declaration::DeclarationBlock;
33
use crate::printer::Printer;
44
use crate::values::ident::CustomIdent;
55

src/rules/keyframes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use cssparser::*;
22
use crate::values::percentage::Percentage;
33
use crate::traits::{Parse, ToCss};
4-
use crate::parser::{PropertyDeclarationParser, DeclarationBlock};
4+
use crate::parser::{PropertyDeclarationParser};
5+
use crate::declaration::DeclarationBlock;
56
use crate::properties::VendorPrefix;
67
use crate::printer::Printer;
78
use std::fmt::Write;

src/rules/media.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::media_query::MediaList;
2+
use crate::traits::ToCss;
3+
use crate::printer::Printer;
4+
use std::fmt::Write;
5+
use super::CssRule;
6+
7+
#[derive(Debug, PartialEq)]
8+
pub struct MediaRule {
9+
pub query: MediaList,
10+
pub rules: Vec<CssRule>
11+
}
12+
13+
impl ToCss for MediaRule {
14+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
15+
dest.write_str("@media ")?;
16+
self.query.to_css(dest)?;
17+
dest.whitespace()?;
18+
dest.write_char('{')?;
19+
dest.indent();
20+
for rule in self.rules.iter() {
21+
dest.newline()?;
22+
rule.to_css(dest)?;
23+
}
24+
dest.dedent();
25+
dest.newline()?;
26+
dest.write_char('}')
27+
}
28+
}

src/rules/mod.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,46 @@ pub mod supports;
55
pub mod counter_style;
66
pub mod namespace;
77
pub mod import;
8+
pub mod media;
9+
pub mod style;
10+
11+
use media::MediaRule;
12+
use import::ImportRule;
13+
use style::StyleRule;
14+
use keyframes::KeyframesRule;
15+
use font_face::FontFaceRule;
16+
use page::PageRule;
17+
use supports::SupportsRule;
18+
use counter_style::CounterStyleRule;
19+
use namespace::NamespaceRule;
20+
use crate::traits::ToCss;
21+
use crate::printer::Printer;
22+
23+
#[derive(Debug, PartialEq)]
24+
pub enum CssRule {
25+
Media(MediaRule),
26+
Import(ImportRule),
27+
Style(StyleRule),
28+
Keyframes(KeyframesRule),
29+
FontFace(FontFaceRule),
30+
Page(PageRule),
31+
Supports(SupportsRule),
32+
CounterStyle(CounterStyleRule),
33+
Namespace(NamespaceRule)
34+
}
35+
36+
impl ToCss for CssRule {
37+
fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write {
38+
match self {
39+
CssRule::Media(media) => media.to_css(dest),
40+
CssRule::Import(import) => import.to_css(dest),
41+
CssRule::Style(style) => style.to_css(dest),
42+
CssRule::Keyframes(keyframes) => keyframes.to_css(dest),
43+
CssRule::FontFace(font_face) => font_face.to_css(dest),
44+
CssRule::Page(font_face) => font_face.to_css(dest),
45+
CssRule::Supports(supports) => supports.to_css(dest),
46+
CssRule::CounterStyle(counter_style) => counter_style.to_css(dest),
47+
CssRule::Namespace(namespace) => namespace.to_css(dest)
48+
}
49+
}
50+
}

src/rules/page.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cssparser::*;
22
use crate::traits::{Parse, ToCss};
3-
use crate::parser::{DeclarationBlock};
3+
use crate::declaration::DeclarationBlock;
44
use crate::printer::Printer;
55
use crate::macros::enum_property;
66
use std::fmt::Write;

0 commit comments

Comments
 (0)