From d3620be4d8baadc460b69196a22436e61f0f8e6f Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sat, 11 Jan 2020 17:24:50 -0500 Subject: [PATCH] Fix css rendering bug where nested styles would get eaten --- src/css.rs | 22 ++++++++++++++++++++-- src/main.rs | 6 +++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/css.rs b/src/css.rs index 169b92a..84ac4a5 100644 --- a/src/css.rs +++ b/src/css.rs @@ -49,6 +49,8 @@ impl Toplevel { pub struct Css { blocks: Vec, idx: usize, + inner_rulesets: usize, + at_root: bool, } impl Css { @@ -56,6 +58,8 @@ impl Css { Css { blocks: Vec::new(), idx: 0, + inner_rulesets: 0, + at_root: true, } } @@ -65,12 +69,20 @@ impl Css { fn parse_stmt(&mut self, stmt: Stmt) { match stmt { - Stmt::Style(s) => self.blocks[self.idx - 1].push_style(s), + Stmt::Style(s) => { + assert!(self.idx >= 1); + if self.at_root { + self.blocks[self.idx - 1].push_style(s) + } else { + self.blocks[self.idx + self.inner_rulesets - 1].push_style(s) + } + }, Stmt::MultilineComment(s) => { + assert!(self.idx >= 1); if self.idx == 0 { self.blocks.push(Toplevel::MultilineComment(s)); } else { - self.blocks[self.idx - 1].push_comment(s) + self.blocks[self.idx + self.inner_rulesets - 1].push_comment(s) } } Stmt::RuleSet(RuleSet { @@ -80,19 +92,25 @@ impl Css { }) => { if self.idx == 0 { self.idx = self.blocks.len() + 1; + self.inner_rulesets = 0; self.blocks .push(Toplevel::new_rule(super_selector.zip(selector))); for rule in rules { + self.at_root = true; self.parse_stmt(rule); + self.at_root = true; } self.idx = 0; } else { self.idx += 1; + self.at_root = false; self.blocks .push(Toplevel::new_rule(super_selector.zip(selector))); for rule in rules { self.parse_stmt(rule); } + self.idx -= 1; + self.inner_rulesets += 1; } } } diff --git a/src/main.rs b/src/main.rs index d072614..b8f4d59 100644 --- a/src/main.rs +++ b/src/main.rs @@ -464,7 +464,6 @@ mod test_css { "a, b {\n a, b {\n color: red\n}\n}\n", "a a, a b, b a, b b {\n color: red;\n}\n" ); - test!(selector_element, "a {\n color: red;\n}\n"); test!(selector_id, "#id {\n color: red;\n}\n"); test!(selector_class, ".class {\n color: red;\n}\n"); @@ -558,6 +557,11 @@ mod test_css { "a b {\n color: red;\n}\n" ); test!(removes_empty_styles, "a {}\n", ""); + test!( + doesnt_eat_style_after_ruleset, + "a {\n b {\n color: red;\n}\n color: blue;\n}\n", + "a {\n color: blue;\n}\na b {\n color: red;\n}\n" + ); test!( removes_inner_comments,