diff --git a/src/lib.rs b/src/lib.rs index 9bac48a7..7e468600 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6471,7 +6471,42 @@ mod tests { }, ); } + #[test] + fn test_merge_layers() { + test( + r#" + @layer foo { + .foo { + color: red; + } + } + @layer foo { + .bar { + background: #fff; + } + + .baz { + color: #fff; + } + } + "#, + indoc! {r#" + @layer foo { + .foo { + color: red; + } + .bar { + background: #fff; + } + + .baz { + color: #fff; + } + } + "#}, + ); + } #[test] fn test_merge_rules() { test( diff --git a/src/rules/layer.rs b/src/rules/layer.rs index e90ebce0..7e3ec012 100644 --- a/src/rules/layer.rs +++ b/src/rules/layer.rs @@ -1,7 +1,7 @@ //! The `@layer` rule. -use super::{CssRuleList, Location}; -use crate::error::{ParserError, PrinterError}; +use super::{CssRuleList, Location, MinifyContext}; +use crate::error::{MinifyError, ParserError, PrinterError}; use crate::printer::Printer; use crate::traits::{Parse, ToCss}; use crate::values::string::CowArcStr; @@ -113,6 +113,18 @@ pub struct LayerBlockRule<'i> { pub loc: Location, } +impl<'i> LayerBlockRule<'i> { + pub(crate) fn minify( + &mut self, + context: &mut MinifyContext<'_, 'i>, + parent_is_unused: bool, + ) -> Result { + self.rules.minify(context, parent_is_unused)?; + + Ok(self.rules.0.is_empty()) + } +} + impl<'i> ToCss for LayerBlockRule<'i> { fn to_css(&self, dest: &mut Printer) -> Result<(), PrinterError> where diff --git a/src/rules/mod.rs b/src/rules/mod.rs index 3d4354c7..74e9cec9 100644 --- a/src/rules/mod.rs +++ b/src/rules/mod.rs @@ -301,6 +301,16 @@ impl<'i> CssRuleList<'i> { continue; } } + CssRule::LayerBlock(layer) => { + if let Some(CssRule::LayerBlock(last_style_rule)) = rules.last_mut() { + if last_style_rule.name == layer.name { + last_style_rule.rules.0.extend(layer.rules.0.drain(..)); + } + if layer.minify(context, parent_is_unused)? { + continue; + } + } + } CssRule::MozDocument(document) => document.minify(context)?, CssRule::Style(style) => { if parent_is_unused || style.minify(context, parent_is_unused)? {