toplevel atrules within selectors

This commit is contained in:
ConnorSkees 2020-04-12 21:47:32 -04:00
parent 62f9f7da4f
commit c017ccfeb4
4 changed files with 24 additions and 27 deletions

View File

@ -50,27 +50,29 @@ impl UnknownAtRule {
} }
let raw_body = eat_stmts(toks, scope, super_selector)?; let raw_body = eat_stmts(toks, scope, super_selector)?;
let mut body = Vec::with_capacity(raw_body.len()); let mut rules = Vec::with_capacity(raw_body.len());
body.push(Spanned { let mut body = Vec::new();
node: Stmt::RuleSet(RuleSet::new()),
span: kind_span,
});
let mut rules = Vec::new();
for stmt in raw_body { for stmt in raw_body {
match stmt.node { match stmt.node {
Stmt::Style(..) => rules.push(stmt), Stmt::Style(..) => body.push(stmt),
_ => body.push(stmt), _ => rules.push(stmt),
} }
} }
body[0] = Spanned { if super_selector.is_empty() {
body.append(&mut rules);
} else {
body = vec![Spanned {
node: Stmt::RuleSet(RuleSet { node: Stmt::RuleSet(RuleSet {
selector: super_selector.clone(), selector: super_selector.clone(),
rules, rules: body,
super_selector: Selector::new(), super_selector: Selector::new(),
}), }),
span: kind_span, span: kind_span,
}; }];
body.append(&mut rules);
}
Ok(UnknownAtRule { Ok(UnknownAtRule {
name: name.to_owned(), name: name.to_owned(),

View File

@ -160,16 +160,6 @@ pub(crate) struct RuleSet {
super_selector: Selector, super_selector: Selector,
} }
impl RuleSet {
pub(crate) const fn new() -> RuleSet {
RuleSet {
selector: Selector::new(),
rules: Vec::new(),
super_selector: Selector::new(),
}
}
}
/// An intermediate representation of what are essentially single lines /// An intermediate representation of what are essentially single lines
/// todo! rename this /// todo! rename this
#[derive(Clone, Debug)] #[derive(Clone, Debug)]

View File

@ -11,6 +11,7 @@ enum Toplevel {
MultilineComment(String), MultilineComment(String),
AtRule(AtRule), AtRule(AtRule),
Newline, Newline,
Style(Box<Style>),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -98,7 +99,7 @@ impl Css {
vals vals
} }
Stmt::MultilineComment(s) => vec![Toplevel::MultilineComment(s)], Stmt::MultilineComment(s) => vec![Toplevel::MultilineComment(s)],
Stmt::Style(_) => panic!("expected toplevel element, found style"), Stmt::Style(s) => vec![Toplevel::Style(s)],
Stmt::AtRule(r) => vec![Toplevel::AtRule(r)], Stmt::AtRule(r) => vec![Toplevel::AtRule(r)],
}) })
} }
@ -165,6 +166,9 @@ impl Css {
} }
_ => todo!("at-rule other than unknown at toplevel"), _ => todo!("at-rule other than unknown at toplevel"),
}, },
Toplevel::Style(s) => {
writeln!(buf, "{}{}", padding, s.to_string()?)?;
}
Toplevel::Newline => { Toplevel::Newline => {
if has_written { if has_written {
writeln!(buf)? writeln!(buf)?

View File

@ -20,3 +20,4 @@ test!(
basic_unknown_at_rule, basic_unknown_at_rule,
"@foo {\n a {\n color: red;\n }\n}\n" "@foo {\n a {\n color: red;\n }\n}\n"
); );
test!(unknown_at_rule_no_selector, "@foo {\n color: red;\n}\n");