Allow @for to include only styles

This commit is contained in:
ConnorSkees 2020-02-29 15:28:48 -05:00
parent 7923d096a7
commit 82813fee6f
5 changed files with 15 additions and 6 deletions

View File

@ -164,19 +164,21 @@ impl AtRule {
}; };
let mut body = Vec::new(); let mut body = Vec::new();
let mut n = 1; let mut n = 1;
for tok in toks { while let Some(tok) = toks.next() {
match tok.kind { match tok.kind {
TokenKind::Symbol(Symbol::OpenCurlyBrace) => n += 1, TokenKind::Symbol(Symbol::OpenCurlyBrace) => n += 1,
TokenKind::Symbol(Symbol::CloseCurlyBrace) => n -= 1, TokenKind::Symbol(Symbol::CloseCurlyBrace) => n -= 1,
TokenKind::Interpolation => n += 1, TokenKind::Interpolation => n += 1,
_ => {} _ => {}
} }
body.push(tok);
if n == 0 { if n == 0 {
break; break;
} }
body.push(tok);
} }
devour_whitespace_or_comment(toks);
let mut scope = scope.clone(); let mut scope = scope.clone();
if from < to { if from < to {
for i in from..(to + through) { for i in from..(to + through) {

View File

@ -145,8 +145,7 @@ impl Css {
writeln!(buf, "{}@{} {} {{", padding, u.name, u.params)?; writeln!(buf, "{}@{} {} {{", padding, u.name, u.params)?;
} }
Css::from_stylesheet(StyleSheet::from_stmts(u.body.clone())) Css::from_stylesheet(StyleSheet::from_stmts(u.body.clone()))
.pretty_print(buf, nesting + 1) .pretty_print(buf, nesting + 1)?;
.unwrap();
writeln!(buf, "{}}}", padding)?; writeln!(buf, "{}}}", padding)?;
} }
_ => todo!(), _ => todo!(),

View File

@ -498,7 +498,10 @@ impl<'a> StyleSheetParser<'a> {
while let Some(expr) = eat_expr(&mut self.lexer, scope, super_selector)? { while let Some(expr) = eat_expr(&mut self.lexer, scope, super_selector)? {
match expr { match expr {
Expr::Style(s) => stmts.push(Stmt::Style(*s)), Expr::Style(s) => stmts.push(Stmt::Style(*s)),
Expr::AtRule(s) => stmts.push(Stmt::AtRule(s)), Expr::AtRule(a) => match a {
AtRule::For(s) => stmts.extend(s),
r => stmts.push(Stmt::AtRule(r)),
},
Expr::Styles(s) => stmts.extend(s.into_iter().map(Stmt::Style)), Expr::Styles(s) => stmts.extend(s.into_iter().map(Stmt::Style)),
Expr::MixinDecl(name, mixin) => { Expr::MixinDecl(name, mixin) => {
scope.insert_mixin(&name, *mixin); scope.insert_mixin(&name, *mixin);

View File

@ -160,7 +160,7 @@ impl Value {
let kind = if let Some(tok) = toks.next() { let kind = if let Some(tok) = toks.next() {
tok.kind tok.kind
} else { } else {
return Err("Unexpected EOF".into()); panic!("Unexpected EOF");
}; };
match kind { match kind {
TokenKind::Number(val) => { TokenKind::Number(val) => {

View File

@ -33,3 +33,8 @@ test!(
"@for $x from 1 to 3 {\n $limit: $x;\n\n a {\n color: $limit;\n }\n}\n", "@for $x from 1 to 3 {\n $limit: $x;\n\n a {\n color: $limit;\n }\n}\n",
"a {\n color: 1;\n}\n\na {\n color: 2;\n}\n" "a {\n color: 1;\n}\n\na {\n color: 2;\n}\n"
); );
test!(
for_styles,
"a {\n @for $i from 1 to 3 {\n color: $i;\n }\n}\n",
"a {\n color: 1;\n color: 2;\n}\n"
);